X-Git-Url: http://git.joshuawise.com/snipe.git/blobdiff_plain/1144856ba9d6018d9922c6ede7e97779a0fe6373..a83f1d602c6f50eb9ab0448a20f0ecb80fefcead:/parse/ast.sml?ds=inline diff --git a/parse/ast.sml b/parse/ast.sml index 84b2356..ce756ab 100644 --- a/parse/ast.sml +++ b/parse/ast.sml @@ -13,13 +13,6 @@ signature AST = sig type ident = Symbol.symbol - datatype vtype = Int | Typedef of ident | Pointer of vtype | Array of vtype | TNull - val typeeq : vtype * vtype -> bool - val castable : vtype * vtype -> bool (* true if the second type can be casted to the first implicitly *) - type variable = ident * vtype - datatype typedef = Struct of variable list - | MarkedTypedef of typedef Mark.marked - datatype oper = PLUS | MINUS @@ -46,6 +39,7 @@ sig datatype exp = Var of ident | ConstExp of Word32.word + | StringExp of string | OpExp of oper * exp list | Marked of (* Kane *) exp Mark.marked | FuncCall of ident * (exp list) @@ -53,9 +47,10 @@ sig | DerefMember of exp * ident | Dereference of exp | ArrIndex of exp * exp - | New of vtype - | NewArr of vtype * exp + | New of Type.vtype + | NewArr of Type.vtype * exp | Null + | Conditional of exp * exp * exp and stm = Assign of exp * exp | AsnOp of oper * exp * exp @@ -70,17 +65,16 @@ sig | MarkedStm of stm Mark.marked datatype function = - Extern of vtype * (variable list) - | Function of vtype * (variable list) * (variable list) * stm list + 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 = typedef Symbol.table * function Symbol.table + type program = Type.typedef Symbol.table * function Symbol.table (* print as source, with redundant parentheses *) structure Print : sig val pp_exp : exp -> string - val pp_type : vtype -> string val pp_stm : stm -> string val pp_program : program -> string end @@ -90,20 +84,6 @@ structure Ast :> AST = struct type ident = Symbol.symbol - datatype vtype = Int | Typedef of ident | Pointer of vtype | Array of vtype | TNull - fun typeeq (Int, Int) = true - | typeeq (Typedef a, Typedef b) = (Symbol.name a) = (Symbol.name b) - | typeeq (Pointer a, Pointer b) = typeeq (a, b) - | typeeq (Array a, Array b) = typeeq (a, b) - | typeeq (TNull, TNull) = true - | typeeq _ = false - fun castable (Pointer _, TNull) = true - | castable (Array _, TNull) = true - | castable (a, b) = typeeq (a, b) - type variable = ident * vtype - datatype typedef = Struct of variable list - | MarkedTypedef of typedef Mark.marked - datatype oper = PLUS | MINUS @@ -130,6 +110,7 @@ struct datatype exp = Var of ident | ConstExp of Word32.word + | StringExp of string | OpExp of oper * exp list | Marked of exp Mark.marked | FuncCall of ident * (exp list) @@ -137,9 +118,10 @@ struct | DerefMember of exp * ident | Dereference of exp | ArrIndex of exp * exp - | New of vtype - | NewArr of vtype * exp + | New of Type.vtype + | NewArr of Type.vtype * exp | Null + | Conditional of exp * exp * exp and stm = Assign of exp * exp | AsnOp of oper * exp * exp @@ -154,11 +136,11 @@ struct | MarkedStm of stm Mark.marked datatype function = - Extern of vtype * (variable list) - | Function of vtype * (variable list) * (variable list) * stm list + 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 = typedef Symbol.table * function Symbol.table + type program = Type.typedef Symbol.table * function Symbol.table (* print programs and expressions in source form * using redundant parentheses to clarify precedence @@ -191,6 +173,7 @@ struct fun pp_exp (Var(id)) = pp_ident id | pp_exp (ConstExp(c)) = Word32Signed.toString c + | pp_exp (StringExp(s)) = "\"" ^ s ^ "\"" | pp_exp (OpExp(oper, [e])) = pp_oper oper ^ "(" ^ pp_exp e ^ ")" | pp_exp (OpExp(oper, [e1,e2])) = @@ -205,9 +188,10 @@ struct | 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(" ^ pp_type t ^ ")" - | pp_exp (NewArr (t, s)) = "new(" ^ pp_type t ^ "[" ^ pp_exp s ^ "])" + | 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)) @@ -240,27 +224,18 @@ struct and pp_stms nil = "" | pp_stms (s::ss) = pp_stm s ^ "\n" ^ pp_stms ss - - and pp_type Int = "int" - | pp_type (Typedef i) = pp_ident i - | pp_type (Pointer t) = pp_type t ^ "*" - | pp_type (Array t) = pp_type t ^ "[]" - | pp_type TNull = "{NULL type}" - + and pp_params nil = "" - | pp_params ((i, t)::a::l) = (pp_ident i) ^ " : " ^ (pp_type t) ^ ", " ^ (pp_params (a::l)) - | pp_params ((i, t)::l) = (pp_ident i) ^ " : " ^ (pp_type t) ^ (pp_params l) + | 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) ^ " : " ^ (pp_type t) ^ ";\n" ^ (pp_vars l) + | 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 " ^ (pp_type t) ^ " " ^ (pp_ident n) ^ "(" ^ (pp_params pl) ^ ");\n" - | pp_function (n, Function(t, pl, vl, stms)) = (pp_type t) ^ " " ^ (pp_ident n) ^ "(" ^ (pp_params pl) ^ ")\n{\n" ^ (pp_vars vl) ^ (String.concat (map pp_stm stms)) ^ "\n}\n" + 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_typedef (i, Struct (v)) = "struct " ^ (pp_ident i) ^ " {\n" ^ (String.concat (map (fn (i', t) => " " ^ (pp_ident i') ^ " : " ^ (pp_type t) ^ ";\n") v)) ^ "}\n" - | pp_typedef (i, MarkedTypedef d) = pp_typedef (i, Mark.data d) - - and pp_program (types, funs) = String.concat ((map pp_typedef (Symbol.elemsi types)) @ (map pp_function (Symbol.elemsi funs))) + + and pp_program (types, funs) = String.concat ((map Type.Print.pp_typedef (Symbol.elemsi types)) @ (map pp_function (Symbol.elemsi funs))) end end