-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression.js
282 lines (230 loc) · 8.08 KB
/
expression.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
Fōrmulæ symbolic package. Module for expression definition & visualization.
Copyright (C) 2015-2023 Laurence R. Ugalde
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
'use strict';
export class Symbolic extends Formulae.Package {}
Symbolic.Symbol = class extends Expression.Literal {
constructor() {
super();
this.color = "blue";
}
getTag() { return "Symbolic.Symbol"; }
getName() { return Symbolic.messages.nameSymbol; }
getLiteral() { return this.literal; }
set(name, value) {
if (name == "Name") {
this.literal = value;
}
else {
super.set(name, value);
}
}
get(name) {
if (name == "Name") {
return this.literal;
}
super.get(name);
}
getSerializationNames() {
return [ "Name" ];
}
async getSerializationStrings() {
return [ this.literal ];
}
setSerializationStrings(strings, promises) {
if (strings[0].length == 0) {
throw "Invalid name of symbol";
}
this.set("Name", strings[0]);
}
isReduced() {
return false;
}
prepareDisplay(context) {
let bkp = context.fontInfo.italic;
context.fontInfo.setItalic(context, true);
this.prepareDisplayAsLiteral(context);
context.fontInfo.setItalic(context, bkp);
}
display(context, x, y) {
let bkp = context.fontInfo.italic;
context.fontInfo.setItalic(context, true);
this.displayAsLiteral(context, x, y);
context.fontInfo.setItalic(context, bkp);
}
}
Symbolic.FunctionExpression = class extends Expression.BinaryExpression {
getTag() { return "Symbolic.Function"; }
getName() { return Symbolic.messages.nameFunction; }
getChildName(index) { return Symbolic.messages.childrenFunction[index]; }
prepareDisplay(context) {
let functionExpression = this.children[0];
let argumentsExpression = this.children[1];
functionExpression.prepareDisplay(context);
if (argumentsExpression.getTag() === "List.List") {
argumentsExpression.prepareDisplayAsList(context, 4, 4);
}
else {
argumentsExpression.prepareDisplay(context);
}
functionExpression.x = 0;
argumentsExpression.x = functionExpression.width + 3;
if (functionExpression.parenthesesAsOperator()) {
functionExpression.x += 4;
argumentsExpression.x += 4 + 4;
}
this.width = argumentsExpression.x + argumentsExpression.width;
this.vertBaseline = Math.round(this.width / 2);
this.horzBaseline = Math.max(functionExpression.horzBaseline, argumentsExpression.horzBaseline);
functionExpression.y = this.horzBaseline - functionExpression.horzBaseline;
argumentsExpression.y = this.horzBaseline - argumentsExpression.horzBaseline;
this.height = this.horzBaseline + Math.max(
functionExpression.height - functionExpression.horzBaseline,
argumentsExpression.height - argumentsExpression.horzBaseline
);
}
display(context, x, y) {
let functionExpression = this.children[0];
let argumentsExpression = this.children[1];
if (functionExpression.parenthesesAsOperator()) {
functionExpression.drawParenthesesAround(context, x + functionExpression.x, y + functionExpression.y);
}
functionExpression.display(context, x + functionExpression.x, y + functionExpression.y);
if (argumentsExpression.getTag() === "List.List") {
argumentsExpression.displayAsList(context, x + argumentsExpression.x, y + argumentsExpression.y);
this.drawParentheses(context, x + argumentsExpression.x, y + argumentsExpression.y, argumentsExpression.height, true );
this.drawParentheses(context, x + argumentsExpression.x + argumentsExpression.width, y + argumentsExpression.y, argumentsExpression.height, false);
}
else {
argumentsExpression.display(context, x + argumentsExpression.x, y + argumentsExpression.y);
}
}
}
Symbolic.LambdaApplication = class extends Expression.BinaryExpression {
getTag() { return "Symbolic.LambdaApplication"; }
getName() { return Symbolic.messages.nameLambdaApplication; }
getChildName(index) { return Symbolic.messages.childrenLambdaApplication[index]; }
prepareDisplay(context) {
let left = this.children[0];
let right = this.children[1];
left.prepareDisplay(context);
right.prepareDisplay(context);
let parenthesesLeft = left.parenthesesAsOperator() || left.parenthesesWhenSuperSubscripted();
let parenthesesRight = right.getTag() !== "List.List";
this.width = 0;
if (parenthesesLeft) this.width += 4;
left.x = this.width;
this.width += left.width;
if (parenthesesLeft) this.width += 4;
this.width += 3;
if (parenthesesRight) this.width += 4;
right.x = this.width;
this.width += right.width;
if (parenthesesRight) this.width += 4;
this.horzBaseline = Math.max(left.horzBaseline, right.horzBaseline);
left.y = this.horzBaseline - left.horzBaseline;
right.y = this.horzBaseline - right.horzBaseline;
this.height = this.horzBaseline + Math.max(left.height - left.horzBaseline, right.height - right.horzBaseline);
this.vertBaseline = this.width / 2;
}
display(context, x, y) {
let left = this.children[0];
let right = this.children[1];
left.display(context, x + left.x, y + left.y);
if (left.parenthesesAsOperator() || left.parenthesesWhenSuperSubscripted()) {
left.drawParenthesesAround(context, x + left.x, y + left.y);
}
right.display(context, x + right.x, y + right.y);
if (right.getTag() !== "List.List") {
right.drawParenthesesAround(context, x + right.x, y + right.y);
}
}
}
Symbolic.setExpressions = function(module) {
Formulae.setExpression(module, "Symbolic.Symbol", Symbolic.Symbol);
Formulae.setExpression(module, "Symbolic.Function", Symbolic.FunctionExpression);
Formulae.setExpression(module, "Symbolic.LambdaApplication", Symbolic.LambdaApplication);
// assignment
Formulae.setExpression(
module,
"Symbolic.Assignment",
{
clazz: Expression.Infix,
getTag: () => "Symbolic.Assignment",
getOperator: () => this.messages.operatorAssignment,
getName: () => this.messages.nameAssignment,
getChildName: index => this.messages.childrenAssignment[index],
parentheses: false,
inverted: true,
min: 2, max: 2
}
);
// local
Formulae.setExpression(
module,
"Symbolic.Local",
{
clazz: Expression.PrefixedLiteral,
getTag: () => "Symbolic.Local",
getLiteral: () => this.messages.literalLocal,
getName: () => this.messages.nameLocal,
color: "orange",
bold: true,
space: 10,
parentheses: false
}
);
// λ and λ-builder
[ "Lambda", "LambdaBuilder" ].forEach(tag => Formulae.setExpression(
module,
"Symbolic." + tag,
{
clazz: Expression.Infix,
getTag: () => "Symbolic." + tag,
getOperator: () => Symbolic.messages.operatorLambda,
getName: () => Symbolic.messages["name" + tag],
color: tag === "LambdaBuilder" ? "orange" : null,
inverted: true,
min: 2, max: 2
}
));
// return
Formulae.setExpression(
module,
"Symbolic.Return",
{
clazz: Expression.PrefixedLiteral,
getTag: () => "Symbolic.Return",
getLiteral: () => this.messages.literalReturn,
getName: () => this.messages.nameReturn,
bold: true,
space: 10,
parentheses: false
}
);
// function
//[ "Return", "Undefine" ].forEach(tag => Formulae.setExpression(
[ "Undefine" ].forEach(tag => Formulae.setExpression(
module,
"Symbolic." + tag,
{
clazz: Expression.Function,
getTag: () => "Symbolic." + tag,
getMnemonic: () => Symbolic.messages["mnemonic" + tag],
getName: () => Symbolic.messages["name" + tag],
parentheses: tag === "Return" ? false : null,
min: 1, max: 1
}
));
};