greybel-interpreter 3.5.0 → 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 +5 -6
  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
@@ -0,0 +1,129 @@
1
+ import { ASTPosition } from 'miniscript-core';
2
+ import { CustomValue } from '../types/base';
3
+ import { CustomFunctionCallback } from '../types/function';
4
+ import { CustomString } from '../types/string';
5
+ export declare enum OpCode {
6
+ NOOP = 0,
7
+ HALT = 1,
8
+ CALL = 2,
9
+ CALL_INTERNAL = 3,
10
+ CALL_WITH_CONTEXT = 4,
11
+ CONSTRUCT_MAP = 5,
12
+ CONSTRUCT_LIST = 6,
13
+ GET_VARIABLE = 7,
14
+ GET_PROPERTY = 8,
15
+ GET_ENVAR = 9,
16
+ GET_SELF = 10,
17
+ GET_OUTER = 11,
18
+ GET_GLOBALS = 12,
19
+ GET_LOCALS = 13,
20
+ GET_SUPER = 14,
21
+ GET_SUPER_PROPERTY = 15,
22
+ CALL_SUPER_PROPERTY = 16,
23
+ FALSIFY = 17,
24
+ NEGATE = 18,
25
+ NEW = 19,
26
+ SLICE = 20,
27
+ ASSIGN = 21,
28
+ PUSH = 22,
29
+ POP = 23,
30
+ ISA = 24,
31
+ ADD = 25,
32
+ SUB = 26,
33
+ MUL = 27,
34
+ DIV = 28,
35
+ MOD = 29,
36
+ POW = 30,
37
+ EQUAL = 31,
38
+ NOT_EQUAL = 32,
39
+ LESS_THAN = 33,
40
+ LESS_THAN_OR_EQUAL = 34,
41
+ GREATER_THAN = 35,
42
+ GREATER_THAN_OR_EQUAL = 36,
43
+ AND = 37,
44
+ OR = 38,
45
+ RETURN = 39,
46
+ FUNCTION_DEFINITION = 40,
47
+ GOTO_A = 41,
48
+ GOTO_A_IF_FALSE = 42,
49
+ PUSH_ITERATOR = 43,
50
+ POP_ITERATOR = 44,
51
+ NEXT = 45,
52
+ BITWISE_OR = 46,
53
+ BITWISE_AND = 47,
54
+ BITWISE_LEFT_SHIFT = 48,
55
+ BITWISE_RIGHT_SHIFT = 49,
56
+ BITWISE_UNSIGNED_RIGHT_SHIFT = 50,
57
+ BREAKPOINT = 51,
58
+ BREAKPOINT_ENABLE = 52,
59
+ IMPORT = 53
60
+ }
61
+ export interface FunctionDefinitionInstructionArgument {
62
+ name: CustomString;
63
+ defaultValue: CustomValue;
64
+ }
65
+ export type SourceLocation = {
66
+ path: string;
67
+ start: ASTPosition;
68
+ end: ASTPosition;
69
+ };
70
+ export interface BaseInstruction {
71
+ op: OpCode;
72
+ source: SourceLocation;
73
+ ip?: number;
74
+ }
75
+ export interface GetVariableInstruction extends BaseInstruction {
76
+ op: OpCode.GET_VARIABLE;
77
+ property: CustomString;
78
+ invoke: boolean;
79
+ }
80
+ export interface GetPropertyInstruction extends BaseInstruction {
81
+ op: OpCode.GET_PROPERTY | OpCode.GET_SUPER_PROPERTY;
82
+ invoke: boolean;
83
+ }
84
+ export interface PushInstruction extends BaseInstruction {
85
+ op: OpCode.PUSH;
86
+ value: CustomValue;
87
+ }
88
+ export interface ConstructMapInstruction extends BaseInstruction {
89
+ op: OpCode.CONSTRUCT_MAP;
90
+ length: number;
91
+ }
92
+ export interface ConstructListInstruction extends BaseInstruction {
93
+ op: OpCode.CONSTRUCT_LIST;
94
+ length: number;
95
+ }
96
+ export interface CallInstruction extends BaseInstruction {
97
+ op: OpCode.CALL;
98
+ length: number;
99
+ }
100
+ export interface CallWithContextInstruction extends BaseInstruction {
101
+ op: OpCode.CALL_WITH_CONTEXT | OpCode.CALL_SUPER_PROPERTY;
102
+ length: number;
103
+ }
104
+ export interface FunctionDefinitionInstruction extends BaseInstruction {
105
+ op: OpCode.FUNCTION_DEFINITION;
106
+ arguments: FunctionDefinitionInstructionArgument[];
107
+ code: Instruction[];
108
+ ignoreOuter: boolean;
109
+ }
110
+ export interface GotoAInstruction extends BaseInstruction {
111
+ op: OpCode.GOTO_A | OpCode.GOTO_A_IF_FALSE;
112
+ goto: Instruction;
113
+ }
114
+ export interface NextInstruction extends BaseInstruction {
115
+ op: OpCode.NEXT;
116
+ goto: Instruction;
117
+ idxVariable: CustomString;
118
+ variable: CustomString;
119
+ }
120
+ export interface CallInternalInstruction extends BaseInstruction {
121
+ op: OpCode.CALL_INTERNAL;
122
+ callback: CustomFunctionCallback;
123
+ arguments: FunctionDefinitionInstructionArgument[];
124
+ }
125
+ export interface ImportInstruction extends BaseInstruction {
126
+ op: OpCode.IMPORT;
127
+ path: string;
128
+ }
129
+ export type Instruction = BaseInstruction | GetVariableInstruction | PushInstruction | ConstructMapInstruction | ConstructListInstruction | FunctionDefinitionInstruction | GotoAInstruction | GetPropertyInstruction | CallInstruction | CallWithContextInstruction | NextInstruction | CallInternalInstruction | ImportInstruction;
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OpCode = void 0;
4
+ var OpCode;
5
+ (function (OpCode) {
6
+ OpCode[OpCode["NOOP"] = 0] = "NOOP";
7
+ OpCode[OpCode["HALT"] = 1] = "HALT";
8
+ OpCode[OpCode["CALL"] = 2] = "CALL";
9
+ OpCode[OpCode["CALL_INTERNAL"] = 3] = "CALL_INTERNAL";
10
+ OpCode[OpCode["CALL_WITH_CONTEXT"] = 4] = "CALL_WITH_CONTEXT";
11
+ OpCode[OpCode["CONSTRUCT_MAP"] = 5] = "CONSTRUCT_MAP";
12
+ OpCode[OpCode["CONSTRUCT_LIST"] = 6] = "CONSTRUCT_LIST";
13
+ OpCode[OpCode["GET_VARIABLE"] = 7] = "GET_VARIABLE";
14
+ OpCode[OpCode["GET_PROPERTY"] = 8] = "GET_PROPERTY";
15
+ OpCode[OpCode["GET_ENVAR"] = 9] = "GET_ENVAR";
16
+ OpCode[OpCode["GET_SELF"] = 10] = "GET_SELF";
17
+ OpCode[OpCode["GET_OUTER"] = 11] = "GET_OUTER";
18
+ OpCode[OpCode["GET_GLOBALS"] = 12] = "GET_GLOBALS";
19
+ OpCode[OpCode["GET_LOCALS"] = 13] = "GET_LOCALS";
20
+ OpCode[OpCode["GET_SUPER"] = 14] = "GET_SUPER";
21
+ OpCode[OpCode["GET_SUPER_PROPERTY"] = 15] = "GET_SUPER_PROPERTY";
22
+ OpCode[OpCode["CALL_SUPER_PROPERTY"] = 16] = "CALL_SUPER_PROPERTY";
23
+ OpCode[OpCode["FALSIFY"] = 17] = "FALSIFY";
24
+ OpCode[OpCode["NEGATE"] = 18] = "NEGATE";
25
+ OpCode[OpCode["NEW"] = 19] = "NEW";
26
+ OpCode[OpCode["SLICE"] = 20] = "SLICE";
27
+ OpCode[OpCode["ASSIGN"] = 21] = "ASSIGN";
28
+ OpCode[OpCode["PUSH"] = 22] = "PUSH";
29
+ OpCode[OpCode["POP"] = 23] = "POP";
30
+ OpCode[OpCode["ISA"] = 24] = "ISA";
31
+ OpCode[OpCode["ADD"] = 25] = "ADD";
32
+ OpCode[OpCode["SUB"] = 26] = "SUB";
33
+ OpCode[OpCode["MUL"] = 27] = "MUL";
34
+ OpCode[OpCode["DIV"] = 28] = "DIV";
35
+ OpCode[OpCode["MOD"] = 29] = "MOD";
36
+ OpCode[OpCode["POW"] = 30] = "POW";
37
+ OpCode[OpCode["EQUAL"] = 31] = "EQUAL";
38
+ OpCode[OpCode["NOT_EQUAL"] = 32] = "NOT_EQUAL";
39
+ OpCode[OpCode["LESS_THAN"] = 33] = "LESS_THAN";
40
+ OpCode[OpCode["LESS_THAN_OR_EQUAL"] = 34] = "LESS_THAN_OR_EQUAL";
41
+ OpCode[OpCode["GREATER_THAN"] = 35] = "GREATER_THAN";
42
+ OpCode[OpCode["GREATER_THAN_OR_EQUAL"] = 36] = "GREATER_THAN_OR_EQUAL";
43
+ OpCode[OpCode["AND"] = 37] = "AND";
44
+ OpCode[OpCode["OR"] = 38] = "OR";
45
+ OpCode[OpCode["RETURN"] = 39] = "RETURN";
46
+ OpCode[OpCode["FUNCTION_DEFINITION"] = 40] = "FUNCTION_DEFINITION";
47
+ OpCode[OpCode["GOTO_A"] = 41] = "GOTO_A";
48
+ OpCode[OpCode["GOTO_A_IF_FALSE"] = 42] = "GOTO_A_IF_FALSE";
49
+ OpCode[OpCode["PUSH_ITERATOR"] = 43] = "PUSH_ITERATOR";
50
+ OpCode[OpCode["POP_ITERATOR"] = 44] = "POP_ITERATOR";
51
+ OpCode[OpCode["NEXT"] = 45] = "NEXT";
52
+ OpCode[OpCode["BITWISE_OR"] = 46] = "BITWISE_OR";
53
+ OpCode[OpCode["BITWISE_AND"] = 47] = "BITWISE_AND";
54
+ OpCode[OpCode["BITWISE_LEFT_SHIFT"] = 48] = "BITWISE_LEFT_SHIFT";
55
+ OpCode[OpCode["BITWISE_RIGHT_SHIFT"] = 49] = "BITWISE_RIGHT_SHIFT";
56
+ OpCode[OpCode["BITWISE_UNSIGNED_RIGHT_SHIFT"] = 50] = "BITWISE_UNSIGNED_RIGHT_SHIFT";
57
+ OpCode[OpCode["BREAKPOINT"] = 51] = "BREAKPOINT";
58
+ OpCode[OpCode["BREAKPOINT_ENABLE"] = 52] = "BREAKPOINT_ENABLE";
59
+ OpCode[OpCode["IMPORT"] = 53] = "IMPORT";
60
+ })(OpCode = exports.OpCode || (exports.OpCode = {}));
@@ -0,0 +1,61 @@
1
+ import { ASTFeatureEnvarExpression, ASTFeatureImportExpression, ASTFeatureIncludeExpression } from 'greybel-core';
2
+ import { ASTBase, ASTCallExpression, ASTForGenericStatement, ASTIfStatement, ASTUnaryExpression, ASTWhileStatement } from 'miniscript-core';
3
+ import { Instruction } from './byte-compiler/instruction';
4
+ import { HandlerContainer } from './handler-container';
5
+ export interface BytecodeCompileResult {
6
+ code: Instruction[];
7
+ imports: Map<string, Instruction[]>;
8
+ }
9
+ export interface BytecodeGeneratorContext {
10
+ code: Instruction[];
11
+ jumpPoints: [Instruction, Instruction][];
12
+ }
13
+ export interface BytecodeConverterOptions {
14
+ target: string;
15
+ handler: HandlerContainer;
16
+ debugMode?: boolean;
17
+ }
18
+ export declare class BytecodeGenerator {
19
+ private handler;
20
+ private context;
21
+ private target;
22
+ private debugMode;
23
+ private imports;
24
+ constructor(options: BytecodeConverterOptions);
25
+ parse(code: string): ASTBase | import("greybel-core").ASTChunkAdvanced;
26
+ compile(code: string): Promise<BytecodeCompileResult>;
27
+ private getCurrentPointer;
28
+ private getSourceLocation;
29
+ private getInternalLocation;
30
+ private pushContext;
31
+ private popContext;
32
+ private getLastJumpoint;
33
+ private pushJumppoint;
34
+ private popJumppoint;
35
+ private push;
36
+ private processNode;
37
+ private processSubNode;
38
+ private processMemberExpression;
39
+ private processIndexExpression;
40
+ private processSliceExpression;
41
+ private processIdentifier;
42
+ private processAssignmentStatement;
43
+ private processLiteral;
44
+ private processEvaluationExpression;
45
+ private processReturn;
46
+ private processBreak;
47
+ private processContinue;
48
+ private processMapConstructorExpression;
49
+ private processListConstructorExpression;
50
+ private processFunctionDeclaration;
51
+ processWhileStatement(node: ASTWhileStatement): Promise<void>;
52
+ processUnaryExpression(node: ASTUnaryExpression): Promise<void>;
53
+ processCallExpression(node: ASTCallExpression): Promise<void>;
54
+ processIfStatement(node: ASTIfStatement): Promise<void>;
55
+ processForGenericStatement(node: ASTForGenericStatement): Promise<void>;
56
+ processEnvarExpression(node: ASTFeatureEnvarExpression): Promise<void>;
57
+ createImport(node: ASTFeatureImportExpression, path: string, code: string): Promise<void>;
58
+ processImportExpression(node: ASTFeatureImportExpression): Promise<void>;
59
+ processIncludeExpression(node: ASTFeatureIncludeExpression): Promise<void>;
60
+ processDebuggerExpression(node: ASTBase): Promise<void>;
61
+ }