qpremake 1.5.2 → 1.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +335 -180
- package/package.json +2 -7
- package/lib/_localizationFiles/English.d.ts +0 -2
- package/lib/_localizationFiles/English.js +0 -307
- package/lib/_mods/parserModules/addSymbol.d.ts +0 -7
- package/lib/_mods/parserModules/addSymbol.js +0 -18
- package/lib/_mods/parserModules/expression.d.ts +0 -7
- package/lib/_mods/parserModules/expression.js +0 -45
- package/lib/_mods/parserModules/expression_parser.d.ts +0 -143
- package/lib/_mods/parserModules/expression_parser.js +0 -728
- package/lib/_mods/parserModules/img.d.ts +0 -8
- package/lib/_mods/parserModules/img.js +0 -42
- package/lib/_mods/parserModules/qpOriginal.d.ts +0 -5
- package/lib/_mods/parserModules/qpOriginal.js +0 -27
- package/lib/_mods/parserModules/sectionID.d.ts +0 -13
- package/lib/_mods/parserModules/sectionID.js +0 -56
- package/lib/_mods/parserModules/tags.d.ts +0 -9
- package/lib/_mods/parserModules/tags.js +0 -29
- package/lib/_mods/parserModules/uaddminus.d.ts +0 -9
- package/lib/_mods/parserModules/uaddminus.js +0 -42
- package/lib/_mods/parserModules/variantCheck.d.ts +0 -10
- package/lib/_mods/parserModules/variantCheck.js +0 -53
- package/lib/_queenSystem/handler/actionGenrator.d.ts +0 -994
- package/lib/_queenSystem/handler/actionGenrator.js +0 -646
- package/lib/_queenSystem/handler/actionInputGenerator.d.ts +0 -95
- package/lib/_queenSystem/handler/actionInputGenerator.js +0 -373
- package/lib/_queenSystem/handler/actionInputRequesterGenerator.d.ts +0 -92
- package/lib/_queenSystem/handler/actionInputRequesterGenerator.js +0 -341
- package/lib/_queenSystem/handler/cardHandler.d.ts +0 -20
- package/lib/_queenSystem/handler/cardHandler.js +0 -61
- package/lib/_queenSystem/handler/effectGenerator.d.ts +0 -1
- package/lib/_queenSystem/handler/effectGenerator.js +0 -127
- package/lib/_queenSystem/handler/localizationHandler.d.ts +0 -28
- package/lib/_queenSystem/handler/localizationHandler.js +0 -166
- package/lib/_queenSystem/handler/modHandler.d.ts +0 -10
- package/lib/_queenSystem/handler/modHandler.js +0 -36
- package/lib/_queenSystem/handler/oldDataProcessor.d.ts +0 -9
- package/lib/_queenSystem/handler/oldDataProcessor.js +0 -129
- package/lib/_queenSystem/handler/registryHandler.d.ts +0 -36
- package/lib/_queenSystem/handler/registryHandler.js +0 -63
- package/lib/_queenSystem/handler/zoneHandler.d.ts +0 -102
- package/lib/_queenSystem/handler/zoneHandler.js +0 -606
- package/lib/_queenSystem/loader/loader_card.d.ts +0 -20
- package/lib/_queenSystem/loader/loader_card.js +0 -124
- package/lib/_queenSystem/loader/loader_effect.d.ts +0 -27
- package/lib/_queenSystem/loader/loader_effect.js +0 -163
- package/lib/_queenSystem/loader/loader_handler.d.ts +0 -11
- package/lib/_queenSystem/loader/loader_handler.js +0 -24
- package/lib/_queenSystem/loader/loader_localization.d.ts +0 -11
- package/lib/_queenSystem/loader/loader_localization.js +0 -73
- package/lib/_queenSystem/loader/loader_mod.d.ts +0 -10
- package/lib/_queenSystem/loader/loader_mod.js +0 -72
- package/lib/_queenSystem/loader/loader_subtype.d.ts +0 -9
- package/lib/_queenSystem/loader/loader_subtype.js +0 -39
- package/lib/_queenSystem/loader/loader_type.d.ts +0 -9
- package/lib/_queenSystem/loader/loader_type.js +0 -39
- package/lib/_queenSystem/loader/loader_zone.d.ts +0 -14
- package/lib/_queenSystem/loader/loader_zone.js +0 -35
- package/lib/_queenSystem/queenSystem.d.ts +0 -127
- package/lib/_queenSystem/queenSystem.js +0 -996
- package/lib/_queenSystem/renderer/rendererInterface.d.ts +0 -9
- package/lib/_queenSystem/renderer/rendererInterface.js +0 -2
- package/lib/data/actionRegistry.d.ts +0 -56
- package/lib/data/actionRegistry.js +0 -60
- package/lib/data/cardRegistry.d.ts +0 -1162
- package/lib/data/cardRegistry.js +0 -716
- package/lib/data/deckRegistry.d.ts +0 -19
- package/lib/data/deckRegistry.js +0 -41
- package/lib/data/effectRegistry.d.ts +0 -465
- package/lib/data/effectRegistry.js +0 -151
- package/lib/data/effectTypeRegistry.d.ts +0 -14
- package/lib/data/effectTypeRegistry.js +0 -14
- package/lib/data/operatorRegistry.d.ts +0 -92
- package/lib/data/operatorRegistry.js +0 -82
- package/lib/data/rarityRegistry.d.ts +0 -28
- package/lib/data/rarityRegistry.js +0 -63
- package/lib/data/registryRegistry.d.ts +0 -10
- package/lib/data/registryRegistry.js +0 -14
- package/lib/data/subtypeRegistry.d.ts +0 -15
- package/lib/data/subtypeRegistry.js +0 -15
- package/lib/data/systemRegistry.d.ts +0 -253
- package/lib/data/systemRegistry.js +0 -56
- package/lib/data/zoneRegistry.d.ts +0 -43
- package/lib/data/zoneRegistry.js +0 -157
- package/lib/effectTextParser/index.d.ts +0 -42
- package/lib/effectTextParser/index.js +0 -316
- package/lib/global/index.d.ts +0 -8
- package/lib/global/index.js +0 -12
- package/lib/index.d.ts +0 -6341
- package/lib/index.js +0 -212
- package/lib/specificEffects/e_cond.d.ts +0 -11
- package/lib/specificEffects/e_cond.js +0 -36
- package/lib/specificEffects/e_fruit.d.ts +0 -3
- package/lib/specificEffects/e_fruit.js +0 -187
- package/lib/specificEffects/e_generic.d.ts +0 -191
- package/lib/specificEffects/e_generic.js +0 -662
- package/lib/specificEffects/e_generic_cardTargetting.d.ts +0 -38
- package/lib/specificEffects/e_generic_cardTargetting.js +0 -82
- package/lib/specificEffects/e_generic_noneTargetting.d.ts +0 -10
- package/lib/specificEffects/e_generic_noneTargetting.js +0 -26
- package/lib/specificEffects/e_status.d.ts +0 -80
- package/lib/specificEffects/e_status.js +0 -151
- package/lib/specificEffects/e_test.d.ts +0 -3
- package/lib/specificEffects/e_test.js +0 -21
- package/lib/types/abstract/gameComponents/API.d.ts +0 -19
- package/lib/types/abstract/gameComponents/API.js +0 -2
- package/lib/types/abstract/gameComponents/card.d.ts +0 -108
- package/lib/types/abstract/gameComponents/card.js +0 -771
- package/lib/types/abstract/gameComponents/effect.d.ts +0 -82
- package/lib/types/abstract/gameComponents/effect.js +0 -331
- package/lib/types/abstract/gameComponents/effectSubtype.d.ts +0 -22
- package/lib/types/abstract/gameComponents/effectSubtype.js +0 -40
- package/lib/types/abstract/gameComponents/effectType.d.ts +0 -11
- package/lib/types/abstract/gameComponents/effectType.js +0 -8
- package/lib/types/abstract/gameComponents/settings.d.ts +0 -80
- package/lib/types/abstract/gameComponents/settings.js +0 -68
- package/lib/types/abstract/gameComponents/zone.d.ts +0 -113
- package/lib/types/abstract/gameComponents/zone.js +0 -614
- package/lib/types/abstract/gameComponents/zone_gridBased.d.ts +0 -18
- package/lib/types/abstract/gameComponents/zone_gridBased.js +0 -58
- package/lib/types/abstract/gameComponents/zone_stackBased.d.ts +0 -13
- package/lib/types/abstract/gameComponents/zone_stackBased.js +0 -55
- package/lib/types/abstract/generics/node.d.ts +0 -23
- package/lib/types/abstract/generics/node.js +0 -46
- package/lib/types/abstract/generics/position.d.ts +0 -23
- package/lib/types/abstract/generics/position.js +0 -91
- package/lib/types/abstract/generics/tree.d.ts +0 -25
- package/lib/types/abstract/generics/tree.js +0 -153
- package/lib/types/abstract/generics/universalResponse.d.ts +0 -4
- package/lib/types/abstract/generics/universalResponse.js +0 -2
- package/lib/types/abstract/generics/wraper.d.ts +0 -21
- package/lib/types/abstract/generics/wraper.js +0 -56
- package/lib/types/abstract/parser/component.d.ts +0 -88
- package/lib/types/abstract/parser/component.js +0 -132
- package/lib/types/abstract/parser/index.d.ts +0 -6
- package/lib/types/abstract/parser/index.js +0 -28
- package/lib/types/abstract/parser/modPack.d.ts +0 -21
- package/lib/types/abstract/parser/modPack.js +0 -48
- package/lib/types/abstract/parser/moduleInputObject.d.ts +0 -12
- package/lib/types/abstract/parser/moduleInputObject.js +0 -21
- package/lib/types/abstract/parser/options.d.ts +0 -31
- package/lib/types/abstract/parser/options.js +0 -45
- package/lib/types/abstract/serializedGameComponents/Gamestate.d.ts +0 -46
- package/lib/types/abstract/serializedGameComponents/Gamestate.js +0 -70
- package/lib/types/abstract/serializedGameComponents/Localized.d.ts +0 -69
- package/lib/types/abstract/serializedGameComponents/Localized.js +0 -84
- package/lib/types/defaultZones/ability.d.ts +0 -10
- package/lib/types/defaultZones/ability.js +0 -22
- package/lib/types/defaultZones/deck.d.ts +0 -25
- package/lib/types/defaultZones/deck.js +0 -90
- package/lib/types/defaultZones/drop.d.ts +0 -5
- package/lib/types/defaultZones/drop.js +0 -13
- package/lib/types/defaultZones/field.d.ts +0 -11
- package/lib/types/defaultZones/field.js +0 -35
- package/lib/types/defaultZones/grave.d.ts +0 -8
- package/lib/types/defaultZones/grave.js +0 -21
- package/lib/types/defaultZones/hand.d.ts +0 -5
- package/lib/types/defaultZones/hand.js +0 -14
- package/lib/types/defaultZones/storage.d.ts +0 -9
- package/lib/types/defaultZones/storage.js +0 -21
- package/lib/types/defaultZones/system.d.ts +0 -12
- package/lib/types/defaultZones/system.js +0 -35
- package/lib/types/defaultZones/void.d.ts +0 -4
- package/lib/types/defaultZones/void.js +0 -10
- package/lib/types/effects/effectSubtypes/subtype_chained.d.ts +0 -10
- package/lib/types/effects/effectSubtypes/subtype_chained.js +0 -17
- package/lib/types/effects/effectSubtypes/subtype_delayed.d.ts +0 -9
- package/lib/types/effects/effectSubtypes/subtype_delayed.js +0 -16
- package/lib/types/effects/effectSubtypes/subtype_fieldLock.d.ts +0 -9
- package/lib/types/effects/effectSubtypes/subtype_fieldLock.js +0 -21
- package/lib/types/effects/effectSubtypes/subtype_graveLock.d.ts +0 -9
- package/lib/types/effects/effectSubtypes/subtype_graveLock.js +0 -21
- package/lib/types/effects/effectSubtypes/subtype_hand_or_fieldLock.d.ts +0 -9
- package/lib/types/effects/effectSubtypes/subtype_hand_or_fieldLock.js +0 -21
- package/lib/types/effects/effectSubtypes/subtype_hardUnique.d.ts +0 -9
- package/lib/types/effects/effectSubtypes/subtype_hardUnique.js +0 -15
- package/lib/types/effects/effectSubtypes/subtype_instant.d.ts +0 -8
- package/lib/types/effects/effectSubtypes/subtype_instant.js +0 -20
- package/lib/types/effects/effectSubtypes/subtype_once.d.ts +0 -12
- package/lib/types/effects/effectSubtypes/subtype_once.js +0 -27
- package/lib/types/effects/effectSubtypes/subtype_unique.d.ts +0 -9
- package/lib/types/effects/effectSubtypes/subtype_unique.js +0 -16
- package/lib/types/effects/effectTypes/initEffect.d.ts +0 -7
- package/lib/types/effects/effectTypes/initEffect.js +0 -23
- package/lib/types/effects/effectTypes/lockEffect.d.ts +0 -9
- package/lib/types/effects/effectTypes/lockEffect.js +0 -25
- package/lib/types/effects/effectTypes/manualEffect.d.ts +0 -9
- package/lib/types/effects/effectTypes/manualEffect.js +0 -18
- package/lib/types/effects/effectTypes/passiveEffect.d.ts +0 -9
- package/lib/types/effects/effectTypes/passiveEffect.js +0 -23
- package/lib/types/effects/effectTypes/triggerEffect.d.ts +0 -9
- package/lib/types/effects/effectTypes/triggerEffect.js +0 -28
- package/lib/types/errors/cannotLoad.d.ts +0 -5
- package/lib/types/errors/cannotLoad.js +0 -13
- package/lib/types/errors/cardNotExist.d.ts +0 -5
- package/lib/types/errors/cardNotExist.js +0 -13
- package/lib/types/errors/cardNotInApplicableZone.d.ts +0 -5
- package/lib/types/errors/cardNotInApplicableZone.js +0 -13
- package/lib/types/errors/effectCondNotMet.d.ts +0 -5
- package/lib/types/errors/effectCondNotMet.js +0 -13
- package/lib/types/errors/effectNotExist.d.ts +0 -5
- package/lib/types/errors/effectNotExist.js +0 -13
- package/lib/types/errors/error.d.ts +0 -17
- package/lib/types/errors/error.js +0 -42
- package/lib/types/errors/incorrectActionType.d.ts +0 -5
- package/lib/types/errors/incorrectActionType.js +0 -13
- package/lib/types/errors/index.d.ts +0 -16
- package/lib/types/errors/index.js +0 -36
- package/lib/types/errors/invalidOrderMap.d.ts +0 -6
- package/lib/types/errors/invalidOrderMap.js +0 -14
- package/lib/types/errors/invalidPosition.d.ts +0 -6
- package/lib/types/errors/invalidPosition.js +0 -13
- package/lib/types/errors/subTypeOverrideConflict.d.ts +0 -7
- package/lib/types/errors/subTypeOverrideConflict.js +0 -15
- package/lib/types/errors/unknownError.d.ts +0 -5
- package/lib/types/errors/unknownError.js +0 -13
- package/lib/types/errors/unregisteredAction.d.ts +0 -6
- package/lib/types/errors/unregisteredAction.js +0 -13
- package/lib/types/errors/wrongEffectIdx.d.ts +0 -5
- package/lib/types/errors/wrongEffectIdx.js +0 -14
- package/lib/types/errors/zoneAttrConflict.d.ts +0 -5
- package/lib/types/errors/zoneAttrConflict.js +0 -15
- package/lib/types/errors/zoneFull.d.ts +0 -5
- package/lib/types/errors/zoneFull.js +0 -13
- package/lib/types/errors/zoneNotExist.d.ts +0 -5
- package/lib/types/errors/zoneNotExist.js +0 -13
- package/lib/types/misc.d.ts +0 -97
- package/lib/types/misc.js +0 -8
- package/lib/types/mods/effectTextParserModule.d.ts +0 -16
- package/lib/types/mods/effectTextParserModule.js +0 -81
- package/lib/types/mods/gameModule.d.ts +0 -5
- package/lib/types/mods/gameModule.js +0 -8
- package/lib/utils/index.d.ts +0 -78
- package/lib/utils/index.js +0 -562
|
@@ -1,728 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
function evaluateNumberOperation(opCode, a, b) {
|
|
4
|
-
switch (opCode) {
|
|
5
|
-
case "!=":
|
|
6
|
-
return a !== b ? 1 : 0;
|
|
7
|
-
case "==":
|
|
8
|
-
return a === b ? 1 : 0;
|
|
9
|
-
case "<":
|
|
10
|
-
return a < b ? 1 : 0;
|
|
11
|
-
case ">":
|
|
12
|
-
return a > b ? 1 : 0;
|
|
13
|
-
case ">=":
|
|
14
|
-
return a >= b ? 1 : 0;
|
|
15
|
-
case "<=":
|
|
16
|
-
return a <= b ? 1 : 0;
|
|
17
|
-
case "&&":
|
|
18
|
-
return (a !== 0) && (b !== 0) ? 1 : 0;
|
|
19
|
-
case "||":
|
|
20
|
-
return (a !== 0) || (b !== 0) ? 1 : 0;
|
|
21
|
-
case "+":
|
|
22
|
-
return a + b;
|
|
23
|
-
case "-":
|
|
24
|
-
return a - b;
|
|
25
|
-
case "*":
|
|
26
|
-
return a * b;
|
|
27
|
-
case "/":
|
|
28
|
-
return a / b;
|
|
29
|
-
case ">>":
|
|
30
|
-
return a >> b;
|
|
31
|
-
case "<<":
|
|
32
|
-
return a << b;
|
|
33
|
-
case "..":
|
|
34
|
-
let precision = Math.pow(10, b);
|
|
35
|
-
return Math.round(a * precision) / precision;
|
|
36
|
-
default: return NaN;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
const trueSring = "_";
|
|
40
|
-
const falseString = "";
|
|
41
|
-
function evaluateStringOperation(opCode, a, b) {
|
|
42
|
-
switch (opCode) {
|
|
43
|
-
case "!=":
|
|
44
|
-
return a !== b ? trueSring : falseString;
|
|
45
|
-
case "==":
|
|
46
|
-
return a === b ? trueSring : falseString;
|
|
47
|
-
case "<":
|
|
48
|
-
return a < b ? trueSring : falseString;
|
|
49
|
-
case ">":
|
|
50
|
-
return a > b ? trueSring : falseString;
|
|
51
|
-
case ">=":
|
|
52
|
-
return a >= b ? trueSring : falseString;
|
|
53
|
-
case "<=":
|
|
54
|
-
return a <= b ? trueSring : falseString;
|
|
55
|
-
case "&&":
|
|
56
|
-
return (a.length !== 0) && (b.length !== 0) ? trueSring : falseString;
|
|
57
|
-
case "||":
|
|
58
|
-
return (a.length !== 0) || (b.length !== 0) ? trueSring : falseString;
|
|
59
|
-
case ">>":
|
|
60
|
-
case "<<":
|
|
61
|
-
case "*":
|
|
62
|
-
case "++":
|
|
63
|
-
case "+":
|
|
64
|
-
return a + b;
|
|
65
|
-
case "/":
|
|
66
|
-
case "-":
|
|
67
|
-
return a.split(b).join("");
|
|
68
|
-
case "..":
|
|
69
|
-
return a + " " + b;
|
|
70
|
-
default: return `<err invalid opcode = ${opCode}>, a = ${a}, b = ${b}`;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
function isNumber(str) {
|
|
74
|
-
return !isNaN(Number(str));
|
|
75
|
-
}
|
|
76
|
-
class evaluate_element {
|
|
77
|
-
get isNum() {
|
|
78
|
-
return this._isNum;
|
|
79
|
-
}
|
|
80
|
-
get isStr() {
|
|
81
|
-
return this._isStr;
|
|
82
|
-
}
|
|
83
|
-
constructor(x, num_var, string_var) {
|
|
84
|
-
let isNum = isNumber(x);
|
|
85
|
-
// variable substitution
|
|
86
|
-
if (!isNum) {
|
|
87
|
-
const char = String(x);
|
|
88
|
-
if (char.length === 1) {
|
|
89
|
-
const cc = char.charCodeAt(0);
|
|
90
|
-
let a = "a".charCodeAt(0);
|
|
91
|
-
let z = "z".charCodeAt(0);
|
|
92
|
-
let charCode = cc - a;
|
|
93
|
-
if (charCode <= z && charCode >= 0
|
|
94
|
-
&& num_var[charCode] !== undefined) {
|
|
95
|
-
isNum = true;
|
|
96
|
-
x = num_var[charCode];
|
|
97
|
-
}
|
|
98
|
-
else {
|
|
99
|
-
a = "A".charCodeAt(0);
|
|
100
|
-
z = "Z".charCodeAt(0);
|
|
101
|
-
charCode = cc - a;
|
|
102
|
-
if (charCode <= z && charCode >= 0
|
|
103
|
-
&& string_var[charCode] !== undefined) {
|
|
104
|
-
x = string_var[charCode];
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
this._isNum = isNum;
|
|
110
|
-
this._isStr = !isNum;
|
|
111
|
-
this.x = x;
|
|
112
|
-
// console.log("x:", this.x, "isNumber:", isNum)
|
|
113
|
-
}
|
|
114
|
-
get num() {
|
|
115
|
-
if (this.isStr)
|
|
116
|
-
this.x = this.x.length;
|
|
117
|
-
return Number(this.x);
|
|
118
|
-
}
|
|
119
|
-
get str() {
|
|
120
|
-
return String(this.x);
|
|
121
|
-
}
|
|
122
|
-
get bool() {
|
|
123
|
-
return this.x == 0; //== is intentional to convert both 0 and "" to true
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
function evaluateOperation(opCode, x, y, num_var, string_var) {
|
|
127
|
-
const a = new evaluate_element(x, num_var, string_var);
|
|
128
|
-
const b = new evaluate_element(y, num_var, string_var);
|
|
129
|
-
//return `[${a.isNum ? "num" : "str"}, ${x}][${b.isNum ? "num" : "str"}, ${y}]`
|
|
130
|
-
if (a.isNum && b.isNum)
|
|
131
|
-
return evaluateNumberOperation(opCode, a.num, b.num);
|
|
132
|
-
if (a.isStr && b.isStr)
|
|
133
|
-
return evaluateStringOperation(opCode, a.str, b.str);
|
|
134
|
-
//the switch below hadles string, num or num, string
|
|
135
|
-
switch (opCode) {
|
|
136
|
-
case "!=":
|
|
137
|
-
case "==":
|
|
138
|
-
case "<":
|
|
139
|
-
case ">":
|
|
140
|
-
case ">=":
|
|
141
|
-
case "<=": {
|
|
142
|
-
x = a.num;
|
|
143
|
-
y = b.num;
|
|
144
|
-
return evaluateNumberOperation(opCode, x, y);
|
|
145
|
-
}
|
|
146
|
-
case "&&":
|
|
147
|
-
return a.bool && b.bool ? 1 : 0;
|
|
148
|
-
case "||":
|
|
149
|
-
return a.bool || b.bool ? 1 : 0;
|
|
150
|
-
case ">>": {
|
|
151
|
-
if (a.isStr) {
|
|
152
|
-
return a.str.slice(b.num);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
case "<<": {
|
|
156
|
-
if (a.isStr) {
|
|
157
|
-
return a.str.slice(0, b.num * -1);
|
|
158
|
-
}
|
|
159
|
-
//num >> string
|
|
160
|
-
return evaluateNumberOperation(opCode, a.num, b.num);
|
|
161
|
-
}
|
|
162
|
-
case "++": return a.str + b.str;
|
|
163
|
-
case "+":
|
|
164
|
-
case "-":
|
|
165
|
-
return a.isNum //first param authoritativee
|
|
166
|
-
? evaluateNumberOperation(opCode, a.num, b.num)
|
|
167
|
-
: evaluateStringOperation(opCode, a.str, b.str);
|
|
168
|
-
case "*":
|
|
169
|
-
return a.isNum
|
|
170
|
-
? evaluateNumberOperation(opCode, a.num, b.num)
|
|
171
|
-
: a.str.repeat(b.num);
|
|
172
|
-
case "/": {
|
|
173
|
-
if (a.isStr) {
|
|
174
|
-
const len = a.str.length;
|
|
175
|
-
const finalLen = Math.floor(len / b.num);
|
|
176
|
-
return a.str.slice(0, finalLen);
|
|
177
|
-
}
|
|
178
|
-
return evaluateNumberOperation(opCode, a.num, b.num);
|
|
179
|
-
}
|
|
180
|
-
case "..":
|
|
181
|
-
return a.isNum
|
|
182
|
-
? evaluateStringOperation(opCode, a.str, b.str)
|
|
183
|
-
: b.str.slice(0, a.num);
|
|
184
|
-
default: return `<err, invalid opcode : ${opCode}>, x = ${x}, y = ${y}`;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
class Expression {
|
|
188
|
-
constructor(operator, operands) {
|
|
189
|
-
this.operator = operator;
|
|
190
|
-
this.operands = operands;
|
|
191
|
-
}
|
|
192
|
-
get numOperands() { return this.operands.length; }
|
|
193
|
-
toString() {
|
|
194
|
-
if (this.operator.type === TokenType.EOF)
|
|
195
|
-
return "[EOF]";
|
|
196
|
-
let res;
|
|
197
|
-
if (this.operator.type === TokenType.immutable) {
|
|
198
|
-
res = `immutable.[]`;
|
|
199
|
-
}
|
|
200
|
-
else
|
|
201
|
-
res = `op.[${this.operator.str}]`;
|
|
202
|
-
if (this.numOperands) {
|
|
203
|
-
res += "{\n";
|
|
204
|
-
this.operands.flatMap(o => o.toString().split("\n")).forEach(str => {
|
|
205
|
-
res += ` ` + str + "\n";
|
|
206
|
-
});
|
|
207
|
-
res += "}\n";
|
|
208
|
-
}
|
|
209
|
-
return res;
|
|
210
|
-
}
|
|
211
|
-
get collapsible() {
|
|
212
|
-
return this.operands.every(k => k instanceof ValueExpression);
|
|
213
|
-
}
|
|
214
|
-
evaluate(evaluator) {
|
|
215
|
-
// console.log(`Evaluating ${this.toString().slice(0, 50).replaceAll("\n", "")}`)
|
|
216
|
-
if (!this.collapsible) {
|
|
217
|
-
let hasChanged = true;
|
|
218
|
-
while (hasChanged && !this.collapsible) {
|
|
219
|
-
this.operands = this.operands.map(op => {
|
|
220
|
-
if (!op) {
|
|
221
|
-
throw new Error(`Operand has undef operator for some reason???, evaluating operator: ${this.operator.str}`);
|
|
222
|
-
}
|
|
223
|
-
const [newOperand, a] = op.evaluate(evaluator);
|
|
224
|
-
hasChanged = a;
|
|
225
|
-
return newOperand;
|
|
226
|
-
});
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
if (this instanceof UnaryExpression)
|
|
230
|
-
return [evaluator.evaluate_unary(this), true];
|
|
231
|
-
if (this instanceof BinaryExpression)
|
|
232
|
-
return [evaluator.evaluate_binary(this), true];
|
|
233
|
-
if (this instanceof TernaryExpression)
|
|
234
|
-
return [evaluator.evaluate_ternary(this), true];
|
|
235
|
-
if (this instanceof PostfixExpression)
|
|
236
|
-
return [evaluator.evaluate_postfix(this), true];
|
|
237
|
-
if (this instanceof ValueExpression)
|
|
238
|
-
return [evaluator.evaluate_value(this), true];
|
|
239
|
-
return [this, false];
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
class ValueExpression extends Expression {
|
|
243
|
-
constructor(val) {
|
|
244
|
-
super(val, []);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
class ValueWithImmutableAttachedExpression extends ValueExpression {
|
|
248
|
-
constructor(expression, immutable_token, attachedAtEnd = true) {
|
|
249
|
-
super(expression.operator);
|
|
250
|
-
this.immutable_token = immutable_token;
|
|
251
|
-
this.attachedAtEnd = attachedAtEnd;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
class ParsedValue extends ValueExpression {
|
|
255
|
-
}
|
|
256
|
-
class ParsedValueWithImmutable extends ValueWithImmutableAttachedExpression {
|
|
257
|
-
}
|
|
258
|
-
class UnaryExpression extends Expression {
|
|
259
|
-
constructor(operator, operand) {
|
|
260
|
-
super(operator, [operand]);
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
class BinaryExpression extends Expression {
|
|
264
|
-
get left() { return this.operands[0]; }
|
|
265
|
-
get right() { return this.operands[1]; }
|
|
266
|
-
constructor(operator, left, right) {
|
|
267
|
-
super(operator, [left, right]);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
class TernaryExpression extends Expression {
|
|
271
|
-
get cond() { return this.operands[0]; }
|
|
272
|
-
get left() { return this.operands[1]; }
|
|
273
|
-
get right() { return this.operands[2]; }
|
|
274
|
-
constructor(operator, cond, left, right) {
|
|
275
|
-
super(operator, [cond, left, right]);
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
class PostfixExpression extends Expression {
|
|
279
|
-
constructor(operator, left) {
|
|
280
|
-
super(operator, [left]);
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
var TokenType;
|
|
284
|
-
(function (TokenType) {
|
|
285
|
-
//immutable is specicial,
|
|
286
|
-
//for keeping refs in the expression tree
|
|
287
|
-
TokenType[TokenType["immutable"] = 0] = "immutable";
|
|
288
|
-
//value
|
|
289
|
-
TokenType[TokenType["value_number"] = 1] = "value_number";
|
|
290
|
-
TokenType[TokenType["value_string"] = 2] = "value_string";
|
|
291
|
-
//comparison and boolean logic
|
|
292
|
-
TokenType[TokenType["equals"] = 3] = "equals";
|
|
293
|
-
TokenType[TokenType["not_equals"] = 4] = "not_equals";
|
|
294
|
-
TokenType[TokenType["lesser"] = 5] = "lesser";
|
|
295
|
-
TokenType[TokenType["greater"] = 6] = "greater";
|
|
296
|
-
TokenType[TokenType["lesser_or_equal"] = 7] = "lesser_or_equal";
|
|
297
|
-
TokenType[TokenType["greater_or_equal"] = 8] = "greater_or_equal";
|
|
298
|
-
TokenType[TokenType["negation"] = 9] = "negation";
|
|
299
|
-
TokenType[TokenType["or"] = 10] = "or";
|
|
300
|
-
TokenType[TokenType["and"] = 11] = "and";
|
|
301
|
-
//binary operator
|
|
302
|
-
TokenType[TokenType["shift_left"] = 12] = "shift_left";
|
|
303
|
-
TokenType[TokenType["shift_right"] = 13] = "shift_right";
|
|
304
|
-
TokenType[TokenType["plus"] = 14] = "plus";
|
|
305
|
-
TokenType[TokenType["minus"] = 15] = "minus";
|
|
306
|
-
TokenType[TokenType["mul"] = 16] = "mul";
|
|
307
|
-
TokenType[TokenType["div"] = 17] = "div";
|
|
308
|
-
TokenType[TokenType["slice_or_round_to_precision"] = 18] = "slice_or_round_to_precision";
|
|
309
|
-
TokenType[TokenType["forced_concat"] = 19] = "forced_concat";
|
|
310
|
-
//all brackets are the same, we arent concern with accuracy too much here
|
|
311
|
-
TokenType[TokenType["bracket_open"] = 20] = "bracket_open";
|
|
312
|
-
TokenType[TokenType["bracket_end"] = 21] = "bracket_end";
|
|
313
|
-
TokenType[TokenType["question_mark"] = 22] = "question_mark";
|
|
314
|
-
TokenType[TokenType["colon"] = 23] = "colon";
|
|
315
|
-
TokenType[TokenType["EOF"] = 24] = "EOF";
|
|
316
|
-
})(TokenType || (TokenType = {}));
|
|
317
|
-
var StringEncapsulation;
|
|
318
|
-
(function (StringEncapsulation) {
|
|
319
|
-
StringEncapsulation[StringEncapsulation["double_quote"] = -100] = "double_quote";
|
|
320
|
-
StringEncapsulation[StringEncapsulation["single_quote"] = -99] = "single_quote";
|
|
321
|
-
StringEncapsulation[StringEncapsulation["tick_mark"] = -98] = "tick_mark";
|
|
322
|
-
})(StringEncapsulation || (StringEncapsulation = {}));
|
|
323
|
-
class Token {
|
|
324
|
-
constructor(type, str, immutable) {
|
|
325
|
-
this.type = type;
|
|
326
|
-
this.str = str;
|
|
327
|
-
this.immutable = immutable;
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
class Parser {
|
|
331
|
-
constructor() {
|
|
332
|
-
// --- Tokenizing section --- //
|
|
333
|
-
this.TokenDictionary = new Map();
|
|
334
|
-
// --- Parsing section --- //
|
|
335
|
-
// Parsing is done using pratt //
|
|
336
|
-
this.preparse_rule = new Map();
|
|
337
|
-
this.normal_rule = new Map();
|
|
338
|
-
this.tokens = [new Token(TokenType.EOF, "")];
|
|
339
|
-
// --- Evaluation section --- //
|
|
340
|
-
// --- bound variable substituion --- //
|
|
341
|
-
this.num_var = [];
|
|
342
|
-
this.string_var = [];
|
|
343
|
-
this.addToken("+", TokenType.plus);
|
|
344
|
-
this.addToken("-", TokenType.minus);
|
|
345
|
-
this.addToken("*", TokenType.mul);
|
|
346
|
-
this.addToken("/", TokenType.div);
|
|
347
|
-
this.addToken("(", TokenType.bracket_open);
|
|
348
|
-
this.addToken("[", TokenType.bracket_open);
|
|
349
|
-
this.addToken("{", TokenType.bracket_open);
|
|
350
|
-
this.addToken(")", TokenType.bracket_end);
|
|
351
|
-
this.addToken("]", TokenType.bracket_end);
|
|
352
|
-
this.addToken("}", TokenType.bracket_end);
|
|
353
|
-
this.addToken("?", TokenType.question_mark);
|
|
354
|
-
this.addToken(":", TokenType.colon);
|
|
355
|
-
this.addToken(`"`, StringEncapsulation.double_quote);
|
|
356
|
-
this.addToken("'", StringEncapsulation.single_quote);
|
|
357
|
-
this.addToken("`", StringEncapsulation.tick_mark);
|
|
358
|
-
this.addToken("!", TokenType.negation);
|
|
359
|
-
this.addToken("==", TokenType.equals);
|
|
360
|
-
this.addToken("===", TokenType.equals);
|
|
361
|
-
this.addToken("!=", TokenType.not_equals);
|
|
362
|
-
this.addToken("!==", TokenType.not_equals);
|
|
363
|
-
this.addToken("<", TokenType.lesser);
|
|
364
|
-
this.addToken(">", TokenType.greater);
|
|
365
|
-
this.addToken("<=", TokenType.lesser_or_equal);
|
|
366
|
-
this.addToken(">=", TokenType.greater_or_equal);
|
|
367
|
-
this.addToken("&&", TokenType.and);
|
|
368
|
-
this.addToken("&", TokenType.and);
|
|
369
|
-
this.addToken("||", TokenType.or);
|
|
370
|
-
this.addToken("|", TokenType.or);
|
|
371
|
-
this.addToken("<<", TokenType.shift_left);
|
|
372
|
-
this.addToken(">>", TokenType.shift_right);
|
|
373
|
-
this.addToken("..", TokenType.slice_or_round_to_precision);
|
|
374
|
-
this.addToken("++", TokenType.forced_concat);
|
|
375
|
-
this.allTokens = Array.from(this.TokenDictionary.keys()).sort((a, b) => b.length - a.length);
|
|
376
|
-
}
|
|
377
|
-
addToken(str, type) {
|
|
378
|
-
this.TokenDictionary.set(str, type);
|
|
379
|
-
}
|
|
380
|
-
splitAndInsert(str, splitPoint) {
|
|
381
|
-
const splitted = str.split(splitPoint);
|
|
382
|
-
const res = [[splitted[0], false]]; //[str, locked], if locked, its not splittable further
|
|
383
|
-
for (let i = 1; i < splitted.length; i++) {
|
|
384
|
-
res.push([splitPoint, true]);
|
|
385
|
-
res.push([splitted[i], false]);
|
|
386
|
-
}
|
|
387
|
-
return res;
|
|
388
|
-
}
|
|
389
|
-
splitStringByTokens(expr) {
|
|
390
|
-
let stack = [[expr, false]]; //[str, locked], if locked, its not splittable further
|
|
391
|
-
this.allTokens.forEach(token => {
|
|
392
|
-
stack = stack.flatMap(str => str[1] ? [str] : this.splitAndInsert(str[0], token));
|
|
393
|
-
});
|
|
394
|
-
return stack.map(k => k[0]);
|
|
395
|
-
}
|
|
396
|
-
tokenize(expr, coerseUnknownToString = true) {
|
|
397
|
-
if (typeof expr === "string")
|
|
398
|
-
expr = [expr];
|
|
399
|
-
const tokens = expr.flatMap(e => typeof e === "string" ? this.splitStringByTokens(e) : e);
|
|
400
|
-
const res = [];
|
|
401
|
-
let currentStringBracket = undefined;
|
|
402
|
-
let currString = "";
|
|
403
|
-
tokens.forEach(token => {
|
|
404
|
-
if (typeof token !== "string") {
|
|
405
|
-
res.push(new Token(TokenType.immutable, "", token));
|
|
406
|
-
return;
|
|
407
|
-
}
|
|
408
|
-
const token_trimed = token.trim();
|
|
409
|
-
const mapped = this.TokenDictionary.get(token_trimed);
|
|
410
|
-
if (mapped !== undefined && mapped < 0) {
|
|
411
|
-
if (currentStringBracket === mapped) {
|
|
412
|
-
//close string encapsulation
|
|
413
|
-
if (currString)
|
|
414
|
-
res.push(new Token(TokenType.value_string, currString));
|
|
415
|
-
currString = "";
|
|
416
|
-
currentStringBracket = undefined;
|
|
417
|
-
}
|
|
418
|
-
else {
|
|
419
|
-
//open string encapsulation
|
|
420
|
-
currentStringBracket = mapped;
|
|
421
|
-
}
|
|
422
|
-
return;
|
|
423
|
-
}
|
|
424
|
-
else if (currentStringBracket !== undefined) {
|
|
425
|
-
//push to string
|
|
426
|
-
currString += token;
|
|
427
|
-
return;
|
|
428
|
-
}
|
|
429
|
-
token = token_trimed;
|
|
430
|
-
if (!token)
|
|
431
|
-
return;
|
|
432
|
-
if (mapped === undefined) {
|
|
433
|
-
//counts as string or number
|
|
434
|
-
if (isNaN(Number(token))) {
|
|
435
|
-
if (coerseUnknownToString)
|
|
436
|
-
res.push(new Token(TokenType.value_string, token));
|
|
437
|
-
else
|
|
438
|
-
throw Error(`Unknown token : ${token}`);
|
|
439
|
-
}
|
|
440
|
-
else {
|
|
441
|
-
res.push(new Token(TokenType.value_number, token));
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
else {
|
|
445
|
-
//registered token
|
|
446
|
-
res.push(new Token(mapped, token));
|
|
447
|
-
}
|
|
448
|
-
});
|
|
449
|
-
res.push(new Token(TokenType.EOF, "EOF"));
|
|
450
|
-
return res;
|
|
451
|
-
}
|
|
452
|
-
registerUnaryRule(token) {
|
|
453
|
-
return this.registerRule(true, token, new Parse_rule_unary());
|
|
454
|
-
}
|
|
455
|
-
registerBinaryRule(token, precedence) {
|
|
456
|
-
return this.registerRule(false, token, new Parse_rule_binary(precedence));
|
|
457
|
-
}
|
|
458
|
-
registerValueRule(token) {
|
|
459
|
-
return this.registerRule(true, token, new Parse_rule_value());
|
|
460
|
-
}
|
|
461
|
-
registerRule(noPre, token, rule) {
|
|
462
|
-
if (noPre)
|
|
463
|
-
this.preparse_rule.set(token, rule);
|
|
464
|
-
else
|
|
465
|
-
this.normal_rule.set(token, rule);
|
|
466
|
-
}
|
|
467
|
-
//return next token
|
|
468
|
-
pop(expect) {
|
|
469
|
-
const res = this.tokens.splice(0, 1)[0];
|
|
470
|
-
if (expect !== undefined && (!res || res.type !== expect)) {
|
|
471
|
-
//unmatched
|
|
472
|
-
throw new Error(`Expected token failed, expect ${expect}, got ${res ? res.type : "<undef>"}`);
|
|
473
|
-
}
|
|
474
|
-
return res;
|
|
475
|
-
}
|
|
476
|
-
assertNext(type) {
|
|
477
|
-
this.pop(type);
|
|
478
|
-
}
|
|
479
|
-
peek() {
|
|
480
|
-
return this.tokens.at(0);
|
|
481
|
-
}
|
|
482
|
-
nextPrecedence() {
|
|
483
|
-
const nextToken = this.peek();
|
|
484
|
-
if (!nextToken)
|
|
485
|
-
return Precedence.min;
|
|
486
|
-
const nextInfix = this.normal_rule.get(nextToken.type);
|
|
487
|
-
if (!nextInfix)
|
|
488
|
-
return Precedence.min;
|
|
489
|
-
return nextInfix.precedence;
|
|
490
|
-
}
|
|
491
|
-
errorStr(token) {
|
|
492
|
-
return `token ${token.str} of type ${token.type}`;
|
|
493
|
-
}
|
|
494
|
-
parse(expr) {
|
|
495
|
-
this.tokens = this.tokenize(expr);
|
|
496
|
-
//parse - single
|
|
497
|
-
// const res = this.next()
|
|
498
|
-
// if(this.peek()){
|
|
499
|
-
// console.warn(`Tree not exhausted`)
|
|
500
|
-
// }
|
|
501
|
-
// return res
|
|
502
|
-
//parse until exhausted
|
|
503
|
-
const res = [];
|
|
504
|
-
while (this.peek()) {
|
|
505
|
-
const n = this.next();
|
|
506
|
-
if (n)
|
|
507
|
-
res.push(n);
|
|
508
|
-
}
|
|
509
|
-
return res;
|
|
510
|
-
}
|
|
511
|
-
//when precence of an infix notation dros below base, we stop
|
|
512
|
-
next(basePrecedence = Precedence.min) {
|
|
513
|
-
let currToken = this.pop();
|
|
514
|
-
if (!currToken || currToken.type === TokenType.EOF)
|
|
515
|
-
return new ValueExpression(new Token(TokenType.EOF, ""));
|
|
516
|
-
const currPreParseRule = this.preparse_rule.get(currToken.type);
|
|
517
|
-
if (!currPreParseRule)
|
|
518
|
-
throw new Error(`Failed to parse, no parse rule for ${this.errorStr(currToken)}`);
|
|
519
|
-
let left = currPreParseRule.parse(this, currToken);
|
|
520
|
-
while (basePrecedence < this.nextPrecedence()) {
|
|
521
|
-
currToken = this.pop();
|
|
522
|
-
if (!currToken || currToken.type === TokenType.EOF)
|
|
523
|
-
return new ValueExpression(new Token(TokenType.EOF, ""));
|
|
524
|
-
let currNormalRule = this.normal_rule.get(currToken.type);
|
|
525
|
-
if (!currNormalRule)
|
|
526
|
-
break;
|
|
527
|
-
left = currNormalRule.parse(this, left, currToken);
|
|
528
|
-
}
|
|
529
|
-
return left;
|
|
530
|
-
}
|
|
531
|
-
bindVariables(num_var, string_var) {
|
|
532
|
-
this.num_var = num_var;
|
|
533
|
-
this.string_var = string_var;
|
|
534
|
-
}
|
|
535
|
-
evaluate_value(expr) {
|
|
536
|
-
if (expr.operator.type === TokenType.EOF)
|
|
537
|
-
return expr;
|
|
538
|
-
if (expr instanceof ParsedValue || expr instanceof ParsedValueWithImmutable)
|
|
539
|
-
return expr;
|
|
540
|
-
const r = (new evaluate_element(expr.operator.str, this.num_var, this.string_var));
|
|
541
|
-
const str = r.str;
|
|
542
|
-
const v = new ParsedValue(new Token(r.isNum ? TokenType.value_number : TokenType.value_string, str));
|
|
543
|
-
if (expr instanceof ValueWithImmutableAttachedExpression) {
|
|
544
|
-
return new ParsedValueWithImmutable(v, expr.immutable_token, expr.attachedAtEnd);
|
|
545
|
-
}
|
|
546
|
-
return v;
|
|
547
|
-
}
|
|
548
|
-
evaluate_unary(expr) {
|
|
549
|
-
const op = expr.operator;
|
|
550
|
-
const operand = expr.operands[0];
|
|
551
|
-
const type = operand.operator.type;
|
|
552
|
-
const str = operand.operator.str;
|
|
553
|
-
switch (op.type) {
|
|
554
|
-
case TokenType.immutable: {
|
|
555
|
-
return new ValueWithImmutableAttachedExpression(operand, op, false);
|
|
556
|
-
}
|
|
557
|
-
case TokenType.plus: return operand;
|
|
558
|
-
case TokenType.minus: {
|
|
559
|
-
return new ValueExpression(new Token(type, type === TokenType.value_string ? str : (str.startsWith("-") ? str.slice(1) : "-" + str)));
|
|
560
|
-
}
|
|
561
|
-
case TokenType.negation: {
|
|
562
|
-
return new ValueExpression(new Token(TokenType.value_number, (str === "0") ? "1" : "0"));
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
throw new Error(`Unexpected unary token type, got ${op.type}`);
|
|
566
|
-
}
|
|
567
|
-
evaluate_binary(expr) {
|
|
568
|
-
const op = expr.operator;
|
|
569
|
-
const left = expr.left.operator;
|
|
570
|
-
const right = expr.right.operator;
|
|
571
|
-
const l = left.type === TokenType.value_number ? Number(left.str) : left.str;
|
|
572
|
-
const r = right.type === TokenType.value_number ? Number(right.str) : right.str;
|
|
573
|
-
const val = evaluateOperation(op.str, l, r, this.num_var, this.string_var);
|
|
574
|
-
// console.log(`Parsing ${l} ${op.str} ${r} = ${val}`)
|
|
575
|
-
const res = new ValueExpression(new Token(typeof val === "number" ? TokenType.value_number : TokenType.value_string, String(val)));
|
|
576
|
-
return res;
|
|
577
|
-
}
|
|
578
|
-
evaluate_ternary(expr) {
|
|
579
|
-
const cond = expr.cond;
|
|
580
|
-
if (cond.operator.str == "0") {
|
|
581
|
-
//false
|
|
582
|
-
return expr.right;
|
|
583
|
-
}
|
|
584
|
-
else {
|
|
585
|
-
//true
|
|
586
|
-
return expr.left;
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
evaluate_postfix(expr) {
|
|
590
|
-
//only pst fix rn is immutable post fix
|
|
591
|
-
return new ValueWithImmutableAttachedExpression(expr.operands[0], expr.operator);
|
|
592
|
-
}
|
|
593
|
-
flattenExpressions(exprs) {
|
|
594
|
-
let res = [];
|
|
595
|
-
exprs.forEach(expr => {
|
|
596
|
-
if (expr instanceof ValueWithImmutableAttachedExpression) {
|
|
597
|
-
if (expr.attachedAtEnd) {
|
|
598
|
-
res.push(expr.operator.str);
|
|
599
|
-
res.push(expr.immutable_token);
|
|
600
|
-
}
|
|
601
|
-
else {
|
|
602
|
-
res.push(expr.immutable_token);
|
|
603
|
-
res.push(expr.operator.str);
|
|
604
|
-
}
|
|
605
|
-
return;
|
|
606
|
-
}
|
|
607
|
-
if (expr instanceof ValueExpression && expr.operator.type !== TokenType.EOF) {
|
|
608
|
-
if (expr.operands.length) {
|
|
609
|
-
res.push(...this.flattenExpressions(expr.operands));
|
|
610
|
-
return;
|
|
611
|
-
}
|
|
612
|
-
const str = expr.operator.str;
|
|
613
|
-
if (typeof res.at(-1) === "string") {
|
|
614
|
-
res[res.length - 1] += " " + str;
|
|
615
|
-
}
|
|
616
|
-
else {
|
|
617
|
-
res.push(str);
|
|
618
|
-
}
|
|
619
|
-
}
|
|
620
|
-
});
|
|
621
|
-
return res;
|
|
622
|
-
}
|
|
623
|
-
}
|
|
624
|
-
class Parse_rule_value {
|
|
625
|
-
parse(_, token) {
|
|
626
|
-
return new ValueExpression(token);
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
class Parse_rule_unary {
|
|
630
|
-
parse(parser, token) {
|
|
631
|
-
const right = parser.next(Precedence.prefix);
|
|
632
|
-
return new UnaryExpression(token, right);
|
|
633
|
-
}
|
|
634
|
-
}
|
|
635
|
-
class Parse_rule_bracket {
|
|
636
|
-
constructor(bracketEndToken = TokenType.bracket_end) {
|
|
637
|
-
this.bracketEndToken = bracketEndToken;
|
|
638
|
-
}
|
|
639
|
-
parse(parser, token) {
|
|
640
|
-
const right = parser.next(parser.nextPrecedence());
|
|
641
|
-
parser.assertNext(this.bracketEndToken);
|
|
642
|
-
return right;
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
class Parse_rule_binary {
|
|
646
|
-
parse(parser, left, token) {
|
|
647
|
-
const right = parser.next(this._precedence);
|
|
648
|
-
return new BinaryExpression(token, left, right);
|
|
649
|
-
}
|
|
650
|
-
constructor(_precedence) {
|
|
651
|
-
this._precedence = _precedence;
|
|
652
|
-
}
|
|
653
|
-
get precedence() { return this._precedence; }
|
|
654
|
-
}
|
|
655
|
-
class Parse_rule_optional {
|
|
656
|
-
parse(parser, cond, token) {
|
|
657
|
-
const left = parser.next(Precedence.min);
|
|
658
|
-
parser.assertNext(TokenType.colon);
|
|
659
|
-
const right = parser.next(Precedence.cond_right_associative);
|
|
660
|
-
return new TernaryExpression(token, cond, left, right);
|
|
661
|
-
}
|
|
662
|
-
get precedence() {
|
|
663
|
-
return Precedence.cond;
|
|
664
|
-
}
|
|
665
|
-
}
|
|
666
|
-
//treat immutable as postfix
|
|
667
|
-
class Parse_rule_immutable_postfix {
|
|
668
|
-
get precedence() {
|
|
669
|
-
return Precedence.slightly_less_than_arithmetic;
|
|
670
|
-
}
|
|
671
|
-
parse(parser, left, token) {
|
|
672
|
-
return new PostfixExpression(token, left);
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
class Parse_rule_immutable_prefix {
|
|
676
|
-
parse(parser, token) {
|
|
677
|
-
const right = parser.next(Precedence.slightly_less_than_arithmetic);
|
|
678
|
-
return new UnaryExpression(token, right);
|
|
679
|
-
}
|
|
680
|
-
}
|
|
681
|
-
/**
|
|
682
|
-
* Precedence is how far the rercursive call inside a rule is allowed to go
|
|
683
|
-
* the parser will keep consuming until the next precedence is lower than the starting value
|
|
684
|
-
* so if we pass in
|
|
685
|
-
* a) next(precedence of sum) -> everything lower than sum is consumed into the return result
|
|
686
|
-
* b) next(parser.nextPrecedence()) -> everything lower than the next expression's precedence is consumed
|
|
687
|
-
* */
|
|
688
|
-
var Precedence;
|
|
689
|
-
(function (Precedence) {
|
|
690
|
-
Precedence[Precedence["min"] = 0] = "min";
|
|
691
|
-
Precedence[Precedence["immutable"] = 1] = "immutable";
|
|
692
|
-
Precedence[Precedence["cond_right_associative"] = 2] = "cond_right_associative";
|
|
693
|
-
Precedence[Precedence["cond"] = 3] = "cond";
|
|
694
|
-
Precedence[Precedence["slightly_less_than_arithmetic"] = 4] = "slightly_less_than_arithmetic";
|
|
695
|
-
Precedence[Precedence["sum"] = 5] = "sum";
|
|
696
|
-
Precedence[Precedence["product"] = 6] = "product";
|
|
697
|
-
Precedence[Precedence["exponent"] = 7] = "exponent";
|
|
698
|
-
Precedence[Precedence["bit_manip"] = 8] = "bit_manip";
|
|
699
|
-
Precedence[Precedence["prefix"] = 9] = "prefix";
|
|
700
|
-
Precedence[Precedence["postfix"] = 10] = "postfix";
|
|
701
|
-
})(Precedence || (Precedence = {}));
|
|
702
|
-
const DefParser = new Parser();
|
|
703
|
-
DefParser.registerValueRule(TokenType.value_number);
|
|
704
|
-
DefParser.registerValueRule(TokenType.value_string);
|
|
705
|
-
DefParser.registerRule(true, TokenType.bracket_open, new Parse_rule_bracket());
|
|
706
|
-
DefParser.registerRule(true, TokenType.immutable, new Parse_rule_immutable_prefix());
|
|
707
|
-
DefParser.registerRule(false, TokenType.immutable, new Parse_rule_immutable_postfix());
|
|
708
|
-
DefParser.registerBinaryRule(TokenType.plus, Precedence.sum);
|
|
709
|
-
DefParser.registerBinaryRule(TokenType.minus, Precedence.sum);
|
|
710
|
-
DefParser.registerBinaryRule(TokenType.forced_concat, Precedence.sum);
|
|
711
|
-
DefParser.registerBinaryRule(TokenType.shift_left, Precedence.bit_manip);
|
|
712
|
-
DefParser.registerBinaryRule(TokenType.shift_right, Precedence.bit_manip);
|
|
713
|
-
DefParser.registerBinaryRule(TokenType.slice_or_round_to_precision, Precedence.bit_manip);
|
|
714
|
-
DefParser.registerUnaryRule(TokenType.negation);
|
|
715
|
-
DefParser.registerUnaryRule(TokenType.plus);
|
|
716
|
-
DefParser.registerUnaryRule(TokenType.minus);
|
|
717
|
-
DefParser.registerBinaryRule(TokenType.mul, Precedence.product);
|
|
718
|
-
DefParser.registerBinaryRule(TokenType.div, Precedence.product);
|
|
719
|
-
DefParser.registerBinaryRule(TokenType.or, Precedence.cond);
|
|
720
|
-
DefParser.registerBinaryRule(TokenType.and, Precedence.cond);
|
|
721
|
-
DefParser.registerBinaryRule(TokenType.equals, Precedence.cond);
|
|
722
|
-
DefParser.registerBinaryRule(TokenType.not_equals, Precedence.cond);
|
|
723
|
-
DefParser.registerBinaryRule(TokenType.lesser, Precedence.cond);
|
|
724
|
-
DefParser.registerBinaryRule(TokenType.greater, Precedence.cond);
|
|
725
|
-
DefParser.registerBinaryRule(TokenType.lesser_or_equal, Precedence.cond);
|
|
726
|
-
DefParser.registerBinaryRule(TokenType.greater_or_equal, Precedence.cond);
|
|
727
|
-
DefParser.registerRule(false, TokenType.question_mark, new Parse_rule_optional());
|
|
728
|
-
exports.default = DefParser;
|