Prelude of builtin functions #7
|
|
@ -468,6 +468,13 @@ export class Compiler {
|
|||
return instructions
|
||||
}
|
||||
|
||||
case terms.Array: {
|
||||
const children = getAllChildren(node)
|
||||
const instructions: ProgramItem[] = children.map((x) => this.#compileNode(x, input)).flat()
|
||||
instructions.push(['MAKE_ARRAY', children.length])
|
||||
return instructions
|
||||
}
|
||||
|
||||
default:
|
||||
throw new CompilerError(
|
||||
`Compiler doesn't know how to handle a "${node.type.name}" node.`,
|
||||
|
|
|
|||
36
src/compiler/tests/literals.test.ts
Normal file
36
src/compiler/tests/literals.test.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import { describe } from 'bun:test'
|
||||
import { expect, test } from 'bun:test'
|
||||
|
||||
describe('array literals', () => {
|
||||
test('work with numbers', () => {
|
||||
expect('[1 2 3]').toEvaluateTo([1, 2, 3])
|
||||
})
|
||||
|
||||
test('work with strings', () => {
|
||||
expect("['one' 'two' 'three']").toEvaluateTo(['one', 'two', 'three'])
|
||||
})
|
||||
|
||||
test('work with identifiers', () => {
|
||||
expect('[one two three]').toEvaluateTo(['one', 'two', 'three'])
|
||||
})
|
||||
|
||||
test('can be nested', () => {
|
||||
expect('[one [two [three]]]').toEvaluateTo(['one', ['two', ['three']]])
|
||||
})
|
||||
|
||||
test('can span multiple lines', () => {
|
||||
expect(`[
|
||||
1
|
||||
2
|
||||
3
|
||||
]`).toEvaluateTo([1, 2, 3])
|
||||
})
|
||||
|
||||
test('can span multiple w/o calling functions', () => {
|
||||
expect(`[
|
||||
one
|
||||
two
|
||||
three
|
||||
]`).toEvaluateTo(['one', 'two', 'three'])
|
||||
})
|
||||
})
|
||||
|
|
@ -93,11 +93,7 @@ expect.extend({
|
|||
}
|
||||
},
|
||||
|
||||
async toEvaluateTo(
|
||||
received: unknown,
|
||||
expected: unknown,
|
||||
globals: Record<string, any> = {}
|
||||
) {
|
||||
async toEvaluateTo(received: unknown, expected: unknown, globals: Record<string, any> = {}) {
|
||||
assert(typeof received === 'string', 'toEvaluateTo can only be used with string values')
|
||||
|
||||
try {
|
||||
|
|
@ -109,7 +105,7 @@ expect.extend({
|
|||
if (expected instanceof RegExp) expected = String(expected)
|
||||
if (value instanceof RegExp) value = String(value)
|
||||
|
||||
if (value === expected) {
|
||||
if (isEqual(value, expected)) {
|
||||
return { pass: true }
|
||||
} else {
|
||||
return {
|
||||
|
defunkt marked this conversation as resolved
Outdated
|
||||
|
|
@ -165,3 +161,7 @@ const trimWhitespace = (str: string): string => {
|
|||
})
|
||||
.join('\n')
|
||||
}
|
||||
|
||||
function isEqual(a: any, b: any): boolean {
|
||||
return typeof a === 'object' ? JSON.stringify(a) === JSON.stringify(b) : a === b
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user
I rewrote this in the branch I'm on too!
If you replace this line with
You get to lean on bun's type checking code and you get all the nice diff support.
Awesome. Done.