2018-02-20 01:28:29 +03:00
|
|
|
(* Opening a library for generic programming (https://github.com/dboulytchev/GT).
|
|
|
|
|
The library provides "@type ..." syntax extension and plugins like show, etc.
|
|
|
|
|
*)
|
2018-02-25 14:48:13 +03:00
|
|
|
open GT
|
|
|
|
|
|
|
|
|
|
(* Opening a library for combinator-based syntax analysis *)
|
2018-03-27 01:51:22 +03:00
|
|
|
open Ostap
|
|
|
|
|
open Combinators
|
2018-04-25 01:06:18 +03:00
|
|
|
|
|
|
|
|
(* Values *)
|
|
|
|
|
module Value =
|
|
|
|
|
struct
|
|
|
|
|
|
2018-11-13 09:54:04 +03:00
|
|
|
@type t = Int of int | String of bytes | Array of t array | Sexp of string * t list (*with show*)
|
2018-04-25 01:06:18 +03:00
|
|
|
|
|
|
|
|
let to_int = function
|
|
|
|
|
| Int n -> n
|
|
|
|
|
| _ -> failwith "int value expected"
|
|
|
|
|
|
|
|
|
|
let to_string = function
|
|
|
|
|
| String s -> s
|
|
|
|
|
| _ -> failwith "string value expected"
|
|
|
|
|
|
|
|
|
|
let to_array = function
|
|
|
|
|
| Array a -> a
|
|
|
|
|
| _ -> failwith "array value expected"
|
|
|
|
|
|
2018-05-01 02:57:09 +03:00
|
|
|
let sexp s vs = Sexp (s, vs)
|
2018-04-25 01:06:18 +03:00
|
|
|
let of_int n = Int n
|
|
|
|
|
let of_string s = String s
|
|
|
|
|
let of_array a = Array a
|
|
|
|
|
|
|
|
|
|
let tag_of = function
|
|
|
|
|
| Sexp (t, _) -> t
|
|
|
|
|
| _ -> failwith "symbolic expression expected"
|
|
|
|
|
|
2018-11-13 09:54:04 +03:00
|
|
|
let update_string s i x = Bytes.set s i x; s
|
|
|
|
|
let update_array a i x = a.(i) <- x; a
|
2018-10-31 20:10:50 +03:00
|
|
|
|
|
|
|
|
let string_val v =
|
|
|
|
|
let buf = Buffer.create 128 in
|
|
|
|
|
let append s = Buffer.add_string buf s in
|
|
|
|
|
let rec inner = function
|
|
|
|
|
| Int n -> append (string_of_int n)
|
2018-11-13 09:54:04 +03:00
|
|
|
| String s -> append "\""; append @@ Bytes.to_string s; append "\""
|
|
|
|
|
| Array a -> let n = Array.length a in
|
|
|
|
|
append "["; Array.iteri (fun i a -> (if i > 0 then append ", "); inner a) a; append "]"
|
2018-10-31 20:10:50 +03:00
|
|
|
| Sexp (t, a) -> let n = List.length a in
|
2018-10-31 21:48:44 +03:00
|
|
|
append "`"; append t; (if n > 0 then (append " ("; List.iteri (fun i a -> (if i > 0 then append ", "); inner a) a; append ")"))
|
2018-10-31 20:10:50 +03:00
|
|
|
in
|
|
|
|
|
inner v;
|
2018-11-13 09:54:04 +03:00
|
|
|
Bytes.of_string @@ Buffer.contents buf
|
2018-10-31 20:10:50 +03:00
|
|
|
|
2018-04-25 01:06:18 +03:00
|
|
|
end
|
|
|
|
|
|
2018-03-27 01:51:22 +03:00
|
|
|
(* States *)
|
|
|
|
|
module State =
|
|
|
|
|
struct
|
|
|
|
|
|
|
|
|
|
(* State: global state, local state, scope variables *)
|
2018-05-01 03:37:29 +03:00
|
|
|
type t =
|
|
|
|
|
| G of (string -> Value.t)
|
|
|
|
|
| L of string list * (string -> Value.t) * t
|
2018-03-27 01:51:22 +03:00
|
|
|
|
2018-05-01 03:37:29 +03:00
|
|
|
(* Undefined state *)
|
|
|
|
|
let undefined x = failwith (Printf.sprintf "Undefined variable: %s" x)
|
2018-05-04 02:59:23 +03:00
|
|
|
|
|
|
|
|
(* Bind a variable to a value in a state *)
|
|
|
|
|
let bind x v s = fun y -> if x = y then v else s y
|
|
|
|
|
|
2018-03-27 01:51:22 +03:00
|
|
|
(* Empty state *)
|
2018-05-01 03:37:29 +03:00
|
|
|
let empty = G undefined
|
2018-03-27 01:51:22 +03:00
|
|
|
|
|
|
|
|
(* Update: non-destructively "modifies" the state s by binding the variable x
|
|
|
|
|
to value v and returns the new state w.r.t. a scope
|
|
|
|
|
*)
|
2018-04-02 05:58:02 +03:00
|
|
|
let update x v s =
|
2018-05-01 03:37:29 +03:00
|
|
|
let rec inner = function
|
2018-05-04 02:59:23 +03:00
|
|
|
| G s -> G (bind x v s)
|
2018-05-01 03:37:29 +03:00
|
|
|
| L (scope, s, enclosing) ->
|
2018-05-04 02:59:23 +03:00
|
|
|
if List.mem x scope then L (scope, bind x v s, enclosing) else L (scope, s, inner enclosing)
|
2018-05-01 03:37:29 +03:00
|
|
|
in
|
|
|
|
|
inner s
|
2018-04-02 05:58:02 +03:00
|
|
|
|
2018-03-27 01:51:22 +03:00
|
|
|
(* Evals a variable in a state w.r.t. a scope *)
|
2018-05-01 03:37:29 +03:00
|
|
|
let rec eval s x =
|
|
|
|
|
match s with
|
|
|
|
|
| G s -> s x
|
|
|
|
|
| L (scope, s, enclosing) -> if List.mem x scope then s x else eval enclosing x
|
2018-03-27 01:51:22 +03:00
|
|
|
|
|
|
|
|
(* Creates a new scope, based on a given state *)
|
2018-05-02 22:36:27 +03:00
|
|
|
let rec enter st xs =
|
2018-05-01 03:37:29 +03:00
|
|
|
match st with
|
|
|
|
|
| G _ -> L (xs, undefined, st)
|
2018-05-02 22:36:27 +03:00
|
|
|
| L (_, _, e) -> enter e xs
|
2018-03-27 01:51:22 +03:00
|
|
|
|
|
|
|
|
(* Drops a scope *)
|
2018-05-02 22:36:27 +03:00
|
|
|
let leave st st' =
|
|
|
|
|
let rec get = function
|
|
|
|
|
| G _ as st -> st
|
|
|
|
|
| L (_, _, e) -> get e
|
|
|
|
|
in
|
|
|
|
|
let g = get st in
|
|
|
|
|
let rec recurse = function
|
|
|
|
|
| L (scope, s, e) -> L (scope, s, recurse e)
|
|
|
|
|
| G _ -> g
|
|
|
|
|
in
|
|
|
|
|
recurse st'
|
|
|
|
|
|
|
|
|
|
(* Push a new local scope *)
|
|
|
|
|
let push st s xs = L (xs, s, st)
|
2018-03-27 01:51:22 +03:00
|
|
|
|
2018-05-02 22:36:27 +03:00
|
|
|
(* Drop a local scope *)
|
|
|
|
|
let drop (L (_, _, e)) = e
|
|
|
|
|
|
2018-03-27 01:51:22 +03:00
|
|
|
end
|
2018-04-25 01:06:18 +03:00
|
|
|
|
|
|
|
|
(* Builtins *)
|
|
|
|
|
module Builtin =
|
|
|
|
|
struct
|
2018-04-30 17:18:41 +03:00
|
|
|
|
2018-04-25 01:06:18 +03:00
|
|
|
let eval (st, i, o, _) args = function
|
2018-04-27 01:27:10 +03:00
|
|
|
| "read" -> (match i with z::i' -> (st, i', o, Some (Value.of_int z)) | _ -> failwith "Unexpected end of input")
|
|
|
|
|
| "write" -> (st, i, o @ [Value.to_int @@ List.hd args], None)
|
2018-04-30 17:18:41 +03:00
|
|
|
| ".elem" -> let [b; j] = args in
|
2018-04-27 01:27:10 +03:00
|
|
|
(st, i, o, let i = Value.to_int j in
|
|
|
|
|
Some (match b with
|
2018-11-13 09:54:04 +03:00
|
|
|
| Value.String s -> Value.of_int @@ Char.code (Bytes.get s i)
|
|
|
|
|
| Value.Array a -> a.(i)
|
2018-05-04 02:59:23 +03:00
|
|
|
| Value.Sexp (_, a) -> List.nth a i
|
2018-04-27 01:27:10 +03:00
|
|
|
)
|
|
|
|
|
)
|
2018-11-13 09:54:04 +03:00
|
|
|
| ".length" -> (st, i, o, Some (Value.of_int (match List.hd args with Value.Sexp (_, a) -> List.length a | Value.Array a -> Array.length a | Value.String s -> Bytes.length s)))
|
|
|
|
|
| ".array" -> (st, i, o, Some (Value.of_array @@ Array.of_list args))
|
2018-11-06 00:21:38 +03:00
|
|
|
| ".stringval" -> let [a] = args in (st, i, o, Some (Value.of_string @@ Value.string_val a))
|
|
|
|
|
|
2018-04-25 01:06:18 +03:00
|
|
|
end
|
2018-03-27 01:51:22 +03:00
|
|
|
|
2018-02-20 01:28:29 +03:00
|
|
|
(* Simple expressions: syntax and semantics *)
|
|
|
|
|
module Expr =
|
|
|
|
|
struct
|
|
|
|
|
|
|
|
|
|
(* The type for expressions. Note, in regular OCaml there is no "@type..."
|
|
|
|
|
notation, it came from GT.
|
|
|
|
|
*)
|
|
|
|
|
@type t =
|
2018-10-31 20:10:50 +03:00
|
|
|
(* integer constant *) | Const of int
|
|
|
|
|
(* array *) | Array of t list
|
|
|
|
|
(* string *) | String of string
|
|
|
|
|
(* S-expressions *) | Sexp of string * t list
|
|
|
|
|
(* variable *) | Var of string
|
|
|
|
|
(* binary operator *) | Binop of string * t * t
|
|
|
|
|
(* element extraction *) | Elem of t * t
|
|
|
|
|
(* length *) | Length of t
|
|
|
|
|
(* string conversion *) | StringVal of t
|
|
|
|
|
(* function call *) | Call of string * t list with show
|
2018-02-20 01:28:29 +03:00
|
|
|
|
|
|
|
|
(* Available binary operators:
|
|
|
|
|
!! --- disjunction
|
|
|
|
|
&& --- conjunction
|
|
|
|
|
==, !=, <=, <, >=, > --- comparisons
|
|
|
|
|
+, - --- addition, subtraction
|
|
|
|
|
*, /, % --- multiplication, division, reminder
|
|
|
|
|
*)
|
2018-04-02 07:00:36 +03:00
|
|
|
|
2018-04-03 07:21:59 +03:00
|
|
|
(* The type of configuration: a state, an input stream, an output stream, an optional value *)
|
2018-04-25 01:06:18 +03:00
|
|
|
type config = State.t * int list * int list * Value.t option
|
2018-04-02 07:00:36 +03:00
|
|
|
|
2018-02-20 01:28:29 +03:00
|
|
|
(* Expression evaluator
|
|
|
|
|
|
2018-04-02 07:00:36 +03:00
|
|
|
val eval : env -> config -> t -> int * config
|
|
|
|
|
|
|
|
|
|
|
2018-04-03 07:21:59 +03:00
|
|
|
Takes an environment, a configuration and an expresion, and returns another configuration. The
|
2018-04-02 07:00:36 +03:00
|
|
|
environment supplies the following method
|
|
|
|
|
|
2018-04-03 07:21:59 +03:00
|
|
|
method definition : env -> string -> int list -> config -> config
|
2018-04-02 07:00:36 +03:00
|
|
|
|
|
|
|
|
which takes an environment (of the same type), a name of the function, a list of actual parameters and a configuration,
|
|
|
|
|
an returns a pair: the return value for the call and the resulting configuration
|
2018-03-27 01:51:22 +03:00
|
|
|
*)
|
2018-04-02 05:58:02 +03:00
|
|
|
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)
|
|
|
|
|
|
2018-04-03 07:21:59 +03:00
|
|
|
let rec eval env ((st, i, o, r) as conf) expr =
|
2018-04-02 05:58:02 +03:00
|
|
|
match expr with
|
2018-10-31 20:10:50 +03:00
|
|
|
| Const n -> (st, i, o, Some (Value.of_int n))
|
2018-11-13 09:54:04 +03:00
|
|
|
| String s -> (st, i, o, Some (Value.of_string @@ Bytes.of_string s))
|
2018-10-31 20:10:50 +03:00
|
|
|
| StringVal s ->
|
|
|
|
|
let (st, i, o, Some s) = eval env conf s in
|
|
|
|
|
(st, i, o, Some (Value.of_string @@ Value.string_val s))
|
2018-04-25 01:06:18 +03:00
|
|
|
| Var x -> (st, i, o, Some (State.eval st x))
|
|
|
|
|
| Array xs ->
|
|
|
|
|
let (st, i, o, vs) = eval_list env conf xs in
|
2018-04-30 17:18:41 +03:00
|
|
|
env#definition env ".array" vs (st, i, o, None)
|
2018-04-25 01:06:18 +03:00
|
|
|
| Sexp (t, xs) ->
|
|
|
|
|
let (st, i, o, vs) = eval_list env conf xs in
|
|
|
|
|
(st, i, o, Some (Value.Sexp (t, vs)))
|
2018-04-02 07:00:36 +03:00
|
|
|
| Binop (op, x, y) ->
|
2018-04-03 07:21:59 +03:00
|
|
|
let (_, _, _, Some x) as conf = eval env conf x in
|
|
|
|
|
let (st, i, o, Some y) as conf = eval env conf y in
|
2018-04-25 01:06:18 +03:00
|
|
|
(st, i, o, Some (Value.of_int @@ to_func op (Value.to_int x) (Value.to_int y)))
|
|
|
|
|
| Elem (b, i) ->
|
|
|
|
|
let (st, i, o, args) = eval_list env conf [b; i] in
|
2018-04-30 17:18:41 +03:00
|
|
|
env#definition env ".elem" args (st, i, o, None)
|
2018-04-25 01:06:18 +03:00
|
|
|
| Length e ->
|
|
|
|
|
let (st, i, o, Some v) = eval env conf e in
|
2018-04-30 17:18:41 +03:00
|
|
|
env#definition env ".length" [v] (st, i, o, None)
|
2018-04-02 07:00:36 +03:00
|
|
|
| Call (f, args) ->
|
2018-04-25 01:06:18 +03:00
|
|
|
let (st, i, o, args) = eval_list env conf args in
|
|
|
|
|
env#definition env f args (st, i, o, None)
|
|
|
|
|
and eval_list env conf xs =
|
|
|
|
|
let vs, (st, i, o, _) =
|
|
|
|
|
List.fold_left
|
|
|
|
|
(fun (acc, conf) x ->
|
|
|
|
|
let (_, _, _, Some v) as conf = eval env conf x in
|
|
|
|
|
v::acc, conf
|
|
|
|
|
)
|
|
|
|
|
([], conf)
|
|
|
|
|
xs
|
|
|
|
|
in
|
|
|
|
|
(st, i, o, List.rev vs)
|
2018-04-02 07:00:36 +03:00
|
|
|
|
2018-02-25 19:10:00 +03:00
|
|
|
(* Expression parser. You can use the following terminals:
|
|
|
|
|
|
2018-02-25 19:41:23 +03:00
|
|
|
IDENT --- a non-empty identifier a-zA-Z[a-zA-Z0-9_]* as a string
|
2018-04-02 07:00:36 +03:00
|
|
|
DECIMAL --- a decimal constant [0-9]+ as a string
|
2018-02-25 19:10:00 +03:00
|
|
|
*)
|
2018-02-25 17:39:54 +03:00
|
|
|
ostap (
|
2018-11-04 12:54:26 +03:00
|
|
|
parse:
|
2018-04-02 05:58:02 +03:00
|
|
|
!(Ostap.Util.expr
|
|
|
|
|
(fun x -> x)
|
|
|
|
|
(Array.map (fun (a, s) -> a,
|
2018-05-25 09:53:10 +03:00
|
|
|
List.map (fun s -> ostap(- $(s)),
|
|
|
|
|
(fun x y ->
|
2018-11-04 12:54:26 +03:00
|
|
|
match s with
|
|
|
|
|
| "++" -> Call ("strcat", [x; y])
|
|
|
|
|
| _ -> Binop (s, x, y)
|
2018-05-25 09:53:10 +03:00
|
|
|
)
|
|
|
|
|
) s
|
2018-04-02 05:58:02 +03:00
|
|
|
)
|
|
|
|
|
[|
|
|
|
|
|
`Lefta, ["!!"];
|
|
|
|
|
`Lefta, ["&&"];
|
|
|
|
|
`Nona , ["=="; "!="; "<="; "<"; ">="; ">"];
|
2018-05-25 09:53:10 +03:00
|
|
|
`Lefta, ["++"; "+" ; "-"];
|
2018-04-02 05:58:02 +03:00
|
|
|
`Lefta, ["*" ; "/"; "%"];
|
|
|
|
|
|]
|
|
|
|
|
)
|
2018-11-04 12:54:26 +03:00
|
|
|
primary);
|
2018-10-31 20:10:50 +03:00
|
|
|
primary: b:base is:(-"[" i:parse -"]" {`Elem i} | -"." (%"length" {`Len} | %"string" {`Str})) *
|
2018-11-04 12:54:26 +03:00
|
|
|
{List.fold_left (fun b -> function `Elem i -> Elem (b, i) | `Len -> Length b | `Str -> StringVal b) b is};
|
2018-04-25 01:06:18 +03:00
|
|
|
base:
|
2018-11-04 12:54:26 +03:00
|
|
|
n:DECIMAL {Const n}
|
2018-04-25 01:06:18 +03:00
|
|
|
| s:STRING {String (String.sub s 1 (String.length s - 2))}
|
|
|
|
|
| c:CHAR {Const (Char.code c)}
|
|
|
|
|
| "[" es:!(Util.list0)[parse] "]" {Array es}
|
|
|
|
|
| "`" t:IDENT args:(-"(" !(Util.list)[parse] -")")? {Sexp (t, match args with None -> [] | Some args -> args)}
|
|
|
|
|
| x:IDENT s:("(" args:!(Util.list0)[parse] ")" {Call (x, args)} | empty {Var x}) {s}
|
2018-11-04 12:54:26 +03:00
|
|
|
| -"(" parse -")"
|
2018-02-25 14:48:13 +03:00
|
|
|
)
|
2018-02-25 19:41:23 +03:00
|
|
|
|
2018-02-20 01:28:29 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
(* Simple statements: syntax and sematics *)
|
|
|
|
|
module Stmt =
|
|
|
|
|
struct
|
|
|
|
|
|
2018-05-01 02:57:09 +03:00
|
|
|
(* Patterns in statements *)
|
|
|
|
|
module Pattern =
|
|
|
|
|
struct
|
|
|
|
|
|
|
|
|
|
(* The type for patterns *)
|
|
|
|
|
@type t =
|
|
|
|
|
(* wildcard "-" *) | Wildcard
|
|
|
|
|
(* S-expression *) | Sexp of string * t list
|
2018-11-05 18:21:41 +03:00
|
|
|
(* array *) | Array of t list
|
|
|
|
|
(* identifier *) | Named of string * t
|
|
|
|
|
(* ground integer *) | Const of int
|
|
|
|
|
(* ground string *) | String of string
|
|
|
|
|
(* boxed value *) | Boxed
|
|
|
|
|
(* unboxed value *) | UnBoxed
|
|
|
|
|
(* any string value *) | StringTag
|
|
|
|
|
(* any sexp value *) | SexpTag
|
|
|
|
|
(* any array value *) | ArrayTag
|
2018-05-02 22:36:27 +03:00
|
|
|
with show, foldl
|
2018-05-01 02:57:09 +03:00
|
|
|
|
|
|
|
|
(* Pattern parser *)
|
|
|
|
|
ostap (
|
|
|
|
|
parse:
|
|
|
|
|
%"_" {Wildcard}
|
|
|
|
|
| "`" t:IDENT ps:(-"(" !(Util.list)[parse] -")")? {Sexp (t, match ps with None -> [] | Some ps -> ps)}
|
2018-11-05 18:21:41 +03:00
|
|
|
| "[" ps:(!(Util.list0)[parse]) "]" {Array ps}
|
|
|
|
|
| x:IDENT y:(-"@" parse)? {match y with None -> Named (x, Wildcard) | Some y -> Named (x, y)}
|
|
|
|
|
| c:DECIMAL {Const c}
|
2018-11-06 00:21:38 +03:00
|
|
|
| s:STRING {String (String.sub s 1 (String.length s - 2))}
|
|
|
|
|
| c:CHAR {Const (Char.code c)}
|
2018-11-05 18:21:41 +03:00
|
|
|
| "#" %"boxed" {Boxed}
|
|
|
|
|
| "#" %"unboxed" {UnBoxed}
|
|
|
|
|
| "#" %"string" {StringTag}
|
|
|
|
|
| "#" %"sexp" {SexpTag}
|
|
|
|
|
| "#" %"array" {ArrayTag}
|
2018-05-01 02:57:09 +03:00
|
|
|
)
|
|
|
|
|
|
2018-05-28 18:44:38 +03:00
|
|
|
let vars p = fix0 (fun f ->
|
2018-11-05 18:21:41 +03:00
|
|
|
transform(t) (object inherit [string list, _] @t[foldl] f method c_Named s name p = name :: f s p end)) [] p
|
2018-05-02 22:36:27 +03:00
|
|
|
|
2018-05-01 02:57:09 +03:00
|
|
|
end
|
|
|
|
|
|
2018-02-20 01:28:29 +03:00
|
|
|
(* The type for statements *)
|
2018-05-01 02:57:09 +03:00
|
|
|
@type t =
|
2018-04-25 01:06:18 +03:00
|
|
|
(* assignment *) | Assign of string * Expr.t list * Expr.t
|
2018-03-11 22:30:01 +03:00
|
|
|
(* composition *) | Seq of t * t
|
|
|
|
|
(* empty statement *) | Skip
|
|
|
|
|
(* conditional *) | If of Expr.t * t * t
|
2018-03-20 20:30:58 +03:00
|
|
|
(* loop with a pre-condition *) | While of Expr.t * t
|
2018-03-27 01:51:22 +03:00
|
|
|
(* loop with a post-condition *) | Repeat of t * Expr.t
|
2018-05-04 02:59:23 +03:00
|
|
|
(* pattern-matching *) | Case of Expr.t * (Pattern.t * t) list
|
2018-04-02 07:00:36 +03:00
|
|
|
(* return statement *) | Return of Expr.t option
|
2018-05-02 22:36:27 +03:00
|
|
|
(* call a procedure *) | Call of string * Expr.t list
|
|
|
|
|
(* leave a scope *) | Leave with show
|
|
|
|
|
|
2018-02-20 01:28:29 +03:00
|
|
|
(* Statement evaluator
|
|
|
|
|
|
2018-03-27 01:51:22 +03:00
|
|
|
val eval : env -> config -> t -> config
|
|
|
|
|
|
|
|
|
|
Takes an environment, a configuration and a statement, and returns another configuration. The
|
2018-04-02 07:00:36 +03:00
|
|
|
environment is the same as for expressions
|
2018-02-20 01:28:29 +03:00
|
|
|
*)
|
2018-04-25 01:06:18 +03:00
|
|
|
let update st x v is =
|
|
|
|
|
let rec update a v = function
|
|
|
|
|
| [] -> v
|
|
|
|
|
| i::tl ->
|
|
|
|
|
let i = Value.to_int i in
|
|
|
|
|
(match a with
|
|
|
|
|
| Value.String s when tl = [] -> Value.String (Value.update_string s i (Char.chr @@ Value.to_int v))
|
2018-11-13 09:54:04 +03:00
|
|
|
| Value.Array a -> Value.Array (Value.update_array a i (update a.(i) v tl))
|
2018-04-25 01:06:18 +03:00
|
|
|
)
|
|
|
|
|
in
|
|
|
|
|
State.update x (match is with [] -> v | _ -> update (State.eval st x) v is) st
|
2018-05-02 22:36:27 +03:00
|
|
|
|
2018-04-03 07:21:59 +03:00
|
|
|
let rec eval env ((st, i, o, r) as conf) k stmt =
|
|
|
|
|
let seq x = function Skip -> x | y -> Seq (x, y) in
|
2018-04-02 05:58:02 +03:00
|
|
|
match stmt with
|
2018-05-02 22:36:27 +03:00
|
|
|
| Leave -> eval env (State.drop st, i, o, r) Skip k
|
|
|
|
|
| Assign (x, is, e) ->
|
2018-04-25 01:06:18 +03:00
|
|
|
let (st, i, o, is) = Expr.eval_list env conf is in
|
|
|
|
|
let (st, i, o, Some v) = Expr.eval env (st, i, o, None) e in
|
|
|
|
|
eval env (update st x v is, i, o, None) Skip k
|
|
|
|
|
|
2018-04-02 10:38:54 +03:00
|
|
|
| Seq (s1, s2) -> eval env conf (seq s2 k) s1
|
|
|
|
|
| Skip -> (match k with Skip -> conf | _ -> eval env conf Skip k)
|
2018-04-25 01:06:18 +03:00
|
|
|
| If (e, s1, s2) -> let (_, _, _, Some v) as conf = Expr.eval env conf e in eval env conf k (if Value.to_int v <> 0 then s1 else s2)
|
2018-04-03 07:21:59 +03:00
|
|
|
| While (e, s) -> let (_, _, _, Some v) as conf = Expr.eval env conf e in
|
2018-04-25 01:06:18 +03:00
|
|
|
if Value.to_int v = 0
|
2018-04-02 10:38:54 +03:00
|
|
|
then eval env conf Skip k
|
|
|
|
|
else eval env conf (seq stmt k) s
|
|
|
|
|
| Repeat (s, e) -> eval env conf (seq (While (Expr.Binop ("==", e, Expr.Const 0), s)) k) s
|
2018-04-03 07:21:59 +03:00
|
|
|
| Return e -> (match e with None -> (st, i, o, None) | Some e -> Expr.eval env conf e)
|
|
|
|
|
| Call (f, args) -> eval env (Expr.eval env conf (Expr.Call (f, args))) k Skip
|
2018-05-02 22:36:27 +03:00
|
|
|
| Case (e, bs) ->
|
2018-05-04 02:59:23 +03:00
|
|
|
let (_, _, _, Some v) as conf' = Expr.eval env conf e in
|
|
|
|
|
let rec branch ((st, i, o, _) as conf) = function
|
2018-11-13 09:54:04 +03:00
|
|
|
| [] -> failwith (Printf.sprintf "Pattern matching failed: no branch is selected while matching %s\n" "" (*show(Value.t) v*))
|
2018-05-04 02:59:23 +03:00
|
|
|
| (patt, body)::tl ->
|
|
|
|
|
let rec match_patt patt v st =
|
|
|
|
|
let update x v = function
|
|
|
|
|
| None -> None
|
|
|
|
|
| Some s -> Some (State.bind x v s)
|
2018-05-02 22:36:27 +03:00
|
|
|
in
|
2018-05-04 02:59:23 +03:00
|
|
|
match patt, v with
|
2018-11-06 00:21:38 +03:00
|
|
|
| Pattern.Named (x, p), v -> update x v (match_patt p v st )
|
|
|
|
|
| Pattern.Wildcard , _ -> st
|
|
|
|
|
| Pattern.Sexp (t, ps), Value.Sexp (t', vs) when t = t' && List.length ps = List.length vs -> match_list ps vs st
|
2018-11-13 09:54:04 +03:00
|
|
|
| Pattern.Array ps , Value.Array vs when List.length ps = Array.length vs -> match_list ps (Array.to_list vs) st
|
2018-11-06 00:21:38 +03:00
|
|
|
| Pattern.Const n , Value.Int n' when n = n' -> st
|
2018-11-13 09:54:04 +03:00
|
|
|
| Pattern.String s , Value.String s' when s = Bytes.to_string s' -> st
|
2018-11-06 00:21:38 +03:00
|
|
|
| Pattern.Boxed , Value.String _
|
|
|
|
|
| Pattern.Boxed , Value.Array _
|
|
|
|
|
| Pattern.UnBoxed , Value.Int _
|
|
|
|
|
| Pattern.Boxed , Value.Sexp (_, _)
|
|
|
|
|
| Pattern.StringTag , Value.String _
|
|
|
|
|
| Pattern.ArrayTag , Value.Array _
|
|
|
|
|
| Pattern.SexpTag , Value.Sexp (_, _) -> st
|
|
|
|
|
| _ -> None
|
2018-05-04 02:59:23 +03:00
|
|
|
and match_list ps vs s =
|
|
|
|
|
match ps, vs with
|
|
|
|
|
| [], [] -> s
|
|
|
|
|
| p::ps, v::vs -> match_list ps vs (match_patt p v s)
|
|
|
|
|
| _ -> None
|
|
|
|
|
in
|
|
|
|
|
match match_patt patt v (Some State.undefined) with
|
|
|
|
|
| None -> branch conf tl
|
|
|
|
|
| Some st' -> eval env (State.push st st' (Pattern.vars patt), i, o, None) k (Seq (body, Leave))
|
2018-05-02 22:36:27 +03:00
|
|
|
in
|
|
|
|
|
branch conf' bs
|
|
|
|
|
|
2018-02-25 14:48:13 +03:00
|
|
|
(* Statement parser *)
|
|
|
|
|
ostap (
|
2018-04-02 05:58:02 +03:00
|
|
|
parse:
|
|
|
|
|
s:stmt ";" ss:parse {Seq (s, ss)}
|
|
|
|
|
| stmt;
|
|
|
|
|
stmt:
|
2018-04-25 01:06:18 +03:00
|
|
|
%"skip" {Skip}
|
2018-04-02 05:58:02 +03:00
|
|
|
| %"if" e:!(Expr.parse)
|
|
|
|
|
%"then" the:parse
|
|
|
|
|
elif:(%"elif" !(Expr.parse) %"then" parse)*
|
|
|
|
|
els:(%"else" parse)?
|
|
|
|
|
%"fi" {
|
|
|
|
|
If (e, the,
|
|
|
|
|
List.fold_right
|
|
|
|
|
(fun (e, t) elif -> If (e, t, elif))
|
|
|
|
|
elif
|
|
|
|
|
(match els with None -> Skip | Some s -> s)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
| %"while" e:!(Expr.parse) %"do" s:parse %"od"{While (e, s)}
|
|
|
|
|
| %"for" i:parse "," c:!(Expr.parse) "," s:parse %"do" b:parse %"od" {
|
|
|
|
|
Seq (i, While (c, Seq (b, s)))
|
|
|
|
|
}
|
|
|
|
|
| %"repeat" s:parse %"until" e:!(Expr.parse) {Repeat (s, e)}
|
2018-05-01 02:57:09 +03:00
|
|
|
| %"return" e:!(Expr.parse)? {Return e}
|
2018-05-04 02:59:23 +03:00
|
|
|
| %"case" e:!(Expr.parse) %"of" bs:!(Util.listBy)[ostap ("|")][ostap (!(Pattern.parse) -"->" parse)] %"esac" {Case (e, bs)}
|
2018-04-02 05:58:02 +03:00
|
|
|
| x:IDENT
|
2018-04-25 01:06:18 +03:00
|
|
|
s:(is:(-"[" !(Expr.parse) -"]")* ":=" e :!(Expr.parse) {Assign (x, is, e)} |
|
2018-04-02 05:58:02 +03:00
|
|
|
"(" args:!(Util.list0)[Expr.parse] ")" {Call (x, args)}
|
|
|
|
|
) {s}
|
2018-02-25 14:48:13 +03:00
|
|
|
)
|
|
|
|
|
|
2018-02-20 01:28:29 +03:00
|
|
|
end
|
2018-02-20 02:53:58 +03:00
|
|
|
|
2018-03-27 01:51:22 +03:00
|
|
|
(* Function and procedure definitions *)
|
|
|
|
|
module Definition =
|
|
|
|
|
struct
|
|
|
|
|
|
|
|
|
|
(* The type for a definition: name, argument list, local variables, body *)
|
|
|
|
|
type t = string * (string list * string list * Stmt.t)
|
|
|
|
|
|
|
|
|
|
ostap (
|
2018-04-02 05:58:02 +03:00
|
|
|
arg : IDENT;
|
|
|
|
|
parse: %"fun" name:IDENT "(" args:!(Util.list0 arg) ")"
|
|
|
|
|
locs:(%"local" !(Util.list arg))?
|
|
|
|
|
"{" body:!(Stmt.parse) "}" {
|
|
|
|
|
(name, (args, (match locs with None -> [] | Some l -> l), body))
|
|
|
|
|
}
|
2018-03-27 01:51:22 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
2018-02-25 14:48:13 +03:00
|
|
|
(* The top-level definitions *)
|
|
|
|
|
|
2018-03-27 01:51:22 +03:00
|
|
|
(* The top-level syntax category is a pair of definition list and statement (program body) *)
|
|
|
|
|
type t = Definition.t list * Stmt.t
|
2018-02-25 14:48:13 +03:00
|
|
|
|
2018-02-20 02:53:58 +03:00
|
|
|
(* Top-level evaluator
|
|
|
|
|
|
2018-02-25 14:48:13 +03:00
|
|
|
eval : t -> int list -> int list
|
|
|
|
|
|
|
|
|
|
Takes a program and its input stream, and returns the output stream
|
|
|
|
|
*)
|
2018-04-02 05:58:02 +03:00
|
|
|
let eval (defs, body) i =
|
|
|
|
|
let module M = Map.Make (String) in
|
2018-04-03 07:21:59 +03:00
|
|
|
let m = List.fold_left (fun m ((name, _) as def) -> M.add name def m) M.empty defs in
|
|
|
|
|
let _, _, o, _ =
|
2018-04-02 07:00:36 +03:00
|
|
|
Stmt.eval
|
|
|
|
|
(object
|
2018-04-25 01:06:18 +03:00
|
|
|
method definition env f args ((st, i, o, r) as conf) =
|
|
|
|
|
try
|
2018-11-13 09:54:04 +03:00
|
|
|
let xs, locs, s = snd @@ M.find f m in
|
2018-04-25 01:06:18 +03:00
|
|
|
let st' = List.fold_left (fun st (x, a) -> State.update x a st) (State.enter st (xs @ locs)) (List.combine xs args) in
|
|
|
|
|
let st'', i', o', r' = Stmt.eval env (st', i, o, r) Skip s in
|
|
|
|
|
(State.leave st'' st, i', o', r')
|
|
|
|
|
with Not_found -> Builtin.eval conf args f
|
2018-04-02 07:00:36 +03:00
|
|
|
end)
|
2018-04-03 07:21:59 +03:00
|
|
|
(State.empty, i, [], None)
|
2018-04-02 10:38:54 +03:00
|
|
|
Skip
|
2018-04-02 07:00:36 +03:00
|
|
|
body
|
|
|
|
|
in
|
|
|
|
|
o
|
2018-04-02 05:58:02 +03:00
|
|
|
|
2018-02-25 14:48:13 +03:00
|
|
|
(* Top-level parser *)
|
2018-04-02 05:58:02 +03:00
|
|
|
let parse = ostap (!(Definition.parse)* !(Stmt.parse))
|