-
Notifications
You must be signed in to change notification settings - Fork 0
/
reduction.js
171 lines (141 loc) · 6.1 KB
/
reduction.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
/*
Fōrmulæ visualization package. Module for reduction.
Copyright (C) 2015-2025 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 Visualization extends Formulae.Package {}
Visualization.createRectangle = async (createRectangle, session) => {
let width = CanonicalArithmetic.getNativeInteger(createRectangle.children[0]);
if (width === undefined || width <= 0) {
ReductionManager.setInError(createRectangle.children[0], "Invalid value");
throw new ReductionError();
}
let height = CanonicalArithmetic.getNativeInteger(createRectangle.children[1]);
if (height === undefined || height <= 0) {
ReductionManager.setInError(createRectangle.children[1], "Invalid value");
throw new ReductionError();
}
let horzBaseline = CanonicalArithmetic.getNativeInteger(createRectangle.children[2]);
if (horzBaseline === undefined || horzBaseline < 0 || horzBaseline > height) {
ReductionManager.setInError(createRectangle.children[2], "Invalid value");
throw new ReductionError();
}
let vertBaseline = CanonicalArithmetic.getNativeInteger(createRectangle.children[3]);
if (vertBaseline === undefined || vertBaseline < 0 || vertBaseline > width) {
ReductionManager.setInError(createRectangle.children[3], "Invalid value");
throw new ReductionError();
}
let result = Formulae.createExpression("Visualization.Rectangle");
result.set("Width", width);
result.set("Height", height);
result.set("HorizontalBaseline", horzBaseline);
result.set("VerticalBaseline", vertBaseline);
createRectangle.replaceBy(result);
return true;
};
Visualization.setColor = async (setColor, session) => {
let colorExpression = setColor.children[1];
if (colorExpression.getTag() !== "Color.Color") {
ReductionManager.setInError(colorExpression, "Expression is not a color");
throw new ReductionError();
}
let result = Formulae.createExpression("Visualization.Color");
result.set("Red", colorExpression.get("Red"));
result.set("Green", colorExpression.get("Green"));
result.set("Blue", colorExpression.get("Blue"));
result.set("Alpha", colorExpression.get("Alpha"));
result.addChild(setColor.children[0]);
setColor.replaceBy(result);
return true;
};
Visualization.setBoldItalic = async (setBoldItalic, session) => {
let value = true;
if (setBoldItalic.children.length >= 2) {
let valueExpression = setBoldItalic.children[1];
let tag = valueExpression.getTag();
if (tag === "Logic.True") {
value = true;
}
else if (tag === "Logic.False") {
value = false;
}
else {
ReductionManager.setInError(valueExpression, "Expression must be boolean");
throw new ReductionError();
}
}
let set = true;
if (setBoldItalic.children.length >= 3) {
let setExpression = setBoldItalic.children[2];
let tag = setExpression.getTag();
if (tag === "Logic.True") {
set = true;
}
else if (tag === "Logic.False") {
set = false;
}
else {
ReductionManager.setInError(setExpression, "Expression must be boolean");
throw new ReductionError();
}
}
let result = Formulae.createExpression(
setBoldItalic.getTag() === "Visualization.SetBold" ?
"Visualization.Bold" :
"Visualization.Italic"
);
result.set("Value", value);
result.set("Set", set);
result.addChild(setBoldItalic.children[0]);
setBoldItalic.replaceBy(result);
return true;
};
Visualization.setFontSizeAndIncrement = async (setFontSizeAndIncrement, session) => {
let isSet = setFontSizeAndIncrement.getTag() === "Visualization.SetFontSize";
let parameterExpression = setFontSizeAndIncrement.children[1];
let parameter = CanonicalArithmetic.getNativeInteger(parameterExpression);
if (parameter === undefined || (isSet && parameter <= 0)) {
ReductionManager.setInError(parameterExpression, "Invalid value");
throw new ReductionError();
}
let result = Formulae.createExpression(
isSet ?
"Visualization.FontSize" :
"Visualization.FontSizeIncrement"
);
result.set(isSet ? "Size" : "Increment", parameter);
result.addChild(setFontSizeAndIncrement.children[0]);
setFontSizeAndIncrement.replaceBy(result);
return true;
};
Visualization.setFontName = async (setFontName, session) => {
let nameExpression = setFontName.children[1];
if (nameExpression.getTag() !== "String.String") {
ReductionManager.setInError(nameExpression, "Expression is not a string");
throw new ReductionError();
}
let result = Formulae.createExpression(Visualization.FontName);
result.set("Name", nameExpression.get("Value"));
result.addChild(setFontName.children[0]);
setFontName.replaceBy(result);
return true;
};
Visualization.setReducers = () => {
ReductionManager.addReducer("Visualization.CreateRectangle", Visualization.createRectangle, "Visualization.createRectangle");
ReductionManager.addReducer("Visualization.SetColor", Visualization.setColor, "Visualization.setColor");
ReductionManager.addReducer("Visualization.SetBold", Visualization.setBoldItalic, "Visualization.setBoldItalic");
ReductionManager.addReducer("Visualization.SetItalic", Visualization.setBoldItalic, "Visualization.setBoldItalic");
ReductionManager.addReducer("Visualization.SetFontSize", Visualization.setFontSizeAndIncrement, "Visualization.setFontSizeAndIncrement");
ReductionManager.addReducer("Visualization.SetFontSizeIncrement", Visualization.setFontSizeAndIncrement, "Visualization.setFontSizeAndIncrement");
ReductionManager.addReducer("Visualization.SetFontName", Visualization.setFontName, "Visualization.setFontName");
};