-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
128 lines (96 loc) · 1.74 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
123
124
125
126
127
128
"use strict";
const setup = require("./setup");
const format = require("./format");
const reset = format.reset;
const geteqn = format.geteqn;
const getconf = format.getconf;
let inenv, inqueue, table;
function reduce(pair)
{
const left = pair.left;
const right = pair.right;
const rule = pair.rule;
if (!rule(left, right))
throw "NO RULES: " + geteqn(pair);
++rule.count;
pair.left = void(0);
pair.right = void(0);
}
function prepare(src, fmt)
{
let system;
inenv = run.inenv;
system = setup(src, inenv);
table = system.rules;
inqueue = system.queue;
reset(fmt, system.types);
return inenv;
}
function debug()
{
const conf = getconf(inqueue);
const pair = inqueue.pop();
if (pair)
reduce(pair);
return conf;
}
function debug0()
{
const pair = inqueue.pop();
if (pair) {
reduce(pair);
return true;
}
}
function debug1()
{
const pair = inqueue.pop();
if (pair) {
const eqn = geteqn(pair);
reduce(pair);
return eqn;
}
}
function getstats()
{
const stats = {};
table.forEach(row => {
row.forEach(rule => {
const count = rule.count;
let human = rule.human;
if (!human || !count)
return;
human = human.split("><");
human = human.sort();
human = human.join("><");
if (stats[human])
stats[human] += count;
else
stats[human] = count;
});
});
return stats;
}
function perform(max)
{
const t0 = Date.now();
for (let i = 0; i < max; i++) {
if (!inqueue.length)
break;
reduce(inqueue.pop());
}
return Date.now() - t0;
}
function run(src, max)
{
prepare(src);
inenv.redtime = perform(max ? max : 1e7);
inenv.stats = getstats();
return inenv;
}
run.inenv = {};
run.prepare = prepare;
run.debug = debug;
run.debug0 = debug0;
run.debug1 = debug1;
module.exports = run;