fixed paren

This commit is contained in:
Michael Zhang 2018-02-20 03:24:52 -06:00
parent 97971a1d0c
commit 1055ba154d
No known key found for this signature in database
GPG key ID: A1B65B603268116B
3 changed files with 20 additions and 19 deletions

View file

@ -44,16 +44,7 @@ let rec lookup ctx name : term =
let rec eval (ctx, t) = let rec eval (ctx, t) =
let rec helper (ctx, t) = let rec helper (ctx, t) =
match t with raise EvaluationComplete
| Assign(name, t') ->
let (ctx', r) = helper(ctx, t') in
(assign ctx (name, r), r)
| TmAbs(name, t') ->
let (ctx', v) = helper (ctx, t') in
(assign ctx (name, v), v)
| TmVar(name) ->
(ctx, lookup ctx name)
| _ -> raise EvaluationComplete
in try in try
let (ctx', t') = helper (ctx, t) in let (ctx', t') = helper (ctx, t) in
eval (ctx', t') eval (ctx', t')
@ -64,11 +55,17 @@ let _ =
let rec loop ctx = let rec loop ctx =
print_string "> "; flush stdout; print_string "> "; flush stdout;
let lexbuf = Lexing.from_channel stdin in let lexbuf = Lexing.from_channel stdin in
let t = Parser.main Lexer.token lexbuf in let x = Parser.main Lexer.token lexbuf in
let (ctx', r) = eval (ctx, t) in match x with
print_endline (string_of_term r); flush stdout; | Types.Term t ->
loop ctx' in let (ctx', r) = eval (ctx, t) in
loop [] print_endline (string_of_term r); flush stdout;
loop ctx'
| Types.Assign (n, t) ->
let (ctx', r) = eval (ctx, t) in
(* bind the name *)
loop ctx'
in loop []
with Lexer.Eof -> with Lexer.Eof ->
print_endline "^D"; print_endline "^D";
exit 0 exit 0

View file

@ -11,20 +11,21 @@
%token Lambda %token Lambda
%start main %start main
%type <Types.term> main %type <Types.input> main
%% %%
main: main:
| assign EOL { $1 } | assign EOL { $1 }
| expr EOL { $1 } | expr EOL { Types.Term($1) }
; ;
assign: assign:
| Ident Equal expr { Types.Assign($1, $3) } | Ident Equal expr { Types.Assign($1, $3) }
; ;
expr: expr:
| var { Types.TmVar($1) } | var { Types.TmVar($1) }
| LParen expr RParen { $2 }
| Lambda var Dot expr { Types.TmAbs($2, $4) } | Lambda var Dot expr { Types.TmAbs($2, $4) }
| LParen expr expr RParen { Types.TmApp ($2, $3) } | expr expr { Types.TmApp($1, $2) }
; ;
var: var:
Ident { $1 } Ident { $1 }

View file

@ -1,10 +1,13 @@
type var = string type var = string
type term = type term =
| Assign of string * term
| TmVar of var | TmVar of var
| TmAbs of var * term | TmAbs of var * term
| TmApp of term * term | TmApp of term * term
type input =
| Term of term
| Assign of string * term
type context = (string * term) list type context = (string * term) list