@holoscript/core 1.0.0-alpha.2 → 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.
Files changed (74) hide show
  1. package/package.json +2 -2
  2. package/src/HoloScript2DParser.js +227 -0
  3. package/src/HoloScript2DParser.ts +5 -0
  4. package/src/HoloScriptCodeParser.js +1102 -0
  5. package/src/HoloScriptCodeParser.ts +145 -20
  6. package/src/HoloScriptDebugger.js +458 -0
  7. package/src/HoloScriptParser.js +338 -0
  8. package/src/HoloScriptPlusParser.js +371 -0
  9. package/src/HoloScriptPlusParser.ts +543 -0
  10. package/src/HoloScriptRuntime.js +1399 -0
  11. package/src/HoloScriptRuntime.test.js +351 -0
  12. package/src/HoloScriptRuntime.ts +17 -3
  13. package/src/HoloScriptTypeChecker.js +356 -0
  14. package/src/__tests__/GraphicsServices.test.js +357 -0
  15. package/src/__tests__/GraphicsServices.test.ts +427 -0
  16. package/src/__tests__/HoloScriptPlusParser.test.js +317 -0
  17. package/src/__tests__/HoloScriptPlusParser.test.ts +392 -0
  18. package/src/__tests__/integration.test.js +336 -0
  19. package/src/__tests__/performance.bench.js +218 -0
  20. package/src/__tests__/type-checker.test.js +60 -0
  21. package/src/__tests__/type-checker.test.ts +73 -0
  22. package/src/index.js +217 -0
  23. package/src/index.ts +158 -18
  24. package/src/interop/Interoperability.js +413 -0
  25. package/src/interop/Interoperability.ts +494 -0
  26. package/src/logger.js +42 -0
  27. package/src/parser/EnhancedParser.js +205 -0
  28. package/src/parser/EnhancedParser.ts +251 -0
  29. package/src/parser/HoloScriptPlusParser.js +928 -0
  30. package/src/parser/HoloScriptPlusParser.ts +1089 -0
  31. package/src/runtime/HoloScriptPlusRuntime.js +674 -0
  32. package/src/runtime/HoloScriptPlusRuntime.ts +861 -0
  33. package/src/runtime/PerformanceTelemetry.js +323 -0
  34. package/src/runtime/PerformanceTelemetry.ts +467 -0
  35. package/src/runtime/RuntimeOptimization.js +361 -0
  36. package/src/runtime/RuntimeOptimization.ts +416 -0
  37. package/src/services/HololandGraphicsPipelineService.js +506 -0
  38. package/src/services/HololandGraphicsPipelineService.ts +662 -0
  39. package/src/services/PlatformPerformanceOptimizer.js +356 -0
  40. package/src/services/PlatformPerformanceOptimizer.ts +503 -0
  41. package/src/state/ReactiveState.js +427 -0
  42. package/src/state/ReactiveState.ts +572 -0
  43. package/src/tools/DeveloperExperience.js +376 -0
  44. package/src/tools/DeveloperExperience.ts +438 -0
  45. package/src/traits/AIDriverTrait.js +322 -0
  46. package/src/traits/AIDriverTrait.test.js +329 -0
  47. package/src/traits/AIDriverTrait.test.ts +357 -0
  48. package/src/traits/AIDriverTrait.ts +474 -0
  49. package/src/traits/LightingTrait.js +313 -0
  50. package/src/traits/LightingTrait.test.js +410 -0
  51. package/src/traits/LightingTrait.test.ts +462 -0
  52. package/src/traits/LightingTrait.ts +505 -0
  53. package/src/traits/MaterialTrait.js +194 -0
  54. package/src/traits/MaterialTrait.test.js +286 -0
  55. package/src/traits/MaterialTrait.test.ts +329 -0
  56. package/src/traits/MaterialTrait.ts +324 -0
  57. package/src/traits/RenderingTrait.js +356 -0
  58. package/src/traits/RenderingTrait.test.js +363 -0
  59. package/src/traits/RenderingTrait.test.ts +427 -0
  60. package/src/traits/RenderingTrait.ts +555 -0
  61. package/src/traits/VRTraitSystem.js +740 -0
  62. package/src/traits/VRTraitSystem.ts +1040 -0
  63. package/src/traits/VoiceInputTrait.js +284 -0
  64. package/src/traits/VoiceInputTrait.test.js +226 -0
  65. package/src/traits/VoiceInputTrait.test.ts +252 -0
  66. package/src/traits/VoiceInputTrait.ts +401 -0
  67. package/src/types/AdvancedTypeSystem.js +226 -0
  68. package/src/types/AdvancedTypeSystem.ts +494 -0
  69. package/src/types/HoloScriptPlus.d.ts +853 -0
  70. package/src/types.js +6 -0
  71. package/src/types.ts +96 -1
  72. package/tsconfig.json +1 -1
  73. package/tsup.config.d.ts +2 -0
  74. package/tsup.config.js +18 -0
