mirror of
https://github.com/ProgramSnail/Lama.git
synced 2025-12-10 00:38: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 +1 @@
|
||||||
let version = "Version 1.00, b7271d16, Tue Feb 18 14:08:39 2020 +0300"
|
let version = "Version 1.00, 7fd85f27, Wed Feb 19 21:21:18 2020 +0300"
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
-- Array package.
|
-- Arrays.
|
||||||
-- (C) Dmitry Boulytchev, JetBrains Research, St. Petersburg State University, 2020
|
-- (C) Dmitry Boulytchev, JetBrains Research, St. Petersburg State University, 2020
|
||||||
--
|
--
|
||||||
-- This package provides a simplistic implementation of immutable set/map/hashtable
|
-- This unit provides a set of array-manipulation primitives. Note, some of these
|
||||||
-- data structures.
|
-- primitives can be applied to any boxed value in Lama, not necessarily arrays
|
||||||
|
-- per se.
|
||||||
|
|
||||||
import List;
|
import List;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
-- Collection package.
|
-- Collections.
|
||||||
-- (C) Dmitry Boulytchev, JetBrains Research, St. Petersburg State University, 2020
|
-- (C) Dmitry Boulytchev, JetBrains Research, St. Petersburg State University, 2020
|
||||||
--
|
--
|
||||||
-- This package provides a simplistic implementation of immutable set/map/hashtable
|
-- This unit provides a simplistic implementation of immutable set/map/hashtable
|
||||||
-- data structures.
|
-- data structures.
|
||||||
|
|
||||||
import List;
|
import List;
|
||||||
|
|
|
||||||
|
|
@ -1,66 +0,0 @@
|
||||||
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 (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)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
@ -1,3 +1,8 @@
|
||||||
|
-- Fun.
|
||||||
|
-- (C) Dmitry Boulytchev, JetBrains Research, St. Petersburg State University, 2020
|
||||||
|
--
|
||||||
|
-- This unit provides some convenient function-manipulation stuff.
|
||||||
|
|
||||||
import Ref;
|
import Ref;
|
||||||
|
|
||||||
public fun id (x) {
|
public fun id (x) {
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,21 @@
|
||||||
|
-- Lazy.
|
||||||
|
-- (C) Dmitry Boulytchev, JetBrains Research, St. Petersburg State University, 2020
|
||||||
|
--
|
||||||
|
-- Deferred computations.
|
||||||
|
|
||||||
public fun makeLazy (f) {
|
public fun makeLazy (f) {
|
||||||
local value, set = false;
|
local value, set = false;
|
||||||
|
|
||||||
fun () {
|
fun () {
|
||||||
if set
|
if set
|
||||||
then value
|
then value
|
||||||
else set := true; value := f (); value
|
else
|
||||||
|
local c; -- need this temporary since in value := f () value would
|
||||||
|
-- create an intermediate managed pointer
|
||||||
|
set := true;
|
||||||
|
c := f ();
|
||||||
|
value := c;
|
||||||
|
c
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,8 @@
|
||||||
|
-- Lists.
|
||||||
|
-- (C) Dmitry Boulytchev, JetBrains Research, St. Petersburg State University, 2020
|
||||||
|
--
|
||||||
|
-- This unit provides a set of list-manipulation primitives.
|
||||||
|
|
||||||
public fun singleton (x) {
|
public fun singleton (x) {
|
||||||
x : {}
|
x : {}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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 List;
|
||||||
import Collection;
|
import Collection;
|
||||||
import Ref;
|
import Ref;
|
||||||
|
|
@ -226,4 +232,66 @@ public fun parseString (p, s) {
|
||||||
acc.result
|
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 ()
|
initOstap ()
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,19 @@
|
||||||
|
-- Emulation of first-class references.
|
||||||
|
-- (C) Dmitry Boulytchev, JetBrains Research, St. Petersburg State University, 2020
|
||||||
|
--
|
||||||
|
-- This unit provides an implementation for first-class references emulation.
|
||||||
|
|
||||||
|
-- Creates a new reference cell with contents x
|
||||||
public fun ref (x) {
|
public fun ref (x) {
|
||||||
[x]
|
[x]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
-- Returns the contents of the cell x
|
||||||
public fun deref (x) {
|
public fun deref (x) {
|
||||||
x[0]
|
x[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
-- Assigns a new value y into the cell x
|
||||||
public infix ::= before := (x, y) {
|
public infix ::= before := (x, y) {
|
||||||
x[0] := y
|
x[0] := y
|
||||||
}
|
}
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
import Ostap;
|
import Ostap;
|
||||||
import Expr;
|
|
||||||
import Fun;
|
import Fun;
|
||||||
|
|
||||||
local a = token ("a"),
|
local a = token ("a"),
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
import Ostap;
|
import Ostap;
|
||||||
import Expr;
|
|
||||||
import Fun;
|
import Fun;
|
||||||
|
|
||||||
fun gen (depth) {
|
fun gen (depth) {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
import Ostap;
|
import Ostap;
|
||||||
import Expr;
|
|
||||||
import Fun;
|
import Fun;
|
||||||
|
|
||||||
local a = token ("a"),
|
local a = token ("a"),
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
import Ostap;
|
import Ostap;
|
||||||
import Expr;
|
|
||||||
import Fun;
|
import Fun;
|
||||||
import List;
|
import List;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
import Ostap;
|
import Ostap;
|
||||||
import Expr;
|
|
||||||
import Fun;
|
import Fun;
|
||||||
import List;
|
import List;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
import Ostap;
|
import Ostap;
|
||||||
import Expr;
|
|
||||||
import Fun;
|
import Fun;
|
||||||
import List;
|
import List;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue