Add let
This commit is contained in:
parent
5aaa261198
commit
2be0b7f794
4 changed files with 28 additions and 0 deletions
5
ast.ml
5
ast.ml
|
@ -99,6 +99,7 @@ end
|
|||
type t =
|
||||
| Value of Value.t
|
||||
| Var of string
|
||||
| Let of string * t
|
||||
| Binop of t * Binop.t * t
|
||||
| Set_binop_pre of Binop.t * t
|
||||
| Get_binop_pre of Binop.t
|
||||
|
@ -117,6 +118,10 @@ let print ast =
|
|||
let rec aux = function
|
||||
| Value n -> pv n
|
||||
| Var v -> pr "%s" v
|
||||
| Let (v, e) ->
|
||||
pr "(let %s " v;
|
||||
aux e;
|
||||
pr ")"
|
||||
| Binop (left, op, right) -> begin
|
||||
let op = Binop.to_string op in
|
||||
pr "(%s " op; aux left; pr " "; aux right; pr ")";
|
||||
|
|
4
eval.ml
4
eval.ml
|
@ -31,6 +31,10 @@ let eval vars ast =
|
|||
| Binop (l, op, r) ->
|
||||
let l = aux l and r = aux r in
|
||||
binop op l r
|
||||
| Let (var, e) ->
|
||||
let v = aux e in
|
||||
Hashtbl.replace vars var v;
|
||||
v
|
||||
| Set_binop_pre (op, l) ->
|
||||
let l =
|
||||
match aux l with
|
||||
|
|
17
parser.ml
17
parser.ml
|
@ -78,6 +78,14 @@ let token tok seq =
|
|||
if x = tok then x, seq
|
||||
else expected @@ Token.to_string tok
|
||||
|
||||
let any_ident seq =
|
||||
match seq () with
|
||||
| Seq.Nil -> expected "ident"
|
||||
| Seq.Cons (x, seq) -> begin match x with
|
||||
| Token.Ident id -> id, seq
|
||||
| _ -> unexpected_token x
|
||||
end
|
||||
|
||||
let idents set seq =
|
||||
match seq () with
|
||||
| Seq.Nil ->
|
||||
|
@ -122,6 +130,7 @@ let rec expr pre seq =
|
|||
seq |> oneof [
|
||||
level;
|
||||
assoc;
|
||||
let_value;
|
||||
value @> binop pre;
|
||||
]
|
||||
|
||||
|
@ -151,6 +160,14 @@ and assoc seq =
|
|||
else
|
||||
failwith "Parser.assoc"
|
||||
|
||||
(* let_value := "let" ident "=" expr *)
|
||||
and let_value seq =
|
||||
let _, seq = ident "let" seq in
|
||||
let id, seq = any_ident seq in
|
||||
let _, seq = token Token.Equal seq in
|
||||
let e, seq = expr min_int seq in
|
||||
Let (id, e), seq
|
||||
|
||||
(* value := int | float | ( expr ) *)
|
||||
and value seq =
|
||||
match seq () with
|
||||
|
|
2
token.ml
2
token.ml
|
@ -10,6 +10,7 @@ type t =
|
|||
| Percent
|
||||
| LParen
|
||||
| RParen
|
||||
| Equal
|
||||
|
||||
let tokens = ref [
|
||||
"+", Plus;
|
||||
|
@ -20,6 +21,7 @@ let tokens = ref [
|
|||
"%", Percent;
|
||||
"(", LParen;
|
||||
")", RParen;
|
||||
"=", Equal;
|
||||
]
|
||||
|
||||
let to_string = function
|
||||
|
|
Loading…
Add table
Reference in a new issue