Commits

Cheikh Younes  committed d47e6e4

optimize code

  • Participants
  • Parent commits 5aba18d

Comments (0)

Files changed (1)

       (******* Exercice 14 **********)
       (******************************)
 
-let print_decl = function 
-  Decl(v,e) -> "/"^v^" "^print_expr(e)^"def\n" 
+let print_decl (Decl(v,e)) =  
+  "/"^v^" "^print_expr(e)^"def\n" 
 ;;
 (* val print_decl : decl -> string = <fun> *)
 
-let rec print_prog = function 
-  Prog(dl, s) -> if List.length dl = 1 then print_decl(List.hd dl)^print_stmt(s)
-                 else print_decl(List.hd dl)^print_prog(Prog(List.tl dl, s)) 
+ (*
+ * function return string
+ * concatenating a list of strings 
+ *)
+let rec print_string_list = function 
+  [] -> ""
+  | s::l -> s^print_string_list(l) 
 ;;
+(* val print_string_list : string list -> string = <fun> *) 
+
+let print_prog = function 
+   Prog(dl, s) -> (print_string_list(List.map print_decl dl))^print_stmt(s) ;; 
+
 (* val print_prog : prog -> string = <fun> *)
 
 type rw_status = 
  *)
 
 let rec vlist_contains = function
-  (_, []) -> 0
-  | ( (a:varname) , (( v:varname)  ,R)::l) -> if a = v then 1 else vlist_contains(a,l)
-  | ( (a:varname) , (( v:varname)  ,RW)::l) -> if a = v then 2 else vlist_contains(a,l)
+  (_, []) -> false (* return false, and don't raise excpetion 
+                      The exception will be raised next on the function that
+                      invoke this function with a personalized message *)
+  | ( (a:varname) , (( v:varname)  ,permission)::l) -> if a = v then true else vlist_contains(a,l)
 ;;
 
 (*
 
 let rec tc_expr = function
     (vars, Const(_))    -> true
-  | (vars, Var(v) )    -> let retVal = vlist_contains(v, vars) in 
-      if retVal<>0 then true 
-      else 
-        failwith("Variable undefined: "^ v)
+  | (vars, Var(v) )    -> vlist_contains(v, vars) 
   | (vars, Add(e1,e2)) -> tc_expr(vars, e1) && tc_expr(vars, e2) 
   | (vars, Sub(e1,e2)) -> tc_expr(vars, e1) && tc_expr(vars, e2) 
   | (vars, Mul(e1,e2)) -> tc_expr(vars, e1) && tc_expr(vars, e2)
   (vars, Basic(c,s)) -> 
     tc_coord(vars, c) && tc_shape(vars, s) 
   | (vars, Assign(v, e) ) -> 
-    if vlist_contains(v, vars) = 2 then 
+    if vlist_contains(v, vars) then 
       tc_expr(vars, e) 
     else
       failwith("Error: Need RW permission"^v)
     else
       tc_stmt(vars, (List.hd s)) && tc_stmt(vars, (Seq(List.tl s))) 
   | (vars, For(v,e1,e2,e3,s)) -> 
-    if vlist_contains(v, vars) <> 0 then
+    if vlist_contains(v, vars) then
       failwith("Error: Double declaration\n"^v^" was here!)") 
     else
       tc_stmt(vars@[v,R], s)
 let rec tc_decls = function
   (vars, [] ) -> vars
   |(vars, Decl(v,e)::ds) -> 
-    if vlist_contains(v, vars) <>0 then
+    if vlist_contains(v, vars) then
       failwith("Error: Double declaration\n"^v^" was here!")
     else if not(tc_expr(vars, e)) then
       failwith("Type Error in: \n"^print_expr(e))