Advertisement
Guest User

Untitled

a guest
Jun 26th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 5.67 KB | None | 0 0
  1. module rec MicroCosmo.Parser
  2.  
  3. open FParsec
  4. open MicroCosmo.Terminants
  5. open MicroCosmo.ParserHelpers
  6.  
  7.  
  8.  
  9. // Non terminal productions
  10. #nowarn "40"
  11.  
  12. let typeSpec : Parser<Ast.TypeSpec, unit> =
  13.     choice_ws [
  14.         keyword NONE    |>> (fun _ -> Ast.None) ;
  15.         keyword ANY     |>> (fun _ -> Ast.Any) ;
  16.         keyword STRING  |>> (fun _ -> Ast.String) ;
  17.         keyword INT     |>> (fun _ -> Ast.Int) ;
  18.         keyword DOUBLE  |>> (fun _ -> Ast.Double) ;
  19.         keyword BOOL    |>> (fun _ -> Ast.Bool) ;
  20.     ]
  21.  
  22. let identifier : Parser<Ast.Identifier, unit> =
  23.     regex_ws IDENTIFIER |>> (fun a -> string a)
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.    
  32.  
  33.    
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.    
  42.  
  43.    
  44.  
  45.    
  46.  
  47.    
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59. let pStringLiteral =
  60.     literal STRING_LIT                    
  61.         |>> (fun s -> Ast.StringLiteral((s.Substring(1, s.Length - 2))))
  62.  
  63. let boolLiteral =
  64.     let trueLiteral = str_ws TRUE_LIT      
  65.                         |>> (fun _ -> Ast.BoolLiteral true)
  66.                        
  67.     let falseLiteral = str_ws FALSE_LIT    
  68.                         |>> (fun _ -> Ast.BoolLiteral false)
  69.                        
  70.     trueLiteral <|> falseLiteral
  71.    
  72. let intLiteral =
  73.     literal INT_LIT
  74.         |>> (fun a -> Ast.IntLiteral (int a))
  75.  
  76. let doubleLiteral =
  77.     literal DOUBLE_LIT
  78.         |>> (fun a -> Ast.DoubleLiteral (double a))
  79.  
  80. let arrayLiteral =
  81.     let typeSpec_ws = typeSpec .>> ws
  82.     let array_ws = regex_ws ARRAY .>> ws
  83.     (typeSpec_ws .>> array_ws) .>>. expression
  84.         |>> (fun (a, b) -> Ast.ArrayAllocationExpression (a, b))
  85.  
  86. // TODO:
  87. let rec expression : Parser<Ast.Expression, unit> =
  88.     keyword BREAK |>> (fun _ -> Ast.IdentifierExpression ({ Identifier = "lolkek" }))
  89.  
  90. let breakStatement : Parser<Ast.Statement, unit> =
  91.     (keyword BREAK |>> (fun _ -> Ast.BreakStatement))
  92.  
  93. let returnStatement : Parser<Ast.Statement, unit> =
  94.     choice_ws [
  95.         (keyword RETURN |>> (fun _ -> Ast.ReturnStatement None))
  96.         (keyword RETURN >>. expression |>> (fun a -> Ast.ReturnStatement (Some a)))
  97.     ]
  98.  
  99. let ifStatement : Parser<Ast.Statement, unit> =
  100.     choice_ws [
  101.         pipe2 (keyword IF >>. expression) (statement)
  102.             (fun a b -> Ast.IfStatement (a, b, None)) ;
  103.         pipe3 (keyword IF >>. expression) (statement)
  104.             (keyword ELSE >>. statement)
  105.             (fun a b c -> Ast.IfStatement (a, b, Some c)) ;
  106.     ]
  107.  
  108. let localDeclaration : Parser<Ast.VariableDeclaration, unit> =
  109.     choice_ws [
  110.         pipe2 (keyword LET >>. identifier) (symbol COLON >>. typeSpec)
  111.             (fun a b -> (a, b, Ast.Empty, false)) ;
  112.         pipe2 (keyword LET >>. identifier) (symbol COLON >>. typeSpec .>> keyword ARRAY)
  113.             (fun a b -> (a, b, Ast.Empty, true)) ;
  114.     ]
  115.  
  116. let localDeclarations : Parser<Ast.VariableDeclaration list, unit> =
  117.     many_ws localDeclaration
  118.  
  119. let blockStatement : Parser<Ast.Statement, unit> =
  120.     pipe2 (symbol OPENCURLY >>. localDeclarations) (statements .>> symbol CLOSECURLY)
  121.         (fun a b -> Ast.BlockStatement (a, b))
  122.        
  123. let whileStatement : Parser<Ast.Statement, unit> =
  124.     pipe2 (keyword WHILE >>. expression) (symbol OPENCURLY >>. statement .>> symbol CLOSECURLY)
  125.         (fun a b -> Ast.WhileStatement (a, b))
  126.  
  127. let expressionStatement : Parser<Ast.Statement, unit> =
  128.     expression |>> (fun a -> Ast.ExpressionStatement (a))
  129.  
  130. let rec statement : Parser<Ast.Statement, unit> =
  131.     choice_ws [
  132.         expressionStatement ;
  133.         blockStatement ;
  134.         ifStatement ;
  135.         whileStatement ;
  136.         returnStatement ;
  137.         breakStatement ;
  138.     ]
  139.  
  140. let statements : Parser<Ast.Statement list, unit> = many_ws statement
  141.    
  142. let parameter : Parser<Ast.VariableDeclaration, unit> =
  143.     choice_ws [
  144.         pipe2 (keyword LET >>. identifier) (symbol COLON >>. typeSpec)
  145.             (fun a b -> (a, b, Ast.Empty, false)) ;
  146.         pipe2 (keyword LET >>. identifier) (symbol COLON >>. typeSpec .>> keyword ARRAY)
  147.             (fun a b -> (a, b, Ast.Empty, true)) ;
  148.     ]
  149.  
  150. let parameters : Parser<Ast.Parameters, unit> = sepBy_ws parameter (symbol COMMA)
  151.  
  152. let functionDeclaration : Parser<Ast.Declaration, unit> =
  153.     choice_ws [
  154.         pipe3 (keyword FUNC >>. identifier) (symbol OPENPAREN >>. parameters .>> symbol CLOSEPAREN)
  155.             (blockStatement)
  156.             (fun a b c -> Ast.FunctionDeclaration (a, b, Ast.None, c)) ;
  157.         pipe4 (keyword FUNC >>. identifier) (symbol OPENPAREN >>. parameters .>> symbol CLOSEPAREN)
  158.             (symbol COLON >>. typeSpec) (blockStatement)
  159.             (fun a b c d -> Ast.FunctionDeclaration (a, b, c, d)) ;
  160.     ]
  161.  
  162. let variableDeclaration : Parser<Ast.Declaration, unit> =
  163.     choice_ws [
  164.         pipe2 (keyword LET >>. identifier) (symbol COLON >>. typeSpec)
  165.             (fun a b -> Ast.VariableDeclaration (a, b, Ast.Empty, false)) ;
  166.         pipe2 (keyword LET >>. identifier) (symbol COLON >>. typeSpec .>> keyword ARRAY)
  167.             (fun a b -> Ast.VariableDeclaration (a, b, Ast.Empty, true)) ;
  168.         pipe3 (keyword LET >>. identifier) (symbol COLON >>. typeSpec) (symbol EQ >>. expression)
  169.             (fun a b c -> Ast.VariableDeclaration (a, b, c, false)) ;
  170.         pipe3 (keyword LET >>. identifier) (symbol COLON >>. typeSpec .>> keyword ARRAY) (symbol EQ >>. expression)
  171.             (fun a b c -> Ast.VariableDeclaration (a, b, c, true)) ;
  172.     ]
  173.  
  174. let declaration = variableDeclaration <|> functionDeclaration
  175.  
  176. let declarationList = many_ws declaration
  177.  
  178. let program = many_ws declarationList
  179.  
  180. let parse (input : string) =
  181.     match run program input with
  182.         | Success(result, _, _)   -> printfn "Success: %A" result
  183.         | Failure(errorMsg, _, _) -> printfn "Failure: %s" errorMsg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement