-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
122 lines (94 loc) · 1.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"use strict";
const encoding = require("./encoding");
const compile = require("./compile");
const inet = require("inet-lib");
const parser = new compile.Parser();
const defalgo = "abstract";
let expanded;
function obj2mlc(obj)
{
const node = obj.node;
if ("atom" == node)
return obj.name;
if ("abst" == node) {
const body = obj.body;
let sep;
if ("abst" == body.node)
sep = ", ";
else
sep = ": ";
return obj.bound + sep + obj2mlc(body);
}
if ("appl" == node) {
const left = obj.left;
const right = obj.right;
const rnode = right.node;
let lmlc = obj2mlc(left);
let rmlc = obj2mlc(right);
if ("abst" == left.node)
lmlc = "(" + lmlc + ")";
if (("abst" == rnode) || ("appl" == rnode))
rmlc = "(" + rmlc + ")";
return lmlc + " " + rmlc;
}
return "[ ]";
}
function mlc2in(mlc, algo, cb)
{
const encode = encoding.get(algo ? algo : defalgo);
let insrc;
if (!encode)
throw `${algo}: Unknown algorithm`;
mlc = parser.parse(mlc);
insrc = encode(mlc);
expanded = mlc.expanded;
inet.inenv = {
cb: cb
};
return insrc;
}
function format(data)
{
if (Array.isArray(data))
return data.toString();
else if ("object" == typeof data)
return obj2mlc(data);
else if ("number" == typeof data)
return data.toString();
else
return data;
}
function prepare(mlc, algo)
{
const src = mlc2in(mlc, algo);
inet.prepare(src, format);
}
function debug()
{
return inet.debug();
}
function debug0()
{
return inet.debug0();
}
function debug1()
{
return inet.debug1();
}
function run(mlc, algo, max, cb)
{
const src = mlc2in(mlc, algo, cb);
const output = inet(src, max);
output.term = obj2mlc(expanded);
if (output.nf)
output.nf = obj2mlc(output.nf);
return output;
}
run.defalgo = defalgo;
run.algos = Array.from(encoding.keys());
run.prepare = prepare;
run.debug = debug;
run.debug0 = debug0;
run.debug1 = debug1;
run.mlc2in = mlc2in;
module.exports = run;