@@ -0,0 +1,543 @@
1
+ /**
2
+ * HoloScriptPlus Parser - Extended DSL with Trait Annotations
3
+ *
4
+ * Extends HoloScript with support for:
5
+ * - @material trait annotations for PBR materials
6
+ * - @lighting trait annotations for dynamic lighting
7
+ * - @rendering trait annotations for GPU optimization
8
+ *
9
+ * Syntax:
10
+ * orb#sphere {
11
+ * @material { type: pbr, metallic: 0.5, roughness: 0.4 }
12
+ * @lighting { preset: studio, shadows: true }
13
+ * @rendering { quality: high, lod: true }
14
+ * }
15
+ */
16
+
17
+ import { HoloScriptCodeParser } from './HoloScriptCodeParser';
18
+ import type {
19
+ ASTNode,
20
+ OrbNode,
21
+ } from './types';
22
+
23
+ // ============================================================================
24
+ // Trait Annotation Types
25
+ // ============================================================================
26
+
27
+ export interface TraitAnnotation {
28
+ type: 'material' | 'lighting' | 'rendering';
29
+ config: Record<string, unknown>;
30
+ line?: number;
31
+ column?: number;
32
+ }
33
+
34
+ export interface MaterialTraitAnnotation extends TraitAnnotation {
35
+ type: 'material';
36
+ config: {
37
+ type?: string;
38
+ pbr?: {
39
+ baseColor?: { r: number; g: number; b: number };
40
+ metallic?: number;
41
+ roughness?: number;
42
+ ambientOcclusion?: number;
43
+ emission?: { r: number; g: number; b: number };
44
+ emissionStrength?: number;
45
+ };
46
+ textures?: Array<{ path: string; channel: string }>;
47
+ compression?: 'none' | 'dxt' | 'astc' | 'basis';
48
+ instancing?: boolean;
49
+ streaming?: boolean;
50
+ };
51
+ }
52
+
53
+ export interface LightingTraitAnnotation extends TraitAnnotation {
54
+ type: 'lighting';
55
+ config: {
56
+ preset?: 'studio' | 'outdoor' | 'interior' | 'night' | 'sunset';
57
+ lights?: Array<{
58
+ type: 'directional' | 'point' | 'spot' | 'area' | 'ambient';
59
+ position?: { x: number; y: number; z: number };
60
+ direction?: { x: number; y: number; z: number };
61
+ color?: { r: number; g: number; b: number };
62
+ intensity?: number;
63
+ range?: number;
64
+ shadows?: boolean;
65
+ }>;
66
+ globalIllumination?: {
67
+ skyColor?: { r: number; g: number; b: number };
68
+ groundColor?: { r: number; g: number; b: number };
69
+ probes?: number;
70
+ };
71
+ shadows?: boolean;
72
+ ao?: boolean;
73
+ };
74
+ }
75
+
76
+ export interface RenderingTraitAnnotation extends TraitAnnotation {
77
+ type: 'rendering';
78
+ config: {
79
+ quality?: 'low' | 'medium' | 'high' | 'ultra';
80
+ platform?: 'mobile' | 'vr' | 'desktop';
81
+ lod?: boolean;
82
+ culling?: boolean;
83
+ batching?: boolean;
84
+ instancing?: boolean;
85
+ maxTextureResolution?: number;
86
+ compression?: 'none' | 'dxt' | 'astc' | 'basis';
87
+ targetFPS?: number;
88
+ };
89
+ }
90
+
91
+ export type AnyTraitAnnotation =
92
+ | MaterialTraitAnnotation
93
+ | LightingTraitAnnotation
94
+ | RenderingTraitAnnotation;
95
+
96
+ // ============================================================================
97
+ // Enhanced OrbNode with Graphics Traits
98
+ // ============================================================================
99
+
100
+ export interface GraphicsConfiguration {
101
+ material?: MaterialTraitAnnotation['config'];
102
+ lighting?: LightingTraitAnnotation['config'];
103
+ rendering?: RenderingTraitAnnotation['config'];
104
+ }
105
+
106
+ export interface EnhancedOrbNode extends OrbNode {
107
+ graphics?: GraphicsConfiguration;
108
+ traits?: AnyTraitAnnotation[];
109
+ }
110
+
111
+ // ============================================================================
112
+ // HoloScriptPlus Parser
113
+ // ============================================================================
114
+
115
+ export class HoloScriptPlusParser {
116
+ private baseParser: HoloScriptCodeParser;
117
+
118
+ constructor() {
119
+ this.baseParser = new HoloScriptCodeParser();
120
+ }
121
+
122
+ /**
123
+ * Parse HoloScript+ code with trait annotations
124
+ */
125
+ parse(code: string): ASTNode[] {
126
+ // First, parse with base parser
127
+ const baseResult = this.baseParser.parse(code);
128
+ const ast = (Array.isArray(baseResult) ? baseResult : [baseResult]) as ASTNode[];
129
+
130
+ // Then enhance with trait annotations
131
+ return this.enhanceWithTraits(ast, code);
132
+ }
133
+
134
+ /**
135
+ * Enhance AST nodes with trait annotations
136
+ */
137
+ private enhanceWithTraits(ast: ASTNode[], code: string): ASTNode[] {
138
+ return ast.map((node) => {
139
+ if (node.type === 'orb') {
140
+ return this.enhanceOrbNodeWithTraits(node, code);
141
+ }
142
+ return node;
143
+ });
144
+ }
145
+
146
+ /**
147
+ * Enhance OrbNode with trait annotations
148
+ */
149
+ private enhanceOrbNodeWithTraits(node: ASTNode, code: string): EnhancedOrbNode {
150
+ const orbNode = node as OrbNode;
151
+ const enhanced: EnhancedOrbNode = {
152
+ ...orbNode,
153
+ traits: [],
154
+ graphics: {},
155
+ };
156
+
157
+ // Find trait annotations in the code near this node
158
+ const traits = this.extractTraitAnnotations(code, node.line);
159
+
160
+ enhanced.traits = traits;
161
+
162
+ // Build graphics configuration from traits
163
+ if (traits.length > 0) {
164
+ enhanced.graphics = this.buildGraphicsConfig(traits);
165
+ }
166
+
167
+ return enhanced;
168
+ }
169
+
170
+ /**
171
+ * Extract trait annotations from code
172
+ */
173
+ extractTraitAnnotations(code: string, _orbLine?: number): AnyTraitAnnotation[] {
174
+ const traits: AnyTraitAnnotation[] = [];
175
+ const traitRegex = /@(material|lighting|rendering)\s*\{([^}]*(?:\{[^}]*\}[^}]*)*)\}/g;
176
+
177
+ let match;
178
+ while ((match = traitRegex.exec(code)) !== null) {
179
+ const type = match[1] as 'material' | 'lighting' | 'rendering';
180
+ const configStr = match[2];
181
+
182
+ try {
183
+ const config = this.parseObjectLiteral(configStr);
184
+
185
+ switch (type) {
186
+ case 'material':
187
+ traits.push({
188
+ type: 'material',
189
+ config: config as MaterialTraitAnnotation['config'],
190
+ });
191
+ break;
192
+
193
+ case 'lighting':
194
+ traits.push({
195
+ type: 'lighting',
196
+ config: config as LightingTraitAnnotation['config'],
197
+ });
198
+ break;
199
+
200
+ case 'rendering':
201
+ traits.push({
202
+ type: 'rendering',
203
+ config: config as RenderingTraitAnnotation['config'],
204
+ });
205
+ break;
206
+ }
207
+ } catch (e) {
208
+ console.warn(`Failed to parse ${type} trait annotation:`, e);
209
+ }
210
+ }
211
+
212
+ return traits;
213
+ }
214
+
215
+ /**
216
+ * Parse object literal from string
217
+ * Supports nested objects and arrays
218
+ */
219
+ parseObjectLiteral(str: string): Record<string, unknown> {
220
+ const config: Record<string, unknown> = {};
221
+
222
+ // Split by comma, but respect nested braces and brackets
223
+ let depth = 0;
224
+ let current = '';
225
+ let pairs: string[] = [];
226
+
227
+ for (let i = 0; i < str.length; i++) {
228
+ const char = str[i];
229
+
230
+ if (char === '{' || char === '[') {
231
+ depth++;
232
+ } else if (char === '}' || char === ']') {
233
+ depth--;
234
+ } else if (char === ',' && depth === 0) {
235
+ pairs.push(current.trim());
236
+ current = '';
237
+ continue;
238
+ }
239
+
240
+ current += char;
241
+ }
242
+
243
+ if (current.trim()) {
244
+ pairs.push(current.trim());
245
+ }
246
+
247
+ // Parse each key:value pair
248
+ for (const pair of pairs) {
249
+ const colonIndex = pair.indexOf(':');
250
+ if (colonIndex === -1) continue;
251
+
252
+ const key = pair.substring(0, colonIndex).trim();
253
+ const value = pair.substring(colonIndex + 1).trim();
254
+
255
+ config[key] = this.parseValue(value);
256
+ }
257
+
258
+ return config;
259
+ }
260
+
261
+ /**
262
+ * Parse individual values
263
+ */
264
+ parseValue(str: string): unknown {
265
+ str = str.trim();
266
+
267
+ // Boolean
268
+ if (str === 'true') return true;
269
+ if (str === 'false') return false;
270
+
271
+ // Number
272
+ if (/^-?\d+(\.\d+)?$/.test(str)) return parseFloat(str);
273
+
274
+ // String
275
+ if ((str.startsWith('"') && str.endsWith('"')) || (str.startsWith("'") && str.endsWith("'"))) {
276
+ return str.slice(1, -1);
277
+ }
278
+
279
+ // Color object: { r: 0.8, g: 0.2, b: 0.2 }
280
+ if (str.startsWith('{') && str.endsWith('}')) {
281
+ return this.parseObjectLiteral(str.slice(1, -1));
282
+ }
283
+
284
+ // Array: [1, 2, 3]
285
+ if (str.startsWith('[') && str.endsWith(']')) {
286
+ const items = str.slice(1, -1).split(',');
287
+ return items.map((item) => this.parseValue(item));
288
+ }
289
+
290
+ return str;
291
+ }
292
+
293
+ /**
294
+ * Build GraphicsConfiguration from trait annotations
295
+ */
296
+ buildGraphicsConfig(traits: AnyTraitAnnotation[]): GraphicsConfiguration {
297
+ const config: GraphicsConfiguration = {};
298
+
299
+ for (const trait of traits) {
300
+ switch (trait.type) {
301
+ case 'material':
302
+ config.material = (trait as MaterialTraitAnnotation).config;
303
+ break;
304
+
305
+ case 'lighting':
306
+ config.lighting = (trait as LightingTraitAnnotation).config;
307
+ break;
308
+
309
+ case 'rendering':
310
+ config.rendering = (trait as RenderingTraitAnnotation).config;
311
+ break;
312
+ }
313
+ }
314
+
315
+ return config;
316
+ }
317
+
318
+ /**
319
+ * Validate trait annotation configuration
320
+ */
321
+ validateTraitAnnotation(trait: AnyTraitAnnotation): { valid: boolean; errors: string[] } {
322
+ const errors: string[] = [];
323
+
324
+ switch (trait.type) {
325
+ case 'material':
326
+ errors.push(...this.validateMaterialTrait(trait as MaterialTraitAnnotation));
327
+ break;
328
+
329
+ case 'lighting':
330
+ errors.push(...this.validateLightingTrait(trait as LightingTraitAnnotation));
331
+ break;
332
+
333
+ case 'rendering':
334
+ errors.push(...this.validateRenderingTrait(trait as RenderingTraitAnnotation));
335
+ break;
336
+ }
337
+
338
+ return {
339
+ valid: errors.length === 0,
340
+ errors,
341
+ };
342
+ }
343
+
344
+ /**
345
+ * Validate material trait configuration
346
+ */
347
+ private validateMaterialTrait(trait: MaterialTraitAnnotation): string[] {
348
+ const errors: string[] = [];
349
+ const { config } = trait;
350
+
351
+ if (config.pbr) {
352
+ if (config.pbr.metallic !== undefined && (config.pbr.metallic < 0 || config.pbr.metallic > 1)) {
353
+ errors.push('material.pbr.metallic must be between 0 and 1');
354
+ }
355
+
356
+ if (config.pbr.roughness !== undefined && (config.pbr.roughness < 0 || config.pbr.roughness > 1)) {
357
+ errors.push('material.pbr.roughness must be between 0 and 1');
358
+ }
359
+ }
360
+
361
+ if (config.compression && !['none', 'dxt', 'astc', 'basis'].includes(config.compression)) {
362
+ errors.push(`material.compression must be one of: none, dxt, astc, basis`);
363
+ }
364
+
365
+ return errors;
366
+ }
367
+
368
+ /**
369
+ * Validate lighting trait configuration
370
+ */
371
+ private validateLightingTrait(trait: LightingTraitAnnotation): string[] {
372
+ const errors: string[] = [];
373
+ const { config } = trait;
374
+
375
+ if (config.preset && !['studio', 'outdoor', 'interior', 'night', 'sunset'].includes(config.preset)) {
376
+ errors.push('lighting.preset must be one of: studio, outdoor, interior, night, sunset');
377
+ }
378
+
379
+ if (config.lights) {
380
+ config.lights.forEach((light, index) => {
381
+ if (!['directional', 'point', 'spot', 'area', 'ambient'].includes(light.type)) {
382
+ errors.push(`lighting.lights[${index}].type must be a valid light type`);
383
+ }
384
+
385
+ if (light.intensity !== undefined && light.intensity < 0) {
386
+ errors.push(`lighting.lights[${index}].intensity must be >= 0`);
387
+ }
388
+ });
389
+ }
390
+
391
+ return errors;
392
+ }
393
+
394
+ /**
395
+ * Validate rendering trait configuration
396
+ */
397
+ private validateRenderingTrait(trait: RenderingTraitAnnotation): string[] {
398
+ const errors: string[] = [];
399
+ const { config } = trait;
400
+
401
+ if (config.quality && !['low', 'medium', 'high', 'ultra'].includes(config.quality)) {
402
+ errors.push('rendering.quality must be one of: low, medium, high, ultra');
403
+ }
404
+
405
+ if (config.platform && !['mobile', 'vr', 'desktop'].includes(config.platform)) {
406
+ errors.push('rendering.platform must be one of: mobile, vr, desktop');
407
+ }
408
+
409
+ if (config.compression && !['none', 'dxt', 'astc', 'basis'].includes(config.compression)) {
410
+ errors.push('rendering.compression must be one of: none, dxt, astc, basis');
411
+ }
412
+
413
+ if (config.maxTextureResolution && config.maxTextureResolution < 128) {
414
+ errors.push('rendering.maxTextureResolution must be >= 128');
415
+ }
416
+
417
+ if (config.targetFPS && (config.targetFPS < 24 || config.targetFPS > 240)) {
418
+ errors.push('rendering.targetFPS must be between 24 and 240');
419
+ }
420
+
421
+ return errors;
422
+ }
423
+
424
+ /**
425
+ * Get trait annotations as graphics traits
426
+ */
427
+ createGraphicsTraits(config: GraphicsConfiguration): any {
428
+ // This will be called by the runtime to create actual trait instances
429
+ return {
430
+ material: config.material ? this.createMaterialTrait(config.material) : null,
431
+ lighting: config.lighting ? this.createLightingTrait(config.lighting) : null,
432
+ rendering: config.rendering ? this.createRenderingTrait(config.rendering) : null,
433
+ };
434
+ }
435
+
436
+ /**
437
+ * Create MaterialTrait from config
438
+ */
439
+ private createMaterialTrait(config: any): any {
440
+ // Lazy import to avoid circular dependencies
441
+ const { MaterialTrait } = require('./traits/MaterialTrait');
442
+
443
+ const material = new MaterialTrait({
444
+ type: config.type || 'pbr',
445
+ pbr: config.pbr,
446
+ });
447
+
448
+ if (config.compression) {
449
+ material.setCompression(config.compression);
450
+ }
451
+
452
+ if (config.instancing) {
453
+ material.setInstanced(true);
454
+ }
455
+
456
+ if (config.streaming) {
457
+ material.setTextureStreaming(true);
458
+ }
459
+
460
+ if (config.textures) {
461
+ config.textures.forEach((tex: any) => {
462
+ material.addTexture(tex);
463
+ });
464
+ }
465
+
466
+ return material;
467
+ }
468
+
469
+ /**
470
+ * Create LightingTrait from config
471
+ */
472
+ private createLightingTrait(config: any): any {
473
+ const { LightingTrait, LIGHTING_PRESETS } = require('./traits/LightingTrait');
474
+
475
+ let lighting: any;
476
+
477
+ if (config.preset) {
478
+ const preset = LIGHTING_PRESETS[config.preset];
479
+ lighting = new LightingTrait(preset);
480
+ } else {
481
+ lighting = new LightingTrait();
482
+ }
483
+
484
+ if (config.globalIllumination) {
485
+ lighting.setGlobalIllumination(config.globalIllumination);
486
+ }
487
+
488
+ if (config.lights) {
489
+ config.lights.forEach((light: any) => {
490
+ lighting.addLight(light);
491
+ });
492
+ }
493
+
494
+ return lighting;
495
+ }
496
+
497
+ /**
498
+ * Create RenderingTrait from config
499
+ */
500
+ private createRenderingTrait(config: any): any {
501
+ const { RenderingTrait } = require('./traits/RenderingTrait');
502
+
503
+ const rendering = new RenderingTrait();
504
+
505
+ if (config.quality) {
506
+ rendering.applyQualityPreset(config.quality);
507
+ }
508
+
509
+ if (config.platform) {
510
+ switch (config.platform) {
511
+ case 'mobile':
512
+ rendering.optimizeForMobile();
513
+ break;
514
+ case 'vr':
515
+ rendering.optimizeForVRAR(config.targetFPS || 90);
516
+ break;
517
+ case 'desktop':
518
+ rendering.optimizeForDesktop();
519
+ break;
520
+ }
521
+ }
522
+
523
+ if (config.lod !== false) {
524
+ rendering.setupLODLevels('automatic');
525
+ }
526
+
527
+ if (config.culling !== false) {
528
+ rendering.setFrustumCulling(true);
529
+ }
530
+
531
+ if (config.compression) {
532
+ rendering.setTextureCompression(config.compression);
533
+ }
534
+
535
+ if (config.maxTextureResolution) {
536
+ rendering.setMaxTextureResolution(config.maxTextureResolution);
537
+ }
538
+
539
+ return rendering;
540
+ }
541
+ }
542
+
543
+ export default HoloScriptPlusParser;