forked from reactorlabs/sourir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.mly
218 lines (191 loc) · 5.97 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
%token NIL
%token<bool> BOOL
%token<int> INT
%token<string> IDENTIFIER
%token SINGLE_QUOTE
%token DOUBLE_EQUAL NOT_EQUAL LT LTE GT GTE PLUS MINUS TIMES DIVIDE MOD DOUBLE_AMP DOUBLE_PIPE BANG
%token LPAREN RPAREN LBRACKET RBRACKET LBRACE RBRACE
%token COLON EQUAL LEFTARROW TRIPLE_DOT COMMA DOLLAR
%token VAR GUARD_HINT BRANCH GOTO PRINT ASSERT ASSUME ELSE STOP READ DROP RETURN CALL VERSION FUNCTION
%token ARRAY LENGTH
%token<string> COMMENT
%token NEWLINE
%token EOF
%start<Instr.program> program
%start<Instr.value> value
%{ open Instr
let next_cont_label =
let cur = ref 0 in
fun () ->
cur := !cur + 1;
"__cont_"^string_of_int(!cur)
let scope_annotation (mode, xs) =
let xs = Instr.VarSet.of_list xs in
match mode with
| `Exact -> Instr.ExactScope xs
| `At_least -> Instr.AtLeastScope xs
%}
%%
program: optional_newlines prog=program_code EOF { prog }
program_code:
| l1=instruction_line prog=list(instruction_line) fs=list(afunction)
{
let annotations, instructions = List.split prog in
let a1, i1 = l1 in
{ main = {
name = "main";
formals = [];
body = [{
label = "anon";
instrs = Array.of_list (i1::instructions);
annotations = Some (Array.of_list (a1::annotations));}] };
functions = fs;
}
}
| f1=afunction fs=list(afunction)
{
let fs = f1 :: fs in
let main = (try List.find (fun {name = n} -> n = "main") fs with
| Not_found -> {
(* This is a bit dodgy, but will be caught and reported by the checker later *)
name = "invalid"; formals = []; body = [];
}
) in
let rest = List.filter (fun {name = n} -> n <> "main") fs in
{ main = main;
functions = rest; }
}
formal_param:
| VAR x=variable
{ Param x }
afunction:
| FUNCTION name=variable LPAREN formals=separated_list(COMMA, formal_param) RPAREN NEWLINE optional_newlines prog=list(instruction_line)
{
let annotations, instructions = List.split prog in
{ name = name;
formals = formals;
body = [{
label = "anon";
instrs = Array.of_list instructions;
annotations = Some (Array.of_list annotations)}]; }
}
| FUNCTION name=variable LPAREN formals=separated_list(COMMA, formal_param) RPAREN NEWLINE optional_newlines v1=version vs=list(version)
{
let vs = v1 :: vs in
{ name = name;
formals = formals;
body = vs; }
}
version:
| VERSION label=variable NEWLINE optional_newlines prog=list(instruction_line)
{
let annotations, instructions = List.split prog in
{ label = label;
instrs = Array.of_list instructions;
annotations = Some (Array.of_list annotations); }
}
instruction_line:
| a=scope_annotation i=instruction NEWLINE optional_newlines { (a, i) }
scope_annotation:
| { None }
| annot=delimited(LBRACE, scope, RBRACE) optional_newlines { Some (scope_annotation annot) }
optional_newlines: list(NEWLINE) { () }
scope:
| x=variable COMMA sc=scope { let (mode, xs) = sc in (mode, x::xs) }
| x=variable { (`Exact, [x]) }
| { (`Exact, []) }
| TRIPLE_DOT { (`At_least, []) }
var_def:
| VAR x=variable EQUAL e=expression { (x, e) }
| VAR x=variable { (x, Simple (Constant Nil)) }
varmap_entry: d=var_def { let (x, e) = d in (x, e) }
varmap: LBRACKET map=separated_list(COMMA, varmap_entry) RBRACKET { map }
extra_frame:
| LPAREN func=label COMMA version=label COMMA pos=label RPAREN LBRACKET VAR cont_res=variable EQUAL DOLLAR varmap=list(preceded(COMMA, varmap_entry)) RBRACKET
{ {varmap; cont_pos={func; version; pos}; cont_res} }
instruction:
| CALL x=variable EQUAL f=expression LPAREN args=separated_list(COMMA, argument) RPAREN label=label
{ Call (label, x, f, args) }
| CALL x=variable EQUAL f=expression LPAREN args=separated_list(COMMA, argument) RPAREN
{ Call (next_cont_label (), x, f, args) }
| RETURN e=expression
{ Return e }
| d=var_def { let (x, e) = d in Decl_var (x, e) }
| ARRAY x=variable LBRACKET e=expression RBRACKET
{ Decl_array (x, Length e) }
| ARRAY x=variable EQUAL LBRACKET es=separated_list(COMMA, expression) RBRACKET
{ Decl_array (x, List es) }
| x=variable LEFTARROW e=expression
{ Assign (x, e) }
| x=variable LBRACKET i=expression RBRACKET LEFTARROW e=expression
{ Array_assign (x, i, e) }
| BRANCH e=expression DOLLAR l1=label DOLLAR l2=label
{ Branch (e, l1, l2) }
| l=label COLON
{ Label (MergeLabel l) }
| DOLLAR l=label COLON
{ Label (BranchLabel l) }
| GOTO l=label
{ Goto l }
| READ x=variable
{ Read x }
| DROP x=variable
{ Drop x }
| PRINT e=expression
{ Print e }
| ASSERT e=expression
{ Assert e }
| GUARD_HINT es=separated_list(COMMA, expression)
{ Guard_hint es }
| ASSUME
label=label
LBRACKET guards=separated_list(COMMA, expression) RBRACKET
ELSE
LPAREN func=label COMMA version=label COMMA pos=label RPAREN
varmap=varmap
extra_frames=list(preceded(COMMA, extra_frame))
{ Assume {label; guards; target= {func; version; pos}; varmap; extra_frames;} }
| STOP e=expression
{ Stop e }
| s=COMMENT
{ Comment s }
simple_expression:
| v=lit { Constant v }
| x=variable { Var x }
argument: e=expression { e }
expression:
| e = simple_expression { Simple e }
| LPAREN e1=simple_expression op=infixop e2=simple_expression RPAREN
{ Binop (op, e1, e2) }
| LPAREN op=prefixop e=simple_expression RPAREN
{ Unop (op, e) }
| x=variable LBRACKET index=simple_expression RBRACKET
{ Array_index (x, index) }
| LENGTH LPAREN e=simple_expression RPAREN
{ Array_length e }
label: id=IDENTIFIER { (id : Label.t) }
variable: id=IDENTIFIER { (id : Variable.t) }
prefixop:
| MINUS { Neg }
| BANG { Not }
infixop:
| DOUBLE_EQUAL { Eq }
| NOT_EQUAL { Neq }
| LT { Lt }
| LTE { Lte }
| GT { Gt }
| GTE { Gte }
| PLUS { Plus }
| MINUS { Sub }
| TIMES { Mult }
| DIVIDE { Div }
| MOD { Mod }
| DOUBLE_AMP { And }
| DOUBLE_PIPE { Or }
lit:
| NIL { (Nil : value) }
| SINGLE_QUOTE f=variable { (Fun_ref f : value) }
| b=BOOL { (Bool b : value) }
| n=INT { (Int n : value) }
value:
| lit { $1 }