fix execution of piped sketches
This commit is contained in:
		@ -1243,10 +1243,10 @@ function debuggerr(tokens: Token[], indexes: number[], msg=''): string {
 | 
			
		||||
    topString += top
 | 
			
		||||
    bottomString += bottom
 | 
			
		||||
  })
 | 
			
		||||
  const result = [`${msg} - debuggerr: ${sortedIndexes}`, topString, bottomString].join(
 | 
			
		||||
  const debugResult = [`${msg} - debuggerr: ${sortedIndexes}`, topString, bottomString].join(
 | 
			
		||||
    '\n'
 | 
			
		||||
  )
 | 
			
		||||
  console.log(result)
 | 
			
		||||
  return result
 | 
			
		||||
  console.log(debugResult)
 | 
			
		||||
  return debugResult
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -3,6 +3,7 @@ import fs from 'node:fs'
 | 
			
		||||
import { abstractSyntaxTree } from './abstractSyntaxTree'
 | 
			
		||||
import { lexer } from './tokeniser'
 | 
			
		||||
import { executor, ProgramMemory } from './executor'
 | 
			
		||||
import { Transform } from './sketch'
 | 
			
		||||
 | 
			
		||||
describe('test', () => {
 | 
			
		||||
  it('test assigning two variables, the second summing with the first', () => {
 | 
			
		||||
@ -97,7 +98,61 @@ show(mySketch)
 | 
			
		||||
    expect(root.myVar).toBe(7)
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  
 | 
			
		||||
  it('rotated sketch', () => {
 | 
			
		||||
    const code = [
 | 
			
		||||
      'sketch mySk1 {',
 | 
			
		||||
      '  lineTo(1,1)',
 | 
			
		||||
      '  path myPath = lineTo(0, 1)',
 | 
			
		||||
      '  lineTo(1,1)',
 | 
			
		||||
      '}',
 | 
			
		||||
      'const rotated = rx(90, mySk1)',
 | 
			
		||||
      // 'show(mySk1)',
 | 
			
		||||
    ].join('\n')
 | 
			
		||||
    const { root } = exe(code)
 | 
			
		||||
    expect(root.mySk1).toHaveLength(4)
 | 
			
		||||
    expect(root?.rotated?.type).toBe('transform')
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  it('execute pipe sketch into call expression', () => {
 | 
			
		||||
    const code = [
 | 
			
		||||
      'sketch mySk1 {',
 | 
			
		||||
      '  lineTo(1,1)',
 | 
			
		||||
      '  path myPath = lineTo(0, 1)',
 | 
			
		||||
      '  lineTo(1,1)',
 | 
			
		||||
      '} |> rx(90, %)',
 | 
			
		||||
    ].join('\n')
 | 
			
		||||
    const { root } = exe(code)
 | 
			
		||||
    const striptVersion = removeGeoFromSketch(root.mySk1)
 | 
			
		||||
    expect(striptVersion).toEqual({
 | 
			
		||||
      type: 'transform',
 | 
			
		||||
      rotation: [1.5707963267948966, 0, 0],
 | 
			
		||||
      transform: [0, 0, 0],
 | 
			
		||||
      sketch: [
 | 
			
		||||
        {
 | 
			
		||||
          type: 'base',
 | 
			
		||||
          from: [0, 0],
 | 
			
		||||
          sourceRange: [0, 0],
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
          type: 'toPoint',
 | 
			
		||||
          to: [1, 1],
 | 
			
		||||
          sourceRange: [17, 28],
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
          type: 'toPoint',
 | 
			
		||||
          to: [0, 1],
 | 
			
		||||
          sourceRange: [36, 57],
 | 
			
		||||
          name: 'myPath',
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
          type: 'toPoint',
 | 
			
		||||
          to: [1, 1],
 | 
			
		||||
          sourceRange: [60, 71],
 | 
			
		||||
        },
 | 
			
		||||
      ],
 | 
			
		||||
      sourceRange: [77, 86],
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
// helpers
 | 
			
		||||
@ -110,3 +165,13 @@ function exe(
 | 
			
		||||
  const ast = abstractSyntaxTree(tokens)
 | 
			
		||||
  return executor(ast, programMemory)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function removeGeoFromSketch(sketch: Transform): any {
 | 
			
		||||
  if (!Array.isArray(sketch.sketch)) {
 | 
			
		||||
    return removeGeoFromSketch(sketch.sketch)
 | 
			
		||||
  }
 | 
			
		||||
  return {
 | 
			
		||||
    ...sketch,
 | 
			
		||||
    sketch: sketch.sketch.map(({ geo, previousPath, ...rest }: any) => rest),
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -107,14 +107,14 @@ export const executor = (
 | 
			
		||||
            _programMemory._sketch = result.programMemory._sketch
 | 
			
		||||
            _programMemory.root[variableName] = result.currentPath
 | 
			
		||||
          } else if ('rx' === fnName) {
 | 
			
		||||
            if (declaration.init.arguments[1].type !== 'Identifier')
 | 
			
		||||
              throw new Error('rx must be called with an identifier')
 | 
			
		||||
            const id = declaration.init.arguments[1].name
 | 
			
		||||
            const sketch = declaration.init.arguments[1]
 | 
			
		||||
            if(sketch.type !== 'Identifier') throw new Error('rx must be called with an identifier')
 | 
			
		||||
            const sketchVal = _programMemory.root[sketch.name]
 | 
			
		||||
            const result = sketchFns[fnName](
 | 
			
		||||
              _programMemory,
 | 
			
		||||
              [declaration.start, declaration.end],
 | 
			
		||||
              fnArgs[0],
 | 
			
		||||
              id
 | 
			
		||||
              sketchVal
 | 
			
		||||
            )
 | 
			
		||||
            _programMemory.root[variableName] = result
 | 
			
		||||
          } else {
 | 
			
		||||
@ -216,11 +216,9 @@ function executePipeBody(body: PipeExpression['body'], programMemory: ProgramMem
 | 
			
		||||
      } else if (arg.type === 'PipeSubstitution') {
 | 
			
		||||
        return previousResults[expressionIndex-1]
 | 
			
		||||
      }
 | 
			
		||||
      console.log('yo',arg)
 | 
			
		||||
      throw new Error('Invalid argument type')
 | 
			
		||||
    })
 | 
			
		||||
    if (fnName === 'rx') {
 | 
			
		||||
      console.log('rx', fnArgs[1])
 | 
			
		||||
      const result = sketchFns[fnName](
 | 
			
		||||
        programMemory,
 | 
			
		||||
        [expression.start, expression.end],
 | 
			
		||||
@ -302,7 +300,7 @@ export const processShownObjects =
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
    } else if (geoMeta.type === 'transform') {
 | 
			
		||||
      const referencedVar = programMemory.root[geoMeta.id]
 | 
			
		||||
      const referencedVar = geoMeta.sketch
 | 
			
		||||
      const parentArtifact: ViewerArtifact = {
 | 
			
		||||
        type: 'parent',
 | 
			
		||||
        sourceRange: geoMeta.sourceRange,
 | 
			
		||||
 | 
			
		||||
@ -59,7 +59,7 @@ export interface Transform {
 | 
			
		||||
  type: 'transform'
 | 
			
		||||
  rotation: Rotation3
 | 
			
		||||
  transform: Translate3
 | 
			
		||||
  id: string
 | 
			
		||||
  sketch: Path[] | Transform
 | 
			
		||||
  sourceRange: SourceRange
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -207,15 +207,14 @@ export const sketchFns = {
 | 
			
		||||
    programMemory: ProgramMemory,
 | 
			
		||||
    sourceRange: SourceRange,
 | 
			
		||||
    rotationD: number,
 | 
			
		||||
    id: string
 | 
			
		||||
    sketch: Path[] | Transform
 | 
			
		||||
  ): Transform => {
 | 
			
		||||
    if (!programMemory.root[id]) throw new Error(`No variable with name ${id}`)
 | 
			
		||||
    const rotationR = rotationD * (Math.PI / 180)
 | 
			
		||||
    return {
 | 
			
		||||
      type: 'transform',
 | 
			
		||||
      rotation: [rotationR, 0, 0],
 | 
			
		||||
      transform: [0, 0, 0],
 | 
			
		||||
      id,
 | 
			
		||||
      sketch,
 | 
			
		||||
      sourceRange,
 | 
			
		||||
    }
 | 
			
		||||
  },
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user