Generalized state

This commit is contained in:
Dmitry Boulytchev 2019-09-29 02:47:07 +03:00
parent c3e6d4c76d
commit 36685d1592
56 changed files with 64 additions and 65 deletions

View file

@ -1,3 +1,3 @@
global x = read (), y = read (), z = x * y * 3;
local x = read (), y = read (), z = x * y * 3;
write (z)

View file

@ -1,4 +1,4 @@
global x, y, z;
local x, y, z;
x := read ();
y := read ();

View file

@ -1,4 +1,4 @@
global x, y;
local x, y;
x := read ();
y := read ();

View file

@ -1,4 +1,4 @@
global x, y, z;
local x, y, z;
x := read ();
y := read ();

View file

@ -1,4 +1,4 @@
global x, y, z;
local x, y, z;
x := read ();
y := read ();

View file

@ -1,4 +1,4 @@
global x, y, z;
local x, y, z;
x := read ();
y := read ();

View file

@ -1,4 +1,4 @@
global x, y, z;
local x, y, z;
x := 1;
y := 2;

View file

@ -1,4 +1,4 @@
global z;
local z;
z := 0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9;
write (z)

View file

@ -1,4 +1,4 @@
global n = 2, k = 10;
local n = 2, k = 10;
{local res = 1;
while k > 0 do

View file

@ -1,4 +1,4 @@
global i, s;
local i, s;
i := 0;
s := 0;

View file

@ -1,4 +1,4 @@
global x;
local x;
x:=0;
if x

View file

@ -1,4 +1,4 @@
global n;
local n;
n := read ();
while n >= 0 do

View file

@ -1,4 +1,4 @@
global n = read ();
local n = read ();
repeat

View file

@ -1,4 +1,4 @@
global n;
local n;
n := read ();

View file

@ -1,4 +1,4 @@
global s, n, p;
local s, n, p;
s := 0;
n := read ();

View file

@ -1,4 +1,4 @@
global s, n;
local s, n;
n := read ();

View file

@ -1,4 +1,4 @@
global n, fib_1, fib_2, fib, i;
local n, fib_1, fib_2, fib, i;
n := read ();

View file

@ -1,4 +1,4 @@
global n, c, p, q, i, m, d;
local n, c, p, q, i, m, d;
n := read ();

View file

@ -1,4 +1,4 @@
global i, s;
local i, s;
i := 0;
s := 0;

View file

@ -1,4 +1,4 @@
global s, n, p;
local s, n, p;
s := 0;
n := read ();

View file

@ -1,4 +1,4 @@
global n, f;
local n, f;
n := read ();
f := 1;

View file

@ -1,4 +1,4 @@
global n, fib_1, fib_2, fib, i;
local n, fib_1, fib_2, fib, i;
n := read ();

View file

@ -1,4 +1,4 @@
global s, n;
local s, n;
s := 0;

View file

@ -6,7 +6,7 @@ fun test2 (b) {
a := b
}
global x, a, b;
local x, a, b;
x := read ();

View file

@ -1,4 +1,4 @@
global x, a, b, c;
local x, a, b, c;
fun test1 (a) {
write (a)

View file

@ -1,4 +1,4 @@
global x, a, b, c;
local x, a, b, c;
fun test1 (a) {
write (a);

View file

@ -1,4 +1,4 @@
global x, a, b, c;
local x, a, b, c;
fun print () {
write (a);

View file

@ -1,4 +1,4 @@
global n, i, result;
local n, i, result;
fun fact (n) {
if n <= 1

View file

@ -1,4 +1,4 @@
global i, n, result;
local i, n, result;
fun fib (n) {
local r;

View file

@ -1,4 +1,4 @@
global n, i;
local n, i;
fun fib (n) {
if n <= 1

View file

@ -1,4 +1,4 @@
global n, i;
local n, i;
fun fact (n) {
if n <= 1

View file

@ -1,4 +1,4 @@
global x, m, n;
local x, m, n;
fun ack (m, n) {
if m == 0 then return (n+1)

View file

@ -1,4 +1,4 @@
global x;
local x;
fun test (n, m) {
local i, s;

View file

@ -1,4 +1,4 @@
global n, x, i;
local n, x, i;
fun printString (x) {
for i:=0, i<x.length, i:=i+1 do

View file

@ -1,4 +1,4 @@
global n, x, i;
local n, x, i;
fun printArray (x) {
local elem;

View file

@ -1,4 +1,4 @@
global x, y, i, j;
local x, y, i, j;
fun printAS (x) {
local i, j;

View file

@ -1,4 +1,4 @@
global n, x, i;
local n, x, i;
fun sort (x) {
local i, j, y, n;

View file

@ -1,4 +1,4 @@
global n, x, y;
local n, x, y;
fun append (x, y) {
case x of

View file

@ -1,4 +1,4 @@
global n, t;
local n, t;
fun insert (t, x) {
case t of

View file

@ -1,4 +1,4 @@
global x;
local x;
fun f (x) {
case x of

View file

@ -1,4 +1,4 @@
global x;
local x;
fun f (a) {
case a of

View file

@ -1,4 +1,4 @@
global x, y, i;
local x, y, i;
fun f (x) {
case x of

View file

@ -1,4 +1,4 @@
global x;
local x;
fun sum (x) {
case x of

View file

@ -1,4 +1,4 @@
global x, y, z;
local x, y, z;
fun zip (x) {
case x of Pair (x, y) ->

View file

@ -1,4 +1,4 @@
global x;
local x;
fun printString (s) {
local i;

View file

@ -1,4 +1,4 @@
global n;
local n;
n := read ();

View file

@ -1,4 +1,4 @@
global n;
local n;
fun collect_ints_acc (v, tail) {
local i;

View file

@ -1,4 +1,4 @@
global n, y;
local n, y;
fun test (n, m) {
local i, s;

View file

@ -1,4 +1,4 @@
global n, y, y2, t;
local n, y, y2, t;
fun test (n, m) {
local i, s;

View file

@ -1,4 +1,4 @@
global n, x, y;
local n, x, y;
n := read ();
x := [1];

View file

@ -1,4 +1,4 @@
global n;
local n;
fun sum (l) {
case l of

View file

@ -1,4 +1,4 @@
global n;
local n;
fun hd (l) {
case l of

View file

@ -1,4 +1,4 @@
global n;
local n;
infix "===" at "==" (v1, v2) {
local s1, s2, i;

View file

@ -1,4 +1,4 @@
global x, y, z, s, i, n;
local x, y, z, s, i, n;
x := y := z := 105;

View file

@ -8,7 +8,7 @@ let parse infile =
"while"; "do"; "od";
"repeat"; "until";
"for";
"fun"; "local"; "return"; "global";
"fun"; "local"; "return";
"length";
"string";
"case"; "of"; "esac"; "when";

View file

@ -119,8 +119,8 @@ module State =
(* State: global state, local state, scope variables *)
type 'a t =
| I
| G of (string * bool) list * (string -> ('a, 'a t) Value.t)
| L of (string * bool) list * (string -> ('a, 'a t) Value.t) * 'a t
| G of (string * bool) list * (string -> 'a)
| L of (string * bool) list * (string -> 'a) * 'a t
(* Undefined state *)
let undefined x = failwith (Printf.sprintf "Undefined variable: %s" x)
@ -265,7 +265,8 @@ module Expr =
(* The type of configuration: a state, an input stream, an output stream,
and a stack of values
*)
type 'a config = 'a State.t * int list * int list * ('a, 'a State.t) Value.t list
type 'a value = ('a, 'a value State.t) Value.t
type 'a config = 'a value State.t * int list * int list * 'a value list
(* The type for expressions. Note, in regular OCaml there is no "@type..."
notation, it came from GT.
@ -770,8 +771,8 @@ module Definition =
| `Fail msg -> raise (Semantic_error msg)
};
local_var[infix][expr][def]: name:LIDENT value:(-"=" expr[def][infix][Expr.Val])? {name, `Variable value};
parse[kind][infix][expr][def]:
kind locs:!(Util.list (local_var infix expr def)) ";" {locs, infix}
parse[infix][expr][def]:
%"local" locs:!(Util.list (local_var infix expr def)) ";" {locs, infix}
| <(name, infix')> : head[infix] "(" args:!(Util.list0 arg) ")"
body:expr[def][infix'][Expr.Void] {
[(name, `Fun (args, body))], infix'
@ -797,10 +798,8 @@ let eval expr i =
(* Top-level parser *)
ostap (
parse[infix]: !(Expr.scope (definitions global) infix Expr.Void (Expr.parse (definitions local)));
local: %"local" {`Local};
global: %"global" {`Global};
definitions[kind][infix]:
<(def, infix')> : !(Definition.parse kind infix Expr.basic (definitions local)) <(defs, infix'')> : definitions[kind][infix'] {def @ defs, infix''}
parse[infix]: !(Expr.scope definitions infix Expr.Void (Expr.parse definitions));
definitions[infix]:
<(def, infix')> : !(Definition.parse infix Expr.basic definitions) <(defs, infix'')> : definitions[infix'] {def @ defs, infix''}
| empty {[], infix}
)