mirror of
https://github.com/ProgramSnail/Lama.git
synced 2025-12-15 19:28:47 +00:00
Cosmetics in stdlib
This commit is contained in:
parent
7fd85f27ef
commit
5933f4c3b1
15 changed files with 105 additions and 79 deletions
|
|
@ -1,3 +1,9 @@
|
|||
-- Ostap.
|
||||
-- (C) Dmitry Boulytchev, JetBrains Research, St. Petersburg State University, 2020
|
||||
--
|
||||
-- This unit provides an implementation of monadic parser combinators in CPS with
|
||||
-- memoization.
|
||||
|
||||
import List;
|
||||
import Collection;
|
||||
import Ref;
|
||||
|
|
@ -226,4 +232,66 @@ public fun parseString (p, s) {
|
|||
acc.result
|
||||
}
|
||||
|
||||
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 (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(assfun (sema))
|
||||
esac
|
||||
}, ps) of
|
||||
p : ps -> foldl (infix |, p, ps)
|
||||
esac
|
||||
esac
|
||||
}
|
||||
|
||||
public fun expr (ops, opnd) {
|
||||
fun inner (ops) {
|
||||
case ops of
|
||||
{} -> fun (c) {opnd @ c}
|
||||
| level : tl ->
|
||||
local lops = altl (level),
|
||||
next = inner (tl);
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
inner (ops)
|
||||
}
|
||||
|
||||
initOstap ()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue