massive overhall to how program memory works and how artifacts their metada are rendered

This commit is contained in:
Kurt Hutten IrevDev
2023-01-08 16:37:31 +11:00
parent 0515acf459
commit 9ad6b946c0
13 changed files with 1090 additions and 884 deletions

View File

@ -1,9 +1,9 @@
import { abstractSyntaxTree } from './abstractSyntaxTree'
import { lexer } from './tokeniser'
import { executor, ViewerArtifact, processShownObjects } from './executor'
import { executor, SketchGroup, ExtrudeGroup } from './executor'
describe('findClosingBrace', () => {
test('finds the closing brace', () => {
describe('testing artifacts', () => {
test('sketch artifacts', () => {
const code = `
sketch mySketch001 {
lineTo(-1.59, -1.54)
@ -12,34 +12,86 @@ sketch mySketch001 {
|> rx(45, %)
show(mySketch001)`
const programMemory = executor(abstractSyntaxTree(lexer(code)))
const geos: ViewerArtifact[] =
programMemory?.return?.flatMap(
({ name }: { name: string }) =>
processShownObjects(programMemory, programMemory?.root?.[name]) || []
) || []
const artifactsWithouGeos = removeGeo(geos)
expect(artifactsWithouGeos).toEqual([
const geos = programMemory?.return?.map(
(a) => programMemory?.root?.[a.name]
)
const artifactsWithoutGeos = removeGeo(geos as any)
expect(artifactsWithoutGeos).toEqual([
{
type: 'parent',
sourceRange: [74, 83],
children: [
type: 'sketchGroup',
value: [
{
type: 'toPoint',
to: [-1.59, -1.54],
from: [0, 0],
__geoMeta: {
sourceRange: [24, 44],
pathToNode: [],
geos: ['line', 'lineEnd'],
},
},
{
type: 'toPoint',
to: [0.46, -5.82],
from: [-1.59, -1.54],
__geoMeta: {
sourceRange: [47, 66],
pathToNode: [],
geos: ['line', 'lineEnd'],
},
},
],
position: [0, 0, 0],
rotation: [0.3826834323650898, 0, 0, 0.9238795325112867],
__meta: [
{
type: 'sketch',
sourceRange: [20, 68],
children: [
{
type: 'sketchBase',
sourceRange: [0, 0],
},
{
type: 'sketchLine',
sourceRange: [24, 44],
},
{
type: 'sketchLine',
sourceRange: [47, 66],
},
],
pathToNode: ['body', 0, 'declarations', 0, 'init', 0],
},
{
sourceRange: [74, 83],
pathToNode: [],
},
],
},
])
})
test('extrude artifacts', () => {
const code = `
sketch mySketch001 {
lineTo(-1.59, -1.54)
lineTo(0.46, -5.82)
}
|> rx(45, %)
|> extrude(2, %)
show(mySketch001)`
const programMemory = executor(abstractSyntaxTree(lexer(code)))
const geos = programMemory?.return?.map(
(a) => programMemory?.root?.[a.name]
)
const artifactsWithoutGeos = removeGeo(geos as any)
expect(artifactsWithoutGeos).toEqual([
{
type: 'extrudeGroup',
value: [
{
type: 'extrudePlane',
position: [0, 0, 0],
rotation: [0.3826834323650898, 0, 0, 0.9238795325112867],
__geoMeta: {
geo: 'PlaneGeometry',
sourceRange: [47, 66],
pathToNode: [],
},
},
],
height: 2,
position: [0, 0, 0],
rotation: [0.3826834323650898, 0, 0, 0.9238795325112867],
__meta: [
{
sourceRange: [89, 102],
pathToNode: [],
},
],
},
@ -47,24 +99,29 @@ show(mySketch001)`
})
})
function removeGeo(arts: ViewerArtifact[]): any {
function removeGeo(arts: (SketchGroup | ExtrudeGroup)[]): any {
return arts.map((art) => {
if (art.type === 'sketchLine' || art.type === 'sketchBase') {
const { geo, ...rest } = art
return rest
}
if (art.type === 'parent') {
if (art.type === 'extrudeGroup') {
return {
...art,
children: removeGeo(art.children),
value: art.value.map((v) => ({
...v,
__geoMeta: {
...v.__geoMeta,
geo: v.__geoMeta.geo.type,
},
})),
}
}
if (art.type === 'sketch') {
return {
...art,
children: removeGeo(art.children),
}
return {
...art,
value: art.value.map((v) => ({
...v,
__geoMeta: {
...v.__geoMeta,
geos: v.__geoMeta.geos.map((g) => g.type),
},
})),
}
return art
})
}