/
ast.ml
206 lines (191 loc) · 3.88 KB
/
ast.ml
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
open Base
type zarith = Z.t
let sexp_of_zarith z = Z.to_string z |> sexp_of_string
let zarith_of_sexp s = string_of_sexp s |> Z.of_string
type boolop =
| And
| Or
[@@deriving sexp]
type unaryop =
| Invert
| Not
| UAdd
| USub
[@@deriving sexp]
type operator =
| Add
| Sub
| Mult
| MatMult
| Div
| Mod
| Pow
| LShift
| RShift
| BitOr
| BitXor
| BitAnd
| FloorDiv
[@@deriving sexp]
type cmpop =
| Eq
| NotEq
| Lt
| LtE
| Gt
| GtE
| Is
| IsNot
| In
| NotIn
[@@deriving sexp]
type position = Lexing.position =
{ pos_fname : string
; pos_lnum : int
; pos_bol : int
; pos_cnum : int
}
[@@deriving sexp]
type 'a with_loc =
{ loc : position * position
; value : 'a
}
[@@deriving sexp]
type stmt =
| FunctionDef of
{ name : string
; args : arguments
; body : stmt with_loc list
}
| ClassDef of
{ name : string
; args : arguments
; body : stmt with_loc list
}
| If of
{ test : expr with_loc
; body : stmt with_loc list
; orelse : stmt with_loc list
}
| For of
{ target : expr with_loc
; iter : expr with_loc
; body : stmt with_loc list
; orelse : stmt with_loc list
}
| While of
{ test : expr with_loc
; body : stmt with_loc list
; orelse : stmt with_loc list
}
| Raise of
{ exc : expr with_loc option
; cause : expr with_loc option
}
| Try of
{ body : stmt with_loc list
; handlers : excepthandler list
; orelse : stmt with_loc list
; finalbody : stmt with_loc list
}
| With of
{ body : stmt with_loc list
; context : expr with_loc
; vars : expr with_loc option
}
| Assert of
{ test : expr with_loc
; msg : expr with_loc option
}
| Import of importname list
| ImportFrom of string * importname list
| Expr of { value : expr with_loc }
| Assign of
{ targets : expr with_loc list
; value : expr with_loc
}
| AugAssign of
{ target : expr with_loc
; op : operator
; value : expr with_loc
}
| Return of { value : expr with_loc option }
| Delete of { targets : expr with_loc list }
| Pass
| Break
| Continue
and expr =
| None_
| Bool of bool
| Num of zarith
| Float of float
| Str of string
| Name of string
| List of expr with_loc array
| Dict of { key_values : (expr with_loc * expr with_loc) list }
| ListComp of
{ elt : expr with_loc
; generators : comprehension list
}
| Tuple of expr with_loc array
| Lambda of
{ args : arguments
; body : expr with_loc
}
| BoolOp of
{ op : boolop
; values : expr with_loc list
}
| BinOp of
{ left : expr with_loc
; op : operator
; right : expr with_loc
}
| UnaryOp of
{ op : unaryop
; operand : expr with_loc
}
| IfExp of
{ test : expr with_loc
; body : expr with_loc
; orelse : expr with_loc
}
| Compare of
{ left : expr with_loc
; ops_and_exprs : (cmpop * expr with_loc) list
}
| Call of
{ func : expr with_loc
; args : expr with_loc list
; keywords : (string * expr with_loc) list
}
| Attribute of
{ value : expr with_loc
; attr : string
}
| Subscript of
{ value : expr with_loc
; slice : expr with_loc (* TODO: proper slices *)
}
and comprehension =
{ target : expr with_loc
; iter : expr with_loc
; ifs : expr with_loc list
}
and arguments =
{ args : string list
; vararg : string option
; kwonlyargs : (string * expr with_loc) list
; kwarg : string option
}
and excepthandler =
{ type_ : expr with_loc option
; name : string option
; body : stmt with_loc list
}
and importname =
{ import_name : string
; as_name : string option
}
[@@deriving sexp]
type t = stmt with_loc list [@@deriving sexp]