mirror of
https://github.com/ProgramSnail/Lama.git
synced 2025-12-26 00:28:47 +00:00
Added X86 codegeneration interface and tests
This commit is contained in:
parent
77ec064c5c
commit
de018e76aa
12 changed files with 320 additions and 34 deletions
|
|
@ -43,19 +43,62 @@ module Expr =
|
|||
|
||||
Takes a state and an expression, and returns the value of the expression in
|
||||
the given state.
|
||||
*)
|
||||
let eval _ = failwith "Not implemented yet"
|
||||
*)
|
||||
let to_func op =
|
||||
let bti = function true -> 1 | _ -> 0 in
|
||||
let itb b = b <> 0 in
|
||||
let (|>) f g = fun x y -> f (g x y) in
|
||||
match op with
|
||||
| "+" -> (+)
|
||||
| "-" -> (-)
|
||||
| "*" -> ( * )
|
||||
| "/" -> (/)
|
||||
| "%" -> (mod)
|
||||
| "<" -> bti |> (< )
|
||||
| "<=" -> bti |> (<=)
|
||||
| ">" -> bti |> (> )
|
||||
| ">=" -> bti |> (>=)
|
||||
| "==" -> bti |> (= )
|
||||
| "!=" -> bti |> (<>)
|
||||
| "&&" -> fun x y -> bti (itb x && itb y)
|
||||
| "!!" -> fun x y -> bti (itb x || itb y)
|
||||
| _ -> failwith (Printf.sprintf "Unknown binary operator %s" op)
|
||||
|
||||
let rec eval st expr =
|
||||
match expr with
|
||||
| Const n -> n
|
||||
| Var x -> st x
|
||||
| Binop (op, x, y) -> to_func op (eval st x) (eval st y)
|
||||
|
||||
(* Expression parser. You can use the following terminals:
|
||||
|
||||
IDENT --- a non-empty identifier a-zA-Z[a-zA-Z0-9_]* as a string
|
||||
DECIMAL --- a decimal constant [0-9]+ as a string
|
||||
|
||||
|
||||
*)
|
||||
ostap (
|
||||
parse: empty {failwith "Not implemented yet"}
|
||||
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
|
||||
|
||||
(* Simple statements: syntax and sematics *)
|
||||
|
|
@ -74,15 +117,26 @@ module Stmt =
|
|||
|
||||
(* Statement evaluator
|
||||
|
||||
val eval : config -> t -> config
|
||||
val eval : config -> t -> config
|
||||
|
||||
Takes a configuration and a statement, and returns another configuration
|
||||
*)
|
||||
let eval _ = failwith "Not implemented yet"
|
||||
|
||||
let rec eval ((st, i, o) as conf) stmt =
|
||||
match stmt with
|
||||
| Read x -> (match i with z::i' -> (Expr.update x z st, i', o) | _ -> failwith "Unexpected end of input")
|
||||
| Write e -> (st, i, o @ [Expr.eval st e])
|
||||
| Assign (x, e) -> (Expr.update x (Expr.eval st e) st, i, o)
|
||||
| Seq (s1, s2) -> eval (eval conf s1) s2
|
||||
|
||||
(* 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
|
||||
|
|
@ -100,3 +154,6 @@ type t = Stmt.t
|
|||
*)
|
||||
let eval p i =
|
||||
let _, _, o = Stmt.eval (Expr.empty, i, []) p in o
|
||||
|
||||
(* Top-level parser *)
|
||||
let parse = Stmt.parse
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue