mirror of
https://github.com/ProgramSnail/Lama.git
synced 2025-12-24 07:38:46 +00:00
Testing
This commit is contained in:
parent
4f040532b2
commit
0b6f64646b
10 changed files with 727 additions and 60 deletions
49
src/DSL.ml
49
src/DSL.ml
|
|
@ -1,49 +0,0 @@
|
|||
(*
|
||||
open Expr
|
||||
|
||||
(* read x;
|
||||
read y;
|
||||
z = y*y;
|
||||
write (x+z)
|
||||
*)
|
||||
let t =
|
||||
Seq (
|
||||
Read "x",
|
||||
Seq (
|
||||
Read "y",
|
||||
Seq (
|
||||
Assign ("z", Mul (Var "y", Var "y")),
|
||||
Write (Add (Var "x", Var "z"))
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
let _ = match run [2; 3] t with [result] -> Printf.printf "Result: %d\n" result
|
||||
let _ = try ignore (run [2] t) with Failure s -> Printf.printf "Error: %s\n" s
|
||||
|
||||
let ( ! ) x = Var x
|
||||
let ( % ) n = Const n
|
||||
let ( + ) e1 e2 = Add (e1, e2)
|
||||
let ( * ) e1 e2 = Mul (e1, e2)
|
||||
|
||||
let skip = Skip
|
||||
let (:=) x e = Assign (x, e)
|
||||
let read x = Read x
|
||||
let write e = Write e
|
||||
let (|>) s1 s2 = Seq (s1, s2)
|
||||
|
||||
let t =
|
||||
read "x" |>
|
||||
read "y" |>
|
||||
("z" := !"y" * !"y") |>
|
||||
write (!"x" + !"z")
|
||||
|
||||
let _ = match run [2; 3] t with [result] -> Printf.printf "Result: %d\n" result
|
||||
let _ = try ignore (run [2] t) with Failure s -> Printf.printf "Error: %s\n" s
|
||||
|
||||
let run input stmt = srun input (compile_stmt stmt)
|
||||
|
||||
let _ = match run [2; 3] t with [result] -> Printf.printf "Result: %d\n" result
|
||||
let _ = try ignore (run [2] t) with Failure s -> Printf.printf "Error: %s\n" s
|
||||
|
||||
*)
|
||||
40
src/Embedding.ml
Normal file
40
src/Embedding.ml
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
(* A deep embedding of simple expressions in OCaml. *)
|
||||
|
||||
(* Opening GT yet again. *)
|
||||
open GT
|
||||
|
||||
(* Opening the substrate module for convenience. *)
|
||||
open Expr
|
||||
|
||||
(* Shortcuts for leaf constructors *)
|
||||
let ( ! ) x = Var x
|
||||
let ( !? ) n = Const n
|
||||
|
||||
(* Implementation of operators *)
|
||||
let binop op x y = Binop (op, x, y)
|
||||
|
||||
let ( + ) = binop "+"
|
||||
let ( - ) = binop "-"
|
||||
let ( * ) = binop "*"
|
||||
let ( / ) = binop "/"
|
||||
let ( % ) = binop "%"
|
||||
let ( < ) = binop "<"
|
||||
let ( <= ) = binop "<="
|
||||
let ( > ) = binop ">"
|
||||
let ( >= ) = binop ">="
|
||||
let ( == ) = binop "=="
|
||||
let ( != ) = binop "!="
|
||||
let ( && ) = binop "&&"
|
||||
let ( || ) = binop "!!"
|
||||
|
||||
(* Some predefined names for variables *)
|
||||
let x = !"x"
|
||||
let y = !"y"
|
||||
let z = !"z"
|
||||
let t = !"t"
|
||||
|
||||
(* Voila; comment this out before submitting the solution *)
|
||||
let _ =
|
||||
List.iter (fun e -> Printf.printf "eval s (%s) = %d\n" (show(expr) e) (eval s e)) [x+y*z- !?3; t-z+y && x]
|
||||
|
||||
|
||||
28
src/Expr.ml
28
src/Expr.ml
|
|
@ -1,10 +1,12 @@
|
|||
(* Simple expressions: syntax and semantics *)
|
||||
|
||||
(* Opening a library for generic programming (https://github.com/dboulytchev/GT).
|
||||
The library provides "@type ..." syntax extension and plugins like show, gmap, etc.
|
||||
The library provides "@type ..." syntax extension and plugins like show, etc.
|
||||
*)
|
||||
open GT
|
||||
|
||||
(* The type for the expression. Note, in regular OCaml there is no "@type..." notation,
|
||||
it came from GT. *)
|
||||
(* The type for the expression. Note, in regular OCaml there is no "@type..."
|
||||
notation, it came from GT. *)
|
||||
@type expr =
|
||||
(* integer constant *) | Const of int
|
||||
(* variable *) | Var of string
|
||||
|
|
@ -24,22 +26,28 @@ type state = string -> int
|
|||
(* Empty state: maps every variable into nothing. *)
|
||||
let empty = fun x -> failwith (Printf.sprintf "Undefined variable %s" x)
|
||||
|
||||
(* Update: non-destructively "modifies" the state s by binding the variable x to value v and
|
||||
returns the new state.
|
||||
(* Update: non-destructively "modifies" the state s by binding the variable x
|
||||
to value v and returns the new state.
|
||||
*)
|
||||
let update x v s = fun y -> if x = y then v else s y
|
||||
|
||||
(* An example of a non-trivial state: *)
|
||||
let s = update "x" 1 @@ update "y" 2 @@ update "z" 3 @@ update "t" 4 empty
|
||||
|
||||
(* Some testing *)
|
||||
(* Some testing; comment this definition out when submitting the solution. *)
|
||||
let _ =
|
||||
List.iter
|
||||
(fun x ->
|
||||
try Printf.printf "%s=%d\n" x @@ s x
|
||||
with Failure s -> Printf.printf "%s\n" s
|
||||
) ["x"; "a"; "y"; "z"; "t"; "b"]
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
(* Expression evaluator
|
||||
|
||||
val eval : state -> expr -> int
|
||||
|
||||
Takes a state and an expression, andreturns the value of the expression in
|
||||
the given state.
|
||||
*)
|
||||
let eval = failwith "Not implemented yet"
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ TOPFILE = rc
|
|||
OCAMLC = ocamlc
|
||||
OCAMLOPT = ocamlopt
|
||||
OCAMLDEP = ocamldep
|
||||
SOURCES = $(wildcard *.ml)
|
||||
SOURCES = Expr.ml Embedding.ml
|
||||
LIBS = GT.cma unix.cma re.cma re_emacs.cma re_str.cma
|
||||
CAMLP5 = -pp "camlp5o -I `ocamlfind -query GT.syntax` -I `ocamlfind -query ostap.syntax` pa_ostap.cmo pa_gt.cmo -L `ocamlfind -query GT.syntax`"
|
||||
PXFLAGS = $(CAMLP5)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue