2022-11-26 08:34:23 +11:00
|
|
|
import fs from 'node:fs'
|
2022-11-14 14:04:23 +11:00
|
|
|
|
2022-11-26 08:34:23 +11:00
|
|
|
import { abstractSyntaxTree } from './abstractSyntaxTree'
|
|
|
|
import { lexer } from './tokeniser'
|
|
|
|
import { executor, ProgramMemory } from './executor'
|
2022-12-06 05:40:05 +11:00
|
|
|
import { Transform, SketchGeo } from './sketch'
|
2022-11-14 14:04:23 +11:00
|
|
|
|
2022-11-26 08:34:23 +11:00
|
|
|
describe('test', () => {
|
|
|
|
it('test assigning two variables, the second summing with the first', () => {
|
2022-11-14 14:04:23 +11:00
|
|
|
const code = `const myVar = 5
|
2022-11-26 08:34:23 +11:00
|
|
|
const newVar = myVar + 1`
|
|
|
|
const { root } = exe(code)
|
|
|
|
expect(root.myVar).toBe(5)
|
|
|
|
expect(root.newVar).toBe(6)
|
|
|
|
})
|
|
|
|
it('test assigning a var with a string', () => {
|
|
|
|
const code = `const myVar = "a str"`
|
|
|
|
const { root } = exe(code)
|
|
|
|
expect(root.myVar).toBe('a str')
|
|
|
|
})
|
|
|
|
it('test assigning a var by cont concatenating two strings string', () => {
|
2022-11-20 09:41:21 +11:00
|
|
|
const code = fs.readFileSync(
|
2022-11-26 08:34:23 +11:00
|
|
|
'./src/lang/testExamples/variableDeclaration.cado',
|
|
|
|
'utf-8'
|
|
|
|
)
|
|
|
|
const { root } = exe(code)
|
|
|
|
expect(root.myVar).toBe('a str another str')
|
|
|
|
})
|
|
|
|
it('test with function call', () => {
|
2022-11-14 14:04:23 +11:00
|
|
|
const code = `
|
|
|
|
const myVar = "hello"
|
2022-11-26 08:34:23 +11:00
|
|
|
log(5, myVar)`
|
2022-11-14 14:04:23 +11:00
|
|
|
const programMemoryOverride = {
|
|
|
|
log: jest.fn(),
|
2022-11-26 08:34:23 +11:00
|
|
|
}
|
2022-11-21 08:57:44 +11:00
|
|
|
const { root } = executor(abstractSyntaxTree(lexer(code)), {
|
2022-11-20 09:41:21 +11:00
|
|
|
root: programMemoryOverride,
|
2022-11-21 08:57:44 +11:00
|
|
|
_sketch: [],
|
2022-11-26 08:34:23 +11:00
|
|
|
})
|
|
|
|
expect(root.myVar).toBe('hello')
|
|
|
|
expect(programMemoryOverride.log).toHaveBeenCalledWith(5, 'hello')
|
|
|
|
})
|
|
|
|
it('fn funcN = () => {}', () => {
|
2022-11-21 08:57:44 +11:00
|
|
|
const { root } = exe(
|
2022-11-20 09:41:21 +11:00
|
|
|
[
|
2022-11-26 08:34:23 +11:00
|
|
|
'fn funcN = (a, b) => {',
|
|
|
|
' return a + b',
|
|
|
|
'}',
|
|
|
|
'const theVar = 60',
|
|
|
|
'const magicNum = funcN(9, theVar)',
|
|
|
|
].join('\n')
|
|
|
|
)
|
|
|
|
expect(root.theVar).toBe(60)
|
|
|
|
expect(root.magicNum).toBe(69)
|
|
|
|
})
|
|
|
|
it('sketch declaration', () => {
|
2022-11-21 08:57:44 +11:00
|
|
|
let code = `sketch mySketch {
|
|
|
|
path myPath = lineTo(0,1)
|
|
|
|
lineTo(1,1)
|
|
|
|
path rightPath = lineTo(1,0)
|
|
|
|
close()
|
|
|
|
}
|
2022-11-21 09:16:24 +11:00
|
|
|
show(mySketch)
|
2022-11-26 08:34:23 +11:00
|
|
|
`
|
|
|
|
const { root, return: _return } = exe(code)
|
2022-11-23 21:28:38 +11:00
|
|
|
expect(
|
2022-12-06 05:40:05 +11:00
|
|
|
root.mySketch.sketch.map(
|
2022-11-28 09:37:46 +11:00
|
|
|
({ previousPath, firstPath, geo, ...rest }: any) => rest
|
|
|
|
)
|
2022-11-23 21:28:38 +11:00
|
|
|
).toEqual([
|
2022-11-26 21:03:38 +11:00
|
|
|
{ type: 'base', from: [0, 0], sourceRange: [0, 0] },
|
2022-11-26 08:34:23 +11:00
|
|
|
{ type: 'toPoint', to: [0, 1], sourceRange: [25, 45], name: 'myPath' },
|
|
|
|
{ type: 'toPoint', to: [1, 1], sourceRange: [48, 59] },
|
|
|
|
{ type: 'toPoint', to: [1, 0], sourceRange: [67, 90], name: 'rightPath' },
|
2022-11-23 21:28:38 +11:00
|
|
|
{
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'close',
|
2022-11-23 21:28:38 +11:00
|
|
|
sourceRange: [93, 100],
|
|
|
|
},
|
2022-11-26 08:34:23 +11:00
|
|
|
])
|
2022-12-30 14:09:07 +11:00
|
|
|
// expect(root.mySketch.sketch[0]).toEqual(root.mySketch.sketch[4].firstPath)
|
2022-11-21 09:16:24 +11:00
|
|
|
expect(_return).toEqual([
|
|
|
|
{
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'Identifier',
|
2022-11-21 09:16:24 +11:00
|
|
|
start: 108,
|
|
|
|
end: 116,
|
2022-11-26 08:34:23 +11:00
|
|
|
name: 'mySketch',
|
2022-11-21 09:16:24 +11:00
|
|
|
},
|
2022-11-26 08:34:23 +11:00
|
|
|
])
|
|
|
|
})
|
2022-12-03 22:50:46 +11:00
|
|
|
|
|
|
|
it('pipe binary expression into call expression', () => {
|
|
|
|
const code = [
|
|
|
|
'fn myFn = (a) => { return a + 1 }',
|
|
|
|
'const myVar = 5 + 1 |> myFn(%)',
|
|
|
|
].join('\n')
|
|
|
|
const { root } = exe(code)
|
|
|
|
expect(root.myVar).toBe(7)
|
|
|
|
})
|
|
|
|
|
2022-12-04 08:16:04 +11:00
|
|
|
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)
|
2022-12-06 05:40:05 +11:00
|
|
|
expect(root.mySk1.sketch).toHaveLength(4)
|
2022-12-04 08:16:04 +11:00
|
|
|
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({
|
2022-12-06 05:40:05 +11:00
|
|
|
type: 'sketchGeo',
|
2022-12-04 08:16:04 +11:00
|
|
|
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],
|
|
|
|
},
|
|
|
|
],
|
2022-12-06 05:40:05 +11:00
|
|
|
sourceRange: [13, 73],
|
2022-12-04 08:16:04 +11:00
|
|
|
})
|
2022-12-06 05:40:05 +11:00
|
|
|
// old expect
|
|
|
|
// 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],
|
|
|
|
// })
|
2022-12-04 08:16:04 +11:00
|
|
|
})
|
2022-11-26 08:34:23 +11:00
|
|
|
})
|
2022-11-14 14:04:23 +11:00
|
|
|
|
|
|
|
// helpers
|
|
|
|
|
2022-11-20 09:41:21 +11:00
|
|
|
function exe(
|
|
|
|
code: string,
|
2022-11-21 08:57:44 +11:00
|
|
|
programMemory: ProgramMemory = { root: {}, _sketch: [] }
|
2022-11-20 09:41:21 +11:00
|
|
|
) {
|
2022-11-26 08:34:23 +11:00
|
|
|
const tokens = lexer(code)
|
|
|
|
const ast = abstractSyntaxTree(tokens)
|
|
|
|
return executor(ast, programMemory)
|
2022-11-20 09:41:21 +11:00
|
|
|
}
|
2022-12-04 08:16:04 +11:00
|
|
|
|
2022-12-06 05:40:05 +11:00
|
|
|
function removeGeoFromSketch(sketch: Transform | SketchGeo): any {
|
2022-12-30 14:09:07 +11:00
|
|
|
if (sketch.type !== 'sketchGeo' && sketch.type === 'transform') {
|
|
|
|
return removeGeoFromSketch(sketch.sketch as any) // TODO fix type
|
2022-12-04 08:16:04 +11:00
|
|
|
}
|
2022-12-30 14:09:07 +11:00
|
|
|
if (sketch.type === 'sketchGeo') {
|
|
|
|
return {
|
|
|
|
...sketch,
|
|
|
|
sketch: sketch.sketch.map(({ geo, previousPath, ...rest }: any) => rest),
|
|
|
|
}
|
2022-12-04 08:16:04 +11:00
|
|
|
}
|
2022-12-30 14:09:07 +11:00
|
|
|
throw new Error('not a sketch')
|
2022-12-04 08:16:04 +11:00
|
|
|
}
|