forked from caterinaurban/function
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbanal_abstract_syntax.ml
182 lines (148 loc) · 4.57 KB
/
banal_abstract_syntax.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
(*
Abstract sytax tree output by the parser.
Copyright (C) 2011 Antoine Miné
*)
open Lexing
open Banal_datatypes
(************************************************************************)
(* TYPES *)
(************************************************************************)
(* position in the source file *)
type position = Lexing.position
type extent = position * position (* start/end *)
(* tree nodes are tagged with a source position *)
type 'a ext = 'a * extent
type int_type =
| A_CHAR (* 8-bit *)
| A_SHORT (* 16-bit *)
| A_INT (* 32-bit *)
| A_LONG (* 64-bit *)
| A_INTEGER (* arbitrary precision *)
type int_sign =
| A_SIGNED | A_UNSIGNED
type float_type =
| A_FLOAT | A_DOUBLE | A_REAL
type typ =
| A_int of int_type * int_sign
| A_float of float_type
| A_BOOL
type unary_op =
| A_UNARY_PLUS
| A_UNARY_MINUS
| A_NOT
| A_cast of typ ext
and binary_op =
| A_PLUS
| A_MINUS
| A_MULTIPLY
| A_DIVIDE
| A_MODULO
| A_EQUAL
| A_NOT_EQUAL
| A_LESS
| A_LESS_EQUAL
| A_GREATER
| A_GREATER_EQUAL
| A_AND
| A_OR
and incr =
| A_INCR
| A_DECR
and prepost =
| A_PRE
| A_POST
and binary_assign_op =
| A_PLUS_ASSIGN
| A_MINUS_ASSIGN
| A_MULTIPLY_ASSIGN
| A_DIVIDE_ASSIGN
| A_MODULO_ASSIGN
and expr =
| A_unary of unary_op * (expr ext)
| A_binary of binary_op * (expr ext) * (expr ext)
| A_assign of (lvalue ext) * (binary_assign_op option) * (expr ext)
| A_increment of (lvalue ext) * incr * prepost
| A_call of (string ext) * (expr ext list)
| A_identifier of string
| A_float_const of string
| A_int_const of string
| A_bool_const of bool
| A_float_itv of (string ext) * (string ext)
| A_int_itv of (string ext) * (string ext)
and stat =
| A_SKIP
| A_BREAK
| A_expr of expr ext
| A_if of (expr ext) * (stat ext) (* then *) * (stat ext option) (* else *)
| A_while of (expr ext) * (stat ext) (* body *)
| A_return of expr ext option
| A_block of stat ext list
| A_local of vardecl
| A_label of string ext
| A_assert of expr ext
| A_assume of expr ext
| A_print of (lvalue ext) list
and lvalue = string
and decl =
| A_global of (vardecl ext) * global_kind
| A_function of fundecl ext
and global_kind = A_VARIABLE | A_INPUT | A_VOLATILE
and vardecl = (typ ext) * (((string ext) * (expr ext option)) list)
and fundecl =
(typ ext option) *
(string ext) *
((string ext) * (typ ext)) list *
(stat ext list)
(************************************************************************)
(* UTILITIES *)
(************************************************************************)
let position_unknown = Lexing.dummy_pos
let extent_unknown = (position_unknown, position_unknown)
let cmp_neg = function
| A_EQUAL -> A_NOT_EQUAL
| A_NOT_EQUAL -> A_EQUAL
| A_LESS -> A_GREATER_EQUAL
| A_GREATER -> A_LESS_EQUAL
| A_LESS_EQUAL -> A_GREATER
| A_GREATER_EQUAL -> A_LESS
| _ -> invalid_arg "not a comparison operator"
(* order in increasing filename, and the position in file *)
let compare_position p1 p2 =
match compare p1.pos_fname p2.pos_fname with
| 0 -> compare p1.pos_cnum p2.pos_cnum
| _ -> compare p1.pos_fname p2.pos_fname
(* order in increasing start position, and then in reverse ending position *)
let compare_extent (b1,e1) (b2,e2) =
match compare_position b1 b2 with
| 0 -> compare_position e2 e1
| x -> x
(************************************************************************)
(* PRINTERS *)
(************************************************************************)
let string_of_position p =
Printf.sprintf "%s:%i:%i" p.pos_fname p.pos_lnum (p.pos_cnum - p.pos_bol)
let string_of_extent (p,q) =
if p.pos_fname = q.pos_fname then
if p.pos_lnum = q.pos_lnum then
if p.pos_cnum = q.pos_cnum then
Printf.sprintf "%s:%i.%i" p.pos_fname p.pos_lnum (p.pos_cnum - p.pos_bol)
else
Printf.sprintf "%s:%i.%i-%i" p.pos_fname p.pos_lnum (p.pos_cnum - p.pos_bol) (q.pos_cnum - q.pos_bol)
else
Printf.sprintf "%s:%i.%i-%i.%i" p.pos_fname p.pos_lnum (p.pos_cnum - p.pos_bol) q.pos_lnum (q.pos_cnum - q.pos_bol)
else
Printf.sprintf "%s:%i.%i-%s:%i.%i" p.pos_fname p.pos_lnum (p.pos_cnum - p.pos_bol) q.pos_fname q.pos_lnum (q.pos_cnum - q.pos_bol)
let string_of_typ = function
| A_BOOL -> "bool"
| A_float A_FLOAT -> "float"
| A_float A_DOUBLE -> "double"
| A_float A_REAL -> "real"
| A_int (k,s) ->
(if s = A_UNSIGNED then "unsigned " else "") ^
(match k with
| A_CHAR -> "char"
| A_SHORT -> "short"
| A_INT -> "int"
| A_LONG -> "long"
| A_INTEGER -> "integer"
)