greybel-interpreter 3.5.1 → 4.0.0
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/dist/byte-compiler/instruction.d.ts +129 -0
- package/dist/byte-compiler/instruction.js +60 -0
- package/dist/bytecode-generator.d.ts +61 -0
- package/dist/bytecode-generator.js +1165 -0
- package/dist/context.d.ts +20 -88
- package/dist/context.js +44 -273
- package/dist/index.d.ts +5 -29
- package/dist/index.js +14 -87
- package/dist/interpreter.d.ts +9 -11
- package/dist/interpreter.js +75 -87
- package/dist/types/function.d.ts +11 -25
- package/dist/types/function.js +41 -105
- package/dist/types/list.d.ts +4 -5
- package/dist/types/list.js +10 -52
- package/dist/types/map.d.ts +4 -5
- package/dist/types/map.js +15 -74
- package/dist/types/number.d.ts +4 -5
- package/dist/types/number.js +5 -12
- package/dist/types/string.d.ts +4 -5
- package/dist/types/string.js +6 -18
- package/dist/types/with-intrinsics.d.ts +4 -5
- package/dist/utils/error.d.ts +5 -5
- package/dist/utils/error.js +4 -5
- package/dist/utils/stack.d.ts +10 -0
- package/dist/utils/stack.js +30 -0
- package/dist/vm/evaluation.d.ts +26 -0
- package/dist/vm/evaluation.js +341 -0
- package/dist/vm.d.ts +66 -0
- package/dist/vm.js +662 -0
- package/package.json +1 -1
- package/dist/cps.d.ts +0 -16
- package/dist/cps.js +0 -202
- package/dist/operations/assign-globals.d.ts +0 -11
- package/dist/operations/assign-globals.js +0 -31
- package/dist/operations/assign-locals.d.ts +0 -11
- package/dist/operations/assign-locals.js +0 -31
- package/dist/operations/assign-outer.d.ts +0 -11
- package/dist/operations/assign-outer.js +0 -31
- package/dist/operations/assign-self.d.ts +0 -11
- package/dist/operations/assign-self.js +0 -41
- package/dist/operations/assign.d.ts +0 -13
- package/dist/operations/assign.js +0 -60
- package/dist/operations/block.d.ts +0 -13
- package/dist/operations/block.js +0 -48
- package/dist/operations/break.d.ts +0 -10
- package/dist/operations/break.js +0 -21
- package/dist/operations/call.d.ts +0 -13
- package/dist/operations/call.js +0 -64
- package/dist/operations/chunk.d.ts +0 -12
- package/dist/operations/chunk.js +0 -31
- package/dist/operations/continue.d.ts +0 -10
- package/dist/operations/continue.js +0 -21
- package/dist/operations/debugger-statement.d.ts +0 -10
- package/dist/operations/debugger-statement.js +0 -19
- package/dist/operations/envar.d.ts +0 -10
- package/dist/operations/envar.js +0 -21
- package/dist/operations/evaluate.d.ts +0 -45
- package/dist/operations/evaluate.js +0 -369
- package/dist/operations/for.d.ts +0 -14
- package/dist/operations/for.js +0 -87
- package/dist/operations/function-reference.d.ts +0 -12
- package/dist/operations/function-reference.js +0 -48
- package/dist/operations/function.d.ts +0 -19
- package/dist/operations/function.js +0 -84
- package/dist/operations/if-statement.d.ts +0 -19
- package/dist/operations/if-statement.js +0 -87
- package/dist/operations/import.d.ts +0 -20
- package/dist/operations/import.js +0 -56
- package/dist/operations/include.d.ts +0 -14
- package/dist/operations/include.js +0 -40
- package/dist/operations/list.d.ts +0 -11
- package/dist/operations/list.js +0 -40
- package/dist/operations/literal.d.ts +0 -11
- package/dist/operations/literal.js +0 -38
- package/dist/operations/map.d.ts +0 -11
- package/dist/operations/map.js +0 -42
- package/dist/operations/negated-binary.d.ts +0 -11
- package/dist/operations/negated-binary.js +0 -40
- package/dist/operations/new-instance.d.ts +0 -11
- package/dist/operations/new-instance.js +0 -37
- package/dist/operations/noop.d.ts +0 -7
- package/dist/operations/noop.js +0 -14
- package/dist/operations/not.d.ts +0 -11
- package/dist/operations/not.js +0 -33
- package/dist/operations/operation.d.ts +0 -15
- package/dist/operations/operation.js +0 -13
- package/dist/operations/reference-globals.d.ts +0 -7
- package/dist/operations/reference-globals.js +0 -13
- package/dist/operations/reference-locals.d.ts +0 -7
- package/dist/operations/reference-locals.js +0 -13
- package/dist/operations/reference-outer.d.ts +0 -7
- package/dist/operations/reference-outer.js +0 -13
- package/dist/operations/reference-self.d.ts +0 -7
- package/dist/operations/reference-self.js +0 -13
- package/dist/operations/reference.d.ts +0 -9
- package/dist/operations/reference.js +0 -17
- package/dist/operations/resolve-globals.d.ts +0 -5
- package/dist/operations/resolve-globals.js +0 -24
- package/dist/operations/resolve-locals.d.ts +0 -5
- package/dist/operations/resolve-locals.js +0 -24
- package/dist/operations/resolve-outer.d.ts +0 -5
- package/dist/operations/resolve-outer.js +0 -24
- package/dist/operations/resolve-self.d.ts +0 -5
- package/dist/operations/resolve-self.js +0 -24
- package/dist/operations/resolve.d.ts +0 -55
- package/dist/operations/resolve.js +0 -269
- package/dist/operations/return.d.ts +0 -11
- package/dist/operations/return.js +0 -43
- package/dist/operations/while.d.ts +0 -13
- package/dist/operations/while.js +0 -70
- package/dist/utils/create-assign.d.ts +0 -3
- package/dist/utils/create-assign.js +0 -25
- package/dist/utils/create-resolve.d.ts +0 -5
- package/dist/utils/create-resolve.js +0 -51
- package/dist/utils/path.d.ts +0 -10
- package/dist/utils/path.js +0 -33
package/dist/utils/error.js
CHANGED
|
@@ -2,19 +2,18 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PrepareError = exports.RuntimeError = void 0;
|
|
4
4
|
class RuntimeError extends Error {
|
|
5
|
-
constructor(message,
|
|
5
|
+
constructor(message, vm, source) {
|
|
6
6
|
var _a;
|
|
7
7
|
super(message);
|
|
8
|
-
this.target =
|
|
9
|
-
this.stackTrace = (_a =
|
|
8
|
+
this.target = vm === null || vm === void 0 ? void 0 : vm.target;
|
|
9
|
+
this.stackTrace = (_a = vm === null || vm === void 0 ? void 0 : vm.getStacktrace()) !== null && _a !== void 0 ? _a : [];
|
|
10
10
|
this.stack = this.createTrace();
|
|
11
11
|
this.source = source;
|
|
12
12
|
}
|
|
13
13
|
createTrace() {
|
|
14
14
|
return this.stackTrace
|
|
15
15
|
.map((op) => {
|
|
16
|
-
|
|
17
|
-
return `at ${op.target}:${(_b = (_a = op.item) === null || _a === void 0 ? void 0 : _a.start.line) !== null && _b !== void 0 ? _b : 0}:${(_d = (_c = op.item) === null || _c === void 0 ? void 0 : _c.start.character) !== null && _d !== void 0 ? _d : 0}`;
|
|
16
|
+
return `at ${op.source.path}:${op.source.start.line}:${op.source.start.character}`;
|
|
18
17
|
})
|
|
19
18
|
.join('\n');
|
|
20
19
|
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Stack = void 0;
|
|
4
|
+
class Stack {
|
|
5
|
+
constructor(value) {
|
|
6
|
+
this.stack = [];
|
|
7
|
+
if (value != null)
|
|
8
|
+
this.push(value);
|
|
9
|
+
}
|
|
10
|
+
push(value) {
|
|
11
|
+
const len = this.stack.push(value);
|
|
12
|
+
this.last = value;
|
|
13
|
+
return len;
|
|
14
|
+
}
|
|
15
|
+
pop() {
|
|
16
|
+
const ret = this.stack.pop();
|
|
17
|
+
this.last = this.stack[this.stack.length - 1];
|
|
18
|
+
return ret;
|
|
19
|
+
}
|
|
20
|
+
peek() {
|
|
21
|
+
return this.last;
|
|
22
|
+
}
|
|
23
|
+
includes(value) {
|
|
24
|
+
return this.stack.includes(value);
|
|
25
|
+
}
|
|
26
|
+
values() {
|
|
27
|
+
return this.stack.slice(0);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.Stack = Stack;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { CustomValue } from '../types/base';
|
|
2
|
+
import { CustomBoolean } from '../types/boolean';
|
|
3
|
+
import { CustomList } from '../types/list';
|
|
4
|
+
import { CustomMap } from '../types/map';
|
|
5
|
+
import { CustomNil } from '../types/nil';
|
|
6
|
+
import { CustomNumber } from '../types/number';
|
|
7
|
+
import { CustomString } from '../types/string';
|
|
8
|
+
export declare function evalAdd(a: CustomValue, b: CustomValue): CustomNil | CustomNumber | CustomString | CustomMap | CustomList;
|
|
9
|
+
export declare function evalSub(a: CustomValue, b: CustomValue): CustomValue | CustomNumber;
|
|
10
|
+
export declare function evalMul(a: CustomValue, b: CustomValue): CustomValue | CustomNumber;
|
|
11
|
+
export declare function evalDiv(a: CustomValue, b: CustomValue): CustomValue | CustomNumber;
|
|
12
|
+
export declare function evalBitwiseOr(a: CustomValue, b: CustomValue): CustomNil | CustomNumber;
|
|
13
|
+
export declare function evalBitwiseAnd(a: CustomValue, b: CustomValue): CustomNil | CustomNumber;
|
|
14
|
+
export declare function evalBitwiseRightShift(a: CustomValue, b: CustomValue): CustomNil | CustomNumber;
|
|
15
|
+
export declare function evalBitwiseUnsignedRightShift(a: CustomValue, b: CustomValue): CustomNil | CustomNumber;
|
|
16
|
+
export declare function evalBitwiseLeftShift(a: CustomValue, b: CustomValue): CustomNil | CustomNumber;
|
|
17
|
+
export declare function evalLessThan(a: CustomValue, b: CustomValue): CustomNil | CustomBoolean;
|
|
18
|
+
export declare function evalLessThanOrEqual(a: CustomValue, b: CustomValue): CustomNil | CustomBoolean;
|
|
19
|
+
export declare function evalGreaterThan(a: CustomValue, b: CustomValue): CustomNil | CustomBoolean;
|
|
20
|
+
export declare function evalGreaterThanOrEqual(a: CustomValue, b: CustomValue): CustomNil | CustomBoolean;
|
|
21
|
+
export declare function evalEqual(a: CustomValue, b: CustomValue): CustomBoolean;
|
|
22
|
+
export declare function evalNotEqual(a: CustomValue, b: CustomValue): CustomBoolean;
|
|
23
|
+
export declare function evalMod(a: CustomValue, b: CustomValue): CustomNil | CustomNumber;
|
|
24
|
+
export declare function evalPow(a: CustomValue, b: CustomValue): CustomNil | CustomNumber;
|
|
25
|
+
export declare function evalAnd(a: CustomValue, b: CustomValue): CustomNumber;
|
|
26
|
+
export declare function evalOr(a: CustomValue, b: CustomValue): CustomNumber;
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.evalOr = exports.evalAnd = exports.evalPow = exports.evalMod = exports.evalNotEqual = exports.evalEqual = exports.evalGreaterThanOrEqual = exports.evalGreaterThan = exports.evalLessThanOrEqual = exports.evalLessThan = exports.evalBitwiseLeftShift = exports.evalBitwiseUnsignedRightShift = exports.evalBitwiseRightShift = exports.evalBitwiseAnd = exports.evalBitwiseOr = exports.evalDiv = exports.evalMul = exports.evalSub = exports.evalAdd = void 0;
|
|
4
|
+
const boolean_1 = require("../types/boolean");
|
|
5
|
+
const default_1 = require("../types/default");
|
|
6
|
+
const function_1 = require("../types/function");
|
|
7
|
+
const list_1 = require("../types/list");
|
|
8
|
+
const map_1 = require("../types/map");
|
|
9
|
+
const nil_1 = require("../types/nil");
|
|
10
|
+
const number_1 = require("../types/number");
|
|
11
|
+
const string_1 = require("../types/string");
|
|
12
|
+
const deep_equal_1 = require("../utils/deep-equal");
|
|
13
|
+
const absClamp01 = (d) => {
|
|
14
|
+
if (d < 0)
|
|
15
|
+
d = -d;
|
|
16
|
+
if (d > 1)
|
|
17
|
+
return 1;
|
|
18
|
+
return d;
|
|
19
|
+
};
|
|
20
|
+
function evalAdd(a, b) {
|
|
21
|
+
if (a instanceof string_1.CustomString || b instanceof string_1.CustomString) {
|
|
22
|
+
if (a instanceof nil_1.CustomNil)
|
|
23
|
+
a = new string_1.CustomString('');
|
|
24
|
+
if (b instanceof nil_1.CustomNil)
|
|
25
|
+
b = new string_1.CustomString('');
|
|
26
|
+
return new string_1.CustomString(a.toString() + b.toString());
|
|
27
|
+
}
|
|
28
|
+
else if (a instanceof number_1.CustomNumber) {
|
|
29
|
+
if (b instanceof nil_1.CustomNil)
|
|
30
|
+
b = new number_1.CustomNumber(0);
|
|
31
|
+
if (b instanceof number_1.CustomNumber)
|
|
32
|
+
return new number_1.CustomNumber(a.toNumber() + b.toNumber());
|
|
33
|
+
}
|
|
34
|
+
else if (a instanceof list_1.CustomList) {
|
|
35
|
+
if (b instanceof list_1.CustomList) {
|
|
36
|
+
return a.fork().extend(b);
|
|
37
|
+
}
|
|
38
|
+
return default_1.DefaultType.Void;
|
|
39
|
+
}
|
|
40
|
+
else if (a instanceof map_1.CustomMap) {
|
|
41
|
+
if (b instanceof map_1.CustomMap) {
|
|
42
|
+
return a.fork().extend(b);
|
|
43
|
+
}
|
|
44
|
+
return default_1.DefaultType.Void;
|
|
45
|
+
}
|
|
46
|
+
return default_1.DefaultType.Void;
|
|
47
|
+
}
|
|
48
|
+
exports.evalAdd = evalAdd;
|
|
49
|
+
const minusString = (a, b) => {
|
|
50
|
+
const origin = a.toString();
|
|
51
|
+
const toRemove = b.toString();
|
|
52
|
+
if (origin.endsWith(toRemove)) {
|
|
53
|
+
return new string_1.CustomString(origin.substring(0, origin.length - toRemove.length));
|
|
54
|
+
}
|
|
55
|
+
return new string_1.CustomString(origin);
|
|
56
|
+
};
|
|
57
|
+
function evalSub(a, b) {
|
|
58
|
+
if (a instanceof string_1.CustomString) {
|
|
59
|
+
if (b instanceof nil_1.CustomNil)
|
|
60
|
+
b = new string_1.CustomString('');
|
|
61
|
+
return minusString(a, b);
|
|
62
|
+
}
|
|
63
|
+
else if (a instanceof number_1.CustomNumber) {
|
|
64
|
+
if (b instanceof nil_1.CustomNil)
|
|
65
|
+
b = new number_1.CustomNumber(0);
|
|
66
|
+
if (b instanceof number_1.CustomNumber)
|
|
67
|
+
return new number_1.CustomNumber(a.toNumber() - b.toNumber());
|
|
68
|
+
}
|
|
69
|
+
return default_1.DefaultType.Void;
|
|
70
|
+
}
|
|
71
|
+
exports.evalSub = evalSub;
|
|
72
|
+
const multiplyString = (a, b) => {
|
|
73
|
+
const factor = b.toNumber();
|
|
74
|
+
if (factor <= 0) {
|
|
75
|
+
return new string_1.CustomString('');
|
|
76
|
+
}
|
|
77
|
+
let newString = '';
|
|
78
|
+
const max = Math.floor(a.value.length * factor);
|
|
79
|
+
for (let index = 0; index < max; index++) {
|
|
80
|
+
newString += a.value[index % a.value.length];
|
|
81
|
+
}
|
|
82
|
+
return new string_1.CustomString(newString);
|
|
83
|
+
};
|
|
84
|
+
const multiplyList = (a, b) => {
|
|
85
|
+
const factor = b.toNumber();
|
|
86
|
+
if (factor <= 0) {
|
|
87
|
+
return new list_1.CustomList();
|
|
88
|
+
}
|
|
89
|
+
const newListValue = [];
|
|
90
|
+
const max = Math.floor(a.value.length * factor);
|
|
91
|
+
for (let index = 0; index < max; index++) {
|
|
92
|
+
newListValue.push(a.value[index % a.value.length]);
|
|
93
|
+
}
|
|
94
|
+
return new list_1.CustomList(newListValue);
|
|
95
|
+
};
|
|
96
|
+
function evalMul(a, b) {
|
|
97
|
+
if (a instanceof string_1.CustomString) {
|
|
98
|
+
if (b instanceof nil_1.CustomNil)
|
|
99
|
+
b = new string_1.CustomString('');
|
|
100
|
+
return multiplyString(a, b);
|
|
101
|
+
}
|
|
102
|
+
else if (a instanceof number_1.CustomNumber) {
|
|
103
|
+
if (b instanceof nil_1.CustomNil)
|
|
104
|
+
b = new number_1.CustomNumber(0);
|
|
105
|
+
if (b instanceof number_1.CustomNumber)
|
|
106
|
+
return new number_1.CustomNumber(a.toNumber() * b.toNumber());
|
|
107
|
+
}
|
|
108
|
+
else if (a instanceof list_1.CustomList && b instanceof number_1.CustomNumber) {
|
|
109
|
+
return multiplyList(a, b);
|
|
110
|
+
}
|
|
111
|
+
return default_1.DefaultType.Void;
|
|
112
|
+
}
|
|
113
|
+
exports.evalMul = evalMul;
|
|
114
|
+
const divideString = (a, b) => {
|
|
115
|
+
const factor = 1 / b.toNumber();
|
|
116
|
+
if (factor <= 0) {
|
|
117
|
+
return new string_1.CustomString('');
|
|
118
|
+
}
|
|
119
|
+
let newString = '';
|
|
120
|
+
const max = Math.floor(a.value.length * factor);
|
|
121
|
+
for (let index = 0; index < max; index++) {
|
|
122
|
+
newString += a.value[index % a.value.length];
|
|
123
|
+
}
|
|
124
|
+
return new string_1.CustomString(newString);
|
|
125
|
+
};
|
|
126
|
+
const divideList = (a, b) => {
|
|
127
|
+
const factor = 1 / b.toNumber();
|
|
128
|
+
if (factor <= 0) {
|
|
129
|
+
return new list_1.CustomList();
|
|
130
|
+
}
|
|
131
|
+
const newListValue = [];
|
|
132
|
+
const max = Math.floor(a.value.length * factor);
|
|
133
|
+
for (let index = 0; index < max; index++) {
|
|
134
|
+
newListValue.push(a.value[index % a.value.length]);
|
|
135
|
+
}
|
|
136
|
+
return new list_1.CustomList(newListValue);
|
|
137
|
+
};
|
|
138
|
+
function evalDiv(a, b) {
|
|
139
|
+
if (a instanceof string_1.CustomString) {
|
|
140
|
+
if (b instanceof nil_1.CustomNil)
|
|
141
|
+
b = new string_1.CustomString('');
|
|
142
|
+
return divideString(a, b);
|
|
143
|
+
}
|
|
144
|
+
else if (a instanceof number_1.CustomNumber) {
|
|
145
|
+
if (b instanceof nil_1.CustomNil)
|
|
146
|
+
b = new number_1.CustomNumber(0);
|
|
147
|
+
if (b instanceof number_1.CustomNumber)
|
|
148
|
+
return new number_1.CustomNumber(a.toNumber() / b.toNumber());
|
|
149
|
+
}
|
|
150
|
+
else if (a instanceof list_1.CustomList && b instanceof number_1.CustomNumber) {
|
|
151
|
+
return divideList(a, b);
|
|
152
|
+
}
|
|
153
|
+
return default_1.DefaultType.Void;
|
|
154
|
+
}
|
|
155
|
+
exports.evalDiv = evalDiv;
|
|
156
|
+
function evalBitwiseOr(a, b) {
|
|
157
|
+
if (a instanceof number_1.CustomNumber) {
|
|
158
|
+
if (b instanceof nil_1.CustomNil)
|
|
159
|
+
b = new number_1.CustomNumber(0);
|
|
160
|
+
if (b instanceof number_1.CustomNumber)
|
|
161
|
+
return new number_1.CustomNumber(a.toNumber() | b.toNumber());
|
|
162
|
+
}
|
|
163
|
+
return default_1.DefaultType.Void;
|
|
164
|
+
}
|
|
165
|
+
exports.evalBitwiseOr = evalBitwiseOr;
|
|
166
|
+
function evalBitwiseAnd(a, b) {
|
|
167
|
+
if (a instanceof number_1.CustomNumber) {
|
|
168
|
+
if (b instanceof nil_1.CustomNil)
|
|
169
|
+
b = new number_1.CustomNumber(0);
|
|
170
|
+
if (b instanceof number_1.CustomNumber)
|
|
171
|
+
return new number_1.CustomNumber(a.toNumber() & b.toNumber());
|
|
172
|
+
}
|
|
173
|
+
return default_1.DefaultType.Void;
|
|
174
|
+
}
|
|
175
|
+
exports.evalBitwiseAnd = evalBitwiseAnd;
|
|
176
|
+
function evalBitwiseRightShift(a, b) {
|
|
177
|
+
if (a instanceof number_1.CustomNumber) {
|
|
178
|
+
if (b instanceof nil_1.CustomNil)
|
|
179
|
+
b = new number_1.CustomNumber(0);
|
|
180
|
+
if (b instanceof number_1.CustomNumber)
|
|
181
|
+
return new number_1.CustomNumber(a.toNumber() >> b.toNumber());
|
|
182
|
+
}
|
|
183
|
+
return default_1.DefaultType.Void;
|
|
184
|
+
}
|
|
185
|
+
exports.evalBitwiseRightShift = evalBitwiseRightShift;
|
|
186
|
+
function evalBitwiseUnsignedRightShift(a, b) {
|
|
187
|
+
if (a instanceof number_1.CustomNumber) {
|
|
188
|
+
if (b instanceof nil_1.CustomNil)
|
|
189
|
+
b = new number_1.CustomNumber(0);
|
|
190
|
+
if (b instanceof number_1.CustomNumber)
|
|
191
|
+
return new number_1.CustomNumber(a.toNumber() >>> b.toNumber());
|
|
192
|
+
}
|
|
193
|
+
return default_1.DefaultType.Void;
|
|
194
|
+
}
|
|
195
|
+
exports.evalBitwiseUnsignedRightShift = evalBitwiseUnsignedRightShift;
|
|
196
|
+
function evalBitwiseLeftShift(a, b) {
|
|
197
|
+
if (a instanceof number_1.CustomNumber) {
|
|
198
|
+
if (b instanceof nil_1.CustomNil)
|
|
199
|
+
b = new number_1.CustomNumber(0);
|
|
200
|
+
if (b instanceof number_1.CustomNumber)
|
|
201
|
+
return new number_1.CustomNumber(a.toNumber() << b.toNumber());
|
|
202
|
+
}
|
|
203
|
+
return default_1.DefaultType.Void;
|
|
204
|
+
}
|
|
205
|
+
exports.evalBitwiseLeftShift = evalBitwiseLeftShift;
|
|
206
|
+
function evalLessThan(a, b) {
|
|
207
|
+
if (a instanceof number_1.CustomNumber) {
|
|
208
|
+
if (b instanceof nil_1.CustomNil)
|
|
209
|
+
b = new number_1.CustomNumber(0);
|
|
210
|
+
if (b instanceof number_1.CustomNumber)
|
|
211
|
+
return new boolean_1.CustomBoolean(a.toNumber() < b.toNumber());
|
|
212
|
+
}
|
|
213
|
+
else if (a instanceof string_1.CustomString) {
|
|
214
|
+
if (b instanceof nil_1.CustomNil)
|
|
215
|
+
b = new string_1.CustomString('');
|
|
216
|
+
return new boolean_1.CustomBoolean(a.toString() < b.toString());
|
|
217
|
+
}
|
|
218
|
+
return default_1.DefaultType.Void;
|
|
219
|
+
}
|
|
220
|
+
exports.evalLessThan = evalLessThan;
|
|
221
|
+
function evalLessThanOrEqual(a, b) {
|
|
222
|
+
if (a instanceof number_1.CustomNumber) {
|
|
223
|
+
if (b instanceof nil_1.CustomNil)
|
|
224
|
+
b = new number_1.CustomNumber(0);
|
|
225
|
+
if (b instanceof number_1.CustomNumber)
|
|
226
|
+
return new boolean_1.CustomBoolean(a.toNumber() <= b.toNumber());
|
|
227
|
+
}
|
|
228
|
+
else if (a instanceof string_1.CustomString) {
|
|
229
|
+
if (b instanceof nil_1.CustomNil)
|
|
230
|
+
b = new string_1.CustomString('');
|
|
231
|
+
return new boolean_1.CustomBoolean(a.toString() <= b.toString());
|
|
232
|
+
}
|
|
233
|
+
return default_1.DefaultType.Void;
|
|
234
|
+
}
|
|
235
|
+
exports.evalLessThanOrEqual = evalLessThanOrEqual;
|
|
236
|
+
function evalGreaterThan(a, b) {
|
|
237
|
+
if (a instanceof number_1.CustomNumber) {
|
|
238
|
+
if (b instanceof nil_1.CustomNil)
|
|
239
|
+
b = new number_1.CustomNumber(0);
|
|
240
|
+
if (b instanceof number_1.CustomNumber)
|
|
241
|
+
return new boolean_1.CustomBoolean(a.toNumber() > b.toNumber());
|
|
242
|
+
}
|
|
243
|
+
else if (a instanceof string_1.CustomString) {
|
|
244
|
+
if (b instanceof nil_1.CustomNil)
|
|
245
|
+
b = new string_1.CustomString('');
|
|
246
|
+
return new boolean_1.CustomBoolean(a.toString() > b.toString());
|
|
247
|
+
}
|
|
248
|
+
return default_1.DefaultType.Void;
|
|
249
|
+
}
|
|
250
|
+
exports.evalGreaterThan = evalGreaterThan;
|
|
251
|
+
function evalGreaterThanOrEqual(a, b) {
|
|
252
|
+
if (a instanceof number_1.CustomNumber) {
|
|
253
|
+
if (b instanceof nil_1.CustomNil)
|
|
254
|
+
b = new number_1.CustomNumber(0);
|
|
255
|
+
if (b instanceof number_1.CustomNumber)
|
|
256
|
+
return new boolean_1.CustomBoolean(a.toNumber() >= b.toNumber());
|
|
257
|
+
}
|
|
258
|
+
else if (a instanceof string_1.CustomString && b instanceof string_1.CustomString) {
|
|
259
|
+
if (b instanceof nil_1.CustomNil)
|
|
260
|
+
b = new string_1.CustomString('');
|
|
261
|
+
return new boolean_1.CustomBoolean(a.toString() >= b.toString());
|
|
262
|
+
}
|
|
263
|
+
return default_1.DefaultType.Void;
|
|
264
|
+
}
|
|
265
|
+
exports.evalGreaterThanOrEqual = evalGreaterThanOrEqual;
|
|
266
|
+
function evalEqual(a, b) {
|
|
267
|
+
if (a instanceof number_1.CustomNumber) {
|
|
268
|
+
return new boolean_1.CustomBoolean(a.value === b.value);
|
|
269
|
+
}
|
|
270
|
+
else if (a instanceof string_1.CustomString) {
|
|
271
|
+
return new boolean_1.CustomBoolean(a.value === b.value);
|
|
272
|
+
}
|
|
273
|
+
else if (a instanceof list_1.CustomList && b instanceof list_1.CustomList) {
|
|
274
|
+
return new boolean_1.CustomBoolean((0, deep_equal_1.deepEqual)(a, b));
|
|
275
|
+
}
|
|
276
|
+
else if (a instanceof map_1.CustomMap && b instanceof map_1.CustomMap) {
|
|
277
|
+
return new boolean_1.CustomBoolean((0, deep_equal_1.deepEqual)(a, b));
|
|
278
|
+
}
|
|
279
|
+
else if (a instanceof function_1.CustomFunction) {
|
|
280
|
+
return new boolean_1.CustomBoolean(a === b);
|
|
281
|
+
}
|
|
282
|
+
else if (a instanceof nil_1.CustomNil) {
|
|
283
|
+
return new boolean_1.CustomBoolean(b instanceof nil_1.CustomNil);
|
|
284
|
+
}
|
|
285
|
+
return default_1.DefaultType.False;
|
|
286
|
+
}
|
|
287
|
+
exports.evalEqual = evalEqual;
|
|
288
|
+
function evalNotEqual(a, b) {
|
|
289
|
+
if (a instanceof number_1.CustomNumber) {
|
|
290
|
+
return new boolean_1.CustomBoolean(a.value !== b.value);
|
|
291
|
+
}
|
|
292
|
+
else if (a instanceof string_1.CustomString) {
|
|
293
|
+
return new boolean_1.CustomBoolean(a.value !== b.value);
|
|
294
|
+
}
|
|
295
|
+
else if (a instanceof list_1.CustomList && b instanceof list_1.CustomList) {
|
|
296
|
+
return new boolean_1.CustomBoolean(!(0, deep_equal_1.deepEqual)(a, b));
|
|
297
|
+
}
|
|
298
|
+
else if (a instanceof map_1.CustomMap && b instanceof map_1.CustomMap) {
|
|
299
|
+
return new boolean_1.CustomBoolean(!(0, deep_equal_1.deepEqual)(a, b));
|
|
300
|
+
}
|
|
301
|
+
else if (a instanceof function_1.CustomFunction) {
|
|
302
|
+
return new boolean_1.CustomBoolean(a !== b);
|
|
303
|
+
}
|
|
304
|
+
else if (a instanceof nil_1.CustomNil) {
|
|
305
|
+
return new boolean_1.CustomBoolean(!(b instanceof nil_1.CustomNil));
|
|
306
|
+
}
|
|
307
|
+
return default_1.DefaultType.False;
|
|
308
|
+
}
|
|
309
|
+
exports.evalNotEqual = evalNotEqual;
|
|
310
|
+
function evalMod(a, b) {
|
|
311
|
+
if (a instanceof number_1.CustomNumber) {
|
|
312
|
+
if (b instanceof nil_1.CustomNil)
|
|
313
|
+
b = new number_1.CustomNumber(0);
|
|
314
|
+
if (b instanceof number_1.CustomNumber)
|
|
315
|
+
return new number_1.CustomNumber(a.toNumber() % b.toNumber());
|
|
316
|
+
}
|
|
317
|
+
return default_1.DefaultType.Void;
|
|
318
|
+
}
|
|
319
|
+
exports.evalMod = evalMod;
|
|
320
|
+
function evalPow(a, b) {
|
|
321
|
+
if (a instanceof number_1.CustomNumber) {
|
|
322
|
+
if (b instanceof nil_1.CustomNil)
|
|
323
|
+
b = new number_1.CustomNumber(0);
|
|
324
|
+
if (b instanceof number_1.CustomNumber)
|
|
325
|
+
return new number_1.CustomNumber(Math.pow(a.toNumber(), b.toNumber()));
|
|
326
|
+
}
|
|
327
|
+
return default_1.DefaultType.Void;
|
|
328
|
+
}
|
|
329
|
+
exports.evalPow = evalPow;
|
|
330
|
+
function evalAnd(a, b) {
|
|
331
|
+
const left = Number(a instanceof number_1.CustomNumber ? a.toNumber() : a.toTruthy());
|
|
332
|
+
const right = Number(b instanceof number_1.CustomNumber ? b.toNumber() : b.toTruthy());
|
|
333
|
+
return new number_1.CustomNumber(absClamp01(left * right));
|
|
334
|
+
}
|
|
335
|
+
exports.evalAnd = evalAnd;
|
|
336
|
+
function evalOr(a, b) {
|
|
337
|
+
const left = Number(a instanceof number_1.CustomNumber ? a.toNumber() : a.toTruthy());
|
|
338
|
+
const right = Number(b instanceof number_1.CustomNumber ? b.toNumber() : b.toTruthy());
|
|
339
|
+
return new number_1.CustomNumber(absClamp01(left + right - left * right));
|
|
340
|
+
}
|
|
341
|
+
exports.evalOr = evalOr;
|
package/dist/vm.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { OperationContext } from "./context";
|
|
2
|
+
import { ContextTypeIntrinsics } from "./context/types";
|
|
3
|
+
import { HandlerContainer } from "./handler-container";
|
|
4
|
+
import { CustomValue } from "./types/base";
|
|
5
|
+
import { Instruction, SourceLocation } from "./byte-compiler/instruction";
|
|
6
|
+
export declare class Debugger {
|
|
7
|
+
private breakpoint;
|
|
8
|
+
private nextStep;
|
|
9
|
+
private vm;
|
|
10
|
+
debug(...segments: any[]): CustomValue;
|
|
11
|
+
setBreakpoint(breakpoint: boolean): Debugger;
|
|
12
|
+
getBreakpoint(vm: VM): boolean;
|
|
13
|
+
next(): Debugger;
|
|
14
|
+
resume(): Promise<void>;
|
|
15
|
+
interact(vm: VM, _source: SourceLocation): void;
|
|
16
|
+
}
|
|
17
|
+
export interface FrameOptions {
|
|
18
|
+
code: Instruction[];
|
|
19
|
+
self?: CustomValue;
|
|
20
|
+
super?: CustomValue;
|
|
21
|
+
outer?: OperationContext;
|
|
22
|
+
}
|
|
23
|
+
export declare enum VMState {
|
|
24
|
+
PREPARATION = 0,
|
|
25
|
+
PENDING = 1,
|
|
26
|
+
FINISHED = 2
|
|
27
|
+
}
|
|
28
|
+
export interface VMOptions {
|
|
29
|
+
target: string;
|
|
30
|
+
contextTypeIntrinsics: ContextTypeIntrinsics;
|
|
31
|
+
globals: OperationContext;
|
|
32
|
+
handler: HandlerContainer;
|
|
33
|
+
debugger: Debugger;
|
|
34
|
+
environmentVariables?: Map<string, string>;
|
|
35
|
+
imports?: Map<string, Instruction[]>;
|
|
36
|
+
}
|
|
37
|
+
export declare class VM {
|
|
38
|
+
private readonly ACTIONS_PER_LOOP;
|
|
39
|
+
private actionCount;
|
|
40
|
+
private state;
|
|
41
|
+
private frames;
|
|
42
|
+
private signal;
|
|
43
|
+
private readonly STACK_LIMIT;
|
|
44
|
+
private sp;
|
|
45
|
+
private stack;
|
|
46
|
+
private iterators;
|
|
47
|
+
private time;
|
|
48
|
+
readonly target: string;
|
|
49
|
+
readonly contextTypeIntrinsics: ContextTypeIntrinsics;
|
|
50
|
+
readonly environmentVariables: Map<string, string>;
|
|
51
|
+
readonly handler: HandlerContainer;
|
|
52
|
+
readonly debugger: Debugger;
|
|
53
|
+
readonly imports: Map<string, Instruction[]>;
|
|
54
|
+
constructor(options: VMOptions);
|
|
55
|
+
getTime(): number;
|
|
56
|
+
isPending(): boolean;
|
|
57
|
+
getStacktrace(): Instruction[];
|
|
58
|
+
getOpenHandles(): number;
|
|
59
|
+
exit(): void;
|
|
60
|
+
getFrame(): OperationContext;
|
|
61
|
+
private pushStack;
|
|
62
|
+
private popStack;
|
|
63
|
+
private createFrame;
|
|
64
|
+
private popFrame;
|
|
65
|
+
exec(): Promise<void>;
|
|
66
|
+
}
|