Added tests; weird & ugly workaround in the parser

This commit is contained in:
Dmitry Boulytchev 2019-09-22 22:25:05 +03:00
parent d69cb3d49d
commit 39388d77fd
13 changed files with 108 additions and 10 deletions

View file

@ -0,0 +1,3 @@
> 6
7
8

View file

@ -0,0 +1,9 @@
> 1
2
3
2
3
4
3
4
5

View file

@ -0,0 +1,3 @@
> 0
1
2

View file

@ -0,0 +1,5 @@
> 1
1
1
1
0

12
regression/test057.expr Normal file
View file

@ -0,0 +1,12 @@
fun a (x) {return x + 1}
fun b (x) {return x + 2}
fun c (x) {return x + 3}
{
local funs = [a, b, c];
local n = read (), i;
for i := 0, i < 3, i := i+1 do
write (funs[i] (n))
od
}

1
regression/test057.input Normal file
View file

@ -0,0 +1 @@
5

21
regression/test058.expr Normal file
View file

@ -0,0 +1,21 @@
fun map (f, l) {
case l of
{} -> return {}
| h : tl -> return (f (h) : map (f, tl))
esac
}
fun a (x) {return x + 1}
fun b (x) {return x + 2}
fun print_list (x) {
case x of
{} -> skip
| h : tl -> write (h); print_list (tl)
esac
}
print_list ({1, 2, 3});
print_list (map (a, {1, 2, 3}));
print_list (map (b, {1, 2, 3}))

1
regression/test058.input Normal file
View file

@ -0,0 +1 @@
5

20
regression/test059.expr Normal file
View file

@ -0,0 +1,20 @@
fun f (l) {
case l of
{} -> skip
| h : tl -> h (); f (tl)
esac
}
fun a () {
write (0)
}
fun b () {
write (1)
}
fun c () {
write (2)
}
f ({a, b, c})

1
regression/test059.input Normal file
View file

@ -0,0 +1 @@
5

17
regression/test060.expr Normal file
View file

@ -0,0 +1,17 @@
fun f (l) {
infix "===" at "==" (a, b) {
return a == b
}
case l of
{} -> return 1
| {_} -> return 1
| a : b : tl -> return a === b && f (b : tl)
esac
}
write (f ({}));
write (f ({1}));
write (f ({1, 1}));
write (f ({1, 1, 1}));
write (f ({1, 2, 1}))

1
regression/test060.input Normal file
View file

@ -0,0 +1 @@
5

View file

@ -431,7 +431,7 @@ module Expr =
let st' = List.fold_left (fun st (x, a) -> State.update x a st) (State.enter st (List.map (fun x -> x, true) args)) (List.combine args es) in let st' = List.fold_left (fun st (x, a) -> State.update x a st) (State.enter st (List.map (fun x -> x, true) args)) (List.combine args es) in
let st'', i', o', vs'' = eval env (st', i, o, []) Skip body in let st'', i', o', vs'' = eval env (st', i, o, []) Skip body in
(State.leave st'' st, i', o', match vs'' with [v] -> v::vs' | _ -> Value.Empty :: vs') (State.leave st'' st, i', o', match vs'' with [v] -> v::vs' | _ -> Value.Empty :: vs')
| _ -> invalid_arg "callee did not evaluate to a function" | _ -> invalid_arg (Printf.sprintf "callee did not evaluate to a function: %s" (show(Value.t) (fun _ -> "<expr>") f))
))])) ))]))
| Leave -> eval env (State.drop st, i, o, vs) Skip k | Leave -> eval env (State.drop st, i, o, vs) Skip k
@ -588,9 +588,12 @@ module Expr =
basic[def][infix][atr]: !(expr (fun x -> x) (Array.map (fun (a, (atr, l)) -> a, (atr, List.map (fun (s, f) -> ostap (- $(s)), f) l)) infix) (primary def infix) atr); basic[def][infix][atr]: !(expr (fun x -> x) (Array.map (fun (a, (atr, l)) -> a, (atr, List.map (fun (s, f) -> ostap (- $(s)), f) l)) infix) (primary def infix) atr);
primary[def][infix][atr]: primary[def][infix][atr]:
b:base[def][infix][Val] is:(-"[" i:parse[def][infix][Val] -"]" {`Elem i} | -"." (%"length" {`Len} | %"string" {`Str} | f:LIDENT {`Post f}))+ b:base[def][infix][Val] is:( "[" i:parse[def][infix][Val] "]" {`Elem i}
| -"." (%"length" {`Len} | %"string" {`Str} | f:LIDENT {`Post f})
| "(" args:!(Util.list0)[parse def infix Val] ")" {`Call args}
)+
=> {match (List.hd (List.rev is)), atr with => {match (List.hd (List.rev is)), atr with
| `Elem i, Reff -> true | `Elem i, Reff -> true
| _, Reff -> false | _, Reff -> false
| _, _ -> true} => | _, _ -> true} =>
{ {
@ -600,10 +603,11 @@ module Expr =
List.fold_left List.fold_left
(fun b -> (fun b ->
function function
| `Elem i -> Elem (b, i) | `Elem i -> Elem (b, i)
| `Len -> Length b | `Len -> Length b
| `Str -> StringVal b | `Str -> StringVal b
| `Post f -> Call (Var f, [b]) | `Post f -> Call (Var f, [b])
| `Call args -> (match b with Sexp _ -> invalid_arg "retry!" | _ -> Call (b, args))
) )
b b
is is
@ -614,6 +618,7 @@ module Expr =
| `Len, _ -> Length b | `Len, _ -> Length b
| `Str, _ -> StringVal b | `Str, _ -> StringVal b
| `Post f, _ -> Call (Var f, [b]) | `Post f, _ -> Call (Var f, [b])
| `Call args, _ -> (match b with Sexp _ -> invalid_arg "retry!" | _ -> Call (b, args))
in in
ignore atr res ignore atr res
} }
@ -632,10 +637,9 @@ module Expr =
| None -> [] | None -> []
| Some args -> args)) | Some args -> args))
} }
| x:LIDENT s:( "(" args:!(Util.list0)[parse def infix Val] ")" => {notRef atr} => {Call (Var x, args)} | x:LIDENT {if notRef atr then Var x else Ref x}
| empty {if notRef atr then Var x else Ref x}) {ignore atr s}
| {isVoid atr} => %"skip" {Skip} | {isVoid atr} => %"skip" {Skip}
| %"if" e:parse[def][infix][Val] %"then" the:scope[`Local][def][infix][atr][parse def] | %"if" e:parse[def][infix][Val] %"then" the:scope[`Local][def][infix][atr][parse def]
elif:(%"elif" parse[def][infix][Val] %"then" scope[`Local][def][infix][atr][parse def])* elif:(%"elif" parse[def][infix][Val] %"then" scope[`Local][def][infix][atr][parse def])*