]> Joshua Wise's Git repositories - snipe.git/blobdiff - parse/ast.sml
Initial import of l3c
[snipe.git] / parse / ast.sml
index ec538808be4c0eb5b40555154de402072cc8cba6..fce70ab9d9e69275415d4acdf39563621bece8b2 100644 (file)
@@ -1,4 +1,4 @@
-(* L2 Compiler
+(* L3 Compiler
  * Abstract Syntax Trees
  * Author: Alex Vaynberg
  * Modified: Frank Pfenning <fp@cs.cmu.edu>
@@ -12,6 +12,9 @@
 signature AST =
 sig
   type ident = Symbol.symbol
+  
+  datatype vtype = Int
+  type variable = ident * vtype
 
   datatype oper = 
      PLUS
@@ -41,6 +44,7 @@ sig
    | ConstExp of Word32.word
    | OpExp of oper * exp list
    | Marked of (* Kane *) exp Mark.marked
+   | FuncCall of ident * (exp list)
   and stm =
      Assign of ident * exp
    | Return of exp
@@ -52,7 +56,11 @@ sig
    | While of exp * stm list
    | MarkedStm of stm Mark.marked
 
-  type program = stm list
+  datatype function =
+     Extern of vtype * ident * (variable list)
+   | Function of vtype * ident * (variable list) * (variable list) * stm list
+  
+  type program = function list
 
   (* print as source, with redundant parentheses *)
   structure Print :
@@ -68,6 +76,9 @@ structure Ast :> AST =
 struct
   type ident = Symbol.symbol
 
+  datatype vtype = Int
+  type variable = ident * vtype
+
   datatype oper = 
      PLUS
    | MINUS
@@ -96,6 +107,7 @@ struct
    | ConstExp of Word32.word
    | OpExp of oper * exp list
    | Marked of exp Mark.marked
+   | FuncCall of ident * (exp list)
   and stm =
      Assign of ident * exp
    | Return of exp
@@ -107,7 +119,11 @@ struct
    | While of exp * stm list
    | MarkedStm of stm Mark.marked
 
-  type program = stm list
+  datatype function =
+     Extern of vtype * ident * (variable list)
+   | Function of vtype * ident * (variable list) * (variable list) * stm list
+  
+  type program = function list
 
   (* print programs and expressions in source form
    * using redundant parentheses to clarify precedence
@@ -147,8 +163,13 @@ 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)
+    
+    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)
 
     fun pp_stm (Assign (id,e)) =
        pp_ident id ^ " = " ^ pp_exp e ^ ";"
@@ -168,12 +189,24 @@ 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_type Int = "int"
+    
+    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)
+    
+    and pp_vars nil = ""
+      | pp_vars ((i, t)::l) = "var " ^ (pp_ident i) ^ " : " ^ (pp_type t) ^ ";\n" ^ (pp_vars l)
+
+    and pp_function (Extern(t, n, pl)) = "extern " ^ (pp_type t) ^ " " ^ (pp_ident n) ^ "(" ^ (pp_params pl) ^ ");\n"
+      | pp_function (Function(t, n, 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_program (p) = String.concat (map pp_function p)
   end
 end
This page took 0.025221 seconds and 4 git commands to generate.