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.
|
|
|
|
|
*)
|
2019-04-02 19:51:46 +03:00
|
|
|
module OrigList = List
|
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
|
|
|
|
2019-03-25 00:13:42 +03:00
|
|
|
exception Semantic_error of string
|
|
|
|
|
|
|
|
|
|
let unquote s = String.sub s 1 (String.length s - 2)
|
|
|
|
|
|
2018-04-25 01:06:18 +03:00
|
|
|
(* Values *)
|
|
|
|
|
module Value =
|
|
|
|
|
struct
|
|
|
|
|
|
2019-04-02 19:51:46 +03:00
|
|
|
@type t =
|
|
|
|
|
| Empty
|
|
|
|
|
| Var of string
|
2019-04-07 23:42:20 +03:00
|
|
|
| Elem of t * int
|
2019-04-02 19:51:46 +03:00
|
|
|
| Int of int
|
|
|
|
|
| String of bytes
|
|
|
|
|
| Array of t array
|
|
|
|
|
| Sexp of string * t array
|
|
|
|
|
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"
|
|
|
|
|
|
2019-04-02 19:51:46 +03:00
|
|
|
let sexp s vs = Sexp (s, Array.of_list 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
|
2019-04-07 23:42:20 +03:00
|
|
|
|
|
|
|
|
let update_elem x i v =
|
|
|
|
|
match x with
|
|
|
|
|
| Sexp (_, a) | Array a -> ignore (update_array a i v)
|
|
|
|
|
| String a -> ignore (update_string a i (Char.chr @@ to_int v))
|
|
|
|
|
|
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 "]"
|
2019-04-02 19:51:46 +03:00
|
|
|
| Sexp (t, a) -> let n = Array.length a in
|
2019-03-07 21:12:43 +03:00
|
|
|
if t = "cons"
|
|
|
|
|
then (
|
|
|
|
|
append "{";
|
|
|
|
|
let rec inner_list = function
|
2019-04-02 19:51:46 +03:00
|
|
|
| [||] -> ()
|
|
|
|
|
| [|x; Int 0|] -> inner x
|
|
|
|
|
| [|x; Sexp ("cons", a)|] -> inner x; append ", "; inner_list a
|
2019-03-07 21:12:43 +03:00
|
|
|
in inner_list a;
|
|
|
|
|
append "}"
|
|
|
|
|
)
|
|
|
|
|
else (
|
|
|
|
|
append t;
|
2019-04-02 19:51:46 +03:00
|
|
|
(if n > 0 then (append " ("; Array.iteri (fun i a -> (if i > 0 then append ", "); inner a) a;
|
2019-03-07 21:12:43 +03:00
|
|
|
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
|
|
|
|
2019-04-02 19:51:46 +03:00
|
|
|
let eval (st, i, o, vs) args = function
|
|
|
|
|
| "read" -> (match i with z::i' -> (st, i', o, (Value.of_int z)::vs) | _ -> failwith "Unexpected end of input")
|
2019-04-07 23:42:20 +03:00
|
|
|
| "write" -> (st, i, o @ [Value.to_int @@ List.hd args], (*Value.Empty ::*) vs)
|
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
|
2019-04-02 19:51:46 +03:00
|
|
|
(match b with
|
|
|
|
|
| Value.String s -> Value.of_int @@ Char.code (Bytes.get s i)
|
|
|
|
|
| Value.Array a -> a.(i)
|
|
|
|
|
| Value.Sexp (_, a) -> a.(i)
|
|
|
|
|
) :: vs
|
2018-04-27 01:27:10 +03:00
|
|
|
)
|
2019-04-02 19:51:46 +03:00
|
|
|
| ".length" -> (st, i, o, (Value.of_int (match List.hd args with Value.Sexp (_, a) | Value.Array a -> Array.length a | Value.String s -> Bytes.length s))::vs)
|
|
|
|
|
| ".array" -> (st, i, o, (Value.of_array @@ Array.of_list args)::vs)
|
|
|
|
|
| ".stringval" -> let [a] = args in (st, i, o, (Value.of_string @@ Value.string_val a)::vs)
|
2018-11-06 00:21:38 +03:00
|
|
|
|
2018-04-25 01:06:18 +03:00
|
|
|
end
|
2019-04-02 19:51:46 +03:00
|
|
|
|
|
|
|
|
(* Patterns *)
|
|
|
|
|
module Pattern =
|
|
|
|
|
struct
|
|
|
|
|
|
|
|
|
|
(* The type for patterns *)
|
|
|
|
|
@type t =
|
|
|
|
|
(* wildcard "-" *) | Wildcard
|
|
|
|
|
(* S-expression *) | Sexp of string * t list
|
|
|
|
|
(* 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
|
|
|
|
|
with show, foldl
|
|
|
|
|
|
|
|
|
|
(* Pattern parser *)
|
|
|
|
|
ostap (
|
|
|
|
|
parse:
|
|
|
|
|
!(Ostap.Util.expr
|
|
|
|
|
(fun x -> x)
|
|
|
|
|
(Array.map (fun (a, s) ->
|
|
|
|
|
a,
|
|
|
|
|
List.map (fun s -> ostap(- $(s)), (fun x y -> Sexp ("cons", [x; y]))) s)
|
|
|
|
|
[|`Righta, [":"]|]
|
|
|
|
|
)
|
|
|
|
|
primary);
|
|
|
|
|
primary:
|
|
|
|
|
%"_" {Wildcard}
|
|
|
|
|
| t:UIDENT ps:(-"(" !(Util.list)[parse] -")")? {Sexp (t, match ps with None -> [] | Some ps -> ps)}
|
|
|
|
|
| "[" ps:(!(Util.list0)[parse]) "]" {Array ps}
|
|
|
|
|
| "{" ps:(!(Util.list0)[parse]) "}" {match ps with
|
|
|
|
|
| [] -> UnBoxed
|
|
|
|
|
| _ -> List.fold_right (fun x acc -> Sexp ("cons", [x; acc])) ps UnBoxed
|
|
|
|
|
}
|
|
|
|
|
| x:LIDENT y:(-"@" parse)? {match y with None -> Named (x, Wildcard) | Some y -> Named (x, y)}
|
|
|
|
|
| c:DECIMAL {Const c}
|
|
|
|
|
| s:STRING {String (unquote s)}
|
|
|
|
|
| c:CHAR {Const (Char.code c)}
|
|
|
|
|
| "#" %"boxed" {Boxed}
|
|
|
|
|
| "#" %"unboxed" {UnBoxed}
|
|
|
|
|
| "#" %"string" {StringTag}
|
|
|
|
|
| "#" %"sexp" {SexpTag}
|
|
|
|
|
| "#" %"array" {ArrayTag}
|
|
|
|
|
| -"(" parse -")"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
let vars p = transform(t) (fun f -> object inherit [string list, _] @t[foldl] f method c_Named s _ name p = name :: f s p end) [] p
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
2018-02-20 01:28:29 +03:00
|
|
|
(* Simple expressions: syntax and semantics *)
|
|
|
|
|
module Expr =
|
|
|
|
|
struct
|
2019-04-02 19:51:46 +03:00
|
|
|
(* The type of configuration: a state, an input stream, an output stream,
|
|
|
|
|
and a stack of values
|
|
|
|
|
*)
|
|
|
|
|
type config = State.t * int list * int list * Value.t list
|
2018-02-20 01:28:29 +03:00
|
|
|
|
|
|
|
|
(* The type for expressions. Note, in regular OCaml there is no "@type..."
|
|
|
|
|
notation, it came from GT.
|
|
|
|
|
*)
|
2019-04-02 19:51:46 +03:00
|
|
|
type t =
|
|
|
|
|
(* 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
|
|
|
|
|
(* reference (aka "lvalue") *) | Ref of string
|
|
|
|
|
(* binary operator *) | Binop of string * t * t
|
|
|
|
|
(* element extraction *) | Elem of t * t
|
|
|
|
|
(* reference to an element *) | ElemRef of t * t
|
|
|
|
|
(* length *) | Length of t
|
|
|
|
|
(* string conversion *) | StringVal of t
|
|
|
|
|
(* function call *) | Call of t * t list
|
|
|
|
|
(* assignment *) | Assign of t * t
|
|
|
|
|
(* composition *) | Seq of t * t
|
|
|
|
|
(* empty statement *) | Skip
|
|
|
|
|
(* conditional *) | If of t * t * t
|
|
|
|
|
(* loop with a pre-condition *) | While of t * t
|
|
|
|
|
(* loop with a post-condition *) | Repeat of t * t
|
|
|
|
|
(* pattern-matching *) | Case of t * (Pattern.t * t) list
|
|
|
|
|
(* return statement *) | Return of t option
|
|
|
|
|
(* leave a scope *) | Leave
|
|
|
|
|
(* intrinsic (for evaluation) *) | Intrinsic of (config -> config)
|
|
|
|
|
(* control (for control flow) *) | Control of (config -> t * config)
|
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
|
|
|
|
2019-04-02 19:51:46 +03:00
|
|
|
(* Update state *)
|
|
|
|
|
let update st x v =
|
|
|
|
|
match x with
|
|
|
|
|
| Value.Var x -> State.update x v st
|
2019-04-07 23:42:20 +03:00
|
|
|
| Value.Elem (x, i) -> Value.update_elem x i v; st
|
|
|
|
|
|
2018-02-20 01:28:29 +03:00
|
|
|
(* Expression evaluator
|
|
|
|
|
|
2019-04-02 19:51:46 +03:00
|
|
|
val eval : env -> config -> k -> t -> config
|
2018-04-02 07:00:36 +03:00
|
|
|
|
|
|
|
|
|
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)
|
2019-04-02 19:51:46 +03:00
|
|
|
|
|
|
|
|
let seq x = function Skip -> x | y -> Seq (x, y)
|
|
|
|
|
|
|
|
|
|
let schedule_list h::tl =
|
|
|
|
|
List.fold_left seq h tl
|
|
|
|
|
|
|
|
|
|
let rec take = function
|
|
|
|
|
| 0 -> fun rest -> [], rest
|
|
|
|
|
| n -> fun h::tl -> let tl', rest = take (n-1) tl in h :: tl', rest
|
|
|
|
|
|
|
|
|
|
let rec eval env ((st, i, o, vs) as conf) k expr =
|
2018-04-02 05:58:02 +03:00
|
|
|
match expr with
|
2019-04-02 19:51:46 +03:00
|
|
|
| Control f ->
|
|
|
|
|
let s, conf' = f conf in
|
|
|
|
|
eval env conf' k s
|
|
|
|
|
| Intrinsic f ->
|
|
|
|
|
eval env (f conf) Skip k
|
|
|
|
|
| Const n ->
|
|
|
|
|
eval env (st, i, o, (Value.of_int n) :: vs) Skip k
|
|
|
|
|
| String s ->
|
|
|
|
|
eval env (st, i, o, (Value.of_string @@ Bytes.of_string s) :: vs) Skip k
|
2018-10-31 20:10:50 +03:00
|
|
|
| StringVal s ->
|
2019-04-02 19:51:46 +03:00
|
|
|
eval env conf k (schedule_list [s; Intrinsic (fun (st, i, o, s::vs) -> (st, i, o, (Value.of_string @@ Value.string_val s)::vs))])
|
|
|
|
|
| Var x ->
|
|
|
|
|
eval env (st, i, o, (State.eval st x) :: vs) Skip k
|
|
|
|
|
| Ref x ->
|
|
|
|
|
eval env (st, i, o, (Value.Var x) :: vs) Skip k
|
2018-04-25 01:06:18 +03:00
|
|
|
| Array xs ->
|
2019-04-02 19:51:46 +03:00
|
|
|
eval env conf k (schedule_list (xs @ [Intrinsic (fun (st, i, o, vs) -> let es, vs' = take (List.length xs) vs in env#definition env ".array" (List.rev es) (st, i, o, vs'))]))
|
2018-04-25 01:06:18 +03:00
|
|
|
| Sexp (t, xs) ->
|
2019-04-02 19:51:46 +03:00
|
|
|
eval env conf k (schedule_list (xs @ [Intrinsic (fun (st, i, o, vs) -> let es, vs' = take (List.length xs) vs in (st, i, o, Value.Sexp (t, Array.of_list (List.rev es)) :: vs'))]))
|
2018-04-02 07:00:36 +03:00
|
|
|
| Binop (op, x, y) ->
|
2019-04-02 19:51:46 +03:00
|
|
|
eval env conf k (schedule_list [x; y; Intrinsic (fun (st, i, o, y::x::vs) -> (st, i, o, (Value.of_int @@ to_func op (Value.to_int x) (Value.to_int y)) :: vs))])
|
|
|
|
|
| Elem (b, i) ->
|
|
|
|
|
eval env conf k (schedule_list [b; i; Intrinsic (fun (st, i, o, j::b::vs) -> env#definition env ".elem" [b; j] (st, i, o, vs))])
|
|
|
|
|
| ElemRef (b, i) ->
|
|
|
|
|
eval env conf k (schedule_list [b; i; Intrinsic (fun (st, i, o, j::b::vs) -> (st, i, o, (Value.Elem (b, Value.to_int j))::vs))])
|
2018-04-25 01:06:18 +03:00
|
|
|
| Length e ->
|
2019-04-02 19:51:46 +03:00
|
|
|
eval env conf k (schedule_list [e; Intrinsic (fun (st, i, o, v::vs) -> env#definition env ".length" [v] (st, i, o, vs))])
|
|
|
|
|
| Call (Var f, args) ->
|
|
|
|
|
eval env conf k (schedule_list (args @ [Intrinsic (fun (st, i, o, vs) -> let es, vs' = take (List.length args) vs in
|
|
|
|
|
env#definition env f (List.rev es) (st, i, o, vs'))]))
|
|
|
|
|
| Leave -> eval env (State.drop st, i, o, vs) Skip k
|
|
|
|
|
| Assign (x, e) ->
|
|
|
|
|
eval env conf k (schedule_list [x; e; Intrinsic (fun (st, i, o, v::x::vs) -> (update st x v, i, o, vs))])
|
|
|
|
|
| Seq (s1, s2) ->
|
|
|
|
|
eval env conf (seq s2 k) s1
|
|
|
|
|
| Skip ->
|
|
|
|
|
(match k with Skip -> conf | _ -> eval env conf Skip k)
|
|
|
|
|
| If (e, s1, s2) ->
|
|
|
|
|
eval env conf k (schedule_list [e; Control (fun (st, i, o, e::vs) -> (if Value.to_int e <> 0 then s1 else s2), (st, i, o, vs))])
|
|
|
|
|
| While (e, s) ->
|
|
|
|
|
eval env conf k (schedule_list [e; Control (fun (st, i, o, e::vs) -> (if Value.to_int e <> 0 then seq s expr else Skip), (st, i, o, vs))])
|
|
|
|
|
| Repeat (s, e) ->
|
|
|
|
|
eval env conf (seq (While (Binop ("==", e, Const 0), s)) k) s
|
|
|
|
|
| Return e -> (match e with None -> (st, i, o, []) | Some e -> eval env (st, i, o, []) Skip e)
|
|
|
|
|
| Case (e, bs)->
|
|
|
|
|
let rec branch ((st, i, o, v::vs) as conf) = function
|
|
|
|
|
| [] -> failwith (Printf.sprintf "Pattern matching failed: no branch is selected while matching %s\n" (show(Value.t) v))
|
|
|
|
|
| (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)
|
|
|
|
|
in
|
|
|
|
|
match patt, v with
|
|
|
|
|
| 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 = Array.length vs -> match_list ps (Array.to_list vs) st
|
|
|
|
|
| Pattern.Array ps , Value.Array vs when List.length ps = Array.length vs -> match_list ps (Array.to_list vs) st
|
|
|
|
|
| Pattern.Const n , Value.Int n' when n = n' -> st
|
|
|
|
|
| Pattern.String s , Value.String s' when s = Bytes.to_string s' -> st
|
|
|
|
|
| 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
|
|
|
|
|
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, vs) k (Seq (body, Leave))
|
|
|
|
|
in
|
|
|
|
|
eval env conf Skip (schedule_list [e; Intrinsic (fun conf -> branch conf bs)])
|
|
|
|
|
|
2018-02-25 19:10:00 +03:00
|
|
|
(* Expression parser. You can use the following terminals:
|
|
|
|
|
|
2019-03-07 19:06:04 +03:00
|
|
|
LIDENT --- a non-empty identifier a-z[a-zA-Z0-9_]* as a string
|
|
|
|
|
UIDENT --- a non-empty identifier A-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
|
|
|
*)
|
2019-04-02 19:51:46 +03:00
|
|
|
|
|
|
|
|
(* Propagates *)
|
|
|
|
|
let rec propagate_ref = function
|
|
|
|
|
| Var x -> Ref x
|
|
|
|
|
| Elem (e, i) -> ElemRef (e, i)
|
|
|
|
|
| Seq (s1, s2) -> Seq (s1, propagate_ref s2)
|
|
|
|
|
| If (e, t1, t2) -> If (e, propagate_ref t1, propagate_ref t2)
|
|
|
|
|
| Case (e, bs) -> Case (e, List.map (fun (p, e) -> p, propagate_ref e) bs)
|
|
|
|
|
| _ -> raise (Semantic_error "not a destination")
|
|
|
|
|
|
|
|
|
|
ostap (
|
|
|
|
|
parse[infix]: h:basic[infix] t:(-";" parse[infix])? {match t with None -> h | Some t -> Seq (h, t)};
|
|
|
|
|
basic[infix]:
|
2018-04-02 05:58:02 +03:00
|
|
|
!(Ostap.Util.expr
|
|
|
|
|
(fun x -> x)
|
2019-03-25 00:13:42 +03:00
|
|
|
(Array.map (fun (a, l) -> a, List.map (fun (s, f) -> ostap (- $(s)), f) l) infix)
|
|
|
|
|
(primary infix));
|
2019-04-02 19:51:46 +03:00
|
|
|
primary[infix]:
|
|
|
|
|
b:base[infix] is:(-"[" i:parse[infix] -"]" {`Elem i} | -"." (%"length" {`Len} | %"string" {`Str} | f:LIDENT {`Post f})) * {
|
2019-03-11 15:24:03 +03:00
|
|
|
List.fold_left
|
|
|
|
|
(fun b ->
|
|
|
|
|
function
|
|
|
|
|
| `Elem i -> Elem (b, i)
|
|
|
|
|
| `Len -> Length b
|
|
|
|
|
| `Str -> StringVal b
|
2019-04-02 19:51:46 +03:00
|
|
|
| `Post f -> Call (Var f, [b])
|
2019-03-11 15:24:03 +03:00
|
|
|
)
|
|
|
|
|
b
|
|
|
|
|
is
|
|
|
|
|
};
|
2019-03-25 00:13:42 +03:00
|
|
|
base[infix]:
|
|
|
|
|
n:DECIMAL {Const n}
|
|
|
|
|
| s:STRING {String (unquote s)}
|
|
|
|
|
| c:CHAR {Const (Char.code c)}
|
|
|
|
|
| "[" es:!(Util.list0)[parse infix] "]" {Array es}
|
|
|
|
|
| "{" es:!(Util.list0)[parse infix] "}" {match es with
|
|
|
|
|
| [] -> Const 0
|
|
|
|
|
| _ -> List.fold_right (fun x acc -> Sexp ("cons", [x; acc])) es (Const 0)
|
|
|
|
|
}
|
|
|
|
|
| t:UIDENT args:(-"(" !(Util.list)[parse infix] -")")? {Sexp (t, match args with None -> [] | Some args -> args)}
|
2019-04-02 19:51:46 +03:00
|
|
|
| x:LIDENT s:("(" args:!(Util.list0)[parse infix] ")" {Call (Var x, args)} | empty {Var x}) {s}
|
|
|
|
|
|
|
|
|
|
| %"skip" {Skip}
|
|
|
|
|
|
|
|
|
|
| %"if" e:!(parse infix)
|
|
|
|
|
%"then" the:parse[infix]
|
|
|
|
|
elif:(%"elif" parse[infix] %"then" parse[infix])*
|
|
|
|
|
els:(%"else" parse[infix])?
|
|
|
|
|
%"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:parse[infix] %"do" s:parse[infix] %"od" {While (e, s)}
|
|
|
|
|
|
|
|
|
|
| %"for" i:parse[infix] "," c:parse[infix] "," s:parse[infix] %"do" b:parse[infix] %"od" {
|
|
|
|
|
Seq (i, While (c, Seq (b, s)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
| %"repeat" s:parse[infix] %"until" e:basic[infix] {Repeat (s, e)}
|
|
|
|
|
| %"return" e:basic[infix]? {Return e}
|
|
|
|
|
|
|
|
|
|
| %"case" e:parse[infix] %"of" bs:!(Util.listBy)[ostap ("|")][ostap (!(Pattern.parse) -"->" parse[infix])] %"esac" {Case (e, bs)}
|
|
|
|
|
|
2019-03-25 00:13:42 +03:00
|
|
|
| -"(" parse[infix] -")"
|
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
|
2019-03-25 00:13:42 +03:00
|
|
|
|
|
|
|
|
(* Infix helpers *)
|
|
|
|
|
module Infix =
|
|
|
|
|
struct
|
|
|
|
|
|
|
|
|
|
type t = ([`Lefta | `Righta | `Nona] * (string * (Expr.t -> Expr.t -> Expr.t)) list) array
|
|
|
|
|
|
|
|
|
|
let name infix =
|
|
|
|
|
let b = Buffer.create 64 in
|
|
|
|
|
Buffer.add_string b "__Infix_";
|
|
|
|
|
Seq.iter (fun c -> Buffer.add_string b (string_of_int @@ Char.code c)) @@ String.to_seq infix;
|
|
|
|
|
Buffer.contents b
|
|
|
|
|
|
|
|
|
|
let default : t =
|
|
|
|
|
Array.map (fun (a, s) ->
|
|
|
|
|
a,
|
|
|
|
|
List.map (fun s -> s,
|
|
|
|
|
(fun x y ->
|
|
|
|
|
match s with
|
2019-04-02 19:51:46 +03:00
|
|
|
| ":" -> Expr.Sexp ("cons", [x; y])
|
|
|
|
|
| "++" -> Expr.Call (Var "strcat", [x; y])
|
|
|
|
|
| ":=" -> Expr.Assign (Expr.propagate_ref x, y)
|
|
|
|
|
| _ -> Expr.Binop (s, x, y)
|
2019-03-25 00:13:42 +03:00
|
|
|
)
|
|
|
|
|
) s
|
|
|
|
|
)
|
|
|
|
|
[|
|
2019-04-02 19:51:46 +03:00
|
|
|
`Righta, [":="];
|
2019-03-25 00:13:42 +03:00
|
|
|
`Righta, [":"];
|
|
|
|
|
`Lefta , ["!!"];
|
|
|
|
|
`Lefta , ["&&"];
|
|
|
|
|
`Nona , ["=="; "!="; "<="; "<"; ">="; ">"];
|
|
|
|
|
`Lefta , ["++"; "+" ; "-"];
|
|
|
|
|
`Lefta , ["*" ; "/"; "%"];
|
|
|
|
|
|]
|
|
|
|
|
|
|
|
|
|
exception Break of [`Ok of t | `Fail of string]
|
|
|
|
|
|
|
|
|
|
let find_op infix op cb ce =
|
|
|
|
|
try
|
|
|
|
|
Array.iteri (fun i (_, l) -> if List.exists (fun (s, _) -> s = op) l then raise (Break (cb i))) infix;
|
|
|
|
|
ce ()
|
|
|
|
|
with Break x -> x
|
|
|
|
|
|
|
|
|
|
let no_op op coord = `Fail (Printf.sprintf "infix ``%s'' not found in the scope at %s" op (Msg.Coord.toString coord))
|
|
|
|
|
|
2019-04-02 19:51:46 +03:00
|
|
|
let sem name x y = Expr.Call (Var name, [x; y])
|
2019-03-25 00:13:42 +03:00
|
|
|
|
|
|
|
|
let at coord op newp name infix =
|
|
|
|
|
find_op infix op
|
|
|
|
|
(fun i ->
|
|
|
|
|
`Ok (Array.init (Array.length infix)
|
|
|
|
|
(fun j ->
|
|
|
|
|
if j = i
|
|
|
|
|
then let (a, l) = infix.(i) in (a, (newp, sem name) :: l)
|
|
|
|
|
else infix.(j)
|
|
|
|
|
))
|
|
|
|
|
)
|
|
|
|
|
(fun _ -> no_op op coord)
|
|
|
|
|
|
|
|
|
|
let before coord op newp ass name infix =
|
|
|
|
|
find_op infix op
|
|
|
|
|
(fun i ->
|
|
|
|
|
`Ok (Array.init (1 + Array.length infix)
|
|
|
|
|
(fun j ->
|
|
|
|
|
if j < i
|
|
|
|
|
then infix.(j)
|
|
|
|
|
else if j = i then (ass, [newp, sem name])
|
|
|
|
|
else infix.(j-1)
|
|
|
|
|
))
|
|
|
|
|
)
|
|
|
|
|
(fun _ -> no_op op coord)
|
|
|
|
|
|
|
|
|
|
let after coord op newp ass name infix =
|
|
|
|
|
find_op infix op
|
|
|
|
|
(fun i ->
|
|
|
|
|
`Ok (Array.init (1 + Array.length infix)
|
|
|
|
|
(fun j ->
|
|
|
|
|
if j <= i
|
|
|
|
|
then infix.(j)
|
|
|
|
|
else if j = i+1 then (ass, [newp, sem name])
|
|
|
|
|
else infix.(j-1)
|
|
|
|
|
))
|
|
|
|
|
)
|
|
|
|
|
(fun _ -> no_op op coord)
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
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 *)
|
2019-04-02 19:51:46 +03:00
|
|
|
type t = string * (string list * string list * Expr.t)
|
2018-03-27 01:51:22 +03:00
|
|
|
|
|
|
|
|
ostap (
|
2019-03-25 00:13:42 +03:00
|
|
|
arg : LIDENT;
|
|
|
|
|
position[ass][coord][newp]:
|
|
|
|
|
%"at" s:STRING {Infix.at coord (unquote s) newp}
|
2019-04-02 19:51:46 +03:00
|
|
|
| f:(%"before" {Infix.before} | %"after" {Infix.after}) s:STRING {f coord (unquote s) newp ass};
|
2019-03-25 00:13:42 +03:00
|
|
|
head[infix]:
|
|
|
|
|
%"fun" name:LIDENT {name, infix}
|
2019-04-02 19:51:46 +03:00
|
|
|
| ass:(%"infix" {`Nona} | %"infixl" {`Lefta} | %"infixr" {`Righta})
|
|
|
|
|
l:$ op:(s:STRING {unquote s})
|
2019-03-25 00:13:42 +03:00
|
|
|
md:position[ass][l#coord][op] {
|
|
|
|
|
let name = Infix.name op in
|
|
|
|
|
match md name infix with
|
|
|
|
|
| `Ok infix' -> name, infix'
|
2019-04-02 19:51:46 +03:00
|
|
|
| `Fail msg -> raise (Semantic_error msg)
|
|
|
|
|
};
|
2019-03-25 00:13:42 +03:00
|
|
|
parse[infix]:
|
|
|
|
|
<(name, infix')> : head[infix] "(" args:!(Util.list0 arg) ")"
|
|
|
|
|
locs:(%"local" !(Util.list arg))?
|
2019-04-02 19:51:46 +03:00
|
|
|
"{" body:!(Expr.parse infix') "}" {
|
2019-03-25 00:13:42 +03:00
|
|
|
(name, (args, (match locs with None -> [] | Some l -> l), body)), infix'
|
2018-04-02 05:58:02 +03:00
|
|
|
}
|
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) *)
|
2019-04-02 19:51:46 +03:00
|
|
|
type t = Definition.t list * Expr.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, _ =
|
2019-04-02 19:51:46 +03:00
|
|
|
Expr.eval
|
2018-04-02 07:00:36 +03:00
|
|
|
(object
|
2019-04-02 19:51:46 +03:00
|
|
|
method definition env f args ((st, i, o, vs) as conf) =
|
2018-04-25 01:06:18 +03:00
|
|
|
try
|
2019-04-02 19:51:46 +03:00
|
|
|
let xs, locs, s = snd @@ M.find f m in
|
|
|
|
|
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', vs' = Expr.eval env (st', i, o, []) Skip s in
|
|
|
|
|
(State.leave st'' st, i', o', match vs' with [v] -> v::vs | _ -> vs)
|
2018-04-25 01:06:18 +03:00
|
|
|
with Not_found -> Builtin.eval conf args f
|
2018-04-02 07:00:36 +03:00
|
|
|
end)
|
2019-04-02 19:51:46 +03:00
|
|
|
(State.empty, i, [], [])
|
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 *)
|
2019-03-25 00:13:42 +03:00
|
|
|
ostap (
|
2019-04-02 19:51:46 +03:00
|
|
|
parse[infix]: <(defs, infix')> : definitions[infix] body:!(Expr.parse infix') {defs, body};
|
2019-03-25 00:13:42 +03:00
|
|
|
definitions[infix]:
|
|
|
|
|
<(def, infix')> : !(Definition.parse infix) <(defs, infix'')> : definitions[infix'] {def::defs, infix''}
|
|
|
|
|
| empty {[], infix}
|
|
|
|
|
)
|