Commits

Antoine Pietri committed 80acc63

functions are now stored in an hashtable

Comments (0)

Files changed (2)

 NAME := blaise
 FILES := AUTHORS ast.mli blaise.ml eval.ml lexer.ml Makefile parser.ml\
-	prettyprint.ml
+	prettyprint.ml parser/parser.mly parser/lexer.mll
 
 LOGIN := pietri_a
 RENDU := ${LOGIN}-mpoc2012
 all: ${FILES}
 	ocamlbuild ${NAME}.native
 	cp ${NAME}.native ${NAME}
-	${MAKE} clean
 
 blaise: all
 
 
 let eval_uniop op i = match op with
   | "!" -> int_of_bool (not (bool_of_int i))
+  | "-" -> -i
   | op  -> failwith ("Unknown operator " ^ op)
 
 let rec get_func f = function
   | Ast.BinOp(e1, op, e2) ->
       eval_binop op (eval_expr env lf e1) (eval_expr env lf e2)
   | Ast.UniOp(op, e) -> eval_uniop op (eval_expr env lf e)
-  | Ast.Call(f, exprl) -> call_func f lf (eval_list_expr env lf exprl)
+  | Ast.Call(f, exprl) -> call_func f env lf (eval_list_expr env lf exprl)
 
 and eval_list_expr env lf = List.map (function x -> eval_expr env lf x)
 
   | [] -> ()
   | st :: sts -> eval_statement env lf st; eval_statement_list env lf sts
 
-and call_func fname lf args = match fname with
-  | "write" -> List.iter (function x -> Printf.printf "%d\n" x) args; 0
-  | s -> let f = List.find (function x -> s = x.Ast.fname) lf in
-      eval_func f lf args
+and call_func fname env lf args = match fname with
+  | "write" -> List.iter (fun x -> print_int x; print_newline ()) args; 0
+  | "read"  -> read_int ()
+  | s       ->
+      let f = Hashtbl.find lf s in
+        eval_func f lf args
 
 and eval_func f lf args =
   let env = Hashtbl.create ((List.length args) + (List.length f.Ast.fvars)) in
   let env = Hashtbl.create (List.length entry.Ast.mainvars) in
     eval_statement_list env lf entry.Ast.mainbody
 
-let eval prg =
-  eval_main prg.Ast.func prg.Ast.main
+let get_functions prg =
+  let lf = Hashtbl.create 0 in
+  let rec add_hashtbl = function
+    | [] -> ()
+    | x :: xs -> Hashtbl.add lf x.Ast.fname x; add_hashtbl xs
+  in add_hashtbl prg.Ast.func;
+     lf
 
+let eval prg =
+  eval_main (get_functions prg) prg.Ast.main