summary refs log tree commit diff
path: root/source/xi_lib/ast.ml
blob: 3123af7a4b3fa5606e76e7a5586e897ff68b4cc5 (plain) (blame)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
type location = Location of { line: int; column: int; file: string }

let string_of_location (Location {line;column;file}) =
  Format.sprintf "%s:%u:%u" file line column

type identifier
 = Identifier of string

let string_of_identifier (Identifier i) = i

type node_tag =
  NodeTag of int

let string_of_node_tag (NodeTag i) =
  Format.sprintf "%%node%i" i

type binop =
  | BINOP_And
  | BINOP_Or
  | BINOP_Add
  | BINOP_Sub
  | BINOP_Mult
  | BINOP_Div
  | BINOP_Rem

let string_of_binop = function
  | BINOP_And -> "&"
  | BINOP_Or -> "|"
  | BINOP_Add -> "+"
  | BINOP_Sub -> "-"
  | BINOP_Mult -> "*"
  | BINOP_Div -> "/"
  | BINOP_Rem -> "%"

type relop =
  | RELOP_Eq
  | RELOP_Ne
  | RELOP_Lt
  | RELOP_Gt
  | RELOP_Le
  | RELOP_Ge

let string_of_relop = function
  | RELOP_Eq -> "=="
  | RELOP_Ne -> "!="
  | RELOP_Lt -> "<"
  | RELOP_Gt -> ">"
  | RELOP_Ge -> ">="
  | RELOP_Le -> "<="

type unop =
  | UNOP_Not
  | UNOP_Neg

type type_expression =
  | TEXPR_Int of
    { loc:location
    }

  | TEXPR_Bool of
    { loc:location
    }

  | TEXPR_Array of
    { loc:location
    ; sub:type_expression
    ; dim:expression option
    }

and expression =
  | EXPR_Id of
    { tag:node_tag
    ; loc:location
    ; id:identifier
    }

  | EXPR_Int of 
    { tag:node_tag
    ; loc:location
    ; value:Int32.t
    }

  | EXPR_Char of 
    { tag:node_tag
    ; loc:location
    ; value:Char.t
    }

  | EXPR_String of
    { tag:node_tag
    ; loc:location
    ; value:string
    }

  | EXPR_Bool of 
    { tag:node_tag
    ; loc:location
    ; value:bool
    }

  | EXPR_Relation of
    { tag:node_tag
    ; loc:location
    ; op:relop
    ; lhs:expression
    ; rhs:expression
    }

  | EXPR_Binop of
    { tag:node_tag
    ; loc:location
    ; op:binop
    ; lhs:expression
    ; rhs:expression
    }

  | EXPR_Length of
    { tag:node_tag
    ; loc:location
    ; arg:expression
    }

  | EXPR_Unop of
    { tag:node_tag
    ; loc:location
    ; op:unop
    ; sub:expression
    }

  | EXPR_Call of
    call

  | EXPR_Index of
    { tag:node_tag
    ; loc:location
    ; expr:expression
    ; index:expression
    }

  | EXPR_Struct of
    { tag:node_tag
    ; loc:location
    ; elements: expression list
    }

and call =
  | Call of 
    { tag:node_tag
    ; loc:location
    ; callee:identifier
    ; arguments:expression list
    }


let location_of_expression = function
  | EXPR_Id {loc; _} -> loc
  | EXPR_Struct {loc; _} -> loc
  | EXPR_Index {loc; _} -> loc
  | EXPR_Call (Call {loc; _}) -> loc
  | EXPR_Unop {loc; _} -> loc
  | EXPR_Binop {loc; _} -> loc
  | EXPR_Relation {loc; _} -> loc
  | EXPR_Length {loc; _} -> loc
  | EXPR_Int {loc; _} -> loc
  | EXPR_Char {loc; _} -> loc
  | EXPR_Bool {loc; _} -> loc
  | EXPR_String {loc; _} -> loc

let tag_of_expression = function
  | EXPR_Id {tag; _} -> tag
  | EXPR_Struct {tag; _} -> tag
  | EXPR_Index {tag; _} -> tag
  | EXPR_Call (Call {tag; _}) -> tag
  | EXPR_Unop {tag; _} -> tag
  | EXPR_Binop {tag; _} -> tag
  | EXPR_Relation {tag; _} -> tag
  | EXPR_Length {tag; _} -> tag
  | EXPR_Int {tag; _} -> tag
  | EXPR_Char {tag; _} -> tag
  | EXPR_Bool {tag; _} -> tag
  | EXPR_String {tag; _} -> tag

let location_of_call (Call {loc; _}) = loc

let identifier_of_call (Call {callee; _}) = callee

type var_declaration =
  |  VarDecl of
    { loc:location
    ; id:identifier
    ; tp:type_expression
    }

let location_of_var_declaration (VarDecl {loc; _}) = loc
let identifier_of_var_declaration (VarDecl {id; _}) = id
let type_expression_of_var_declaration (VarDecl {tp; _}) = tp

module IdMap = Map.Make(struct
  type t = identifier

  let compare = compare
 end)

type lvalue =
  | LVALUE_Id of
    { loc:location
    ; id:identifier
    }

  | LVALUE_Index of
    { loc:location
    ; sub:expression
    ; index:expression
    }

type statement =
  | STMT_Call of
    call

  | STMT_Assign of
    { loc:location
    ; lhs:lvalue
    ; rhs:expression
    }

  | STMT_VarDecl of
    { var:var_declaration
    ; init:expression option
    }

  | STMT_If of
    { loc:location
    ; cond:expression
    ; then_branch: statement
    ; else_branch: statement option
    }

  | STMT_While of
    { loc:location
    ; cond:expression
    ; body: statement
    }

  | STMT_Return of
    { loc:location
    ; values:expression list
    }

  | STMT_MultiVarDecl of
    { loc:location 
    ; vars:var_declaration option list
    ; init:call
    }

  | STMT_Block of
    statement_block 

and statement_block =
  | STMTBlock of 
    { loc:location
    ; body: statement list
    }

let location_of_block (STMTBlock {loc; _}) = loc

let location_of_statement = function
  | STMT_Assign {loc; _} -> loc
  | STMT_While {loc; _} -> loc
  | STMT_Call call -> location_of_call call
  | STMT_Block block -> location_of_block block
  | STMT_Return {loc; _} -> loc 
  | STMT_VarDecl {var; _} -> location_of_var_declaration var
  | STMT_MultiVarDecl {loc; _} -> loc
  | STMT_If {loc; _} -> loc

type global_declaration =
  | GDECL_Function of 
    { loc:location
    ; id:identifier
    ; formal_parameters:var_declaration list
    ; return_types:type_expression list
    ; body:statement_block option
    }
  
type module_definition = ModuleDefinition of
  { global_declarations: global_declaration list
  }