@fraczak/k 4.0.1 → 4.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -4
- package/codes.mjs +1 -1
- package/demo.cast +1772 -0
- package/demo.gif +0 -0
- package/demo2.cast +1705 -0
- package/demo_optimized.gif +0 -0
- package/package.json +1 -1
- package/parser.jison +30 -8
- package/parser.mjs +33 -8
- package/patterns.mjs +10 -5
- package/repl.mjs +40 -33
- package/run.mjs +1 -1
- package/sc.py +25 -0
- package/test-fingerprint.mjs +21 -16
package/README.md
CHANGED
|
@@ -4,11 +4,51 @@
|
|
|
4
4
|
|
|
5
5
|
From javascript:
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
```js
|
|
8
|
+
import k from "@fraczak/k"
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
const fn = k.compile(` -- complete "pure" k-program
|
|
11
|
+
$ nat = < {} o, nat i >; -- unary natural number encoding
|
|
12
|
+
succ = {() i} $nat; -- increment relation definition
|
|
13
|
+
succ succ succ -- +3 (in the above encoding)
|
|
14
|
+
`);
|
|
15
|
+
const three = {i:{i:{i:{o:{}}}}};
|
|
16
|
+
const six = fn(three);
|
|
17
|
+
console.log(JSON.stringify( six ));
|
|
18
|
+
// {"i":{"i":{"i":{"i":{"i":{"i":{"o":{}}}}}}}}
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Working with REPL:
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
$ k-repl
|
|
26
|
+
Very! experimental repl shell for 'k-language'...
|
|
27
|
+
--c print aliased codes
|
|
28
|
+
--C code print 'code' definition
|
|
29
|
+
--r print relations
|
|
30
|
+
--R rel print 'rel' definition with type patterns
|
|
31
|
+
--p (--pp) pretty-print last value
|
|
32
|
+
--s (--g) reg store (get) the current value in (from) register 'reg'
|
|
33
|
+
--regs print register names
|
|
34
|
+
--l file.k load 'file.k'
|
|
35
|
+
> -- DEMO: comments start with '--', like this line \
|
|
36
|
+
-- Code (type) definitions: ` $ code-name = code-expression ;` \
|
|
37
|
+
$ unit = { } ; -- empty product code \
|
|
38
|
+
$ bits = <unit _, bits 0, bits 1>; -- union, 3 variants tagged: _, 0, 1 \
|
|
39
|
+
-- Now, we will define relations: ` name = expression ; ` \
|
|
40
|
+
empty = {{} _} ; -- alternative JSON-like syntax {_: {}} allowed \
|
|
41
|
+
_001 = {0: {0: {1: empty}}}; -- in JSON-like syntax \
|
|
42
|
+
reverse_ = < -- priority merge \
|
|
43
|
+
{.in.0 in, {.out 0} out} reverse_, \
|
|
44
|
+
{.in.1 in, {.out 1} out} reverse_, \
|
|
45
|
+
.out >; \
|
|
46
|
+
reverse = {() in, empty out} reverse_ ; \
|
|
47
|
+
_001 reverse -- run the expression (composition of 2 relations)
|
|
48
|
+
--> {"1":{"0":{"0":{"_":{}}}}}
|
|
49
|
+
> toStr = < .0 ["0",toStr] CONCAT, .1 ["1",toStr] CONCAT, "" >; toStr
|
|
50
|
+
--> "100"
|
|
51
|
+
```
|
|
12
52
|
|
|
13
53
|
---
|
|
14
54
|
|
package/codes.mjs
CHANGED
|
@@ -12,7 +12,7 @@ function isBuiltIn(code) {
|
|
|
12
12
|
return code.match(/^(int|string|bool)$/);
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
function encodeCodeToString(code, codes) {
|
|
15
|
+
function encodeCodeToString(code, codes = theRepository.codes) {
|
|
16
16
|
if (isBuiltIn(code)) return code;
|
|
17
17
|
var i = 0;
|
|
18
18
|
const Q = [[code,i]];
|