mirror of
https://github.com/ProgramSnail/Lama.git
synced 2025-12-06 06:48:48 +00:00
Continue massaging Ostap
This commit is contained in:
parent
848fd31dc4
commit
b4ba38a0f7
8 changed files with 84 additions and 31 deletions
|
|
@ -1162,7 +1162,7 @@ extern void* Lfread (char *fname) {
|
|||
if (f) {
|
||||
if (fseek (f, 0l, SEEK_END) >= 0) {
|
||||
long size = ftell (f);
|
||||
void *s = LmakeString (size);
|
||||
void *s = LmakeString (BOX(size));
|
||||
|
||||
rewind (f);
|
||||
|
||||
|
|
@ -1287,8 +1287,7 @@ extern void __gc_root_scan_stack ();
|
|||
/* Mark-and-copy */
|
||||
/* ======================================== */
|
||||
|
||||
static size_t SPACE_SIZE = 16;
|
||||
// static size_t SPACE_SIZE = 32 * 1024 * 100;
|
||||
static size_t SPACE_SIZE = 16 * 1024;
|
||||
// static size_t SPACE_SIZE = 128;
|
||||
// static size_t SPACE_SIZE = 1024 * 1024;
|
||||
|
||||
|
|
|
|||
|
|
@ -2,25 +2,48 @@ import Ostap;
|
|||
import List;
|
||||
import Fun;
|
||||
|
||||
public fun left (f) {
|
||||
fun (c, x) {
|
||||
fun (y) {
|
||||
f (c (x), y)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public fun right (f) {
|
||||
fun (c, x) {
|
||||
fun (y) {
|
||||
c (f (x, y))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
--- ops -> fun (x, y) {x `op` y}
|
||||
fun altl (ps) {
|
||||
case ps of
|
||||
p : ps -> foldl (infix |, p, ps)
|
||||
fun altl (level) {
|
||||
case level of
|
||||
[assoc, ps] ->
|
||||
case map (fun (p) {
|
||||
case p of
|
||||
[op, sema] -> op @ lift(assoc (sema))
|
||||
esac
|
||||
}, ps) of
|
||||
p : ps -> foldl (infix |, p, ps)
|
||||
esac
|
||||
esac
|
||||
}
|
||||
|
||||
public fun expr (ops, opnd) {
|
||||
fun inner (ops) {
|
||||
case ops of
|
||||
{} -> opnd
|
||||
{} -> fun (c) {opnd @ c}
|
||||
| level : tl ->
|
||||
local lops = altl (level),
|
||||
next = inner (tl);
|
||||
|
||||
fun this (k) {
|
||||
next |> fun (l) {lops |> fun (op) {this @ fun (r) {op (l, r)}}}
|
||||
| next
|
||||
$ k
|
||||
fun this (c) {
|
||||
next (id) |> fun (l) {lops |> fun (op) {this (op (c, l))}}
|
||||
| next (id) @ c
|
||||
-- $ k
|
||||
}
|
||||
|
||||
this
|
||||
|
|
|
|||
|
|
@ -4,7 +4,11 @@ import Ref;
|
|||
import Fun;
|
||||
import Matcher;
|
||||
|
||||
local tab, hct;
|
||||
local tab, hct, log = false;
|
||||
|
||||
public fun logOn () {
|
||||
log := true
|
||||
}
|
||||
|
||||
public fun initOstap () {
|
||||
tab := ref (emptyHashTab ());
|
||||
|
|
@ -16,21 +20,22 @@ public fun memo (f) {
|
|||
|
||||
f := lookupMemo (hct, f);
|
||||
|
||||
--printf ("Memoizing: %x=%s\n", f, f.string);
|
||||
if log then printf ("Memoizing: %x=%s\n", f, f.string) fi;
|
||||
|
||||
case findHashTab (deref (tab), f) of
|
||||
None -> t := ref (emptyMap ()); tab ::= addHashTab (deref (tab), f, t)
|
||||
None -> if log then printf ("new table...\n") fi; t := ref (emptyMap ()); tab ::= addHashTab (deref (tab), f, t)
|
||||
| Some (tt) -> t := tt
|
||||
esac;
|
||||
|
||||
fun (k) {
|
||||
fun (s) {
|
||||
-- printf ("s=%s\n", s.string);
|
||||
local t = case findHashTab (deref (tab), f) of Some (t) -> t esac;
|
||||
if log then printf ("Applying memoized parser to %s\n", s.string) fi;
|
||||
case findMap (deref (t), s) of
|
||||
None ->
|
||||
t ::= addMap (deref (t), s, [addSet (emptySet (), k), emptySet ()]);
|
||||
f (fun (r) {
|
||||
-- printf ("Result: %s\n", r.string);
|
||||
if log then printf ("Running continuation with result %s\n", r.string) fi;
|
||||
case findMap (deref (t), s) of
|
||||
Some ([ks, rs]) ->
|
||||
if memSet (rs, r)
|
||||
|
|
@ -72,6 +77,7 @@ public fun empty (k) {
|
|||
public fun alt (a, b) {
|
||||
fun (k) {
|
||||
fun (s) {
|
||||
if log then printf ("Running alt at %s\n", s.string) fi;
|
||||
a (k) (s);
|
||||
b (k) (s)
|
||||
}
|
||||
|
|
@ -81,11 +87,12 @@ public fun alt (a, b) {
|
|||
public fun seq (a, b) {
|
||||
fun (k) {
|
||||
fun (s) {
|
||||
if log then printf ("Running seq at %s\n", s.string) fi;
|
||||
a (fun (ar) {
|
||||
case ar of
|
||||
Succ (x, s) -> b (x) (k) (s)
|
||||
| _ -> k (ar)
|
||||
esac
|
||||
case ar of
|
||||
Succ (x, s) -> b (x) (k) (s)
|
||||
| _ -> k (ar)
|
||||
esac
|
||||
}) (s)
|
||||
}
|
||||
}
|
||||
|
|
@ -116,11 +123,13 @@ public fun bypass (f) {
|
|||
public fun opt (a) {empty @ lift (None) | a @ fun (x) {Some (x)}}
|
||||
|
||||
public fun rep0 (a) {
|
||||
memo (empty @ lift({}) | a |> fun (x) {rep0 (a) @ fun (as) {x : as}})
|
||||
--memo
|
||||
(empty @ lift({}) | a |> fun (x) {rep0 (a) @ fun (as) {x : as}})
|
||||
}
|
||||
|
||||
public fun rep (a) {
|
||||
memo (a |> (fun (x) {rep0 (a) @ fun (as) {x : as}}))
|
||||
--memo
|
||||
(a |> (fun (x) {rep0 (a) @ fun (as) {x : as}}))
|
||||
}
|
||||
|
||||
public fun listBy (item, sep) {
|
||||
|
|
@ -185,7 +194,7 @@ fun createResult () {
|
|||
fun get () {
|
||||
if deref (hasValue) then Succ (deref (value))
|
||||
elif deref (hasError) then Fail (deref (errors), deref (line), deref (col))
|
||||
else failure ("Ostap::createAcceptor::get: nothing to return")
|
||||
else failure ("Ostap::createAcceptor::get: nothing to return\n")
|
||||
fi
|
||||
}
|
||||
|
||||
|
|
@ -216,4 +225,4 @@ public fun parseString (p, s) {
|
|||
acc.result
|
||||
}
|
||||
|
||||
initOstap ()
|
||||
initOstap ()
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
Succ ("a")
|
||||
Succ (Add ("a", "a"))
|
||||
Succ (Sub ("a", "a"))
|
||||
Succ (Add ("a", Sub ("a", "a")))
|
||||
Succ (Sub (Add ("a", "a"), "a"))
|
||||
Succ (Add ("a", Mul ("a", "a")))
|
||||
Succ (Sub (Mul ("a", "a"), Div ("a", "a")))
|
||||
|
|
|
|||
1
stdlib/regression/orig/test12.log
Normal file
1
stdlib/regression/orig/test12.log
Normal file
File diff suppressed because one or more lines are too long
|
|
@ -14,6 +14,6 @@ fun many (a) {
|
|||
local a = token ("a"), b = token ("b");
|
||||
local bad_alter = a | a |> fun (x) {b @ fun (y) {x ++ y}};
|
||||
|
||||
initOstap ();
|
||||
--logOn ();
|
||||
printf ("Parsing ""aaa"" with many ... %s\n", parseString (many (a) |> bypass (eof), "aaa").string);
|
||||
printf ("Parsing ""ab"" with bad_alter ... %s\n", parseString (bad_alter |> bypass (eof), "ab").string)
|
||||
|
|
@ -1,12 +1,13 @@
|
|||
import Ostap;
|
||||
import Expr;
|
||||
import Fun;
|
||||
|
||||
local a = token ("a"),
|
||||
add = token ("+") @ lift (fun (l, r) {Add (l, r)}),
|
||||
sub = token ("-") @ lift (fun (l, r) {Sub (l, r)}),
|
||||
mul = token ("*") @ lift (fun (l, r) {Mul (l, r)}),
|
||||
div = token ("/") @ lift (fun (l, r) {Div (l, r)}),
|
||||
exp = expr ({{add, sub}, {mul, div}}, 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 ({[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);
|
||||
|
|
|
|||
20
stdlib/regression/test12.expr
Normal file
20
stdlib/regression/test12.expr
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import Ostap;
|
||||
import Expr;
|
||||
import Fun;
|
||||
|
||||
fun gen (depth) {
|
||||
if depth == 0
|
||||
then "a"
|
||||
else gen (depth-1) ++ "*" ++ gen (depth-1)
|
||||
fi
|
||||
}
|
||||
|
||||
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 ({[left, {add, sub}], [left, {mul, div}]}, a) (id),
|
||||
i;
|
||||
|
||||
printf ("%s\n", parseString (exp |> bypass (eof), gen (10)).string)
|
||||
Loading…
Add table
Add a link
Reference in a new issue