wip
This commit is contained in:
parent
e8a1befdcc
commit
3cd1936de4
|
|
@ -17,8 +17,8 @@ import {
|
||||||
getStringParts,
|
getStringParts,
|
||||||
} from '#compiler/utils'
|
} from '#compiler/utils'
|
||||||
|
|
||||||
const DEBUG = false
|
// const DEBUG = false
|
||||||
// const DEBUG = true
|
const DEBUG = true
|
||||||
|
|
||||||
type Label = `.${string}`
|
type Label = `.${string}`
|
||||||
|
|
||||||
|
|
@ -288,6 +288,7 @@ export class Compiler {
|
||||||
instructions.push(['PUSH', positionalArgs.length])
|
instructions.push(['PUSH', positionalArgs.length])
|
||||||
instructions.push(['PUSH', namedArgs.length])
|
instructions.push(['PUSH', namedArgs.length])
|
||||||
instructions.push(['CALL'])
|
instructions.push(['CALL'])
|
||||||
|
|
||||||
return instructions
|
return instructions
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -212,3 +212,10 @@ describe('Regex', () => {
|
||||||
expect('//[unclosed//').toFailEvaluation()
|
expect('//[unclosed//').toFailEvaluation()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe.only('native functions', () => {
|
||||||
|
test('print function', () => {
|
||||||
|
const add = (x: number, y: number) => x + y
|
||||||
|
expect(`add 5 9`).toEvaluateTo(14, { add })
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
|
||||||
|
|
@ -5,11 +5,13 @@ export const highlighting = styleTags({
|
||||||
Number: tags.number,
|
Number: tags.number,
|
||||||
String: tags.string,
|
String: tags.string,
|
||||||
Boolean: tags.bool,
|
Boolean: tags.bool,
|
||||||
fn: tags.keyword,
|
keyword: tags.keyword,
|
||||||
end: tags.keyword,
|
end: tags.keyword,
|
||||||
':': tags.keyword,
|
':': tags.keyword,
|
||||||
|
Null: tags.keyword,
|
||||||
Regex: tags.regexp,
|
Regex: tags.regexp,
|
||||||
Operator: tags.operator,
|
Operator: tags.operator,
|
||||||
|
Word: tags.variableName,
|
||||||
Command: tags.function(tags.variableName),
|
Command: tags.function(tags.variableName),
|
||||||
'Params/Identifier': tags.definition(tags.variableName),
|
'Params/Identifier': tags.definition(tags.variableName),
|
||||||
Paren: tags.paren,
|
Paren: tags.paren,
|
||||||
|
|
|
||||||
|
|
@ -1,25 +1,8 @@
|
||||||
import { expect, describe, test } from 'bun:test'
|
import { expect, describe, test } from 'bun:test'
|
||||||
import { afterEach } from 'bun:test'
|
|
||||||
import { resetCommandSource, setCommandSource } from '#editor/commands'
|
|
||||||
import { beforeEach } from 'bun:test'
|
|
||||||
|
|
||||||
import '../shrimp.grammar' // Importing this so changes cause it to retest!
|
import '../shrimp.grammar' // Importing this so changes cause it to retest!
|
||||||
|
|
||||||
describe('calling functions', () => {
|
describe('calling functions', () => {
|
||||||
beforeEach(() => {
|
|
||||||
setCommandSource(() => [
|
|
||||||
{
|
|
||||||
command: 'echo',
|
|
||||||
args: [{ name: 'path', type: 'string' }],
|
|
||||||
execute: (p: any) => p,
|
|
||||||
},
|
|
||||||
])
|
|
||||||
})
|
|
||||||
|
|
||||||
afterEach(() => {
|
|
||||||
resetCommandSource()
|
|
||||||
})
|
|
||||||
|
|
||||||
test('call with no args', () => {
|
test('call with no args', () => {
|
||||||
expect('tail').toMatchTree(`
|
expect('tail').toMatchTree(`
|
||||||
FunctionCallOrIdentifier
|
FunctionCallOrIdentifier
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ import { parser } from '#parser/shrimp'
|
||||||
import { $ } from 'bun'
|
import { $ } from 'bun'
|
||||||
import { assert, assertNever, errorMessage } from '#utils/utils'
|
import { assert, assertNever, errorMessage } from '#utils/utils'
|
||||||
import { Compiler } from '#compiler/compiler'
|
import { Compiler } from '#compiler/compiler'
|
||||||
import { VM, type Value } from 'reefvm'
|
import { run, VM, type Value } from 'reefvm'
|
||||||
|
|
||||||
const regenerateParser = async () => {
|
const regenerateParser = async () => {
|
||||||
let generate = true
|
let generate = true
|
||||||
|
|
@ -33,7 +33,7 @@ declare module 'bun:test' {
|
||||||
toMatchTree(expected: string): T
|
toMatchTree(expected: string): T
|
||||||
toMatchExpression(expected: string): T
|
toMatchExpression(expected: string): T
|
||||||
toFailParse(): T
|
toFailParse(): T
|
||||||
toEvaluateTo(expected: unknown): Promise<T>
|
toEvaluateTo(expected: unknown, nativeFunctions?: Record<string, Function>): Promise<T>
|
||||||
toFailEvaluation(): Promise<T>
|
toFailEvaluation(): Promise<T>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -93,14 +93,16 @@ expect.extend({
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
async toEvaluateTo(received: unknown, expected: unknown) {
|
async toEvaluateTo(
|
||||||
|
received: unknown,
|
||||||
|
expected: unknown,
|
||||||
|
nativeFunctions: Record<string, Function> = {}
|
||||||
|
) {
|
||||||
assert(typeof received === 'string', 'toEvaluateTo can only be used with string values')
|
assert(typeof received === 'string', 'toEvaluateTo can only be used with string values')
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const compiler = new Compiler(received)
|
const compiler = new Compiler(received)
|
||||||
const vm = new VM(compiler.bytecode)
|
const result = await run(compiler.bytecode, nativeFunctions)
|
||||||
await vm.run()
|
|
||||||
const result = await vm.run()
|
|
||||||
let value = VMResultToValue(result)
|
let value = VMResultToValue(result)
|
||||||
|
|
||||||
// Just treat regex as strings for comparison purposes
|
// Just treat regex as strings for comparison purposes
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user