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.
Files changed (116) hide show
  1. package/dist/byte-compiler/instruction.d.ts +129 -0
  2. package/dist/byte-compiler/instruction.js +60 -0
  3. package/dist/bytecode-generator.d.ts +61 -0
  4. package/dist/bytecode-generator.js +1165 -0
  5. package/dist/context.d.ts +20 -88
  6. package/dist/context.js +44 -273
  7. package/dist/index.d.ts +5 -29
  8. package/dist/index.js +14 -87
  9. package/dist/interpreter.d.ts +9 -11
  10. package/dist/interpreter.js +75 -87
  11. package/dist/types/function.d.ts +11 -25
  12. package/dist/types/function.js +41 -105
  13. package/dist/types/list.d.ts +4 -5
  14. package/dist/types/list.js +10 -52
  15. package/dist/types/map.d.ts +4 -5
  16. package/dist/types/map.js +15 -74
  17. package/dist/types/number.d.ts +4 -5
  18. package/dist/types/number.js +5 -12
  19. package/dist/types/string.d.ts +4 -5
  20. package/dist/types/string.js +6 -18
  21. package/dist/types/with-intrinsics.d.ts +4 -5
  22. package/dist/utils/error.d.ts +5 -5
  23. package/dist/utils/error.js +4 -5
  24. package/dist/utils/stack.d.ts +10 -0
  25. package/dist/utils/stack.js +30 -0
  26. package/dist/vm/evaluation.d.ts +26 -0
  27. package/dist/vm/evaluation.js +341 -0
  28. package/dist/vm.d.ts +66 -0
  29. package/dist/vm.js +662 -0
  30. package/package.json +1 -1
  31. package/dist/cps.d.ts +0 -16
  32. package/dist/cps.js +0 -202
  33. package/dist/operations/assign-globals.d.ts +0 -11
  34. package/dist/operations/assign-globals.js +0 -31
  35. package/dist/operations/assign-locals.d.ts +0 -11
  36. package/dist/operations/assign-locals.js +0 -31
  37. package/dist/operations/assign-outer.d.ts +0 -11
  38. package/dist/operations/assign-outer.js +0 -31
  39. package/dist/operations/assign-self.d.ts +0 -11
  40. package/dist/operations/assign-self.js +0 -41
  41. package/dist/operations/assign.d.ts +0 -13
  42. package/dist/operations/assign.js +0 -60
  43. package/dist/operations/block.d.ts +0 -13
  44. package/dist/operations/block.js +0 -48
  45. package/dist/operations/break.d.ts +0 -10
  46. package/dist/operations/break.js +0 -21
  47. package/dist/operations/call.d.ts +0 -13
  48. package/dist/operations/call.js +0 -64
  49. package/dist/operations/chunk.d.ts +0 -12
  50. package/dist/operations/chunk.js +0 -31
  51. package/dist/operations/continue.d.ts +0 -10
  52. package/dist/operations/continue.js +0 -21
  53. package/dist/operations/debugger-statement.d.ts +0 -10
  54. package/dist/operations/debugger-statement.js +0 -19
  55. package/dist/operations/envar.d.ts +0 -10
  56. package/dist/operations/envar.js +0 -21
  57. package/dist/operations/evaluate.d.ts +0 -45
  58. package/dist/operations/evaluate.js +0 -369
  59. package/dist/operations/for.d.ts +0 -14
  60. package/dist/operations/for.js +0 -87
  61. package/dist/operations/function-reference.d.ts +0 -12
  62. package/dist/operations/function-reference.js +0 -48
  63. package/dist/operations/function.d.ts +0 -19
  64. package/dist/operations/function.js +0 -84
  65. package/dist/operations/if-statement.d.ts +0 -19
  66. package/dist/operations/if-statement.js +0 -87
  67. package/dist/operations/import.d.ts +0 -20
  68. package/dist/operations/import.js +0 -56
  69. package/dist/operations/include.d.ts +0 -14
  70. package/dist/operations/include.js +0 -40
  71. package/dist/operations/list.d.ts +0 -11
  72. package/dist/operations/list.js +0 -40
  73. package/dist/operations/literal.d.ts +0 -11
  74. package/dist/operations/literal.js +0 -38
  75. package/dist/operations/map.d.ts +0 -11
  76. package/dist/operations/map.js +0 -42
  77. package/dist/operations/negated-binary.d.ts +0 -11
  78. package/dist/operations/negated-binary.js +0 -40
  79. package/dist/operations/new-instance.d.ts +0 -11
  80. package/dist/operations/new-instance.js +0 -37
  81. package/dist/operations/noop.d.ts +0 -7
  82. package/dist/operations/noop.js +0 -14
  83. package/dist/operations/not.d.ts +0 -11
  84. package/dist/operations/not.js +0 -33
  85. package/dist/operations/operation.d.ts +0 -15
  86. package/dist/operations/operation.js +0 -13
  87. package/dist/operations/reference-globals.d.ts +0 -7
  88. package/dist/operations/reference-globals.js +0 -13
  89. package/dist/operations/reference-locals.d.ts +0 -7
  90. package/dist/operations/reference-locals.js +0 -13
  91. package/dist/operations/reference-outer.d.ts +0 -7
  92. package/dist/operations/reference-outer.js +0 -13
  93. package/dist/operations/reference-self.d.ts +0 -7
  94. package/dist/operations/reference-self.js +0 -13
  95. package/dist/operations/reference.d.ts +0 -9
  96. package/dist/operations/reference.js +0 -17
  97. package/dist/operations/resolve-globals.d.ts +0 -5
  98. package/dist/operations/resolve-globals.js +0 -24
  99. package/dist/operations/resolve-locals.d.ts +0 -5
  100. package/dist/operations/resolve-locals.js +0 -24
  101. package/dist/operations/resolve-outer.d.ts +0 -5
  102. package/dist/operations/resolve-outer.js +0 -24
  103. package/dist/operations/resolve-self.d.ts +0 -5
  104. package/dist/operations/resolve-self.js +0 -24
  105. package/dist/operations/resolve.d.ts +0 -55
  106. package/dist/operations/resolve.js +0 -269
  107. package/dist/operations/return.d.ts +0 -11
  108. package/dist/operations/return.js +0 -43
  109. package/dist/operations/while.d.ts +0 -13
  110. package/dist/operations/while.js +0 -70
  111. package/dist/utils/create-assign.d.ts +0 -3
  112. package/dist/utils/create-assign.js +0 -25
  113. package/dist/utils/create-resolve.d.ts +0 -5
  114. package/dist/utils/create-resolve.js +0 -51
  115. package/dist/utils/path.d.ts +0 -10
  116. package/dist/utils/path.js +0 -33
