-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.ml
More file actions
131 lines (107 loc) · 3.84 KB
/
ast.ml
File metadata and controls
131 lines (107 loc) · 3.84 KB
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
type var_type = Int | String | Stack | Float | Void | Eos
type binop = Add | Sub | Mult | Div | Mod | Equal | Neq | And | Or| Lt | Leq | Gt | Geq
type unop = Not | Neg
type ident =
Ident of string
type datatype =
Datatype of var_type |
Stacktype of datatype|
Eostype of var_type
type expr =
IntLit of int |
StringLit of string |
FloatLit of float |
EosLit |
Variable of ident |
Unop of unop * expr |
Binop of expr * binop * expr |
Call of ident * expr list |
Push of ident * expr |
Pop of ident |
Peek of ident
type value =
ExprVal of expr
and decl =
VarDecl of datatype * ident |
VarAssignDecl of datatype * ident * value
type stmt =
Block of stmt list |
Expr of expr |
Declaration of decl |
Assign of ident * expr |
Transition of ident * expr |
Return of expr
type formal =
Formal of datatype * ident
type node =
Node of ident * stmt list
type dfa_decl = {
return : datatype;
dfa_name: ident;
formals : formal list;
var_body : decl list;
node_body : node list;
}
type program = dfa_decl list
(* "Pretty printed" version of the AST, meant to generate a MicroC program
from the AST. These functions are only for pretty-printing (the -a flag)
the AST and can be removed. *)
let string_of_ident = function
Ident(l) -> l
let rec string_of_expr = function
IntLit(l) -> string_of_int l
| StringLit(l) -> l
| FloatLit(l) -> string_of_float l
| Variable(id) -> string_of_ident id
| Unop(o, e) ->
string_of_expr e ^ " " ^
(match o with
Not -> "!" |
Neg -> "-")
| Binop(e1, o, e2) ->
string_of_expr e1 ^ " " ^
(match o with
Add -> "+" | Sub -> "-" | Mult -> "*" | Div -> "/"
| Equal -> "==" | Neq -> "!=" | Mod -> "%"
| Lt -> "<" | Leq -> "<=" | Gt -> ">" | Geq -> ">=" | And -> "&&" | Or -> "||" ) ^ " " ^
string_of_expr e2
| Call(id, e_list) -> string_of_ident id ^ " " ^
"(" ^ String.concat ", " (List.map string_of_expr e_list) ^ ")"
| Push(id, e) -> string_of_ident id ^ " " ^ string_of_expr e
| Pop(id) -> string_of_ident id
| Peek(id) -> string_of_ident id
| EosLit -> "EOSLIT"
let rec string_of_datatype = function
Datatype(vartype) ->
(match vartype with
Int -> "int" | String -> "String" | Stack -> "Stack" | Float -> "Float"
| Void -> "Void" | Eos -> "Eos"
)
| Stacktype(datatype) -> "Stack<" ^ string_of_datatype datatype ^ ">"
| Eostype(_) -> "EOS"
let string_of_decl = function
VarDecl(dt, id) -> string_of_datatype dt ^ " " ^ string_of_ident id
| VarAssignDecl(dt,id,value) -> string_of_datatype dt ^ " " ^ string_of_ident id
^ " = " ^ (match value with
ExprVal(e) -> string_of_expr e)
let rec string_of_stmt = function
Block(stmts) ->
"{\n" ^ String.concat "" (List.map string_of_stmt stmts) ^ "}\n"
| Expr(expr) -> string_of_expr expr ^ ";\n";
| Return(expr) -> "return " ^ string_of_expr expr ^ ";\n";
| Assign(id, expr) -> string_of_ident id ^ " = " ^ string_of_expr expr ^
";\n"
| Declaration(decl) -> string_of_decl decl
| Transition(id, expr) -> string_of_ident id ^ " <- (" ^ string_of_expr expr ^ ")"
let string_of_node = function
Node(id, stmtlist) -> string_of_ident id ^ " {\n" ^
String.concat "\n" (List.map string_of_stmt stmtlist) ^ "\n}"
let string_of_formal = function
Formal(dt, id) -> string_of_datatype dt ^ " " ^ string_of_ident id
let string_of_dfadecl dfadecl =
string_of_datatype dfadecl.return ^ " " ^ string_of_ident dfadecl.dfa_name ^ "(" ^ String.concat ", " (List.map string_of_formal dfadecl.formals) ^ ")\n{\n" ^
String.concat "" (List.map string_of_decl dfadecl.var_body) ^
String.concat "" (List.map string_of_node dfadecl.node_body) ^
"}\n"
let string_of_program (program) =
String.concat "" (List.map string_of_dfadecl program)