-
Notifications
You must be signed in to change notification settings - Fork 0
/
sast.ml
114 lines (102 loc) · 3.87 KB
/
sast.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
(*
sast.ml
@authors Aidan Barg
Abby Larson
Claudia Aranda Barrios
Steven Oh
*)
open Ast
(*binop, uniop, dotcall*)
type sexpr = typ * sx
and sx =
SLiteral of int
| SBoolLit of bool
| SString of string
| SFliteral of string
| SId of string
| SBinop of sexpr * op * sexpr
| SCall of string * sexpr list
| SUnop of uop * sexpr
| SAssign of string * sexpr
| SDotCall of string * string * sexpr list
| SDotOp of string * string
| SDotAssign of string * string * sexpr
| SList of sexpr list
| SEdge of sexpr * sexpr
| SNoexpr
type sstmt =
SExpr of sexpr
| SReturn of sexpr
| SBlock of sb_line list
| SIf of sexpr * sstmt * sstmt
| SFor of sexpr * sexpr * sexpr * sstmt
| SWhile of sexpr * sstmt
and sb_line =
| SLocalBind of bind
| SLocalBindAssign of typ * string * sexpr (*CHANGED HERE ASK ABBY*)
| SLocalStatement of sstmt
(*and sblock_body = sb_line list*)
type sdecl =
SStatement of sstmt
| SBindAssign of typ * string * sexpr
| SBind of bind
| SFdecl of sfunc_decl
and sfunc_decl = {
styp : typ;
sfname : string;
sformals : bind list;
sbody : sstmt;
}
type sprogram = sdecl list
(*** Printing Functions Below ***)
let string_of_svdecl(t, id) = string_of_typ t ^ " " ^ id ^ ";\n"
let rec string_of_sbind_assign (t, id, e) =
string_of_typ t ^ " " ^ id ^ " = " ^ string_of_sexpr e ^ ";\n"
and
string_of_sexpr (t, e) =
"(" ^ string_of_typ t ^ " : " ^ (match e with
SLiteral(l) -> string_of_int l
| SBoolLit(l) -> string_of_bool l
| SString(s) -> "\"" ^ s ^ "\""
| SFliteral(f) -> f
| SCall(f, el) ->
f ^ "(" ^ String.concat ", " (List.map string_of_sexpr el) ^ ")"
| SBinop(e1, o, e2) ->
string_of_sexpr e1 ^ " " ^ string_of_op o ^ " " ^ string_of_sexpr e2
| SId(s) -> s
| SUnop(o, e) -> string_of_uop o ^ string_of_sexpr e
| SAssign(n, e) -> n ^ " = " ^ string_of_sexpr e
| SDotCall(n1, n2, el) -> n1 ^ "." ^ n2 ^ "(" ^ String.concat ", " (List.map string_of_sexpr el) ^ ")"
| SDotOp(n1, n2) -> n1 ^ "." ^ n2
| SDotAssign(n1, n2, e) -> n1 ^ "." ^ n2 ^ " = " ^ string_of_sexpr e
| SList(es) -> "[" ^ String.concat ", " (List.map string_of_sexpr es) ^ "]"
| SEdge(e1, e2) -> string_of_sexpr e1 ^ " -> " ^ string_of_sexpr e2
| SNoexpr -> ""
) ^ ")"
let rec string_of_sstmt stmt = match stmt with
SExpr(expr) -> string_of_sexpr expr ^ ";\n"
| SReturn(expr) -> "return " ^ string_of_sexpr expr ^ ";\n"
| SBlock(sbs) -> "{\n" ^ string_of_sblines sbs ^ "}\n"
| SIf(e, s, SBlock([])) ->
"if (" ^ string_of_sexpr e ^ ")\n" ^ string_of_sstmt s
| SIf(e, s1, s2) -> "if (" ^ string_of_sexpr e ^ ")\n" ^
string_of_sstmt s1 ^ "else\n" ^ string_of_sstmt s2
| SFor(e1, e2, e3, s) ->
"for (" ^ string_of_sexpr e1 ^ " ; " ^ string_of_sexpr e2 ^ " ; " ^
string_of_sexpr e3 ^ ") " ^ string_of_sstmt s
| SWhile(e, s) -> "while (" ^ string_of_sexpr e ^ ") " ^ string_of_sstmt s
and string_of_sblines = function
[] -> ""
| SLocalBind b :: ds -> string_of_vdecl b ^ string_of_sblines ds
| SLocalBindAssign(typ, s, e) :: ds -> s ^ " = " ^ string_of_sexpr e ^ ";\n" ^ string_of_sblines ds (*CHANGEED HERE ASK ABBY*)
| SLocalStatement s :: ds -> string_of_sstmt s ^ string_of_sblines ds
let string_of_sfdecl sfdecl =
string_of_typ sfdecl.styp ^ " " ^
sfdecl.sfname ^ "(" ^ String.concat ", " (List.map (fun (t, id) -> string_of_typ t ^ " " ^ id) sfdecl.sformals) ^
") \n" ^ string_of_sstmt sfdecl.sbody
let rec string_of_sprogram = function
[] -> ""
| SStatement s :: ds -> string_of_sstmt s ^ string_of_sprogram ds
| SBindAssign(typ, s, e) :: ds -> s ^ " = " ^ string_of_sexpr e ^ ";\n" ^ string_of_sprogram ds
| SBind b :: ds -> string_of_vdecl b ^ string_of_sprogram ds
| SFdecl f :: ds -> string_of_sfdecl f ^ string_of_sprogram ds