Commits

relsa  committed f3c7533

Ex3.4

  • Participants
  • Parent commits b1a7fcd

Comments (0)

Files changed (5)

 Interpreter/parser.output
 Interpreter/lexer.ml
 Interpreter/.depend
+Interpreter/miniml

File Interpreter/eval.ml

             BoolV true -> eval_exp env exp2 
           | BoolV false -> eval_exp env exp3
           | _ -> err ("Test expression must be boolean: if"))
-
+  | LetExp (id, exp1, exp2) -> (* Ex3.4 *)
+    let value = eval_exp env exp1 in
+    eval_exp (Environment.extend id value env) exp2
 let eval_decl env = function
     Exp e -> let v = eval_exp env e in ("-", env, v)
+  | Decl (id, e) -> (* Ex3.4 *)
+    let v = eval_exp env e in (id, Environment.extend id v env, v)
+;;

File Interpreter/lexer.mll

   ("if", Parser.IF);
   ("then", Parser.THEN);
   ("true", Parser.TRUE);
+  ("in", Parser.IN); (* Ex3.4 *)
+  ("let", Parser.LET); (* Ex3.4 *)
 ] 
 }
 
 | "<" { Parser.LT }
 | "&&" { Parser.AND } (* Ex3.3 *)
 | "||" { Parser.OR } (* Ex3.3 *)
+| "=" { Parser.EQ } (* Ex3.4 *)
 
 | ['a'-'z'] ['a'-'z' '0'-'9' '_' '\'']*
     { let id = Lexing.lexeme lexbuf in

File Interpreter/parser.mly

 %token IF THEN ELSE TRUE FALSE
 
 %token AND OR // Ex3.3
-
+%token LET IN EQ // Ex3.4
 
 
 %token <int> INTV
 
 toplevel :
     Expr SEMISEMI { Exp $1 }
+  | LET ID EQ Expr SEMISEMI { Decl ($2, $4) }
 
 Expr :
     IfExpr { $1 }
+  | LetExpr { $1 }
   | LOExpr { $1 } // Ex3.3
 
+LetExpr : // Ex3.4
+    LET ID EQ Expr IN Expr { LetExp ($2, $4, $6) }
 
 LOExpr : // Ex3.3
     LAExpr OR LOExpr { BinOp (Or, $1, $3) }

File Interpreter/syntax.ml

              And | Or (* Ex3.3 *)
 
 type exp =
-    Var of id
-  | ILit of int
-  | BLit of bool
-  | BinOp of binOp * exp * exp
-  | IfExp of exp * exp * exp
+  Var of id
+| ILit of int
+| BLit of bool
+| BinOp of binOp * exp * exp
+| IfExp of exp * exp * exp
+| LetExp of id * exp * exp
 
 type program = 
-    Exp of exp
+  Exp of exp
+| Decl of id * exp