diff options
author | lrg <lrg@fca1b0fc-160b-0410-b1d3-a4f43f01ea2e> | 2006-10-20 10:38:22 +0000 |
---|---|---|
committer | lrg <lrg@fca1b0fc-160b-0410-b1d3-a4f43f01ea2e> | 2006-10-20 10:38:22 +0000 |
commit | f0198ebf9430d286ce7c9a53b703e967ce86481c (patch) | |
tree | ac069673f4a94e079bf12505c4f0a58baeea34ef /test/littlesemantics/little.ml | |
parent | eb7c8587f462adca878088ef5f610c81734efc70 (diff) | |
download | compcert-f0198ebf9430d286ce7c9a53b703e967ce86481c.tar.gz compcert-f0198ebf9430d286ce7c9a53b703e967ce86481c.zip |
interpreter for "little"
git-svn-id: https://yquem.inria.fr/compcert/svn/compcert/trunk@119 fca1b0fc-160b-0410-b1d3-a4f43f01ea2e
Diffstat (limited to 'test/littlesemantics/little.ml')
-rw-r--r-- | test/littlesemantics/little.ml | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/test/littlesemantics/little.ml b/test/littlesemantics/little.ml new file mode 100644 index 00000000..1a7a77ec --- /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"));; |