-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.fsp
108 lines (87 loc) · 2.5 KB
/
Parser.fsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
%{
open Ast
%}
%token <string> ID
%token <string> INT
%token <string> STR
%token SEMICOLON
%token LPAR RPAR
%token LCUR RCUR
%token LSQR RSQR
%token EQ
%token LET FN
%token COMMA
%token ADD SUB MUL DIV MOD
%token EQUAL NOTEQ LT GT LE GE
%token TrueTok FalseTok NullTok
%token INVALID
%token EOF
%type <Stat list> StatList
%type <Expr> Expr
%type <Expr> Expr1
%type <Expr> Expr2
%type <Expr> Expr3
%type <Expr> Atom
%type <Expr> ExprFn
%type <DataType> PrimitiveTok
%type <bool> BoolValueTok
%type <Stat> Statement
%type <Expr list> Args
%type <Expr list> NonEmptyArgs
%start StatList
%%
StatList:
| { [] }
| EOF { [] }
| Statement StatList { $1 :: $2 }
Statement:
| Expr SEMICOLON { Expr( $1 ) }
| LET ID EQ Expr SEMICOLON { Let( $2, $4 ) }
Args:
| { [] }
| NonEmptyArgs { $1 }
NonEmptyArgs:
| ExprFn { [ $1 ] }
| ExprFn COMMA NonEmptyArgs { $1 :: $3 }
Expr:
| Expr EQ ExprFn { Binary( $1, Assign, $3 ) |> Application }
| ExprFn { $1 }
ExprFn:
| FN LPAR Args RPAR LCUR StatList RCUR { FuncDef( $3, $6 ) |> Application }
| ExprCond { $1 }
ExprCond:
| ExprComp EQUAL ExprComp { Binary( $1, Equal, $3 ) |> Application }
| ExprComp NOTEQ ExprComp { Binary( $1, NotEqual, $3 ) |> Application }
| ExprComp { $1 }
ExprComp:
| Expr1 LT Expr1 { Binary( $1, LessThan, $3 ) |> Application }
| Expr1 GT Expr1 { Binary( $1, LessOrEqual, $3 ) |> Application }
| Expr1 LE Expr1 { Binary( $1, GreaterThan, $3 ) |> Application }
| Expr1 GE Expr1 { Binary( $1, GreaterOrEqual, $3 ) |> Application }
| Expr1 { $1 }
Expr1:
| Expr1 ADD Expr2 { Binary( $1, Add, $3 ) |> Application }
| Expr1 SUB Expr2 { Binary( $1, Sub, $3 ) |> Application }
| Expr2 { $1 }
Expr2:
| Expr2 MUL Expr3 { Binary( $1, Mul, $3 ) |> Application }
| Expr2 DIV Expr3 { Binary( $1, Div, $3 ) |> Application }
| Expr2 MOD Expr3 { Binary( $1, Mod, $3 ) |> Application }
| Expr3 { $1 }
Expr3: Atom { $1 }
Atom:
| INT { Integer( $1 |> int ) |> DataType }
| ID { Identifier( $1 ) |> DataType }
| STR { String( $1 ) |> DataType }
| PrimitiveTok { $1 |> DataType }
| LSQR Args RSQR { $2 |> Array |> DataType }
| ExprFn LSQR ExprFn RSQR { IndexAccess( $3, $1 ) |> Application }
| ExprFn LPAR Args RPAR { FuncCall( $1, $3 ) |> Application }
| INVALID { Error |> DataType }
PrimitiveTok:
| NullTok { Null }
| BoolValueTok { $1 |> Boolean }
BoolValueTok:
| TrueTok { true }
| FalseTok { false }
%%