-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.mly
177 lines (153 loc) · 4.67 KB
/
parser.mly
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* Ocamlyacc parser for Josh */
%{
open Ast
%}
%token SEMI LPAREN RPAREN LBRACK RBRACK LBRACE RBRACE COMMA DOT
%token PLUS MINUS ASSIGN MULT DIV MOD
%token EQ NEQ LT GT LEQ GEQ AND OR NOT
%token IF ELSE FOR IN WHILE BREAK CONTINUE RETURN
%token INT BOOL FLOAT STRING VOID CHAR
%token RECORD
%token <int> INTLIT
%token <char> CHARLIT
%token <string> STRLIT
%token <bool> BOOLLIT
%token TRUE FALSE
%token <float> FLOATLIT
%token <string> ID
%token EOF
%start program
%type <Ast.program> program
%nonassoc NOELSE
%nonassoc ELSE
%right ASSIGN
%left LBRACK
%right NOT
%left OR
%left AND
%left EQ NEQ
%left LT LEQ
%left GT GEQ
%left MOD
%left MULT DIV
%left PLUS MINUS
%left DOT
%%
program:
top_level_list EOF { $1 }
top_level_list:
{ [] }
| top_level top_level_list { $1::$2 }
top_level:
stmt { Stmt $1 }
| fdecl { Fdecl $1 }
typ:
INT { Int }
| BOOL { Bool }
| FLOAT { Float }
| CHAR { Char }
| STRING { String }
| LBRACK typ RBRACK { ListT($2) }
| VOID { Void }
| RECORD ID { RecordType($2) }
| typ LPAREN typs_list RPAREN { FunkType($3, $1) }
/*
record Thing {
string name;
int fptr(char c, float d);
}
record Thing myThing = { "josh", func };
myThing.func(c, d);
*/
stmt_list:
/* nothing */ { [] }
| stmt stmt_list { $1::$2 }
fdecl:
typ ID LPAREN opts_list RPAREN LBRACE stmt_list RBRACE { { rtyp=$1; fname=$2; formals=$4; body=$7 } }
stmt:
expr SEMI { Expr $1 }
| vdecl SEMI { Vdecl $1 }
| LBRACE stmt_list RBRACE { Block $2 }
| IF LPAREN expr RPAREN stmt ELSE stmt { If ($3, $5, $7) }
| IF LPAREN expr RPAREN stmt %prec NOELSE { If ($3, $5, Block([])) }
| FOR LPAREN ID IN expr RPAREN stmt { For ($3, $5, $7) }
| WHILE LPAREN expr RPAREN stmt { While ($3,$5) }
| RECORD ID LBRACE opts_list RBRACE SEMI { RecordDef($2, $4) }
| RETURN SEMI { Return Noexpr }
| RETURN expr SEMI { Return $2 }
| CONTINUE SEMI { Continue }
| BREAK SEMI { Break }
expr_list:
/* nothing */ { [] }
| args { $1 }
args:
expr { [$1] }
| expr COMMA args { $1::$3 }
expr:
/* literals */
BOOLLIT { BoolLit $1 }
| INTLIT { IntLit $1 }
| FLOATLIT { FloatLit $1 }
| CHARLIT { CharLit $1 }
| STRLIT { StrLit $1 }
| TRUE { BoolLit(true) }
| FALSE { BoolLit(false) }
| ID { Id $1 }
/* arithmetic expressions */
| expr PLUS expr { Binop ($1, Add, $3) }
| expr MINUS expr { Binop ($1, Sub, $3) }
| expr DIV expr { Binop ($1, Div, $3) }
| expr MULT expr { Binop ($1, Mul, $3) }
| expr MOD expr { Binop ($1, Mod, $3) }
/* equality */
| expr EQ expr { Binop ($1, Equal, $3) }
| expr NEQ expr { Binop ($1, Neq, $3) }
| expr LT expr { Binop ($1, Less, $3) }
| expr LEQ expr { Binop ($1, Leq, $3) }
| expr GT expr { Binop ($1, Greater, $3) }
| expr GEQ expr { Binop ($1, Geq, $3) }
/* logical */
| expr AND expr { Binop ($1, And, $3) }
| expr OR expr { Binop ($1, Or, $3) }
| NOT expr { Unop (Not, $2) }
| ID ASSIGN expr { Assign ($1, $3) }
| LPAREN expr RPAREN { $2 }
/* list */
| LBRACK expr_list RBRACK { ListLit($2) }
| expr LBRACK expr RBRACK { ListAccess($1, $3) }
| expr DOT ID { RecordAccess($1, $3) }
/* record instantiation */
| ID LBRACE actuals_list RBRACE { RecordCreate($1, $3) }
/* mutation */
| expr DOT ID ASSIGN expr { MutateRecord(($1,$3), $5) }
| expr LBRACK expr RBRACK ASSIGN expr { MutateList(($1,$3), $6) }
/* function call */
| ID LPAREN actuals_list RPAREN { Call($1, $3) }
| expr DOT ID LPAREN actuals_list RPAREN { CallRecord(($1,$3), $5) }
| expr LBRACK expr RBRACK LPAREN actuals_list RPAREN { CallList(($1,$3), $6) }
vdecl:
typ ID { Declare($1, $2) }
| typ ID ASSIGN expr { Initialize($1, $2, $4)}
/* for record field and function argument lists */
opts:
typ ID { [Opt($1,$2)] }
| opts COMMA typ ID { Opt($3,$4) :: $1 }
opts_list:
/* nothing */ { [] }
| opts { List.rev $1 }
/* for function types */
typs:
typ { [$1] }
| typs COMMA typ { $3 :: $1 }
typs_list:
/* nothing */ { [] }
| typs { List.rev $1 }
/* for instantiating records and calling functions */
actuals_list:
/* nothing */ { [] }
| actuals_args { $1 }
actuals_args:
actual { [$1] }
| actual COMMA actuals_args { $1::$3 }
actual:
expr { $1 }