-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.ml
115 lines (100 loc) · 2.7 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
type type_t_aux =
| Int_t
| Bool_t
type type_t =
| Int_t of type_t_aux * bool
| Bool_t of type_t_aux * bool
| Str_t
| Void_t
| Func_t of type_t * type_t list
let rec string_of_type_t t =
match t with
| Void_t -> "Void"
| Int_t (_,_) -> "Int"
| Bool_t (_,_) -> "Bool"
| Str_t -> "Str"
| Func_t (r, a) ->
(if (List.length a) > 1 then "(" else "")
^ (String.concat ", " (List.map string_of_type_t a))
^ (if (List.length a) > 1 then ")" else "")
^ " -> " ^ (string_of_type_t r)
module Syntax = struct
type ident = string
type expr =
| Void of {value: int
; pos: Lexing.position
}
| Int of { value: int
; pos: Lexing.position }
| Bool of { value:bool
; pos: Lexing.position }
| Str of { chaine: string
; pos: Lexing.position
}
| Var of { name: ident
; pos: Lexing.position }
| Call of { func: ident
; args: expr list
; pos: Lexing.position }
type instr =
| Decl of {
name : ident
; type_t: type_t
; pos: Lexing.position
}
| Assign of { var: ident
; expr: expr
; pos: Lexing.position }
| Return of { expr: expr
; pos: Lexing.position }
| Cond of { expr : expr
; block1 : block
; block2 : block
; pos : Lexing.position
}
|Boucle of { init : instr
;condit : expr
;incr : instr
;bloc_f : block
;pos : Lexing.position
}
|Boucle_while of {
condit : expr
;bloc_w : block
;pos : Lexing.position
}
|Call_func of { appel : expr
;pos : Lexing.position
}
and block = instr list
type def =
| Func of { nom: ident
; args: ident list
; block : instr list
; pos: Lexing.position }
type prog = def list
end
type value =
| Void
| Bool of int
| Int of int
| Str of string
module IR = struct
type ident = string
type expr =
| Value of value
| Var of ident
| Call of ident * expr list
type instr =
| Decl of ident
| Assign of ident * expr
| Return of expr
| Cond of expr * block * block
| Boucle of instr * expr * instr * block
| Boucle_while of expr * block
| Call_func of expr
and block = instr list
type def =
| Func of ident * ident list * block
type prog = def list
end