forked from trulyshruti/PLTree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.ml
87 lines (80 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
type vtype =
Int | Char | Double | Bool | String | Any
type expr =
Tree of expr * expr list
| IntLit of string
| ChrLit of string
| FltLit of string
| StrLit of string
| GetBranch of expr * expr
| Void
| FunCall of string * expr
| Eq of expr * expr
| Neq of expr * expr
| Lt of expr * expr
| Leq of expr * expr
| Gt of expr * expr
| Geq of expr * expr
| Add of expr * expr
| Minus of expr * expr
| Mul of expr * expr
| Div of expr * expr
| Mod of expr * expr
| Cast of vtype * expr
| GetWidth of expr
| Id of string
type stmt =
While of expr * stmt
| If of expr * stmt
| IfElse of expr * stmt * stmt
| FuncDec of string * vtype * string * stmt
| VarDec of vtype * string * expr
| Assn of string * expr
| Expr of expr
| Return of expr
| Seq of stmt list
type program = stmt list
let string_of_vtype = function
Int -> "int"
| Char -> "char"
| Double -> "double"
| Bool -> "bool"
| String -> "string"
| Any -> "any"
let rec string_of_expr = function
Tree(e,l) -> string_of_expr e ^ " {{ " ^
String.concat ", " (List.map string_of_expr l) ^ "}}"
| IntLit(s) -> s
| ChrLit(s) -> s
| FltLit(s) -> s
| StrLit(s) -> s
| GetBranch(e1,e2) -> string_of_expr e1 ^ "." ^ string_of_expr e2
| GetWidth(e1) -> string_of_expr e1
| Void -> "void"
| FunCall(s,e) -> s ^ "(" ^ string_of_expr e ^ ")"
| Eq(e1, e2) -> string_of_expr e1 ^ "==" ^ string_of_expr e2
| Neq(e1, e2) -> string_of_expr e1 ^ "!=" ^ string_of_expr e2
| Lt(e1, e2) -> string_of_expr e1 ^ "<" ^ string_of_expr e2
| Leq(e1, e2) -> string_of_expr e1 ^ "<=" ^ string_of_expr e2
| Gt(e1, e2) -> string_of_expr e1 ^ ">" ^ string_of_expr e2
| Geq(e1, e2) -> string_of_expr e1 ^ ">=" ^ string_of_expr e2
| Add(e1, e2) -> string_of_expr e1 ^ "+" ^ string_of_expr e2
| Minus(e1, e2) -> string_of_expr e1 ^ "-" ^ string_of_expr e2
| Mul(e1, e2) -> string_of_expr e1 ^ "*" ^ string_of_expr e2
| Div(e1, e2) -> string_of_expr e1 ^ "/" ^ string_of_expr e2
| Mod(e1, e2) -> string_of_expr e1 ^ "%" ^ string_of_expr e2
| Cast(vt,e) -> string_of_vtype vt ^ " to " ^ string_of_expr e
| Id(s) -> s
(* mixing c-like and pltree-like syntax. not on purpose *)
let rec string_of_stmt = function
While(e,s) -> "While(" ^ string_of_expr e ^ ") {" ^ string_of_stmt s ^ "}"
| If(e,s) -> "If(" ^ string_of_expr e ^ ") {" ^ string_of_stmt s ^ "}"
| IfElse(e,s1,s2) -> "If(" ^ string_of_expr e ^ ") {" ^ string_of_stmt s1 ^ "} Else {" ^ string_of_stmt s2 ^ "}"
| FuncDec(s,vt,vn,l) -> s ^ "[" ^ string_of_stmt l ^ "]"
| VarDec(v,s,e) -> s ^ " = " ^ string_of_expr e
| Assn(s,e) -> s ^ " = " ^ string_of_expr e
| Expr(e) -> string_of_expr e
| Return(e) -> "Return(" ^ string_of_expr e ^ ")"
| Seq(l) -> String.concat ", " (List.map string_of_stmt l)
let string_of_program stmts =
String.concat " " (List.map string_of_stmt stmts)