X-Git-Url: http://git.joshuawise.com/snipe.git/blobdiff_plain/0a24e44d4e9f82f8d3d83de8e58c83c8cf2868b6..141a7120aec6ddf3ab097f275498d8e3a1b90401:/parse/ast.sml?ds=sidebyside diff --git a/parse/ast.sml b/parse/ast.sml index ec53880..75f7042 100644 --- a/parse/ast.sml +++ b/parse/ast.sml @@ -1,4 +1,4 @@ -(* L2 Compiler +(* L3 Compiler * Abstract Syntax Trees * Author: Alex Vaynberg * Modified: Frank Pfenning @@ -12,7 +12,7 @@ signature AST = sig type ident = Symbol.symbol - + datatype oper = PLUS | MINUS @@ -41,8 +41,19 @@ sig | ConstExp of Word32.word | OpExp of oper * exp list | Marked of (* Kane *) exp Mark.marked + | FuncCall of ident * (exp list) + | Member of exp * ident + | DerefMember of exp * ident + | Dereference of exp + | ArrIndex of exp * exp + | New of Type.vtype + | NewArr of Type.vtype * exp + | Null + | Conditional of exp * exp * exp and stm = - Assign of ident * exp + Assign of exp * exp + | AsnOp of oper * exp * exp + | Effect of exp (* Just side effect the expression *) | Return of exp | Nop | Break @@ -52,7 +63,12 @@ sig | While of exp * stm list | MarkedStm of stm Mark.marked - type program = stm list + datatype function = + Extern of Type.vtype * (Type.variable list) + | Function of Type.vtype * (Type.variable list) * (Type.variable list) * stm list + | MarkedFunction of function Mark.marked + + type program = Type.typedef Symbol.table * function Symbol.table (* print as source, with redundant parentheses *) structure Print : @@ -61,7 +77,6 @@ sig val pp_stm : stm -> string val pp_program : program -> string end - end structure Ast :> AST = @@ -96,8 +111,19 @@ struct | ConstExp of Word32.word | OpExp of oper * exp list | Marked of exp Mark.marked + | FuncCall of ident * (exp list) + | Member of exp * ident + | DerefMember of exp * ident + | Dereference of exp + | ArrIndex of exp * exp + | New of Type.vtype + | NewArr of Type.vtype * exp + | Null + | Conditional of exp * exp * exp and stm = - Assign of ident * exp + Assign of exp * exp + | AsnOp of oper * exp * exp + | Effect of exp (* Just side effect the expression *) | Return of exp | Nop | Break @@ -107,7 +133,12 @@ struct | While of exp * stm list | MarkedStm of stm Mark.marked - type program = stm list + datatype function = + Extern of Type.vtype * (Type.variable list) + | Function of Type.vtype * (Type.variable list) * (Type.variable list) * stm list + | MarkedFunction of function Mark.marked + + type program = Type.typedef Symbol.table * function Symbol.table (* print programs and expressions in source form * using redundant parentheses to clarify precedence @@ -147,20 +178,37 @@ struct ^ " " ^ pp_exp e2 ^ ")" | pp_exp (OpExp(oper, _)) = pp_oper oper + | pp_exp (FuncCall(id, l)) = pp_ident id ^ "(" ^ pp_expl l ^ ")" | pp_exp (Marked(marked_exp)) = pp_exp (Mark.data marked_exp) - - fun pp_stm (Assign (id,e)) = - pp_ident id ^ " = " ^ pp_exp e ^ ";" + | pp_exp (Member(e, i)) = pp_exp e ^ "." ^ pp_ident i + | pp_exp (DerefMember(e, i)) = pp_exp e ^ "->" ^ pp_ident i + | pp_exp (Dereference(e)) = "*(" ^ pp_exp e ^ ")" + | pp_exp (ArrIndex(e1, e2)) = pp_exp e1 ^ "[" ^pp_exp e2 ^ "]" + | pp_exp (New t) = "new(" ^ Type.Print.pp_type t ^ ")" + | pp_exp (NewArr (t, s)) = "new(" ^ Type.Print.pp_type t ^ "[" ^ pp_exp s ^ "])" + | pp_exp Null = "NULL" + | pp_exp (Conditional (q, e1, e2)) = "("^(pp_exp q)^"?"^(pp_exp e1)^":"^(pp_exp e2)^")" + + and pp_expl nil = "" + | pp_expl (e::a::l) = (pp_exp e) ^ ", " ^ (pp_expl (a::l)) + | pp_expl (e::l) = (pp_exp e) ^ (pp_expl l) + + and pp_stm (Assign (e1,e2)) = + pp_exp e1 ^ " = " ^ pp_exp e2 ^ ";\n" + | pp_stm (AsnOp (oop, e1, e2)) = + pp_exp e1 ^ " " ^ pp_oper oop ^ "= " ^ pp_exp e2 ^ ";\n" + | pp_stm (Effect (e)) = + pp_exp e ^ ";\n" | pp_stm (Return e) = - "return " ^ pp_exp e ^ ";" - | pp_stm Nop = ";" - | pp_stm Break = "break;" - | pp_stm Continue = "continue;" - | pp_stm (If (e, s, NONE)) = "if ("^pp_exp e^")"^pp_block s - | pp_stm (If (e, s, SOME s2)) = "if ("^pp_exp e^")"^pp_block s^" else "^pp_block s2 - | pp_stm (While (e, s)) = "while ("^pp_exp e^") "^pp_block s - | pp_stm (For (so1, e, so2, s)) = "for ("^ (if (isSome so1) then pp_stm (valOf so1) else "") ^ pp_exp e ^ (if(isSome so2) then pp_stm (valOf so2) else "") ^ ")" ^ pp_block s + "return " ^ pp_exp e ^ ";\n" + | pp_stm Nop = ";\n" + | pp_stm Break = "break;\n" + | pp_stm Continue = "continue;\n" + | pp_stm (If (e, s, NONE)) = "if ("^pp_exp e^")\n"^pp_block s + | pp_stm (If (e, s, SOME s2)) = "if ("^pp_exp e^")\n"^pp_block s^"else\n"^pp_block s2 + | pp_stm (While (e, s)) = "while ("^pp_exp e^")\n"^pp_block s + | pp_stm (For (so1, e, so2, s)) = "for ("^ (if (isSome so1) then pp_stm (valOf so1) else "") ^ pp_exp e ^ (if(isSome so2) then pp_stm (valOf so2) else "") ^ ")\n" ^ pp_block s | pp_stm (MarkedStm m) = pp_stm (Mark.data m) and pp_block (nil) = ";" @@ -168,12 +216,23 @@ struct | pp_block (l) = let val contents = map pp_stm l in - "{" ^ String.concat contents ^ "}" + "{\n" ^ String.concat contents ^ "}\n" end - fun pp_stms nil = "" + and pp_stms nil = "" | pp_stms (s::ss) = pp_stm s ^ "\n" ^ pp_stms ss - fun pp_program ss = "{\n" ^ pp_stms ss ^ "}" + and pp_params nil = "" + | pp_params ((i, t)::a::l) = (pp_ident i) ^ " : " ^ (Type.Print.pp_type t) ^ ", " ^ (pp_params (a::l)) + | pp_params ((i, t)::l) = (pp_ident i) ^ " : " ^ (Type.Print.pp_type t) ^ (pp_params l) + + and pp_vars nil = "" + | pp_vars ((i, t)::l) = "var " ^ (pp_ident i) ^ " : " ^ (Type.Print.pp_type t) ^ ";\n" ^ (pp_vars l) + + and pp_function (n, Extern(t, pl)) = "extern " ^ (Type.Print.pp_type t) ^ " " ^ (pp_ident n) ^ "(" ^ (pp_params pl) ^ ");\n" + | pp_function (n, Function(t, pl, vl, stms)) = (Type.Print.pp_type t) ^ " " ^ (pp_ident n) ^ "(" ^ (pp_params pl) ^ ")\n{\n" ^ (pp_vars vl) ^ (String.concat (map pp_stm stms)) ^ "\n}\n" + | pp_function (n, MarkedFunction d) = pp_function (n, Mark.data d) + + and pp_program (types, funs) = String.concat ((map Type.Print.pp_typedef (Symbol.elemsi types)) @ (map pp_function (Symbol.elemsi funs))) end end