]> Joshua Wise's Git repositories - snipe.git/blobdiff - parse/ast.sml
Initial import of l2c
[snipe.git] / parse / ast.sml
index bd121e1753f15c8bf1a82f08ea03e0dc8945d82f..ec538808be4c0eb5b40555154de402072cc8cba6 100644 (file)
@@ -1,7 +1,9 @@
-(* L1 Compiler
+(* L2 Compiler
  * Abstract Syntax Trees
  * Author: Alex Vaynberg
  * Modified: Frank Pfenning <fp@cs.cmu.edu>
+ * Modified: Joshua Wise <jwise@andrew.cmu.edu>
+ * Modified: Chris Lu <czl@andrew.cmu.edu>
  *
  * Uses pretty printing library
  * structure PP  -- see util/pp.sml
@@ -18,15 +20,37 @@ sig
    | DIVIDEDBY
    | MODULO
    | NEGATIVE                  (* unary minus *)
+   | LSH
+   | RSH
+   | LOGOR
+   | LOGAND
+   | BITAND
+   | BITXOR
+   | BITOR
+   | BITNOT
+   | BANG                       (* logical not *)
+   | NEQ
+   | EQ
+   | LT
+   | LE
+   | GE
+   | GT
 
   datatype exp =
      Var of ident
    | ConstExp of Word32.word
    | OpExp of oper * exp list
-   | Marked of exp Mark.marked
+   | Marked of (* Kane *) exp Mark.marked
   and stm =
      Assign of ident * exp
    | Return of exp
+   | Nop
+   | Break
+   | Continue
+   | If of exp * stm list * stm list option
+   | For of stm option * exp * stm option * stm list
+   | While of exp * stm list
+   | MarkedStm of stm Mark.marked
 
   type program = stm list
 
@@ -51,6 +75,21 @@ struct
    | DIVIDEDBY
    | MODULO
    | NEGATIVE                  (* unary minus *)
+   | LSH
+   | RSH
+   | LOGOR
+   | LOGAND
+   | BITAND
+   | BITXOR
+   | BITOR
+   | BITNOT
+   | BANG
+   | NEQ
+   | EQ
+   | LT
+   | LE
+   | GE
+   | GT
 
   datatype exp =
      Var of ident
@@ -59,7 +98,14 @@ struct
    | Marked of exp Mark.marked
   and stm =
      Assign of ident * exp
-   | Return of exp  
+   | Return of exp
+   | Nop
+   | Break
+   | Continue
+   | If of exp * stm list * stm list option
+   | For of stm option * exp * stm option * stm list
+   | While of exp * stm list
+   | MarkedStm of stm Mark.marked
 
   type program = stm list
 
@@ -76,6 +122,21 @@ struct
       | pp_oper DIVIDEDBY = "/"
       | pp_oper MODULO = "%"
       | pp_oper NEGATIVE = "-"
+      | pp_oper LSH = "<<"
+      | pp_oper RSH = ">>"
+      | pp_oper LOGAND = "&&"
+      | pp_oper LOGOR = "||"
+      | pp_oper BITAND = "&"
+      | pp_oper BITXOR = "^"
+      | pp_oper BITOR = "|"
+      | pp_oper BITNOT = "~"
+      | pp_oper BANG = "!"
+      | pp_oper NEQ = "!="
+      | pp_oper EQ = "=="
+      | pp_oper LT = "<"
+      | pp_oper LE = "<="
+      | pp_oper GT = ">"
+      | pp_oper GE = ">="
 
     fun pp_exp (Var(id)) = pp_ident id
       | pp_exp (ConstExp(c)) = Word32Signed.toString c
@@ -84,6 +145,8 @@ struct
       | pp_exp (OpExp(oper, [e1,e2])) =
          "(" ^ pp_exp e1 ^ " " ^ pp_oper oper
          ^ " " ^ pp_exp e2 ^ ")"
+      | pp_exp (OpExp(oper, _)) =
+          pp_oper oper
       | pp_exp (Marked(marked_exp)) =
          pp_exp (Mark.data marked_exp)
 
@@ -91,6 +154,22 @@ struct
        pp_ident id ^ " = " ^ pp_exp e ^ ";"
       | 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
+      | pp_stm (MarkedStm m) = pp_stm (Mark.data m)
+
+    and pp_block (nil) = ";"
+      | pp_block (a::nil) = pp_stm a
+      | pp_block (l) = let
+          val contents = map pp_stm l
+        in
+          "{" ^ String.concat contents ^ "}"
+        end
 
     fun pp_stms nil = ""
       | pp_stms (s::ss) = pp_stm s ^ "\n" ^ pp_stms ss
This page took 0.123743 seconds and 4 git commands to generate.