]> Joshua Wise's Git repositories - snipe.git/blame - parse/ast.sml
Initial import of l1c
[snipe.git] / parse / ast.sml
CommitLineData
12aa4087
JW
1(* L1 Compiler
2 * Abstract Syntax Trees
3 * Author: Alex Vaynberg
4 * Modified: Frank Pfenning <fp@cs.cmu.edu>
5 *
6 * Uses pretty printing library
7 * structure PP -- see util/pp.sml
8 *)
9
10signature AST =
11sig
12 type ident = Symbol.symbol
13
14 datatype oper =
15 PLUS
16 | MINUS
17 | TIMES
18 | DIVIDEDBY
19 | MODULO
20 | NEGATIVE (* unary minus *)
21
22 datatype exp =
23 Var of ident
24 | ConstExp of Word32.word
25 | OpExp of oper * exp list
26 | Marked of exp Mark.marked
27 and stm =
28 Assign of ident * exp
29 | Return of exp
30
31 type program = stm list
32
33 (* print as source, with redundant parentheses *)
34 structure Print :
35 sig
36 val pp_exp : exp -> string
37 val pp_stm : stm -> string
38 val pp_program : program -> string
39 end
40
41end
42
43structure Ast :> AST =
44struct
45 type ident = Symbol.symbol
46
47 datatype oper =
48 PLUS
49 | MINUS
50 | TIMES
51 | DIVIDEDBY
52 | MODULO
53 | NEGATIVE (* unary minus *)
54
55 datatype exp =
56 Var of ident
57 | ConstExp of Word32.word
58 | OpExp of oper * exp list
59 | Marked of exp Mark.marked
60 and stm =
61 Assign of ident * exp
62 | Return of exp
63
64 type program = stm list
65
66 (* print programs and expressions in source form
67 * using redundant parentheses to clarify precedence
68 *)
69 structure Print =
70 struct
71 fun pp_ident id = Symbol.name id
72
73 fun pp_oper PLUS = "+"
74 | pp_oper MINUS = "-"
75 | pp_oper TIMES = "*"
76 | pp_oper DIVIDEDBY = "/"
77 | pp_oper MODULO = "%"
78 | pp_oper NEGATIVE = "-"
79
80 fun pp_exp (Var(id)) = pp_ident id
81 | pp_exp (ConstExp(c)) = Word32Signed.toString c
82 | pp_exp (OpExp(oper, [e])) =
83 pp_oper oper ^ "(" ^ pp_exp e ^ ")"
84 | pp_exp (OpExp(oper, [e1,e2])) =
85 "(" ^ pp_exp e1 ^ " " ^ pp_oper oper
86 ^ " " ^ pp_exp e2 ^ ")"
87 | pp_exp (Marked(marked_exp)) =
88 pp_exp (Mark.data marked_exp)
89
90 fun pp_stm (Assign (id,e)) =
91 pp_ident id ^ " = " ^ pp_exp e ^ ";"
92 | pp_stm (Return e) =
93 "return " ^ pp_exp e ^ ";"
94
95 fun pp_stms nil = ""
96 | pp_stms (s::ss) = pp_stm s ^ "\n" ^ pp_stms ss
97
98 fun pp_program ss = "{\n" ^ pp_stms ss ^ "}"
99 end
100end
This page took 0.030601 seconds and 4 git commands to generate.