Compare commits
6 Commits
298948ac44
...
0bd36438c8
| Author | SHA1 | Date | |
|---|---|---|---|
| 0bd36438c8 | |||
| 225906179c | |||
| 36fe676495 | |||
| f1367b64ef | |||
| 580af874c1 | |||
| f7b429fbfe |
6
bin/repl
6
bin/repl
|
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/env bun
|
||||
|
||||
import { Compiler } from '../src/compiler/compiler'
|
||||
import { colors, formatValue, nativeFunctions, valueFunctions } from '../src/prelude'
|
||||
import { colors, formatValue, globalFunctions } from '../src/prelude'
|
||||
import { VM, Scope, bytecodeToString } from 'reefvm'
|
||||
import * as readline from 'readline'
|
||||
import { readFileSync, writeFileSync } from 'fs'
|
||||
|
|
@ -48,7 +48,7 @@ async function repl() {
|
|||
return
|
||||
}
|
||||
|
||||
vm ||= new VM({ instructions: [], constants: [] }, nativeFunctions, valueFunctions)
|
||||
vm ||= new VM({ instructions: [], constants: [] }, globalFunctions)
|
||||
|
||||
if (['/exit', 'exit', '/quit', 'quit'].includes(trimmed)) {
|
||||
console.log(`\n${colors.yellow}Goodbye!${colors.reset}`)
|
||||
|
|
@ -211,7 +211,7 @@ async function loadFile(filePath: string): Promise<{ vm: VM; codeHistory: string
|
|||
|
||||
console.log(`${colors.dim}Loading ${basename(filePath)}...${colors.reset}`)
|
||||
|
||||
const vm = new VM({ instructions: [], constants: [] }, nativeFunctions, valueFunctions)
|
||||
const vm = new VM({ instructions: [], constants: [] }, globalFunctions)
|
||||
await vm.run()
|
||||
|
||||
const codeHistory: string[] = []
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/env bun
|
||||
|
||||
import { Compiler } from '../src/compiler/compiler'
|
||||
import { colors, nativeFunctions, valueFunctions } from '../src/prelude'
|
||||
import { colors, globalFunctions } from '../src/prelude'
|
||||
import { VM, fromValue, bytecodeToString } from 'reefvm'
|
||||
import { readFileSync, writeFileSync, mkdirSync } from 'fs'
|
||||
import { randomUUID } from "crypto"
|
||||
|
|
@ -12,7 +12,7 @@ async function runFile(filePath: string) {
|
|||
try {
|
||||
const code = readFileSync(filePath, 'utf-8')
|
||||
const compiler = new Compiler(code)
|
||||
const vm = new VM(compiler.bytecode, nativeFunctions, valueFunctions)
|
||||
const vm = new VM(compiler.bytecode, globalFunctions)
|
||||
await vm.run()
|
||||
return vm.stack.length ? fromValue(vm.stack[vm.stack.length - 1]) : null
|
||||
} catch (error: any) {
|
||||
|
|
|
|||
|
|
@ -9,7 +9,8 @@
|
|||
"scripts": {
|
||||
"dev": "bun generate-parser && bun --hot src/server/server.tsx",
|
||||
"generate-parser": "lezer-generator src/parser/shrimp.grammar --typeScript -o src/parser/shrimp.ts",
|
||||
"repl": "bun generate-parser && bun bin/repl"
|
||||
"repl": "bun generate-parser && bun bin/repl",
|
||||
"update-reef": "cd packages/ReefVM && git pull origin main"
|
||||
},
|
||||
"dependencies": {
|
||||
"reefvm": "workspace:*",
|
||||
|
|
|
|||
|
|
@ -1,6 +1,12 @@
|
|||
// The prelude creates all the builtin Shrimp functions.
|
||||
|
||||
import { toValue, type Value, extractParamInfo, isWrapped, getOriginalFunction } from 'reefvm'
|
||||
import { resolve, parse } from 'path'
|
||||
import { readFileSync } from 'fs'
|
||||
import { Compiler } from '#compiler/compiler'
|
||||
import {
|
||||
VM, Scope, toValue, type Value,
|
||||
extractParamInfo, isWrapped, getOriginalFunction,
|
||||
} from 'reefvm'
|
||||
|
||||
export const colors = {
|
||||
reset: '\x1b[0m',
|
||||
|
|
@ -15,40 +21,28 @@ export const colors = {
|
|||
pink: '\x1b[38;2;255;105;180m'
|
||||
}
|
||||
|
||||
export const valueFunctions = {
|
||||
echo: (...args: Value[]) => {
|
||||
console.log(...args.map(a => formatValue(a)))
|
||||
export const globalFunctions = {
|
||||
// hello
|
||||
echo: (...args: any[]) => {
|
||||
console.log(...args.map(a => {
|
||||
const v = toValue(a)
|
||||
return ['array', 'dict'].includes(v.type) ? formatValue(v, true) : v.value
|
||||
}))
|
||||
return toValue(null)
|
||||
},
|
||||
|
||||
length: (value: Value) => {
|
||||
// info
|
||||
type: (v: any) => toValue(v).type,
|
||||
inspect: (v: any) => formatValue(toValue(v)),
|
||||
length: (v: any) => {
|
||||
const value = toValue(v)
|
||||
switch (value.type) {
|
||||
case 'string': case 'array':
|
||||
return toValue(value.value.length)
|
||||
case 'dict':
|
||||
return toValue(Object.keys(value.value).length)
|
||||
default:
|
||||
return toValue(0)
|
||||
case 'string': case 'array': return value.value.length
|
||||
case 'dict': return value.value.size
|
||||
default: return 0
|
||||
}
|
||||
},
|
||||
|
||||
type: (value: Value) => toValue(value.type),
|
||||
inspect: (value: Value) => toValue(formatValue(value))
|
||||
}
|
||||
|
||||
export const nativeFunctions = {
|
||||
range: (start: number, end: number | null) => {
|
||||
if (end === null) {
|
||||
end = start
|
||||
start = 0
|
||||
}
|
||||
const result: number[] = []
|
||||
for (let i = start; i <= end; i++) {
|
||||
result.push(i)
|
||||
}
|
||||
return result
|
||||
},
|
||||
|
||||
// strings
|
||||
join: (arr: any[], sep: string = ',') => arr.join(sep),
|
||||
split: (str: string, sep: string = ',') => str.split(sep),
|
||||
|
|
@ -61,6 +55,17 @@ export const nativeFunctions = {
|
|||
list: (...args: any[]) => args,
|
||||
dict: (atNamed = {}) => atNamed,
|
||||
slice: (list: any[], start: number, end?: number) => list.slice(start, end),
|
||||
range: (start: number, end: number | null) => {
|
||||
if (end === null) {
|
||||
end = start
|
||||
start = 0
|
||||
}
|
||||
const result: number[] = []
|
||||
for (let i = start; i <= end; i++) {
|
||||
result.push(i)
|
||||
}
|
||||
return result
|
||||
},
|
||||
|
||||
// enumerables
|
||||
map: async (list: any[], cb: Function) => {
|
||||
|
|
@ -71,6 +76,32 @@ export const nativeFunctions = {
|
|||
each: async (list: any[], cb: Function) => {
|
||||
for (const value of list) await cb(value)
|
||||
},
|
||||
|
||||
// modules
|
||||
use: async function (this: VM, path: string) {
|
||||
const scope = this.scope
|
||||
const pc = this.pc
|
||||
|
||||
const fullPath = resolve(path) + '.sh'
|
||||
const code = readFileSync(fullPath, 'utf-8')
|
||||
|
||||
this.pc = this.instructions.length
|
||||
this.scope = new Scope(scope)
|
||||
const compiled = new Compiler(code)
|
||||
this.appendBytecode(compiled.bytecode)
|
||||
|
||||
await this.continue()
|
||||
|
||||
const module: Map<string, Value> = new Map
|
||||
for (const [name, value] of this.scope.locals.entries())
|
||||
module.set(name, value)
|
||||
|
||||
this.scope = scope
|
||||
this.pc = pc
|
||||
this.stopped = false
|
||||
|
||||
this.scope.set(parse(fullPath).name, { type: 'dict', value: module })
|
||||
}
|
||||
}
|
||||
|
||||
export function formatValue(value: Value, inner = false): string {
|
||||
28
src/prelude/tests/use.test.ts
Normal file
28
src/prelude/tests/use.test.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import { expect, describe, test } from 'bun:test'
|
||||
import { globalFunctions } from '#prelude'
|
||||
|
||||
describe('use', () => {
|
||||
test(`imports all a file's functions`, async () => {
|
||||
expect(`
|
||||
use ./src/prelude/tests/math
|
||||
dbl = math | at double
|
||||
dbl 4
|
||||
`).toEvaluateTo(8, globalFunctions)
|
||||
|
||||
expect(`
|
||||
use ./src/prelude/tests/math
|
||||
math | at pi
|
||||
`).toEvaluateTo(3.14, globalFunctions)
|
||||
|
||||
expect(`
|
||||
use ./src/prelude/tests/math
|
||||
math | at 🥧
|
||||
`).toEvaluateTo(3.14159265359, globalFunctions)
|
||||
|
||||
expect(`
|
||||
use ./src/prelude/tests/math
|
||||
call = do x y: x y end
|
||||
call (math | at add1) 5
|
||||
`).toEvaluateTo(6, globalFunctions)
|
||||
})
|
||||
})
|
||||
|
|
@ -3,7 +3,7 @@ import { parser } from '#parser/shrimp'
|
|||
import { $ } from 'bun'
|
||||
import { assert, errorMessage } from '#utils/utils'
|
||||
import { Compiler } from '#compiler/compiler'
|
||||
import { run, VM } from 'reefvm'
|
||||
import { run, VM, type TypeScriptFunction } from 'reefvm'
|
||||
import { treeToString, VMResultToValue } from '#utils/tree'
|
||||
|
||||
const regenerateParser = async () => {
|
||||
|
|
@ -33,7 +33,7 @@ declare module 'bun:test' {
|
|||
toMatchTree(expected: string): T
|
||||
toMatchExpression(expected: string): T
|
||||
toFailParse(): T
|
||||
toEvaluateTo(expected: unknown, nativeFunctions?: Record<string, Function>): Promise<T>
|
||||
toEvaluateTo(expected: unknown, globalFunctions?: Record<string, TypeScriptFunction>): Promise<T>
|
||||
toFailEvaluation(): Promise<T>
|
||||
}
|
||||
}
|
||||
|
|
@ -96,13 +96,13 @@ expect.extend({
|
|||
async toEvaluateTo(
|
||||
received: unknown,
|
||||
expected: unknown,
|
||||
nativeFunctions: Record<string, Function> = {}
|
||||
globalFunctions: Record<string, TypeScriptFunction> = {}
|
||||
) {
|
||||
assert(typeof received === 'string', 'toEvaluateTo can only be used with string values')
|
||||
|
||||
try {
|
||||
const compiler = new Compiler(received)
|
||||
const result = await run(compiler.bytecode, nativeFunctions)
|
||||
const result = await run(compiler.bytecode, globalFunctions)
|
||||
let value = VMResultToValue(result)
|
||||
|
||||
// Just treat regex as strings for comparison purposes
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user