From f0198ebf9430d286ce7c9a53b703e967ce86481c Mon Sep 17 00:00:00 2001 From: lrg Date: Fri, 20 Oct 2006 10:38:22 +0000 Subject: interpreter for "little" git-svn-id: https://yquem.inria.fr/compcert/svn/compcert/trunk@119 fca1b0fc-160b-0410-b1d3-a4f43f01ea2e --- test/littlesemantics/little.ml | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 test/littlesemantics/little.ml (limited to 'test/littlesemantics/little.ml') diff --git a/test/littlesemantics/little.ml b/test/littlesemantics/little.ml new file mode 100644 index 0000000..1a7a77e --- /dev/null +++ b/test/littlesemantics/little.ml @@ -0,0 +1,43 @@ +type exp = Addition of exp*exp | Numeral of int | Variable of string;; +type b_exp = Greater of exp*exp;; +type inst = Assignment of string * exp | Sequence of inst * inst + | While of b_exp * inst | Skip;; + +type environment = (string * int) list;; + +let rec lookup (env:environment) (var:string) = + match env with + [] -> failwith(var ^ " : no such variable in environment for lookup") + | (a,v)::tl -> if a = var then v else lookup tl var;; + +let rec update (env:environment) (var:string) (n:int) = + match env with + [] -> failwith(var ^ " : no such variable in environment for update") + | ((a,v) as pair)::tl -> + if a = var then (a,n)::tl else pair::(update tl var n);; + +let rec evaluate (env:environment)(e:exp) = + match e with + Numeral n -> n + | Variable s -> lookup env s + | Addition(e1,e2) -> evaluate env e1 + evaluate env e2;; + + +let evaluate_bool (env:environment)(e:b_exp) = + match e with + Greater(e1,e2) -> evaluate env e1 > evaluate env e2;; + +let rec execute (env:environment)(i:inst) = + match i with + Skip -> env + | Assignment(s,e) -> update env s (evaluate env e) + | Sequence(i1,i2) -> execute (execute env i1) i2 + | While(b,i) as it -> + if (evaluate_bool env b) then + execute (execute env i) it + else + env;; + +let print_env = + List.iter + (fun (s,n) -> print_string (s ^ " : " ^ (string_of_int n) ^ "\n"));; -- cgit v1.2.3