-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax.sml
89 lines (87 loc) · 2.15 KB
/
syntax.sml
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
structure Syntax = struct
(* abstract syntax tree of expression *)
datatype exp =
(* constant *)
CONST of Const.t
(* variable *)
| VAR of string
(* if M then N_1 else N_2 *)
| IF of exp * exp * exp
(* fn x => M *)
| ABS of string * exp
(* M N *)
| APP of exp * exp
(* let d in N end *)
| LET of dec list * exp
(* (M_1, ... , M_n) *)
| TUPLE of exp list
(* case M of (x_1, ... , x_n) => N *)
| CASE of exp * string list * exp
(* op (+) (M_1, ..., M_n) *)
| PRIM of Prim.t * exp list
(* abstract syntax tree of declaration *)
and dec =
(* val x = M *)
VAL of string * exp
(* val rec f = M *)
| VALREC of string * exp
(* pretty-printer *)
(* as you can see, this implementation is conservative *)
fun seqToString l = PP.seqToString (String.toString, "()", ", ", "(", ")") l
fun expToString (CONST c) = Const.toString c
| expToString (VAR x) = x
| expToString (IF (m, n1, n2)) =
"(if "
^ expToString m
^ " then "
^ expToString n1
^ " else "
^ expToString n2
^ ")"
| expToString (ABS (x, m)) =
"(fn "
^ x
^ " => "
^ expToString m
^ ")"
| expToString (APP (m, n)) =
"("
^ expToString m
^ " "
^ expToString n
^ ")"
| expToString (LET (d, m)) =
"let "
^ decToString d
^ " in "
^ expToString m
^ " end"
| expToString (TUPLE ms) =
expSeqToString ms
| expToString (CASE (m, xs, n)) =
"(case "
^ expToString m
^ " of "
^ seqToString xs
^ " => "
^ expToString n
^ ")"
| expToString (PRIM (p, ms)) =
"(op "
^ Prim.toString p
^ " "
^ expSeqToString ms
^ ")"
and expSeqToString seq = PP.seqToString (expToString, "()", ", ", "(", ")") seq
and decToString dec = PP.seqToString (fn
VAL (x, m) =>
"val "
^ x
^ " = "
^ expToString m
| VALREC (f, m) =>
"val rec "
^ f
^ " = "
^ expToString m, "", "; ", "", "") dec
end