-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOSCompiler.ts
More file actions
251 lines (223 loc) · 7.96 KB
/
Copy pathIOSCompiler.ts
File metadata and controls
251 lines (223 loc) · 7.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import type { Vector3 } from '../types';
/**
* HoloScript → iOS Swift ARKit Compiler
*
* Translates a HoloComposition AST into Swift code targeting
* ARKit for iPhone and iPad augmented reality experiences.
*
* Emits:
* - SwiftUI + ARKit integration
* - ARSCNView with SceneKit nodes
* - Plane detection and hit testing
* - World tracking configuration
* - Gesture recognizers for interaction
* - Spatial audio with SceneKit
*
* @version 1.0.0
*/
import { CompilerBase, type CompilerToken } from './CompilerBase';
import { ANSCapabilityPath, type ANSCapabilityPathValue } from '@holoscript/core-types/ans';
import type {
HoloComposition,
HoloObjectDecl,
HoloLight,
HoloAudio,
HoloValue,
} from '../parser/HoloCompositionTypes';
import type { IOSCompileResult } from './CompilerTypes';
export type { IOSCompileResult } from './CompilerTypes';
import {
generateViewFile,
generateSceneFile,
generateStateFile,
generateInfoPlist,
} from './IOSARGenerators';
import {
hasRoomPlanTraits,
generateRoomPlanFile,
hasLiDARTraits,
generateLiDARScannerFile,
hasNPUSceneTraits,
generateNPUSceneFile,
hasPortalARTraits,
generatePortalARFile,
} from './IOSSpatialGenerators';
import {
hasHandTrackingTraits,
generateHandTrackingFile,
hasObjectCaptureTraits,
generateObjectCaptureFile,
hasFaceTrackingTraits,
generateFaceTrackingFile,
hasSharePlayTraits,
generateSharePlayFile,
hasUWBPositioningTraits,
generateUWBPositioningFile,
hasSpatialAudioTraits,
generateSpatialAudioFile,
} from './IOSPeripheralGenerators';
export interface IOSCompilerOptions {
className?: string;
indent?: string;
iosVersion?: '15.0' | '16.0' | '17.0' | '18.0';
useSwiftUI?: boolean;
useCombine?: boolean;
useRealityKit?: boolean; // For newer iOS, RealityKit is preferred
}
export class IOSCompiler extends CompilerBase {
protected readonly compilerName = 'IOSCompiler';
protected override getRequiredCapability(): ANSCapabilityPathValue {
return ANSCapabilityPath.IOS;
}
public options: Required<IOSCompilerOptions>;
public lines: string[] = [];
public indentLevel: number = 0;
constructor(options: IOSCompilerOptions = {}) {
super();
this.options = {
className: options.className || 'GeneratedARScene',
indent: options.indent || ' ',
iosVersion: options.iosVersion || '17.0',
useSwiftUI: options.useSwiftUI ?? true,
useCombine: options.useCombine ?? true,
useRealityKit: options.useRealityKit ?? false,
};
}
compile(
composition: HoloComposition,
agentToken: CompilerToken,
outputPath?: string
): IOSCompileResult {
this.validateCompilerAccess(agentToken, outputPath);
const result: IOSCompileResult = {
viewFile: generateViewFile(this, composition),
sceneFile: generateSceneFile(this, composition),
stateFile: generateStateFile(this, composition),
infoPlist: generateInfoPlist(this, composition),
};
if (hasRoomPlanTraits(composition)) {
result.roomPlanFile = generateRoomPlanFile(this, composition);
}
if (hasLiDARTraits(composition)) {
result.lidarScannerFile = generateLiDARScannerFile(this, composition);
}
if (hasNPUSceneTraits(composition)) {
result.npuSceneFile = generateNPUSceneFile(this, composition);
}
if (hasPortalARTraits(composition)) {
result.portalARFile = generatePortalARFile(this, composition);
}
if (hasHandTrackingTraits(composition)) {
result.handTrackingFile = generateHandTrackingFile(this, composition);
}
if (hasObjectCaptureTraits(composition)) {
result.objectCaptureFile = generateObjectCaptureFile(this, composition);
}
if (hasFaceTrackingTraits(composition)) {
result.faceTrackingFile = generateFaceTrackingFile(this, composition);
}
if (hasSharePlayTraits(composition)) {
result.sharePlayFile = generateSharePlayFile(this, composition);
}
if (hasUWBPositioningTraits(composition)) {
result.uwbPositioningFile = generateUWBPositioningFile(this, composition);
}
if (hasSpatialAudioTraits(composition)) {
result.spatialAudioFile = generateSpatialAudioFile(this, composition);
}
return result;
}
public emit(line: string): void {
const indent = this.options.indent.repeat(this.indentLevel);
this.lines.push(indent + line);
}
public sanitizeName(name: string): string {
const result = name.replace(/[^a-zA-Z0-9_]/g, '_').replace(/^[0-9]/, '_$&');
return result.charAt(0).toUpperCase() + result.slice(1);
}
public getSceneKitGeometry(meshType: string): string {
const geometries: Record<string, string> = {
cube: 'SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)',
box: 'SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)',
sphere: 'SCNSphere(radius: 0.05)',
cylinder: 'SCNCylinder(radius: 0.05, height: 0.1)',
cone: 'SCNCone(topRadius: 0, bottomRadius: 0.05, height: 0.1)',
capsule: 'SCNCapsule(capRadius: 0.025, height: 0.1)',
plane: 'SCNPlane(width: 0.2, height: 0.2)',
torus: 'SCNTorus(ringRadius: 0.05, pipeRadius: 0.02)',
};
return geometries[meshType] || 'SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)';
}
public findObjProp(obj: HoloObjectDecl, key: string): HoloValue | undefined {
return obj.properties?.find((p) => p.key === key)?.value;
}
public toSwiftType(value: HoloValue): string {
if (value === null) return 'Any?';
if (typeof value === 'boolean') return 'Bool';
if (typeof value === 'number') return Number.isInteger(value) ? 'Int' : 'Double';
if (typeof value === 'string') return 'String';
if (Array.isArray(value)) {
if (value.length === 3 && value.every((v) => typeof v === 'number')) return 'SCNVector3';
return '[Any]';
}
return 'Any';
}
public toSwiftValue(value: HoloValue): string {
if (value === null) return 'nil';
if (typeof value === 'boolean') return value ? 'true' : 'false';
if (typeof value === 'number') return `${value}`;
if (typeof value === 'string') return `"${this.escapeStringValue(value, 'Swift')}"`;
if (Array.isArray(value)) {
if (value.length === 3 && value.every((v) => typeof v === 'number')) {
return `SCNVector3(${value[0]}, ${value[1]}, ${value[2]})`;
}
return `[${value.map((v) => this.toSwiftValue(v)).join(', ')}]`;
}
return 'nil';
}
public toUIColor(value: HoloValue): string {
if (typeof value === 'string') {
if (value.startsWith('#')) {
const hex = value.slice(1);
if (hex.length === 6) {
const r = parseInt(hex.slice(0, 2), 16) / 255;
const g = parseInt(hex.slice(2, 4), 16) / 255;
const b = parseInt(hex.slice(4, 6), 16) / 255;
return `UIColor(red: ${r}, green: ${g}, blue: ${b}, alpha: 1.0)`;
}
}
const colors: Record<string, string> = {
red: 'UIColor.systemRed',
green: 'UIColor.systemGreen',
blue: 'UIColor.systemBlue',
white: 'UIColor.white',
black: 'UIColor.black',
yellow: 'UIColor.systemYellow',
cyan: 'UIColor.systemCyan',
magenta: 'UIColor.systemPink',
};
return colors[value.toLowerCase()] || 'UIColor.systemBlue';
}
if (Array.isArray(value) && value.length >= 3) {
const [r, g, b, a = 1] = value as number[];
return `UIColor(red: ${r}, green: ${g}, blue: ${b}, alpha: ${a})`;
}
return 'UIColor.systemBlue';
}
public compositionHasTrait(composition: HoloComposition, traitName: string): boolean {
for (const obj of composition.objects || []) {
for (const trait of obj.traits || []) {
const name = typeof trait === 'string' ? trait : trait.name;
if (name === traitName) return true;
}
}
return false;
}
}
export function compileToIOS(
composition: HoloComposition,
options?: IOSCompilerOptions
): IOSCompileResult {
const compiler = new IOSCompiler(options);
return compiler.compile(composition, 'test-token');
}