Intermediate

This commit is contained in:
Dmitry Boulytchev 2018-02-25 17:39:54 +03:00
parent 53659aabaf
commit 22659d8fed
3 changed files with 31 additions and 7 deletions

View file

@ -43,8 +43,7 @@ module Expr =
Takes a state and an expression, and returns the value of the expression in
the given state.
*)
*)
let to_func op =
let bti = function true -> 1 | _ -> 0 in
let itb b = b <> 0 in
@ -71,9 +70,28 @@ module Expr =
| Var x -> st x
| Binop (op, x, y) -> to_func op (eval st x) (eval st y)
(* Statement parser *)
ostap (
parse: empty {failwith "Not implemented yet"}
(* Expression parser *)
ostap (
parse:
!(Ostap.Util.expr
(fun x -> x)
(Array.map (fun (a, s) -> a,
List.map (fun s -> ostap(- $(s)), (fun x y -> Binop (s, x, y))) s
)
[|
`Lefta, ["!!"];
`Lefta, ["&&"];
`Nona , ["=="; "!="; "<="; "<"; ">="; ">"];
`Lefta, ["+" ; "-"];
`Lefta, ["*" ; "/"; "%"];
|]
)
primary);
primary:
n:DECIMAL {Const n}
| x:IDENT {Var x}
| -"(" parse -")"
)
end
@ -107,7 +125,13 @@ module Stmt =
(* Statement parser *)
ostap (
parse: empty {failwith "Not implemented yet"}
parse:
s:stmt ";" ss:parse {Seq (s, ss)}
| stmt;
stmt:
"read" "(" x:IDENT ")" {Read x}
| "write" "(" e:!(Expr.parse) ")" {Write e}
| x:IDENT ":=" e:!(Expr.parse) {Assign (x, e)}
)
end