mirror of
https://github.com/ProgramSnail/Lama.git
synced 2025-12-06 14:58:50 +00:00
Generalized state
This commit is contained in:
parent
c3e6d4c76d
commit
36685d1592
56 changed files with 64 additions and 65 deletions
|
|
@ -1,3 +1,3 @@
|
||||||
global x = read (), y = read (), z = x * y * 3;
|
local x = read (), y = read (), z = x * y * 3;
|
||||||
|
|
||||||
write (z)
|
write (z)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global x, y, z;
|
local x, y, z;
|
||||||
|
|
||||||
x := read ();
|
x := read ();
|
||||||
y := read ();
|
y := read ();
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global x, y;
|
local x, y;
|
||||||
|
|
||||||
x := read ();
|
x := read ();
|
||||||
y := read ();
|
y := read ();
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global x, y, z;
|
local x, y, z;
|
||||||
|
|
||||||
x := read ();
|
x := read ();
|
||||||
y := read ();
|
y := read ();
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global x, y, z;
|
local x, y, z;
|
||||||
|
|
||||||
x := read ();
|
x := read ();
|
||||||
y := read ();
|
y := read ();
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global x, y, z;
|
local x, y, z;
|
||||||
|
|
||||||
x := read ();
|
x := read ();
|
||||||
y := read ();
|
y := read ();
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global x, y, z;
|
local x, y, z;
|
||||||
|
|
||||||
x := 1;
|
x := 1;
|
||||||
y := 2;
|
y := 2;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global z;
|
local z;
|
||||||
|
|
||||||
z := 0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9;
|
z := 0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9;
|
||||||
write (z)
|
write (z)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global n = 2, k = 10;
|
local n = 2, k = 10;
|
||||||
|
|
||||||
{local res = 1;
|
{local res = 1;
|
||||||
while k > 0 do
|
while k > 0 do
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global i, s;
|
local i, s;
|
||||||
|
|
||||||
i := 0;
|
i := 0;
|
||||||
s := 0;
|
s := 0;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global x;
|
local x;
|
||||||
|
|
||||||
x:=0;
|
x:=0;
|
||||||
if x
|
if x
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global n;
|
local n;
|
||||||
|
|
||||||
n := read ();
|
n := read ();
|
||||||
while n >= 0 do
|
while n >= 0 do
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global n = read ();
|
local n = read ();
|
||||||
|
|
||||||
repeat
|
repeat
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global n;
|
local n;
|
||||||
|
|
||||||
n := read ();
|
n := read ();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global s, n, p;
|
local s, n, p;
|
||||||
|
|
||||||
s := 0;
|
s := 0;
|
||||||
n := read ();
|
n := read ();
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global s, n;
|
local s, n;
|
||||||
|
|
||||||
n := read ();
|
n := read ();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global n, fib_1, fib_2, fib, i;
|
local n, fib_1, fib_2, fib, i;
|
||||||
|
|
||||||
n := read ();
|
n := read ();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global n, c, p, q, i, m, d;
|
local n, c, p, q, i, m, d;
|
||||||
|
|
||||||
n := read ();
|
n := read ();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global i, s;
|
local i, s;
|
||||||
|
|
||||||
i := 0;
|
i := 0;
|
||||||
s := 0;
|
s := 0;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global s, n, p;
|
local s, n, p;
|
||||||
|
|
||||||
s := 0;
|
s := 0;
|
||||||
n := read ();
|
n := read ();
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global n, f;
|
local n, f;
|
||||||
|
|
||||||
n := read ();
|
n := read ();
|
||||||
f := 1;
|
f := 1;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global n, fib_1, fib_2, fib, i;
|
local n, fib_1, fib_2, fib, i;
|
||||||
|
|
||||||
n := read ();
|
n := read ();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global s, n;
|
local s, n;
|
||||||
|
|
||||||
s := 0;
|
s := 0;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ fun test2 (b) {
|
||||||
a := b
|
a := b
|
||||||
}
|
}
|
||||||
|
|
||||||
global x, a, b;
|
local x, a, b;
|
||||||
|
|
||||||
x := read ();
|
x := read ();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global x, a, b, c;
|
local x, a, b, c;
|
||||||
|
|
||||||
fun test1 (a) {
|
fun test1 (a) {
|
||||||
write (a)
|
write (a)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global x, a, b, c;
|
local x, a, b, c;
|
||||||
|
|
||||||
fun test1 (a) {
|
fun test1 (a) {
|
||||||
write (a);
|
write (a);
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global x, a, b, c;
|
local x, a, b, c;
|
||||||
|
|
||||||
fun print () {
|
fun print () {
|
||||||
write (a);
|
write (a);
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global n, i, result;
|
local n, i, result;
|
||||||
|
|
||||||
fun fact (n) {
|
fun fact (n) {
|
||||||
if n <= 1
|
if n <= 1
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global i, n, result;
|
local i, n, result;
|
||||||
|
|
||||||
fun fib (n) {
|
fun fib (n) {
|
||||||
local r;
|
local r;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global n, i;
|
local n, i;
|
||||||
|
|
||||||
fun fib (n) {
|
fun fib (n) {
|
||||||
if n <= 1
|
if n <= 1
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global n, i;
|
local n, i;
|
||||||
|
|
||||||
fun fact (n) {
|
fun fact (n) {
|
||||||
if n <= 1
|
if n <= 1
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global x, m, n;
|
local x, m, n;
|
||||||
|
|
||||||
fun ack (m, n) {
|
fun ack (m, n) {
|
||||||
if m == 0 then return (n+1)
|
if m == 0 then return (n+1)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global x;
|
local x;
|
||||||
|
|
||||||
fun test (n, m) {
|
fun test (n, m) {
|
||||||
local i, s;
|
local i, s;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global n, x, i;
|
local n, x, i;
|
||||||
|
|
||||||
fun printString (x) {
|
fun printString (x) {
|
||||||
for i:=0, i<x.length, i:=i+1 do
|
for i:=0, i<x.length, i:=i+1 do
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global n, x, i;
|
local n, x, i;
|
||||||
|
|
||||||
fun printArray (x) {
|
fun printArray (x) {
|
||||||
local elem;
|
local elem;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global x, y, i, j;
|
local x, y, i, j;
|
||||||
|
|
||||||
fun printAS (x) {
|
fun printAS (x) {
|
||||||
local i, j;
|
local i, j;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global n, x, i;
|
local n, x, i;
|
||||||
|
|
||||||
fun sort (x) {
|
fun sort (x) {
|
||||||
local i, j, y, n;
|
local i, j, y, n;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global n, x, y;
|
local n, x, y;
|
||||||
|
|
||||||
fun append (x, y) {
|
fun append (x, y) {
|
||||||
case x of
|
case x of
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global n, t;
|
local n, t;
|
||||||
|
|
||||||
fun insert (t, x) {
|
fun insert (t, x) {
|
||||||
case t of
|
case t of
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global x;
|
local x;
|
||||||
|
|
||||||
fun f (x) {
|
fun f (x) {
|
||||||
case x of
|
case x of
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global x;
|
local x;
|
||||||
|
|
||||||
fun f (a) {
|
fun f (a) {
|
||||||
case a of
|
case a of
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global x, y, i;
|
local x, y, i;
|
||||||
|
|
||||||
fun f (x) {
|
fun f (x) {
|
||||||
case x of
|
case x of
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global x;
|
local x;
|
||||||
|
|
||||||
fun sum (x) {
|
fun sum (x) {
|
||||||
case x of
|
case x of
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global x, y, z;
|
local x, y, z;
|
||||||
|
|
||||||
fun zip (x) {
|
fun zip (x) {
|
||||||
case x of Pair (x, y) ->
|
case x of Pair (x, y) ->
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global x;
|
local x;
|
||||||
|
|
||||||
fun printString (s) {
|
fun printString (s) {
|
||||||
local i;
|
local i;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global n;
|
local n;
|
||||||
|
|
||||||
n := read ();
|
n := read ();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global n;
|
local n;
|
||||||
|
|
||||||
fun collect_ints_acc (v, tail) {
|
fun collect_ints_acc (v, tail) {
|
||||||
local i;
|
local i;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global n, y;
|
local n, y;
|
||||||
|
|
||||||
fun test (n, m) {
|
fun test (n, m) {
|
||||||
local i, s;
|
local i, s;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global n, y, y2, t;
|
local n, y, y2, t;
|
||||||
|
|
||||||
fun test (n, m) {
|
fun test (n, m) {
|
||||||
local i, s;
|
local i, s;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global n, x, y;
|
local n, x, y;
|
||||||
|
|
||||||
n := read ();
|
n := read ();
|
||||||
x := [1];
|
x := [1];
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global n;
|
local n;
|
||||||
|
|
||||||
fun sum (l) {
|
fun sum (l) {
|
||||||
case l of
|
case l of
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global n;
|
local n;
|
||||||
|
|
||||||
fun hd (l) {
|
fun hd (l) {
|
||||||
case l of
|
case l of
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global n;
|
local n;
|
||||||
|
|
||||||
infix "===" at "==" (v1, v2) {
|
infix "===" at "==" (v1, v2) {
|
||||||
local s1, s2, i;
|
local s1, s2, i;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
global x, y, z, s, i, n;
|
local x, y, z, s, i, n;
|
||||||
|
|
||||||
x := y := z := 105;
|
x := y := z := 105;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ let parse infile =
|
||||||
"while"; "do"; "od";
|
"while"; "do"; "od";
|
||||||
"repeat"; "until";
|
"repeat"; "until";
|
||||||
"for";
|
"for";
|
||||||
"fun"; "local"; "return"; "global";
|
"fun"; "local"; "return";
|
||||||
"length";
|
"length";
|
||||||
"string";
|
"string";
|
||||||
"case"; "of"; "esac"; "when";
|
"case"; "of"; "esac"; "when";
|
||||||
|
|
|
||||||
|
|
@ -119,8 +119,8 @@ module State =
|
||||||
(* State: global state, local state, scope variables *)
|
(* State: global state, local state, scope variables *)
|
||||||
type 'a t =
|
type 'a t =
|
||||||
| I
|
| I
|
||||||
| G of (string * bool) list * (string -> ('a, 'a t) Value.t)
|
| G of (string * bool) list * (string -> 'a)
|
||||||
| L of (string * bool) list * (string -> ('a, 'a t) Value.t) * 'a t
|
| L of (string * bool) list * (string -> 'a) * 'a t
|
||||||
|
|
||||||
(* Undefined state *)
|
(* Undefined state *)
|
||||||
let undefined x = failwith (Printf.sprintf "Undefined variable: %s" x)
|
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,
|
(* The type of configuration: a state, an input stream, an output stream,
|
||||||
and a stack of values
|
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..."
|
(* The type for expressions. Note, in regular OCaml there is no "@type..."
|
||||||
notation, it came from GT.
|
notation, it came from GT.
|
||||||
|
|
@ -770,8 +771,8 @@ module Definition =
|
||||||
| `Fail msg -> raise (Semantic_error msg)
|
| `Fail msg -> raise (Semantic_error msg)
|
||||||
};
|
};
|
||||||
local_var[infix][expr][def]: name:LIDENT value:(-"=" expr[def][infix][Expr.Val])? {name, `Variable value};
|
local_var[infix][expr][def]: name:LIDENT value:(-"=" expr[def][infix][Expr.Val])? {name, `Variable value};
|
||||||
parse[kind][infix][expr][def]:
|
parse[infix][expr][def]:
|
||||||
kind locs:!(Util.list (local_var infix expr def)) ";" {locs, infix}
|
%"local" locs:!(Util.list (local_var infix expr def)) ";" {locs, infix}
|
||||||
| <(name, infix')> : head[infix] "(" args:!(Util.list0 arg) ")"
|
| <(name, infix')> : head[infix] "(" args:!(Util.list0 arg) ")"
|
||||||
body:expr[def][infix'][Expr.Void] {
|
body:expr[def][infix'][Expr.Void] {
|
||||||
[(name, `Fun (args, body))], infix'
|
[(name, `Fun (args, body))], infix'
|
||||||
|
|
@ -797,10 +798,8 @@ let eval expr i =
|
||||||
|
|
||||||
(* Top-level parser *)
|
(* Top-level parser *)
|
||||||
ostap (
|
ostap (
|
||||||
parse[infix]: !(Expr.scope (definitions global) infix Expr.Void (Expr.parse (definitions local)));
|
parse[infix]: !(Expr.scope definitions infix Expr.Void (Expr.parse definitions));
|
||||||
local: %"local" {`Local};
|
definitions[infix]:
|
||||||
global: %"global" {`Global};
|
<(def, infix')> : !(Definition.parse infix Expr.basic definitions) <(defs, infix'')> : definitions[infix'] {def @ defs, infix''}
|
||||||
definitions[kind][infix]:
|
|
||||||
<(def, infix')> : !(Definition.parse kind infix Expr.basic (definitions local)) <(defs, infix'')> : definitions[kind][infix'] {def @ defs, infix''}
|
|
||||||
| empty {[], infix}
|
| empty {[], infix}
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue