-
Notifications
You must be signed in to change notification settings - Fork 0
/
formulas.js
173 lines (132 loc) · 5.86 KB
/
formulas.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { TokenTypes, lex } from './lexer.js';
import { compileLambda, timedLambdaReduce } from './eval-lambda.js';
import { Application } from './lambda.js';
const formulaNames = [];
const formulaExprs = [];
const formulaReductions = [];
let nHeadless = 0;
export function *formulas() {
for (let i = formulaNames.length - 1; i >= 0; i--) yield {
name: formulaNames[i],
expr: formulaExprs[i],
reductions: formulaReductions[i],
index: i
};
}
function appendSpace(div) {
const whitespaceSpan = document.createElement('span');
whitespaceSpan.textContent = ' ';
div.appendChild(whitespaceSpan);
}
function addFormulaRow(name, expr, index, headless=false) {
const formulaListElement = document.getElementById('formula-list');
const formulaRow = document.createElement('tr');
formulaRow.className = 'formula-row';
formulaRow.dataset.index = index;
const exprCell = document.createElement('td');
const nameCell = document.createElement('td');
if (expr instanceof Application)
exprCell.innerHTML = `(${Lambda}*.* (<span style="color:aquamarine">${expr}</span>))`;
else
exprCell.innerHTML = `(${Lambda}*.* <span style="color:aquamarine">${expr}</span>)`;
nameCell.innerHTML = `${Lambda}<b style="color:aquamarine">${name}</b>.`;
formulaRow.appendChild(exprCell);
formulaRow.appendChild(nameCell);
formulaListElement.appendChild(formulaRow);
if (headless) return;
const buttonCell = document.createElement('td');
const upButton = document.createElement('button');
upButton.className = 'button';
upButton.style.color = 'palegreen';
upButton.textContent = '^';
upButton.onclick = () => moveFormulaUp(parseInt(formulaRow.dataset.index));
buttonCell.appendChild(upButton);
appendSpace(buttonCell);
const downButton = document.createElement('button');
downButton.className = 'button';
downButton.style.color = 'palegreen';
downButton.textContent = 'v';
downButton.onclick = () => moveFormulaDown(parseInt(formulaRow.dataset.index));
buttonCell.appendChild(downButton);
appendSpace(buttonCell);
const deleteButton = document.createElement('button');
deleteButton.className = 'button';
deleteButton.style.color = 'red';
deleteButton.textContent = 'x';
deleteButton.onclick = () => deleteFormula(parseInt(formulaRow.dataset.index));
buttonCell.appendChild(deleteButton);
appendSpace(buttonCell);
formulaRow.appendChild(buttonCell);
}
function error(alertOnError, msg) {
if (alertOnError) alert(msg);
else throw new Error(msg);
}
window.addFormula = function(nameInput, exprInput, successCallback=undefined, alertOnError=true, headless=false) {
const tokens = lex(nameInput);
if (
(tokens.length !== 1) ||
(tokens[0].type !== TokenTypes.ID)
) return error(alertOnError, `Error: Enter a name with none of these special characters: "${Lambda}", "${TokenTypes.Dot.description}", "${TokenTypes.OpenParen.description}", "${TokenTypes.CloseParen.description}" or whitespace`);
const name = tokens[0].value;
if (formulaNames.includes(name))
return error(alertOnError, `Error: Formula '${name}' already exists`);
const result = compileLambda(exprInput);
if (result.failed) return error(alertOnError, result.error);
const expr = result.expr;
const index = formulaNames.length;
timedLambdaReduce(100, expr, reductions => {
formulaNames.push(name);
formulaExprs.push(expr);
formulaReductions.push(reductions);
addFormulaRow(name, expr, index, headless);
if (headless) nHeadless++;
if (successCallback) successCallback();
}, false);
}
function moveFormulaUp(index) {
if (index == /* 0 */ nHeadless) return;
const [formulaName] = formulaNames.splice(index, 1);
formulaNames.splice(index - 1, 0, formulaName);
const [formulaExpr] = formulaExprs.splice(index, 1);
formulaExprs.splice(index - 1, 0, formulaExpr);
formulaReductions.splice(index, 1);
timedLambdaReduce(100, formulaExpr, reductions => {
formulaReductions.splice(index - 1, 0, reductions);
const formulaRow = document.getElementById('formula-list').children[index];
const previous = formulaRow.previousElementSibling;
formulaRow.remove();
previous.insertAdjacentElement('beforebegin', formulaRow);
formulaRow.dataset.index = index - 1;
previous.dataset.index = index;
}, false, index - 1);
}
function moveFormulaDown(index) {
if (index == formulaNames.length - 1) return;
const [[formulaName], [formulaExpr]] = [formulaNames.splice(index, 1), formulaExprs.splice(index, 1)];
formulaNames.splice(index + 1, 0, formulaName); formulaExprs.splice(index + 1, 0, formulaExpr);
formulaReductions.splice(index, 1);
timedLambdaReduce(100, formulaExpr, reductions => {
formulaReductions.splice(index + 1, 0, reductions);
const formulaRow = document.getElementById('formula-list').children[index];
const next = formulaRow.nextElementSibling;
formulaRow.remove();
next.insertAdjacentElement('afterend', formulaRow);
formulaRow.dataset.index = index + 1;
next.dataset.index = index;
}, false, index + 1);
}
function deleteFormula(index) {
if (!confirm(`Are you sure you want to delete formula '${formulaNames[index]}'?`)) return;
const formulaList = document.getElementById('formula-list');
for (let i = index; i < formulaList.childElementCount; i++) {
const child = formulaList.children[i];
const childIndex = parseInt(child.dataset.index);
child.dataset.index = childIndex - 1;
}
const formulaRow = formulaList.children[index];
formulaRow.remove();
formulaNames.splice(index, 1);
formulaExprs.splice(index, 1);
formulaReductions.splice(index, 1);
}