@holoscript/core 1.0.0-alpha.1 → 2.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/package.json +10 -9
- package/src/HoloScript2DParser.js +227 -0
- package/src/HoloScript2DParser.ts +5 -0
- package/src/HoloScriptCodeParser.js +1102 -0
- package/src/HoloScriptCodeParser.ts +145 -20
- package/src/HoloScriptDebugger.js +458 -0
- package/src/HoloScriptParser.js +338 -0
- package/src/HoloScriptPlusParser.js +371 -0
- package/src/HoloScriptPlusParser.ts +543 -0
- package/src/HoloScriptRuntime.js +1399 -0
- package/src/HoloScriptRuntime.test.js +351 -0
- package/src/HoloScriptRuntime.ts +257 -3
- package/src/HoloScriptTypeChecker.js +356 -0
- package/src/__tests__/GraphicsServices.test.js +357 -0
- package/src/__tests__/GraphicsServices.test.ts +427 -0
- package/src/__tests__/HoloScriptPlusParser.test.js +317 -0
- package/src/__tests__/HoloScriptPlusParser.test.ts +392 -0
- package/src/__tests__/integration.test.js +336 -0
- package/src/__tests__/performance.bench.js +218 -0
- package/src/__tests__/type-checker.test.js +60 -0
- package/src/__tests__/type-checker.test.ts +73 -0
- package/src/index.js +217 -0
- package/src/index.ts +158 -18
- package/src/interop/Interoperability.js +413 -0
- package/src/interop/Interoperability.ts +494 -0
- package/src/logger.js +42 -0
- package/src/parser/EnhancedParser.js +205 -0
- package/src/parser/EnhancedParser.ts +251 -0
- package/src/parser/HoloScriptPlusParser.js +928 -0
- package/src/parser/HoloScriptPlusParser.ts +1089 -0
- package/src/runtime/HoloScriptPlusRuntime.js +674 -0
- package/src/runtime/HoloScriptPlusRuntime.ts +861 -0
- package/src/runtime/PerformanceTelemetry.js +323 -0
- package/src/runtime/PerformanceTelemetry.ts +467 -0
- package/src/runtime/RuntimeOptimization.js +361 -0
- package/src/runtime/RuntimeOptimization.ts +416 -0
- package/src/services/HololandGraphicsPipelineService.js +506 -0
- package/src/services/HololandGraphicsPipelineService.ts +662 -0
- package/src/services/PlatformPerformanceOptimizer.js +356 -0
- package/src/services/PlatformPerformanceOptimizer.ts +503 -0
- package/src/state/ReactiveState.js +427 -0
- package/src/state/ReactiveState.ts +572 -0
- package/src/tools/DeveloperExperience.js +376 -0
- package/src/tools/DeveloperExperience.ts +438 -0
- package/src/traits/AIDriverTrait.js +322 -0
- package/src/traits/AIDriverTrait.test.js +329 -0
- package/src/traits/AIDriverTrait.test.ts +357 -0
- package/src/traits/AIDriverTrait.ts +474 -0
- package/src/traits/LightingTrait.js +313 -0
- package/src/traits/LightingTrait.test.js +410 -0
- package/src/traits/LightingTrait.test.ts +462 -0
- package/src/traits/LightingTrait.ts +505 -0
- package/src/traits/MaterialTrait.js +194 -0
- package/src/traits/MaterialTrait.test.js +286 -0
- package/src/traits/MaterialTrait.test.ts +329 -0
- package/src/traits/MaterialTrait.ts +324 -0
- package/src/traits/RenderingTrait.js +356 -0
- package/src/traits/RenderingTrait.test.js +363 -0
- package/src/traits/RenderingTrait.test.ts +427 -0
- package/src/traits/RenderingTrait.ts +555 -0
- package/src/traits/VRTraitSystem.js +740 -0
- package/src/traits/VRTraitSystem.ts +1040 -0
- package/src/traits/VoiceInputTrait.js +284 -0
- package/src/traits/VoiceInputTrait.test.js +226 -0
- package/src/traits/VoiceInputTrait.test.ts +252 -0
- package/src/traits/VoiceInputTrait.ts +401 -0
- package/src/types/AdvancedTypeSystem.js +226 -0
- package/src/types/AdvancedTypeSystem.ts +494 -0
- package/src/types/HoloScriptPlus.d.ts +853 -0
- package/src/types.js +6 -0
- package/src/types.ts +96 -1
- package/tsconfig.json +1 -1
- package/tsup.config.d.ts +2 -0
- package/tsup.config.js +18 -0
- package/LICENSE +0 -21
- package/dist/chunk-3X2EGU7Z.cjs +0 -52
- package/dist/chunk-3X2EGU7Z.cjs.map +0 -1
- package/dist/chunk-723TPVHD.js +0 -1074
- package/dist/chunk-723TPVHD.js.map +0 -1
- package/dist/chunk-EOKNAVDO.cjs +0 -424
- package/dist/chunk-EOKNAVDO.cjs.map +0 -1
- package/dist/chunk-HQZ3HUMY.js +0 -1087
- package/dist/chunk-HQZ3HUMY.js.map +0 -1
- package/dist/chunk-KWYIVRIH.js +0 -344
- package/dist/chunk-KWYIVRIH.js.map +0 -1
- package/dist/chunk-LKH4ZAN6.js +0 -421
- package/dist/chunk-LKH4ZAN6.js.map +0 -1
- package/dist/chunk-SATNCODL.js +0 -45
- package/dist/chunk-SATNCODL.js.map +0 -1
- package/dist/chunk-VMZN4EVR.cjs +0 -347
- package/dist/chunk-VMZN4EVR.cjs.map +0 -1
- package/dist/chunk-VV3UUUYP.cjs +0 -1089
- package/dist/chunk-VV3UUUYP.cjs.map +0 -1
- package/dist/chunk-XRYTSQHZ.cjs +0 -1076
- package/dist/chunk-XRYTSQHZ.cjs.map +0 -1
- package/dist/debugger.cjs +0 -19
- package/dist/debugger.cjs.map +0 -1
- package/dist/debugger.d.cts +0 -171
- package/dist/debugger.d.ts +0 -171
- package/dist/debugger.js +0 -6
- package/dist/debugger.js.map +0 -1
- package/dist/index.cjs +0 -755
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -169
- package/dist/index.d.ts +0 -169
- package/dist/index.js +0 -699
- package/dist/index.js.map +0 -1
- package/dist/parser.cjs +0 -13
- package/dist/parser.cjs.map +0 -1
- package/dist/parser.d.cts +0 -154
- package/dist/parser.d.ts +0 -154
- package/dist/parser.js +0 -4
- package/dist/parser.js.map +0 -1
- package/dist/runtime.cjs +0 -13
- package/dist/runtime.cjs.map +0 -1
- package/dist/runtime.d.cts +0 -147
- package/dist/runtime.d.ts +0 -147
- package/dist/runtime.js +0 -4
- package/dist/runtime.js.map +0 -1
- package/dist/type-checker.cjs +0 -16
- package/dist/type-checker.cjs.map +0 -1
- package/dist/type-checker.d.cts +0 -105
- package/dist/type-checker.d.ts +0 -105
- package/dist/type-checker.js +0 -3
- package/dist/type-checker.js.map +0 -1
- package/dist/types-WQSk1Qs2.d.cts +0 -238
- package/dist/types-WQSk1Qs2.d.ts +0 -238
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HoloScript Type Checker
|
|
3
|
+
*
|
|
4
|
+
* Static type analysis for HoloScript code.
|
|
5
|
+
* Validates types, detects errors, and provides type information.
|
|
6
|
+
*/
|
|
7
|
+
// Built-in type definitions
|
|
8
|
+
const BUILTIN_FUNCTIONS = new Map([
|
|
9
|
+
['add', { type: 'function', parameters: [{ name: 'a', type: 'number' }, { name: 'b', type: 'number' }], returnType: 'number' }],
|
|
10
|
+
['subtract', { type: 'function', parameters: [{ name: 'a', type: 'number' }, { name: 'b', type: 'number' }], returnType: 'number' }],
|
|
11
|
+
['multiply', { type: 'function', parameters: [{ name: 'a', type: 'number' }, { name: 'b', type: 'number' }], returnType: 'number' }],
|
|
12
|
+
['divide', { type: 'function', parameters: [{ name: 'a', type: 'number' }, { name: 'b', type: 'number' }], returnType: 'number' }],
|
|
13
|
+
['concat', { type: 'function', parameters: [{ name: 'a', type: 'string' }, { name: 'b', type: 'string' }], returnType: 'string' }],
|
|
14
|
+
['length', { type: 'function', parameters: [{ name: 'value', type: 'any' }], returnType: 'number' }],
|
|
15
|
+
['push', { type: 'function', parameters: [{ name: 'arr', type: 'array' }, { name: 'item', type: 'any' }], returnType: 'array' }],
|
|
16
|
+
['pop', { type: 'function', parameters: [{ name: 'arr', type: 'array' }], returnType: 'any' }],
|
|
17
|
+
['log', { type: 'function', parameters: [{ name: 'message', type: 'any' }], returnType: 'void' }],
|
|
18
|
+
['print', { type: 'function', parameters: [{ name: 'message', type: 'any' }], returnType: 'void' }],
|
|
19
|
+
['show', { type: 'function', parameters: [{ name: 'target', type: 'orb' }], returnType: 'void' }],
|
|
20
|
+
['hide', { type: 'function', parameters: [{ name: 'target', type: 'orb' }], returnType: 'void' }],
|
|
21
|
+
['pulse', { type: 'function', parameters: [{ name: 'target', type: 'orb' }], returnType: 'void' }],
|
|
22
|
+
['animate', { type: 'function', parameters: [{ name: 'target', type: 'orb' }, { name: 'config', type: 'object' }], returnType: 'void' }],
|
|
23
|
+
['spawn', { type: 'function', parameters: [{ name: 'type', type: 'string' }, { name: 'position', type: 'object' }], returnType: 'orb' }],
|
|
24
|
+
['isNumber', { type: 'function', parameters: [{ name: 'value', type: 'any' }], returnType: 'boolean' }],
|
|
25
|
+
['isString', { type: 'function', parameters: [{ name: 'value', type: 'any' }], returnType: 'boolean' }],
|
|
26
|
+
['isArray', { type: 'function', parameters: [{ name: 'value', type: 'any' }], returnType: 'boolean' }],
|
|
27
|
+
]);
|
|
28
|
+
export class HoloScriptTypeChecker {
|
|
29
|
+
constructor() {
|
|
30
|
+
this.typeMap = new Map();
|
|
31
|
+
this.diagnostics = [];
|
|
32
|
+
this.currentLine = 0;
|
|
33
|
+
this.currentColumn = 0;
|
|
34
|
+
// Initialize with built-in functions
|
|
35
|
+
BUILTIN_FUNCTIONS.forEach((type, name) => {
|
|
36
|
+
this.typeMap.set(name, type);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Type check an AST
|
|
41
|
+
*/
|
|
42
|
+
check(ast) {
|
|
43
|
+
this.diagnostics = [];
|
|
44
|
+
// First pass: collect declarations
|
|
45
|
+
for (const node of ast) {
|
|
46
|
+
this.collectDeclaration(node);
|
|
47
|
+
}
|
|
48
|
+
// Second pass: validate types
|
|
49
|
+
for (const node of ast) {
|
|
50
|
+
this.checkNode(node);
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
valid: this.diagnostics.filter(d => d.severity === 'error').length === 0,
|
|
54
|
+
diagnostics: this.diagnostics,
|
|
55
|
+
typeMap: new Map(this.typeMap),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Collect type declarations
|
|
60
|
+
*/
|
|
61
|
+
collectDeclaration(node) {
|
|
62
|
+
switch (node.type) {
|
|
63
|
+
case 'orb':
|
|
64
|
+
this.collectOrbDeclaration(node);
|
|
65
|
+
break;
|
|
66
|
+
case 'method':
|
|
67
|
+
this.collectMethodDeclaration(node);
|
|
68
|
+
break;
|
|
69
|
+
case 'variable-declaration':
|
|
70
|
+
this.collectVariableDeclaration(node);
|
|
71
|
+
break;
|
|
72
|
+
case 'stream':
|
|
73
|
+
this.collectStreamDeclaration(node);
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
collectOrbDeclaration(node) {
|
|
78
|
+
const properties = new Map();
|
|
79
|
+
// Add default orb properties
|
|
80
|
+
properties.set('position', { type: 'object', properties: new Map([
|
|
81
|
+
['x', { type: 'number' }],
|
|
82
|
+
['y', { type: 'number' }],
|
|
83
|
+
['z', { type: 'number' }],
|
|
84
|
+
]) });
|
|
85
|
+
properties.set('color', { type: 'string' });
|
|
86
|
+
properties.set('glow', { type: 'boolean' });
|
|
87
|
+
properties.set('interactive', { type: 'boolean' });
|
|
88
|
+
properties.set('visible', { type: 'boolean' });
|
|
89
|
+
// Add user-defined properties
|
|
90
|
+
for (const [key, value] of Object.entries(node.properties)) {
|
|
91
|
+
properties.set(key, this.inferType(value));
|
|
92
|
+
}
|
|
93
|
+
this.typeMap.set(node.name, {
|
|
94
|
+
type: 'orb',
|
|
95
|
+
properties,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
collectMethodDeclaration(node) {
|
|
99
|
+
const parameters = node.parameters.map(p => ({
|
|
100
|
+
name: p.name,
|
|
101
|
+
type: this.parseTypeString(p.dataType),
|
|
102
|
+
optional: p.defaultValue !== undefined,
|
|
103
|
+
defaultValue: p.defaultValue,
|
|
104
|
+
}));
|
|
105
|
+
this.typeMap.set(node.name, {
|
|
106
|
+
type: 'function',
|
|
107
|
+
parameters,
|
|
108
|
+
returnType: node.returnType ? this.parseTypeString(node.returnType) : 'void',
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
collectVariableDeclaration(node) {
|
|
112
|
+
let type = 'any';
|
|
113
|
+
if (node.dataType) {
|
|
114
|
+
type = this.parseTypeString(node.dataType);
|
|
115
|
+
}
|
|
116
|
+
else if (node.value !== undefined) {
|
|
117
|
+
type = this.inferType(node.value).type;
|
|
118
|
+
}
|
|
119
|
+
this.typeMap.set(node.name, { type });
|
|
120
|
+
}
|
|
121
|
+
collectStreamDeclaration(node) {
|
|
122
|
+
this.typeMap.set(node.name, {
|
|
123
|
+
type: 'stream',
|
|
124
|
+
properties: new Map([
|
|
125
|
+
['source', { type: 'string' }],
|
|
126
|
+
['result', { type: 'any' }],
|
|
127
|
+
]),
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Check a node for type errors
|
|
132
|
+
*/
|
|
133
|
+
checkNode(node) {
|
|
134
|
+
this.currentLine = node.position?.x ?? 0;
|
|
135
|
+
this.currentColumn = node.position?.y ?? 0;
|
|
136
|
+
switch (node.type) {
|
|
137
|
+
case 'connection':
|
|
138
|
+
this.checkConnection(node);
|
|
139
|
+
break;
|
|
140
|
+
case 'gate':
|
|
141
|
+
this.checkGate(node);
|
|
142
|
+
break;
|
|
143
|
+
case 'for-loop':
|
|
144
|
+
this.checkForLoop(node);
|
|
145
|
+
break;
|
|
146
|
+
case 'while-loop':
|
|
147
|
+
this.checkWhileLoop(node);
|
|
148
|
+
break;
|
|
149
|
+
case 'foreach-loop':
|
|
150
|
+
this.checkForEachLoop(node);
|
|
151
|
+
break;
|
|
152
|
+
case 'import':
|
|
153
|
+
this.checkImport(node);
|
|
154
|
+
break;
|
|
155
|
+
case 'export':
|
|
156
|
+
this.checkExport(node);
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
checkConnection(node) {
|
|
161
|
+
const fromType = this.typeMap.get(node.from);
|
|
162
|
+
const toType = this.typeMap.get(node.to);
|
|
163
|
+
if (!fromType) {
|
|
164
|
+
this.addDiagnostic('error', `Unknown source '${node.from}' in connection`, 'E001');
|
|
165
|
+
}
|
|
166
|
+
if (!toType) {
|
|
167
|
+
this.addDiagnostic('error', `Unknown target '${node.to}' in connection`, 'E002');
|
|
168
|
+
}
|
|
169
|
+
// Check if types are compatible
|
|
170
|
+
if (fromType && toType && node.dataType !== 'any') {
|
|
171
|
+
// Warn if connecting incompatible types
|
|
172
|
+
if (fromType.type !== toType.type && fromType.type !== 'any' && toType.type !== 'any') {
|
|
173
|
+
this.addDiagnostic('warning', `Connection from '${node.from}' (${fromType.type}) to '${node.to}' (${toType.type}) may be incompatible`, 'W001', [`Consider using 'as "any"' to bypass type checking`]);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
checkGate(node) {
|
|
178
|
+
// Validate condition references valid variables
|
|
179
|
+
const conditionVars = this.extractVariables(node.condition);
|
|
180
|
+
for (const varName of conditionVars) {
|
|
181
|
+
if (!this.typeMap.has(varName) && !this.isLiteral(varName)) {
|
|
182
|
+
this.addDiagnostic('error', `Unknown variable '${varName}' in gate condition`, 'E003');
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
checkForLoop(node) {
|
|
187
|
+
// Check init references
|
|
188
|
+
const initVars = this.extractVariables(node.init);
|
|
189
|
+
// For loops can declare new variables in init, so we add them
|
|
190
|
+
for (const varName of initVars) {
|
|
191
|
+
if (!this.typeMap.has(varName)) {
|
|
192
|
+
this.typeMap.set(varName, { type: 'number' }); // Loop vars are typically numbers
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
// Check condition
|
|
196
|
+
const condVars = this.extractVariables(node.condition);
|
|
197
|
+
for (const varName of condVars) {
|
|
198
|
+
if (!this.typeMap.has(varName) && !this.isLiteral(varName)) {
|
|
199
|
+
this.addDiagnostic('error', `Unknown variable '${varName}' in for loop condition`, 'E004');
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
checkWhileLoop(node) {
|
|
204
|
+
const condVars = this.extractVariables(node.condition);
|
|
205
|
+
for (const varName of condVars) {
|
|
206
|
+
if (!this.typeMap.has(varName) && !this.isLiteral(varName)) {
|
|
207
|
+
this.addDiagnostic('error', `Unknown variable '${varName}' in while loop condition`, 'E005');
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
checkForEachLoop(node) {
|
|
212
|
+
// Check collection exists
|
|
213
|
+
if (!this.typeMap.has(node.collection)) {
|
|
214
|
+
this.addDiagnostic('error', `Unknown collection '${node.collection}' in forEach loop`, 'E006');
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
const collectionType = this.typeMap.get(node.collection);
|
|
218
|
+
if (collectionType && collectionType.type !== 'array' && collectionType.type !== 'any') {
|
|
219
|
+
this.addDiagnostic('error', `'${node.collection}' is not iterable (type: ${collectionType.type})`, 'E007');
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
// Add loop variable to scope
|
|
223
|
+
this.typeMap.set(node.variable, { type: 'any' });
|
|
224
|
+
}
|
|
225
|
+
checkImport(node) {
|
|
226
|
+
if (!node.modulePath) {
|
|
227
|
+
this.addDiagnostic('error', 'Import statement missing module path', 'E008');
|
|
228
|
+
}
|
|
229
|
+
// Register imported names as 'any' type (can't resolve external modules)
|
|
230
|
+
for (const name of node.imports) {
|
|
231
|
+
this.typeMap.set(name, { type: 'any' });
|
|
232
|
+
}
|
|
233
|
+
if (node.defaultImport) {
|
|
234
|
+
this.typeMap.set(node.defaultImport, { type: 'any' });
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
checkExport(node) {
|
|
238
|
+
if (node.exports) {
|
|
239
|
+
for (const name of node.exports) {
|
|
240
|
+
if (!this.typeMap.has(name)) {
|
|
241
|
+
this.addDiagnostic('error', `Cannot export unknown identifier '${name}'`, 'E009');
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Infer type from a value
|
|
248
|
+
*/
|
|
249
|
+
inferType(value) {
|
|
250
|
+
if (value === null || value === undefined) {
|
|
251
|
+
return { type: 'any', nullable: true };
|
|
252
|
+
}
|
|
253
|
+
if (typeof value === 'number') {
|
|
254
|
+
return { type: 'number' };
|
|
255
|
+
}
|
|
256
|
+
if (typeof value === 'string') {
|
|
257
|
+
return { type: 'string' };
|
|
258
|
+
}
|
|
259
|
+
if (typeof value === 'boolean') {
|
|
260
|
+
return { type: 'boolean' };
|
|
261
|
+
}
|
|
262
|
+
if (Array.isArray(value)) {
|
|
263
|
+
const elementType = value.length > 0 ? this.inferType(value[0]).type : 'any';
|
|
264
|
+
return { type: 'array', elementType };
|
|
265
|
+
}
|
|
266
|
+
if (typeof value === 'object') {
|
|
267
|
+
const properties = new Map();
|
|
268
|
+
for (const [key, val] of Object.entries(value)) {
|
|
269
|
+
properties.set(key, this.inferType(val));
|
|
270
|
+
}
|
|
271
|
+
return { type: 'object', properties };
|
|
272
|
+
}
|
|
273
|
+
return { type: 'any' };
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Parse type string to HoloScriptType
|
|
277
|
+
*/
|
|
278
|
+
parseTypeString(typeStr) {
|
|
279
|
+
const normalized = typeStr.toLowerCase().trim();
|
|
280
|
+
const validTypes = [
|
|
281
|
+
'number', 'string', 'boolean', 'array', 'object',
|
|
282
|
+
'function', 'void', 'any', 'unknown', 'never',
|
|
283
|
+
'orb', 'stream', 'connection', 'gate'
|
|
284
|
+
];
|
|
285
|
+
if (validTypes.includes(normalized)) {
|
|
286
|
+
return normalized;
|
|
287
|
+
}
|
|
288
|
+
return 'any';
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Extract variable names from an expression string
|
|
292
|
+
*/
|
|
293
|
+
extractVariables(expr) {
|
|
294
|
+
const varPattern = /\b([a-zA-Z_][a-zA-Z0-9_]*)\b/g;
|
|
295
|
+
const matches = expr.match(varPattern) || [];
|
|
296
|
+
const keywords = ['true', 'false', 'null', 'undefined', 'if', 'else', 'for', 'while', 'return'];
|
|
297
|
+
return matches.filter(m => !keywords.includes(m));
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Check if string is a literal
|
|
301
|
+
*/
|
|
302
|
+
isLiteral(str) {
|
|
303
|
+
// Number literal
|
|
304
|
+
if (/^-?\d+(\.\d+)?$/.test(str))
|
|
305
|
+
return true;
|
|
306
|
+
// Boolean
|
|
307
|
+
if (str === 'true' || str === 'false')
|
|
308
|
+
return true;
|
|
309
|
+
// String literal
|
|
310
|
+
if (/^["'].*["']$/.test(str))
|
|
311
|
+
return true;
|
|
312
|
+
return false;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Add a diagnostic
|
|
316
|
+
*/
|
|
317
|
+
addDiagnostic(severity, message, code, suggestions) {
|
|
318
|
+
this.diagnostics.push({
|
|
319
|
+
severity,
|
|
320
|
+
message,
|
|
321
|
+
line: this.currentLine,
|
|
322
|
+
column: this.currentColumn,
|
|
323
|
+
code,
|
|
324
|
+
suggestions,
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Get type info for a name
|
|
329
|
+
*/
|
|
330
|
+
getType(name) {
|
|
331
|
+
return this.typeMap.get(name);
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Get all registered types
|
|
335
|
+
*/
|
|
336
|
+
getAllTypes() {
|
|
337
|
+
return new Map(this.typeMap);
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Reset the type checker
|
|
341
|
+
*/
|
|
342
|
+
reset() {
|
|
343
|
+
this.typeMap.clear();
|
|
344
|
+
this.diagnostics = [];
|
|
345
|
+
// Re-add built-ins
|
|
346
|
+
BUILTIN_FUNCTIONS.forEach((type, name) => {
|
|
347
|
+
this.typeMap.set(name, type);
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Create a type checker instance
|
|
353
|
+
*/
|
|
354
|
+
export function createTypeChecker() {
|
|
355
|
+
return new HoloScriptTypeChecker();
|
|
356
|
+
}
|