@@ -1,31 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.Chunk = void 0;
13
- const block_1 = require("./block");
14
- const operation_1 = require("./operation");
15
- class Chunk extends operation_1.OperationBlock {
16
- constructor(item, target) {
17
- super(null, target);
18
- this.item = item;
19
- }
20
- build(visit) {
21
- return __awaiter(this, void 0, void 0, function* () {
22
- const stack = yield Promise.all(this.item.body.map((child) => visit(child)));
23
- this.block = new block_1.Block(this.item, stack);
24
- return this;
25
- });
26
- }
27
- handle(ctx) {
28
- return this.block.handle(ctx);
29
- }
30
- }
31
- exports.Chunk = Chunk;
@@ -1,10 +0,0 @@
1
- import { ASTBase } from 'miniscript-core';
2
- import { OperationContext } from '../context';
3
- import { CustomValue } from '../types/base';
4
- import { CPSVisit, Operation } from './operation';
5
- export declare class Continue extends Operation {
6
- readonly item: ASTBase;
7
- constructor(item: ASTBase, target?: string);
8
- build(_visit: CPSVisit): Promise<Continue>;
9
- handle(ctx: OperationContext): Promise<CustomValue>;
10
- }
@@ -1,21 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Continue = void 0;
4
- const default_1 = require("../types/default");
5
- const operation_1 = require("./operation");
6
- class Continue extends operation_1.Operation {
7
- constructor(item, target) {
8
- super(null, target);
9
- this.item = item;
10
- }
11
- build(_visit) {
12
- return Promise.resolve(this);
13
- }
14
- handle(ctx) {
15
- if (ctx.loopState !== null) {
16
- ctx.loopState.isContinue = true;
17
- }
18
- return Promise.resolve(default_1.DefaultType.Void);
19
- }
20
- }
21
- exports.Continue = Continue;
@@ -1,10 +0,0 @@
1
- import { ASTBase } from 'miniscript-core';
2
- import { OperationContext } from '../context';
3
- import { CustomValue } from '../types/base';
4
- import { CPSVisit, Operation } from './operation';
5
- export declare class DebuggerStatement extends Operation {
6
- readonly item: ASTBase;
7
- constructor(item: ASTBase, target?: string);
8
- build(_visit: CPSVisit): Promise<DebuggerStatement>;
9
- handle(ctx: OperationContext): Promise<CustomValue>;
10
- }
@@ -1,19 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DebuggerStatement = void 0;
4
- const default_1 = require("../types/default");
5
- const operation_1 = require("./operation");
6
- class DebuggerStatement extends operation_1.Operation {
7
- constructor(item, target) {
8
- super(null, target);
9
- this.item = item;
10
- }
11
- build(_visit) {
12
- return Promise.resolve(this);
13
- }
14
- handle(ctx) {
15
- ctx.debugger.setBreakpoint(true);
16
- return Promise.resolve(default_1.DefaultType.Void);
17
- }
18
- }
19
- exports.DebuggerStatement = DebuggerStatement;
@@ -1,10 +0,0 @@
1
- import { ASTFeatureEnvarExpression } from 'greybel-core';
2
- import { OperationContext } from '../context';
3
- import { CustomValue } from '../types/base';
4
- import { CPSVisit, Operation } from './operation';
5
- export declare class EnvarExpression extends Operation {
6
- readonly item: ASTFeatureEnvarExpression;
7
- constructor(item: ASTFeatureEnvarExpression, target?: string);
8
- build(_visit: CPSVisit): Promise<Operation>;
9
- handle(ctx: OperationContext): Promise<CustomValue>;
10
- }
@@ -1,21 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EnvarExpression = void 0;
4
- const string_1 = require("../types/string");
5
- const operation_1 = require("./operation");
6
- class EnvarExpression extends operation_1.Operation {
7
- constructor(item, target) {
8
- super(null, target);
9
- this.item = item;
10
- }
11
- build(_visit) {
12
- return Promise.resolve(this);
13
- }
14
- handle(ctx) {
15
- if (ctx.environmentVariables.has(this.item.name)) {
16
- return Promise.resolve(new string_1.CustomString(ctx.environmentVariables.get(this.item.name)));
17
- }
18
- throw new Error('Unknown environment variable.');
19
- }
20
- }
21
- exports.EnvarExpression = EnvarExpression;
@@ -1,45 +0,0 @@
1
- import { ASTEvaluationExpression } from 'miniscript-core';
2
- import { OperationContext } from '../context';
3
- import { CustomValue } from '../types/base';
4
- import { CustomList } from '../types/list';
5
- import { CustomNumber } from '../types/number';
6
- import { CustomString } from '../types/string';
7
- import { CPSVisit, Operation } from './operation';
8
- export interface ProcessorHandlerFunction {
9
- (op: string, a: CustomValue, b: CustomValue): CustomValue;
10
- }
11
- export interface ProcessorHandler {
12
- [op: string]: (a: CustomValue, b: CustomValue) => CustomValue;
13
- }
14
- export declare const GenericProcessorHandler: ProcessorHandler;
15
- export declare const NumberProcessorHandler: ProcessorHandler;
16
- export declare const minusString: (a: CustomString, b: CustomValue) => CustomValue;
17
- export declare const multiplyString: (a: CustomString, b: CustomValue) => CustomValue;
18
- export declare const divideString: (a: CustomString, b: CustomValue) => CustomValue;
19
- export declare const StringProcessorHandler: ProcessorHandler;
20
- export declare const multiplyList: (a: CustomList, b: CustomNumber) => CustomValue;
21
- export declare const divideList: (a: CustomList, b: CustomNumber) => CustomValue;
22
- export declare const ListProcessorHandler: ProcessorHandler;
23
- export declare const MapProcessorHandler: ProcessorHandler;
24
- export declare const NilProcessorHandler: ProcessorHandler;
25
- export declare const FunctionProcessorHandler: ProcessorHandler;
26
- export declare const handleNumber: ProcessorHandlerFunction;
27
- export declare const handleString: ProcessorHandlerFunction;
28
- export declare const handleList: ProcessorHandlerFunction;
29
- export declare const handleMap: ProcessorHandlerFunction;
30
- export declare const handleNil: ProcessorHandlerFunction;
31
- export declare const handleFunction: ProcessorHandlerFunction;
32
- export declare const handle: (op: string, a: CustomValue, b: CustomValue) => CustomValue;
33
- export declare class Evaluate extends Operation {
34
- readonly item: ASTEvaluationExpression;
35
- type: string;
36
- op: string;
37
- left: Operation;
38
- right: Operation;
39
- constructor(item: ASTEvaluationExpression, target?: string);
40
- build(visit: CPSVisit): Promise<Operation>;
41
- resolveBinaryExpression(ctx: OperationContext, expr: Evaluate): Promise<CustomValue>;
42
- resolveLogicalExpression(ctx: OperationContext, expr: Evaluate): Promise<CustomValue | CustomNumber>;
43
- resolve(ctx: OperationContext, op: Operation): Promise<CustomValue>;
44
- handle(ctx: OperationContext): Promise<CustomValue>;
45
- }
@@ -1,369 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.Evaluate = exports.handle = exports.handleFunction = exports.handleNil = exports.handleMap = exports.handleList = exports.handleString = exports.handleNumber = exports.FunctionProcessorHandler = exports.NilProcessorHandler = exports.MapProcessorHandler = exports.ListProcessorHandler = exports.divideList = exports.multiplyList = exports.StringProcessorHandler = exports.divideString = exports.multiplyString = exports.minusString = exports.NumberProcessorHandler = exports.GenericProcessorHandler = void 0;
13
- const operators_1 = require("greybel-core/dist/types/operators");
14
- const miniscript_core_1 = require("miniscript-core");
15
- const boolean_1 = require("../types/boolean");
16
- const default_1 = require("../types/default");
17
- const function_1 = require("../types/function");
18
- const list_1 = require("../types/list");
19
- const map_1 = require("../types/map");
20
- const nil_1 = require("../types/nil");
21
- const number_1 = require("../types/number");
22
- const string_1 = require("../types/string");
23
- const deep_equal_1 = require("../utils/deep-equal");
24
- const operation_1 = require("./operation");
25
- const absClamp01 = (d) => {
26
- if (d < 0)
27
- d = -d;
28
- if (d > 1)
29
- return 1;
30
- return d;
31
- };
32
- exports.GenericProcessorHandler = {
33
- [miniscript_core_1.Operator.And]: (a, b) => {
34
- const left = Number(a instanceof number_1.CustomNumber ? a.toNumber() : a.toTruthy());
35
- const right = Number(b instanceof number_1.CustomNumber ? b.toNumber() : b.toTruthy());
36
- return new number_1.CustomNumber(absClamp01(left * right));
37
- },
38
- [miniscript_core_1.Operator.Or]: (a, b) => {
39
- const left = Number(a instanceof number_1.CustomNumber ? a.toNumber() : a.toTruthy());
40
- const right = Number(b instanceof number_1.CustomNumber ? b.toNumber() : b.toTruthy());
41
- return new number_1.CustomNumber(absClamp01(left + right - left * right));
42
- }
43
- };
44
- exports.NumberProcessorHandler = {
45
- [miniscript_core_1.Operator.Plus]: (a, b) => new number_1.CustomNumber(a.toNumber() + b.toNumber()),
46
- [miniscript_core_1.Operator.Minus]: (a, b) => new number_1.CustomNumber(a.toNumber() - b.toNumber()),
47
- [miniscript_core_1.Operator.Slash]: (a, b) => new number_1.CustomNumber(a.toNumber() / b.toNumber()),
48
- [miniscript_core_1.Operator.Asterik]: (a, b) => new number_1.CustomNumber(a.toNumber() * b.toNumber()),
49
- [miniscript_core_1.Operator.Power]: (a, b) => new number_1.CustomNumber(Math.pow(a.toNumber(), b.toNumber())),
50
- [operators_1.Operator.BitwiseOr]: (a, b) => new number_1.CustomNumber(a.toInt() | b.toInt()),
51
- [miniscript_core_1.Operator.LessThan]: (a, b) => new boolean_1.CustomBoolean(a.toNumber() < b.toNumber()),
52
- [miniscript_core_1.Operator.GreaterThan]: (a, b) => new boolean_1.CustomBoolean(a.toNumber() > b.toNumber()),
53
- [operators_1.Operator.LeftShift]: (a, b) => new number_1.CustomNumber(a.toInt() << b.toInt()),
54
- [operators_1.Operator.RightShift]: (a, b) => new number_1.CustomNumber(a.toInt() >> b.toInt()),
55
- [operators_1.Operator.UnsignedRightShift]: (a, b) => new number_1.CustomNumber(a.toInt() >> b.toInt()),
56
- [operators_1.Operator.BitwiseAnd]: (a, b) => new number_1.CustomNumber(a.toInt() & b.toInt()),
57
- [miniscript_core_1.Operator.Modulo]: (a, b) => new number_1.CustomNumber(a.toNumber() % b.toNumber()),
58
- [miniscript_core_1.Operator.GreaterThanOrEqual]: (a, b) => new boolean_1.CustomBoolean(a.toNumber() >= b.toNumber()),
59
- [miniscript_core_1.Operator.Equal]: (a, b) => new boolean_1.CustomBoolean(a.toNumber() === b.toNumber()),
60
- [miniscript_core_1.Operator.LessThanOrEqual]: (a, b) => new boolean_1.CustomBoolean(a.toNumber() <= b.toNumber()),
61
- [miniscript_core_1.Operator.NotEqual]: (a, b) => new boolean_1.CustomBoolean(a.toNumber() !== b.toNumber())
62
- };
63
- const minusString = (a, b) => {
64
- const origin = a.toString();
65
- const toRemove = b.toString();
66
- if (origin.endsWith(toRemove)) {
67
- return new string_1.CustomString(origin.substring(0, origin.length - toRemove.length));
68
- }
69
- return new string_1.CustomString(origin);
70
- };
71
- exports.minusString = minusString;
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
- exports.multiplyString = multiplyString;
85
- const divideString = (a, b) => {
86
- const factor = 1 / b.toNumber();
87
- if (factor <= 0) {
88
- return new string_1.CustomString('');
89
- }
90
- let newString = '';
91
- const max = Math.floor(a.value.length * factor);
92
- for (let index = 0; index < max; index++) {
93
- newString += a.value[index % a.value.length];
94
- }
95
- return new string_1.CustomString(newString);
96
- };
97
- exports.divideString = divideString;
98
- exports.StringProcessorHandler = {
99
- [miniscript_core_1.Operator.Plus]: (a, b) => new string_1.CustomString(a.toString() + b.toString()),
100
- [miniscript_core_1.Operator.Minus]: (a, b) => (0, exports.minusString)(a, b),
101
- [miniscript_core_1.Operator.Asterik]: (a, b) => (0, exports.multiplyString)(a, b),
102
- [miniscript_core_1.Operator.Slash]: (a, b) => (0, exports.divideString)(a, b),
103
- [miniscript_core_1.Operator.LessThan]: (a, b) => new boolean_1.CustomBoolean(a.toString() < b.toString()),
104
- [miniscript_core_1.Operator.GreaterThan]: (a, b) => new boolean_1.CustomBoolean(a.toString() > b.toString()),
105
- [miniscript_core_1.Operator.GreaterThanOrEqual]: (a, b) => new boolean_1.CustomBoolean(a.toString() >= b.toString()),
106
- [miniscript_core_1.Operator.Equal]: (a, b) => new boolean_1.CustomBoolean(a.toString() === b.toString()),
107
- [miniscript_core_1.Operator.LessThanOrEqual]: (a, b) => new boolean_1.CustomBoolean(a.toString() <= b.toString()),
108
- [miniscript_core_1.Operator.NotEqual]: (a, b) => new boolean_1.CustomBoolean(a.toString() !== b.toString())
109
- };
110
- const multiplyList = (a, b) => {
111
- const factor = b.toNumber();
112
- if (factor <= 0) {
113
- return new list_1.CustomList();
114
- }
115
- const newListValue = [];
116
- const max = Math.floor(a.value.length * factor);
117
- for (let index = 0; index < max; index++) {
118
- newListValue.push(a.value[index % a.value.length]);
119
- }
120
- return new list_1.CustomList(newListValue);
121
- };
122
- exports.multiplyList = multiplyList;
123
- const divideList = (a, b) => {
124
- const factor = 1 / b.toNumber();
125
- if (factor <= 0) {
126
- return new list_1.CustomList();
127
- }
128
- const newListValue = [];
129
- const max = Math.floor(a.value.length * factor);
130
- for (let index = 0; index < max; index++) {
131
- newListValue.push(a.value[index % a.value.length]);
132
- }
133
- return new list_1.CustomList(newListValue);
134
- };
135
- exports.divideList = divideList;
136
- exports.ListProcessorHandler = {
137
- [miniscript_core_1.Operator.Plus]: (left, right) => {
138
- if (right instanceof list_1.CustomList) {
139
- return left.fork().extend(right);
140
- }
141
- return default_1.DefaultType.Void;
142
- },
143
- [miniscript_core_1.Operator.Equal]: (left, right) => {
144
- if (right instanceof list_1.CustomList) {
145
- return new boolean_1.CustomBoolean((0, deep_equal_1.deepEqual)(left, right));
146
- }
147
- return default_1.DefaultType.Void;
148
- },
149
- [miniscript_core_1.Operator.NotEqual]: (left, right) => {
150
- if (right instanceof list_1.CustomList) {
151
- return new boolean_1.CustomBoolean(!(0, deep_equal_1.deepEqual)(left, right));
152
- }
153
- return default_1.DefaultType.Void;
154
- },
155
- [miniscript_core_1.Operator.Asterik]: (left, right) => {
156
- if (right instanceof number_1.CustomNumber) {
157
- return (0, exports.multiplyList)(left, right);
158
- }
159
- return default_1.DefaultType.Void;
160
- },
161
- [miniscript_core_1.Operator.Slash]: (left, right) => {
162
- if (right instanceof number_1.CustomNumber) {
163
- return (0, exports.divideList)(left, right);
164
- }
165
- return default_1.DefaultType.Void;
166
- }
167
- };
168
- exports.MapProcessorHandler = {
169
- [miniscript_core_1.Operator.Plus]: (left, right) => {
170
- if (right instanceof map_1.CustomMap) {
171
- return left.fork().extend(right);
172
- }
173
- return default_1.DefaultType.Void;
174
- },
175
- [miniscript_core_1.Operator.Equal]: (left, right) => {
176
- if (right instanceof map_1.CustomMap) {
177
- return new boolean_1.CustomBoolean((0, deep_equal_1.deepEqual)(left, right));
178
- }
179
- return default_1.DefaultType.Void;
180
- },
181
- [miniscript_core_1.Operator.NotEqual]: (left, right) => {
182
- if (right instanceof map_1.CustomMap) {
183
- return new boolean_1.CustomBoolean(!(0, deep_equal_1.deepEqual)(left, right));
184
- }
185
- return default_1.DefaultType.Void;
186
- }
187
- };
188
- exports.NilProcessorHandler = {
189
- [miniscript_core_1.Operator.Plus]: () => default_1.DefaultType.Void,
190
- [miniscript_core_1.Operator.Minus]: () => default_1.DefaultType.Void,
191
- [miniscript_core_1.Operator.Slash]: () => default_1.DefaultType.Void,
192
- [miniscript_core_1.Operator.Asterik]: () => default_1.DefaultType.Void,
193
- [miniscript_core_1.Operator.Power]: () => default_1.DefaultType.Void,
194
- [miniscript_core_1.Operator.Modulo]: () => default_1.DefaultType.Void,
195
- [miniscript_core_1.Operator.Equal]: (_a, b) => new boolean_1.CustomBoolean(b instanceof nil_1.CustomNil),
196
- [miniscript_core_1.Operator.NotEqual]: (_a, b) => new boolean_1.CustomBoolean(!(b instanceof nil_1.CustomNil))
197
- };
198
- exports.FunctionProcessorHandler = {
199
- [miniscript_core_1.Operator.Equal]: (a, b) => new boolean_1.CustomBoolean(a === b),
200
- [miniscript_core_1.Operator.NotEqual]: (a, b) => new boolean_1.CustomBoolean(a !== b)
201
- };
202
- const handleNumber = (op, a, b) => {
203
- if (b instanceof nil_1.CustomNil)
204
- b = new number_1.CustomNumber(0);
205
- if (op in exports.NumberProcessorHandler && b instanceof number_1.CustomNumber) {
206
- return exports.NumberProcessorHandler[op](a, b);
207
- }
208
- else if (op in exports.GenericProcessorHandler) {
209
- return exports.GenericProcessorHandler[op](a, b);
210
- }
211
- return default_1.DefaultType.Void;
212
- };
213
- exports.handleNumber = handleNumber;
214
- const handleString = (op, a, b) => {
215
- if (a instanceof nil_1.CustomNil)
216
- a = new string_1.CustomString('');
217
- if (b instanceof nil_1.CustomNil)
218
- b = new string_1.CustomString('');
219
- if (op in exports.StringProcessorHandler) {
220
- return exports.StringProcessorHandler[op](a, b);
221
- }
222
- else if (op in exports.GenericProcessorHandler) {
223
- return exports.GenericProcessorHandler[op](a, b);
224
- }
225
- return default_1.DefaultType.Void;
226
- };
227
- exports.handleString = handleString;
228
- const handleList = (op, a, b) => {
229
- if (op in exports.ListProcessorHandler) {
230
- return exports.ListProcessorHandler[op](a, b);
231
- }
232
- else if (op in exports.GenericProcessorHandler) {
233
- return exports.GenericProcessorHandler[op](a, b);
234
- }
235
- return default_1.DefaultType.Void;
236
- };
237
- exports.handleList = handleList;
238
- const handleMap = (op, a, b) => {
239
- if (op in exports.MapProcessorHandler) {
240
- return exports.MapProcessorHandler[op](a, b);
241
- }
242
- else if (op in exports.GenericProcessorHandler) {
243
- return exports.GenericProcessorHandler[op](a, b);
244
- }
245
- return default_1.DefaultType.Void;
246
- };
247
- exports.handleMap = handleMap;
248
- const handleNil = (op, a, b) => {
249
- if (op in exports.NilProcessorHandler) {
250
- return exports.NilProcessorHandler[op](a, b);
251
- }
252
- else if (op in exports.GenericProcessorHandler) {
253
- return exports.GenericProcessorHandler[op](a, b);
254
- }
255
- return default_1.DefaultType.Void;
256
- };
257
- exports.handleNil = handleNil;
258
- const handleFunction = (op, a, b) => {
259
- if (op in exports.FunctionProcessorHandler) {
260
- return exports.FunctionProcessorHandler[op](a, b);
261
- }
262
- else if (op in exports.GenericProcessorHandler) {
263
- return exports.GenericProcessorHandler[op](a, b);
264
- }
265
- return default_1.DefaultType.Void;
266
- };
267
- exports.handleFunction = handleFunction;
268
- const handle = (op, a, b) => {
269
- if (a instanceof boolean_1.CustomBoolean) {
270
- a = new number_1.CustomNumber(a.toInt());
271
- }
272
- if (b instanceof boolean_1.CustomBoolean) {
273
- b = new number_1.CustomNumber(b.toInt());
274
- }
275
- if (op === miniscript_core_1.Operator.Equal && a.getCustomType() !== b.getCustomType()) {
276
- return default_1.DefaultType.False;
277
- }
278
- else if (op === miniscript_core_1.Operator.NotEqual &&
279
- a.getCustomType() !== b.getCustomType()) {
280
- return default_1.DefaultType.True;
281
- }
282
- if ((a instanceof string_1.CustomString || b instanceof string_1.CustomString) &&
283
- op === miniscript_core_1.Operator.Plus) {
284
- return (0, exports.handleString)(op, a, b);
285
- }
286
- else if (a instanceof number_1.CustomNumber) {
287
- return (0, exports.handleNumber)(op, a, b);
288
- }
289
- else if (a instanceof string_1.CustomString) {
290
- return (0, exports.handleString)(op, a, b);
291
- }
292
- else if (a instanceof list_1.CustomList) {
293
- return (0, exports.handleList)(op, a, b);
294
- }
295
- else if (a instanceof map_1.CustomMap) {
296
- return (0, exports.handleMap)(op, a, b);
297
- }
298
- else if (a instanceof function_1.CustomFunction) {
299
- return (0, exports.handleFunction)(op, a, b);
300
- }
301
- else if (a instanceof nil_1.CustomNil) {
302
- return (0, exports.handleNil)(op, a, b);
303
- }
304
- return default_1.DefaultType.Void;
305
- };
306
- exports.handle = handle;
307
- class Evaluate extends operation_1.Operation {
308
- constructor(item, target) {
309
- super(null, target);
310
- this.item = item;
311
- }
312
- build(visit) {
313
- return __awaiter(this, void 0, void 0, function* () {
314
- this.type = this.item.type;
315
- this.op = this.item.operator;
316
- this.left = yield visit(this.item.left);
317
- this.right = yield visit(this.item.right);
318
- return this;
319
- });
320
- }
321
- resolveBinaryExpression(ctx, expr) {
322
- return __awaiter(this, void 0, void 0, function* () {
323
- const left = yield this.resolve(ctx, expr.left);
324
- const right = yield this.resolve(ctx, expr.right);
325
- return (0, exports.handle)(expr.op, left, right);
326
- });
327
- }
328
- resolveLogicalExpression(ctx, expr) {
329
- return __awaiter(this, void 0, void 0, function* () {
330
- const left = yield this.resolve(ctx, expr.left);
331
- if (expr.op === miniscript_core_1.Operator.And && !left.toTruthy()) {
332
- return new boolean_1.CustomBoolean(false);
333
- }
334
- else if (expr.op === miniscript_core_1.Operator.Or && left.toTruthy()) {
335
- if (left instanceof number_1.CustomNumber) {
336
- return new number_1.CustomNumber(absClamp01(left.value));
337
- }
338
- return new boolean_1.CustomBoolean(true);
339
- }
340
- const right = yield this.resolve(ctx, expr.right);
341
- return (0, exports.handle)(expr.op, left, right);
342
- });
343
- }
344
- resolve(ctx, op) {
345
- return __awaiter(this, void 0, void 0, function* () {
346
- if (op instanceof Evaluate) {
347
- const expr = op;
348
- switch (expr.type) {
349
- case miniscript_core_1.ASTType.IsaExpression: {
350
- const left = yield this.resolve(ctx, expr.left);
351
- const right = yield this.resolve(ctx, expr.right);
352
- return new boolean_1.CustomBoolean(left.instanceOf(right, ctx.contextTypeIntrinsics));
353
- }
354
- case miniscript_core_1.ASTType.BinaryExpression:
355
- return this.resolveBinaryExpression(ctx, expr);
356
- case miniscript_core_1.ASTType.LogicalExpression:
357
- return this.resolveLogicalExpression(ctx, expr);
358
- default:
359
- break;
360
- }
361
- }
362
- return op.handle(ctx);
363
- });
364
- }
365
- handle(ctx) {
366
- return this.resolve(ctx, this);
367
- }
368
- }
369
- exports.Evaluate = Evaluate;
@@ -1,14 +0,0 @@
1
- import { ASTForGenericStatement, ASTIdentifier } from 'miniscript-core';
2
- import { OperationContext } from '../context';
3
- import { CustomValue } from '../types/base';
4
- import { Block } from './block';
5
- import { CPSVisit, Operation, OperationBlock } from './operation';
6
- export declare class For extends OperationBlock {
7
- readonly item: ASTForGenericStatement;
8
- block: Block;
9
- variable: ASTIdentifier;
10
- iterator: Operation;
11
- constructor(item: ASTForGenericStatement, target?: string);
12
- build(visit: CPSVisit): Promise<Operation>;
13
- handle(ctx: OperationContext): Promise<CustomValue>;
14
- }
@@ -1,87 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.For = void 0;
13
- const context_1 = require("../context");
14
- const default_1 = require("../types/default");
15
- const number_1 = require("../types/number");
16
- const string_1 = require("../types/string");
17
- const set_immediate_1 = require("../utils/set-immediate");
18
- const block_1 = require("./block");
19
- const operation_1 = require("./operation");
20
- class For extends operation_1.OperationBlock {
21
- constructor(item, target) {
22
- super(null, target);
23
- this.item = item;
24
- }
25
- build(visit) {
26
- return __awaiter(this, void 0, void 0, function* () {
27
- const stack = yield Promise.all(this.item.body.map((child) => visit(child)));
28
- this.block = new block_1.Block(this.item, stack);
29
- this.variable = this.item.variable;
30
- this.iterator = yield visit(this.item.iterator);
31
- return this;
32
- });
33
- }
34
- handle(ctx) {
35
- return __awaiter(this, void 0, void 0, function* () {
36
- const forCtx = ctx.fork({
37
- type: context_1.ContextType.Loop,
38
- state: context_1.ContextState.Temporary
39
- });
40
- const identifier = this.variable.name;
41
- const iteratorValue = (yield this.iterator.handle(ctx));
42
- if (typeof iteratorValue[Symbol.iterator] !== 'function') {
43
- return Promise.resolve(default_1.DefaultType.Void);
44
- }
45
- const loopState = new context_1.LoopState();
46
- const exitObserver = ctx.processState.createExitObserver();
47
- forCtx.loopState = loopState;
48
- return new Promise((resolve, reject) => {
49
- const iterator = iteratorValue[Symbol.iterator]();
50
- const idxIdentifier = new string_1.CustomString(`__${identifier}_idx`);
51
- const varIdentifier = new string_1.CustomString(identifier);
52
- let iteratorResult = iterator.next();
53
- const iteration = () => __awaiter(this, void 0, void 0, function* () {
54
- try {
55
- if (iteratorResult.done) {
56
- exitObserver.close();
57
- resolve(default_1.DefaultType.Void);
58
- return;
59
- }
60
- const current = iteratorResult.value;
61
- loopState.isContinue = false;
62
- forCtx.set(idxIdentifier, new number_1.CustomNumber(iterator.index - 1));
63
- forCtx.set(varIdentifier, current);
64
- yield this.block.handle(forCtx);
65
- if (loopState.isBreak ||
66
- forCtx.functionState.isReturn ||
67
- exitObserver.occured()) {
68
- exitObserver.close();
69
- resolve(default_1.DefaultType.Void);
70
- return;
71
- }
72
- const idxValue = forCtx.get(idxIdentifier).toNumber();
73
- iterator.index += idxValue - (iterator.index - 1);
74
- iteratorResult = iterator.next();
75
- (0, set_immediate_1.setImmediate)(iteration);
76
- }
77
- catch (err) {
78
- exitObserver.close();
79
- reject(err);
80
- }
81
- });
82
- iteration();
83
- });
84
- });
85
- }
86
- }
87
- exports.For = For;
@@ -1,12 +0,0 @@
1
- import { ASTUnaryExpression } from 'miniscript-core';
2
- import { OperationContext } from '../context';
3
- import { CustomValue } from '../types/base';
4
- import { CPSVisit, Operation } from './operation';
5
- import { Resolve } from './resolve';
6
- export declare class FunctionReference extends Operation {
7
- readonly item: ASTUnaryExpression;
8
- ref: Resolve;
9
- constructor(item: ASTUnaryExpression, target?: string);
10
- build(visit: CPSVisit): Promise<FunctionReference>;
11
- handle(ctx: OperationContext): Promise<CustomValue>;
12
- }