mirror of
https://github.com/ProgramSnail/Lama.git
synced 2025-12-05 22:38:44 +00:00
Expr.expr --- associativity
This commit is contained in:
parent
b4ba38a0f7
commit
4c031ce95f
11 changed files with 98 additions and 17 deletions
|
|
@ -22,9 +22,10 @@ public fun right (f) {
|
|||
fun altl (level) {
|
||||
case level of
|
||||
[assoc, ps] ->
|
||||
local assfun = case assoc of Left -> left | Right -> right | Nona -> left esac;
|
||||
case map (fun (p) {
|
||||
case p of
|
||||
[op, sema] -> op @ lift(assoc (sema))
|
||||
[op, sema] -> op @ lift(assfun (sema))
|
||||
esac
|
||||
}, ps) of
|
||||
p : ps -> foldl (infix |, p, ps)
|
||||
|
|
@ -39,14 +40,24 @@ public fun expr (ops, opnd) {
|
|||
| level : tl ->
|
||||
local lops = altl (level),
|
||||
next = inner (tl);
|
||||
|
||||
fun this (c) {
|
||||
next (id) |> fun (l) {lops |> fun (op) {this (op (c, l))}}
|
||||
| next (id) @ c
|
||||
-- $ k
|
||||
}
|
||||
|
||||
this
|
||||
case level.fst of
|
||||
Nona ->
|
||||
fun this (c) {
|
||||
next (id) |> fun (l) {lops |> fun (op) {next (id) @ fun (r) {c (op)(id, l)(r)}}}
|
||||
| next (id) @ c
|
||||
}
|
||||
|
||||
this
|
||||
|
||||
| _ ->
|
||||
fun this (c) {
|
||||
next (id) |> fun (l) {lops |> fun (op) {this (op (c, l))}}
|
||||
| next (id) @ c
|
||||
}
|
||||
|
||||
this
|
||||
esac
|
||||
esac
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,27 +4,27 @@ import Ref;
|
|||
import Fun;
|
||||
import Matcher;
|
||||
|
||||
local tab, hct, log = false;
|
||||
local tab, hct, restab, log = false;
|
||||
|
||||
public fun logOn () {
|
||||
log := true
|
||||
}
|
||||
|
||||
public fun initOstap () {
|
||||
tab := ref (emptyHashTab ());
|
||||
hct := emptyMemo ()
|
||||
tab := ref (emptyHashTab ());
|
||||
restab := emptyMemo ();
|
||||
hct := emptyMemo ()
|
||||
}
|
||||
|
||||
public fun memo (f) {
|
||||
local t;
|
||||
|
||||
f := lookupMemo (hct, f);
|
||||
|
||||
if log then printf ("Memoizing: %x=%s\n", f, f.string) fi;
|
||||
|
||||
case findHashTab (deref (tab), f) of
|
||||
None -> if log then printf ("new table...\n") fi; t := ref (emptyMap ()); tab ::= addHashTab (deref (tab), f, t)
|
||||
| Some (tt) -> t := tt
|
||||
None -> if log then printf ("new table...\n") fi;
|
||||
tab ::= addHashTab (deref (tab), f, ref (emptyMap ()))
|
||||
| Some (tt) -> skip
|
||||
esac;
|
||||
|
||||
fun (k) {
|
||||
|
|
@ -35,6 +35,7 @@ public fun memo (f) {
|
|||
None ->
|
||||
t ::= addMap (deref (t), s, [addSet (emptySet (), k), emptySet ()]);
|
||||
f (fun (r) {
|
||||
r := lookupMemo (restab, r);
|
||||
if log then printf ("Running continuation with result %s\n", r.string) fi;
|
||||
case findMap (deref (t), s) of
|
||||
Some ([ks, rs]) ->
|
||||
|
|
|
|||
3
stdlib/regression/orig/test13.log
Normal file
3
stdlib/regression/orig/test13.log
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Succ (Add ("a", Sub ("a", "a")))
|
||||
Succ (Mul (Div (Mul ("a", "a"), "a"), "a"))
|
||||
Succ (Add (Mul ("a", "a"), Sub (Div ("a", "a"), Mul ("a", "a"))))
|
||||
3
stdlib/regression/orig/test14.log
Normal file
3
stdlib/regression/orig/test14.log
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Succ (Add ("a", Sub ("a", "a")))
|
||||
Succ (Mul (Div (Mul ("a", "a"), "a"), "a"))
|
||||
Succ (Add (Mul ("a", "a"), Sub (Div ("a", "a"), Mul ("a", "a"))))
|
||||
4
stdlib/regression/orig/test15.log
Normal file
4
stdlib/regression/orig/test15.log
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
Succ (Eq ("a", "a"))
|
||||
Succ (Eq (Mul ("a", "a"), Mul ("a", "a")))
|
||||
Succ (Eq (Add (Mul ("a", "a"), Sub (Div ("a", "a"), Mul ("a", "a"))), Sub (Mul ("a", "a"), "a")))
|
||||
Fail ({""*" expected at"}, 1, 2)
|
||||
12
stdlib/regression/test09.expr
Normal file
12
stdlib/regression/test09.expr
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import Ostap;
|
||||
import Matcher;
|
||||
|
||||
local a = token ("a"), acc;
|
||||
|
||||
initOstap ();
|
||||
|
||||
printf ("Parsing a*| against ""aa""... %s\n", parseString (rep0 (a) |> fun (x) {eof @ lift (x)}, "aa").string);
|
||||
printf ("Parsing a+| against ""aa""... %s\n", parseString (rep (a) |> fun (x) {eof @ lift (x)}, "aa").string);
|
||||
printf ("Parsing list(a)| against ""a""... %s\n", parseString (list (a) |> bypass (eof), "a").string);
|
||||
printf ("Parsing list(a)| against ""a,a""... %s\n", parseString (list (a) |> bypass (eof), "a,a").string);
|
||||
printf ("Parsing list0(a)| against """"... %s\n", parseString (list0 (a) |> bypass (eof), "").string)
|
||||
|
|
@ -7,7 +7,7 @@ local a = token ("a"),
|
|||
sub = [token ("-"), fun (l, r) {Sub (l, r)}],
|
||||
mul = [token ("*"), fun (l, r) {Mul (l, r)}],
|
||||
div = [token ("/"), fun (l, r) {Div (l, r)}],
|
||||
exp = expr ({[left, {add, sub}], [left, {mul, div}]}, a) (id);
|
||||
exp = expr ({[Left, {add, sub}], [Left, {mul, div}]}, a) (id);
|
||||
|
||||
printf ("%s\n", parseString (exp |> bypass (eof), "a").string);
|
||||
printf ("%s\n", parseString (exp |> bypass (eof), "a+a").string);
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ local a = token ("a"),
|
|||
sub = [token ("-"), fun (l, r) {Sub (l, r)}],
|
||||
mul = [token ("*"), fun (l, r) {Mul (l, r)}],
|
||||
div = [token ("/"), fun (l, r) {Div (l, r)}],
|
||||
exp = expr ({[left, {add, sub}], [left, {mul, div}]}, a) (id),
|
||||
exp = expr ({[Left, {add, sub}], [Left, {mul, div}]}, a) (id),
|
||||
i;
|
||||
|
||||
printf ("%s\n", parseString (exp |> bypass (eof), gen (10)).string)
|
||||
|
|
|
|||
14
stdlib/regression/test13.expr
Normal file
14
stdlib/regression/test13.expr
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import Ostap;
|
||||
import Expr;
|
||||
import Fun;
|
||||
|
||||
local a = token ("a"),
|
||||
add = [token ("+"), fun (l, r) {Add (l, r)}],
|
||||
sub = [token ("-"), fun (l, r) {Sub (l, r)}],
|
||||
mul = [token ("*"), fun (l, r) {Mul (l, r)}],
|
||||
div = [token ("/"), fun (l, r) {Div (l, r)}],
|
||||
exp = expr ({[Right, {add, sub}], [Left, {mul, div}]}, a) (id);
|
||||
|
||||
printf ("%s\n", parseString (exp |> bypass (eof), "a+a-a").string);
|
||||
printf ("%s\n", parseString (exp |> bypass (eof), "a*a/a*a").string);
|
||||
printf ("%s\n", parseString (exp |> bypass (eof), "a*a+a/a-a*a").string)
|
||||
16
stdlib/regression/test14.expr
Normal file
16
stdlib/regression/test14.expr
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import Ostap;
|
||||
import Expr;
|
||||
import Fun;
|
||||
import List;
|
||||
|
||||
local a = token ("a"),
|
||||
eq = [token ("="), fun (l, r) {Eq (l, r)}],
|
||||
add = [token ("+"), fun (l, r) {Add (l, r)}],
|
||||
sub = [token ("-"), fun (l, r) {Sub (l, r)}],
|
||||
mul = [token ("*"), fun (l, r) {Mul (l, r)}],
|
||||
div = [token ("/"), fun (l, r) {Div (l, r)}],
|
||||
exp = expr ({[Nona, singleton (eq)], [Right, {add, sub}], [Left, {mul, div}]}, a) (id);
|
||||
|
||||
printf ("%s\n", parseString (exp |> bypass (eof), "a+a-a").string);
|
||||
printf ("%s\n", parseString (exp |> bypass (eof), "a*a/a*a").string);
|
||||
printf ("%s\n", parseString (exp |> bypass (eof), "a*a+a/a-a*a").string)
|
||||
17
stdlib/regression/test15.expr
Normal file
17
stdlib/regression/test15.expr
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import Ostap;
|
||||
import Expr;
|
||||
import Fun;
|
||||
import List;
|
||||
|
||||
local a = token ("a"),
|
||||
eq = [token ("="), fun (l, r) {Eq (l, r)}],
|
||||
add = [token ("+"), fun (l, r) {Add (l, r)}],
|
||||
sub = [token ("-"), fun (l, r) {Sub (l, r)}],
|
||||
mul = [token ("*"), fun (l, r) {Mul (l, r)}],
|
||||
div = [token ("/"), fun (l, r) {Div (l, r)}],
|
||||
exp = expr ({[Nona, singleton (eq)], [Right, {add, sub}], [Left, {mul, div}]}, a) (id);
|
||||
|
||||
printf ("%s\n", parseString (exp |> bypass (eof), "a=a").string);
|
||||
printf ("%s\n", parseString (exp |> bypass (eof), "a*a=a*a").string);
|
||||
printf ("%s\n", parseString (exp |> bypass (eof), "a*a+a/a-a*a=a*a-a").string);
|
||||
printf ("%s\n", parseString (exp |> bypass (eof), "a=a=a+a").string)
|
||||
Loading…
Add table
Add a link
Reference in a new issue