Commits

Anonymous committed 87d37bc

i love it when a parser comes together

  • Participants
  • Parent commits d2dc535

Comments (0)

Files changed (3)

   | EAssign of (expr * oper * expr)
   | EBinary of (expr * oper * expr)
   | ENone
+  | EId of string
 
 type stmt = 
   | SIf
   | SWith
   | SReturn
   | SEndSemi
-  | SList
-  | SExprs
+  | SExprs of expr list
+  | SList of stmt list
 
 type elem = 
   | ELFunc of (string * string list * stmt list)
   | ELStmt of stmt
 
-type prog = elem list
-
-(*type expr =
-    EInt of int
-  | ENil
-  | ESelf
-  | EString of string
-  | ELocal of string    (* Read a local variable *)
-  | EField of string    (* Read a field *)
-  | EIf of expr * expr * expr
-  | ESeq of expr * expr
-  | EWrite of string * expr  (* Write a local variable *)
-  | EWriteField of string * expr  (* Write a field *)
-  | ENew of string
-  | EInvoke of expr * string * (expr list)
-  | ECast of expr * typ
-
-and typ =
-  | TBot
-  | TClass of string
-  | TMeth of (typ list) * typ
-
-(*  meth name * arg name list * method body *)
-type meth = { meth_name : string;
-	      meth_ret: typ;
-	      meth_args : (string * typ) list;
-	      meth_locals : (string * typ) list;
-	      meth_body : expr }
-
-(* class name * superclass name * methods *)
-type cls = { cls_name : string;
-	     cls_super : string;
-	     cls_fields : (string * typ) list;
-	     cls_meths : meth list }
-
-(* classes * top-level expression *)
-type prog = { prog_clss : cls list;
-	      prog_main : expr }
-*)
+type prog = elem list
     close_in chan;
     p
 
+let rec print_params params = match params with 
+  | [] -> ()
+  | (c::r) -> (match r with 
+                | [] -> Printf.printf "%s" c 
+                | _ -> Printf.printf "%s," c
+              );
+              print_params r
+
+let rec print_statement s = match s with
+  | SIf -> Printf.printf("If\n")
+  | SWhile -> Printf.printf("While\n")
+  | SFor -> Printf.printf("For\n")
+  | SIfElse -> Printf.printf("IfElse\n")
+  | SBreak -> Printf.printf("Break\n")
+  | SContinue -> Printf.printf("Continue\n")
+  | SWith -> Printf.printf("With\n")
+  | SReturn -> Printf.printf("Return\n")
+  | SEndSemi -> () (*Printf.printf("EndSemi\n") *)
+  | SExprs exprs -> Printf.printf("Exprs\n")
+  | SList stmts -> print_statements stmts
+and print_statements statements = match statements with 
+  [] -> ()
+  | (c::r) -> print_statement c;
+              print_statements r
+
+let print_elem (e: elem) : unit = match e with 
+  | ELStmt s -> Printf.printf "ELStmt\n";
+  | ELFunc (name, params, statements) -> 
+    Printf.printf "Func name == %s\nParams == " name;
+    print_params params;
+    Printf.printf "Statements == \n";
+    print_statements statements
+
+let print_program (p : prog ) : unit = List.fold_left (fun _ i -> print_elem i ) () p
+
 let main () = match Array.length Sys.argv with 
-  | 2 -> let p = parse_file Sys.argv.(1) in
-          Printf.printf "Write this part";
+  | 2 ->  let p = parse_file Sys.argv.(1) in
+          print_program p
   | _ -> Printf.printf "bad args\n";
 ;;
 
 
 program:
  { [] }
- | element program { $1::$2 }
+ | element program { print_endline "program"; $1::$2 }
 ;
 
 element:
 ;
 
 multiplicativeexpression:
-	unaryexpression { $1 } 
+	unaryexpression { print_endline "use multplicative expr"; $1 } 
 	| unaryexpression multiplicativeoperator multiplicativeexpression { ENone }
 ;
 
 
 primaryexpression:
 	LP expression RP { ENone }
-	| ID { ENone }
+	| ID { print_endline "using EID"; EId($1) }
 	| INT { ENone }
 	| STR { ENone }
 	| FALSE { ENone }
 ;
 
 memberexpression:
-	primaryexpression { $1 }
+	primaryexpression { print_endline "memberexpression"; $1 }
 	| primaryexpression DOT memberexpression { ENone }
 	| primaryexpression LBAR expression RBAR { ENone }
 	| primaryexpression LP argumentlistopt RP { ENone }
 ;
 
 unaryexpression:
-	memberexpression { $1 }
+	memberexpression { print_endline "using unary expression"; $1 }
 	| unaryoperator unaryexpression { ENone }
 	| MINUS unaryexpression { ENone }
 	| incrementoperator memberexpression { ENone }
 
 relationaloperator:
 	NEQ { ONeq }
+	|LT { OLT }
+	|GT { OGT }
 ;
 
 relationalexpression:
   | CONTINUE SEMI { SContinue }
   | WITH LP expression RP statement { SWith }
   | RETURN expressionopt { SReturn }
+  | compoundstatement { SList($1) }
+  | variablesorexpression { SExprs($1::[]) }
 ;