Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- open System
- open System.Collections.Generic
- open TreeGen
- let globalScope = new Scope()
- let mutable scope = globalScope
- let enterScope (newScope:Function) =
- newScope.EmitEntry (Console.Out)
- scope <- newScope :> Scope
- let leaveScope () =
- (scope :?> Function).EmitExit (Console.Out)
- scope <- globalScope
- %}
- // The start token becomes a parser function in the compiled code:
- %start start
- // Regular tokens
- %token <string> ID
- // characters
- %token LPAREN RPAREN SEMICOLON COLON COMMA DOT LBRACKET RBRACKET
- // keywords
- %token PROGRAM BEGIN END VAR ARRAY OF INTEGER REAL FUNCTION PROCEDURE
- // operators
- %token <string> SIGN MULOP
- %token ASSIGNOP
- // literals
- %token <System.Int32> NUM
- // Misc tokens
- %token EOF
- // This is the type of the data produced by a successful reduction of the 'start'
- // symbol:
- %type < unit > start
- %%
- // These are the rules of the grammar along with the F# code of the
- // actions executed as rules are reduced.
- start: Program EOF { () }
- Program:
- ProgramHeader
- Declarations
- SubprogramDeclarations
- CompoundStatement
- DOT { () }
- ProgramHeader:
- PROGRAM ID SEMICOLON { Console.WriteLine("Program: "+ $2) }
- IdentifierList:
- | ID { [$1] }
- | IdentifierList COMMA ID { $3 :: $1 }
- Declarations:
- | { () } // empty rule
- | Declarations VAR ID COLON Type SEMICOLON
- Type: { new Variable(scope, $3, $5) |> ignore }
- | StandardType { $1 } { new VType.Array
- | ARRAY LBRACKET NUM DOT DOT NUM RBRACKET OF StandardType
- StandardType: (ElementType = $9, StartIndex = $3, EndIndex = $6) :> VType }
- | INTEGER { new VType.Integer() :> VType }
- | REAL { new VType.Real() :> VType }
- SubprogramDeclarations:
- | { () }
- | SubprogramDeclarations SubprogramDeclaration SEMICOLON { () }
- SubprogramDeclaration:
- SubprogramHead
- Declarations
- CompoundStatement { leaveScope () |> ignore }
- /* KOCHAM CIĘ! <3 */
- SubprogramHead:
- | FUNCTION ID Arguments COLON StandardType SEMICOLON { enterScope(new Function(scope, $2, $3, $5)) }
- | PROCEDURE ID Arguments SEMICOLON { enterScope ( new Function(scope, $2, $3))}
- Arguments:
- | { new ArgumentList() }
- | LPAREN ParameterList RPAREN { $2 }
- ParameterList:
- | IdentifierList COLON Type { new ArgumentList( new List<string>($1), $3) }
- | ParameterList SEMICOLON IdentifierList COLON Type { $1.Add(new List<string>($3), $5) }
- CompoundStatement:
- BEGIN
- OptionalStatements
- END { [] }
- OptionalStatements:
- | { [] } // empty rule
- | StatementList { [] }
- StatementList:
- | Statement { [] }
- | StatementList SEMICOLON Statement { [] }
- Statement:
- | ID ASSIGNOP Expression { new Assignment(scope.LookupLeftHand($1), $3) |> ignore }
- | CompoundStatement { () }
- Expression:
- SimpleExpression { $1 }
- SimpleExpression:
- | Term { $1 }
- | SIGN Term { new SignOperation(scope, $2, $1) :> IExpression }
- | SimpleExpression SIGN Term { new BinaryOperation (scope, $1, $3, $2) :> IExpression }
- Term:
- | Factor { $1 }
- | Term MULOP Factor { new BinaryOperation (scope, $1, $3, $2) :> IExpression }
- Factor:
- | ID { scope.Lookup( $1 ) }
- | NUM { new IntLiteral( Value = $1 ) :> IExpression}
- | LPAREN Expression RPAREN { $2 }
Add Comment
Please, Sign In to add comment