mirror of
https://github.com/ProgramSnail/Lama.git
synced 2025-12-05 22:38:44 +00:00
Global definitions; regression tests fixed
This commit is contained in:
parent
a3b12425fd
commit
f5b802ebed
56 changed files with 121 additions and 10 deletions
|
|
@ -1,3 +1,5 @@
|
|||
global x, y, z;
|
||||
|
||||
x := read ();
|
||||
y := read ();
|
||||
z := x*y*3;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global x, y, z;
|
||||
|
||||
x := read ();
|
||||
y := read ();
|
||||
z := y*y;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global x, y;
|
||||
|
||||
x := read ();
|
||||
y := read ();
|
||||
write (x-y);
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global x, y, z;
|
||||
|
||||
x := read ();
|
||||
y := read ();
|
||||
z := ((x + y) + (x - y)) + ((x - y) - (x + y));
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global x, y, z;
|
||||
|
||||
x := read ();
|
||||
y := read ();
|
||||
z := ((x + y) + (x - y)) + ((x - y) - (x / y));
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global x, y, z;
|
||||
|
||||
x := read ();
|
||||
y := read ();
|
||||
z := x < y;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global x, y, z;
|
||||
|
||||
x := 1;
|
||||
y := 2;
|
||||
z := x - y - 3;
|
||||
|
|
|
|||
|
|
@ -1,2 +1,4 @@
|
|||
global z;
|
||||
|
||||
z := 0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9;
|
||||
write (z)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global n, k, res;
|
||||
|
||||
n := 2;
|
||||
k := 10;
|
||||
res := 1;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global i, s, j;
|
||||
|
||||
i := 0;
|
||||
s := 0;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global x;
|
||||
|
||||
x:=0;
|
||||
if x
|
||||
then write(1)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global n;
|
||||
|
||||
n := read ();
|
||||
while n >= 0 do
|
||||
if n > 1
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global n;
|
||||
|
||||
n := read ();
|
||||
|
||||
repeat
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global n;
|
||||
|
||||
n := read ();
|
||||
|
||||
while n > 0 do
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
global s, n, p;
|
||||
|
||||
s := 0;
|
||||
n := read ();
|
||||
p := 2;
|
||||
|
||||
while n > 0 do
|
||||
c := 2;
|
||||
f := 1;
|
||||
while n > 0 do {
|
||||
local c = 2, f = 1;
|
||||
|
||||
while c*c <= p && f do
|
||||
f := (p % c) != 0;
|
||||
|
|
@ -17,4 +18,5 @@ while n > 0 do
|
|||
else skip fi;
|
||||
|
||||
p := p + 1
|
||||
}
|
||||
od
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global s, n;
|
||||
|
||||
n := read ();
|
||||
|
||||
s := 1;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global n, fib_1, fib_2, fib, i;
|
||||
|
||||
n := read ();
|
||||
|
||||
i := 2;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global n, c, p, cc, q, i, m, d;
|
||||
|
||||
n := read ();
|
||||
|
||||
c := 1;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global i, s, j;
|
||||
|
||||
i := 0;
|
||||
s := 0;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global s, n, p, c, f;
|
||||
|
||||
s := 0;
|
||||
n := read ();
|
||||
p := 2;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global n, f;
|
||||
|
||||
n := read ();
|
||||
f := 1;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global n, fib_1, fib_2, fib, i;
|
||||
|
||||
n := read ();
|
||||
|
||||
fib_1 := 1;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global s, n;
|
||||
|
||||
s := 0;
|
||||
|
||||
repeat
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ fun test2 (b) {
|
|||
a := b
|
||||
}
|
||||
|
||||
global x, a, b;
|
||||
|
||||
x := read ();
|
||||
|
||||
test1 ();
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global x, a, b, c;
|
||||
|
||||
fun test1 (a) {
|
||||
write (a)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global x, a, b, c;
|
||||
|
||||
fun test1 (a) {
|
||||
write (a);
|
||||
print ()
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global x, a, b, c;
|
||||
|
||||
fun print () {
|
||||
write (a);
|
||||
write (b);
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global n, i, result;
|
||||
|
||||
fun fact (n) {
|
||||
if n <= 1
|
||||
then result := 1
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global i, n, result;
|
||||
|
||||
fun fib (n) {
|
||||
local r;
|
||||
if n <= 1
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global n, i;
|
||||
|
||||
fun fib (n) {
|
||||
if n <= 1
|
||||
then return 1
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global n, i;
|
||||
|
||||
fun fact (n) {
|
||||
if n <= 1
|
||||
then return 1
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global x, m, n;
|
||||
|
||||
fun ack (m, n) {
|
||||
if m == 0 then return (n+1)
|
||||
elif m > 0 && n == 0 then return ack (m-1, 1)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global x;
|
||||
|
||||
fun test (n, m) {
|
||||
local i, s;
|
||||
s := 0;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global n, x, i;
|
||||
|
||||
fun printString (x) {
|
||||
for i:=0, i<x.length, i:=i+1 do
|
||||
write (x[i])
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global n, x, i;
|
||||
|
||||
fun printArray (x) {
|
||||
local elem;
|
||||
if x.length == 0 then return fi;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global x, y, i, j;
|
||||
|
||||
fun printAS (x) {
|
||||
local i, j;
|
||||
for i := 0, i<x.length, i:=i+1 do
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global n, x, i;
|
||||
|
||||
fun sort (x) {
|
||||
local i, j, y, n;
|
||||
n := x.length;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global n, x, y;
|
||||
|
||||
fun append (x, y) {
|
||||
case x of
|
||||
Nil -> return y
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global n, t;
|
||||
|
||||
fun insert (t, x) {
|
||||
case t of
|
||||
Leaf -> return Node (x, Leaf, Leaf)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global x;
|
||||
|
||||
fun f (x) {
|
||||
case x of
|
||||
A -> write (1)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global x;
|
||||
|
||||
fun f (a) {
|
||||
case a of
|
||||
A (x, y, z) -> write (x + y + z)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global x, y, i;
|
||||
|
||||
fun f (x) {
|
||||
case x of
|
||||
Nil -> write (0)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global x;
|
||||
|
||||
fun sum (x) {
|
||||
case x of
|
||||
Nil -> return 0
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global x, y, z;
|
||||
|
||||
fun zip (x) {
|
||||
case x of Pair (x, y) ->
|
||||
case x of
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global x;
|
||||
|
||||
fun printString (s) {
|
||||
local i;
|
||||
for i := 0, i < s.length, i := i + 1 do
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global n;
|
||||
|
||||
n := read ();
|
||||
|
||||
case 3 of
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global n;
|
||||
|
||||
fun collect_ints_acc (v, tail) {
|
||||
local i;
|
||||
case v of
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global n, y;
|
||||
|
||||
fun test (n, m) {
|
||||
local i, s;
|
||||
write (n);
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global n, y, y2, t;
|
||||
|
||||
fun test (n, m) {
|
||||
local i, s;
|
||||
s := 0;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global n, x, y;
|
||||
|
||||
n := read ();
|
||||
x := [1];
|
||||
y := [x, x];
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global n;
|
||||
|
||||
fun sum (l) {
|
||||
case l of
|
||||
{} -> return 0
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global n;
|
||||
|
||||
fun hd (l) {
|
||||
case l of
|
||||
h : _ -> return h
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global n;
|
||||
|
||||
infix "===" at "==" (v1, v2) {
|
||||
local s1, s2, i;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
global x, y, z, s, i, n;
|
||||
|
||||
x := y := z := 105;
|
||||
|
||||
n := read ();
|
||||
|
|
|
|||
|
|
@ -91,7 +91,7 @@ module State =
|
|||
|
||||
(* State: global state, local state, scope variables *)
|
||||
type t =
|
||||
| G of (string -> Value.t)
|
||||
| G of string list * (string -> Value.t)
|
||||
| L of string list * (string -> Value.t) * t
|
||||
|
||||
(* Undefined state *)
|
||||
|
|
@ -101,14 +101,17 @@ module State =
|
|||
let bind x v s = fun y -> if x = y then v else s y
|
||||
|
||||
(* Empty state *)
|
||||
let empty = G undefined
|
||||
let global vars = G (vars, undefined)
|
||||
|
||||
(* 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
|
||||
*)
|
||||
let update x v s =
|
||||
let rec inner = function
|
||||
| G s -> G (bind x v s)
|
||||
| G (scope, s) ->
|
||||
if not (List.mem x scope)
|
||||
then invalid_arg (Printf.sprintf "undefined variable %s" x)
|
||||
else G (scope, bind x v s)
|
||||
| L (scope, s, enclosing) ->
|
||||
if List.mem x scope then L (scope, bind x v s, enclosing) else L (scope, s, inner enclosing)
|
||||
in
|
||||
|
|
@ -117,7 +120,7 @@ module State =
|
|||
(* Evals a variable in a state w.r.t. a scope *)
|
||||
let rec eval s x =
|
||||
match s with
|
||||
| G s -> s x
|
||||
| G (_, s) -> s x
|
||||
| L (scope, s, enclosing) -> if List.mem x scope then s x else eval enclosing x
|
||||
|
||||
(* Creates a new scope, based on a given state *)
|
||||
|
|
@ -730,7 +733,7 @@ type t = Definition.t list * Expr.t
|
|||
*)
|
||||
let eval ((defs, body) : t) i =
|
||||
let module M = Map.Make (String) in
|
||||
let m = List.fold_left (fun m ((name, proc) as def) -> match proc with `Fun (args, stmt) -> M.add name (name, (args, stmt)) m | _ -> m) M.empty defs in
|
||||
let m, gvars = List.fold_left (fun (m, gv) ((name, proc) as def) -> match proc with `Fun (args, stmt) -> M.add name (name, (args, stmt)) m, gv | _ -> m, name::gv) (M.empty, []) defs in
|
||||
let _, _, o, _ =
|
||||
Expr.eval
|
||||
(object
|
||||
|
|
@ -742,7 +745,7 @@ let eval ((defs, body) : t) i =
|
|||
(State.leave st'' st, i', o', match vs' with [v] -> v::vs | _ -> Value.Empty :: vs)
|
||||
with Not_found -> Builtin.eval conf args f
|
||||
end)
|
||||
(State.empty, i, [], [])
|
||||
(State.global gvars, i, [], [])
|
||||
Skip
|
||||
body
|
||||
in
|
||||
|
|
|
|||
|
|
@ -143,7 +143,7 @@ let run p i =
|
|||
(cstack, (match r with [r] -> r::stack' | _ -> Value.Empty :: stack'), (st, i, o))
|
||||
end
|
||||
)
|
||||
([], [], (State.empty, i, []))
|
||||
([], [], (State.global [] (* TODO! *), i, []))
|
||||
p
|
||||
in
|
||||
o
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue