Compare commits

...

2 Commits

Author SHA1 Message Date
a21ba54ad7 describe? 2025-10-29 12:21:11 -07:00
df3d483de5 update repl and shrimp 2025-10-29 12:21:02 -07:00
4 changed files with 30 additions and 20 deletions

View File

@ -1,7 +1,7 @@
#!/usr/bin/env bun #!/usr/bin/env bun
import { Compiler } from '../src/compiler/compiler' import { Compiler } from '../src/compiler/compiler'
import { colors, formatValue, globalFunctions } from '../src/prelude' import { colors, formatValue, globals } from '../src/prelude'
import { VM, Scope, bytecodeToString } from 'reefvm' import { VM, Scope, bytecodeToString } from 'reefvm'
import * as readline from 'readline' import * as readline from 'readline'
import { readFileSync, writeFileSync } from 'fs' import { readFileSync, writeFileSync } from 'fs'
@ -48,7 +48,7 @@ async function repl() {
return return
} }
vm ||= new VM({ instructions: [], constants: [] }, globalFunctions) vm ||= new VM({ instructions: [], constants: [] }, globals)
if (['/exit', 'exit', '/quit', 'quit'].includes(trimmed)) { if (['/exit', 'exit', '/quit', 'quit'].includes(trimmed)) {
console.log(`\n${colors.yellow}Goodbye!${colors.reset}`) console.log(`\n${colors.yellow}Goodbye!${colors.reset}`)
@ -147,7 +147,7 @@ async function repl() {
codeHistory.push(trimmed) codeHistory.push(trimmed)
try { try {
const compiler = new Compiler(trimmed) const compiler = new Compiler(trimmed, Object.keys(globals))
vm.appendBytecode(compiler.bytecode) vm.appendBytecode(compiler.bytecode)
@ -211,7 +211,7 @@ async function loadFile(filePath: string): Promise<{ vm: VM; codeHistory: string
console.log(`${colors.dim}Loading ${basename(filePath)}...${colors.reset}`) console.log(`${colors.dim}Loading ${basename(filePath)}...${colors.reset}`)
const vm = new VM({ instructions: [], constants: [] }, globalFunctions) const vm = new VM({ instructions: [], constants: [] }, globals)
await vm.run() await vm.run()
const codeHistory: string[] = [] const codeHistory: string[] = []

View File

@ -1,7 +1,7 @@
#!/usr/bin/env bun #!/usr/bin/env bun
import { Compiler } from '../src/compiler/compiler' import { Compiler } from '../src/compiler/compiler'
import { colors, globalFunctions } from '../src/prelude' import { colors, globals } from '../src/prelude'
import { VM, fromValue, bytecodeToString } from 'reefvm' import { VM, fromValue, bytecodeToString } from 'reefvm'
import { readFileSync, writeFileSync, mkdirSync } from 'fs' import { readFileSync, writeFileSync, mkdirSync } from 'fs'
import { randomUUID } from "crypto" import { randomUUID } from "crypto"
@ -11,8 +11,8 @@ import { join } from 'path'
async function runFile(filePath: string) { async function runFile(filePath: string) {
try { try {
const code = readFileSync(filePath, 'utf-8') const code = readFileSync(filePath, 'utf-8')
const compiler = new Compiler(code) const compiler = new Compiler(code, Object.keys(globals))
const vm = new VM(compiler.bytecode, globalFunctions) const vm = new VM(compiler.bytecode, globals)
await vm.run() await vm.run()
return vm.stack.length ? fromValue(vm.stack[vm.stack.length - 1]) : null return vm.stack.length ? fromValue(vm.stack[vm.stack.length - 1]) : null
} catch (error: any) { } catch (error: any) {

View File

@ -11,19 +11,6 @@ import { list } from './list'
import { math } from './math' import { math } from './math'
import { str } from './str' import { str } from './str'
export const colors = {
reset: '\x1b[0m',
bright: '\x1b[1m',
dim: '\x1b[2m',
cyan: '\x1b[36m',
yellow: '\x1b[33m',
green: '\x1b[32m',
red: '\x1b[31m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
pink: '\x1b[38;2;255;105;180m'
}
export const globals = { export const globals = {
dict, dict,
load, load,
@ -43,6 +30,10 @@ export const globals = {
// info // info
type: (v: any) => toValue(v).type, type: (v: any) => toValue(v).type,
inspect: (v: any) => formatValue(toValue(v)), inspect: (v: any) => formatValue(toValue(v)),
describe: (v: any) => {
const val = toValue(v)
return { [val.type]: formatValue(toValue(v)) }
},
length: (v: any) => { length: (v: any) => {
const value = toValue(v) const value = toValue(v)
switch (value.type) { switch (value.type) {
@ -106,6 +97,19 @@ export const globals = {
} }
export const colors = {
reset: '\x1b[0m',
bright: '\x1b[1m',
dim: '\x1b[2m',
cyan: '\x1b[36m',
yellow: '\x1b[33m',
green: '\x1b[32m',
red: '\x1b[31m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
pink: '\x1b[38;2;255;105;180m'
}
export function formatValue(value: Value, inner = false): string { export function formatValue(value: Value, inner = false): string {
switch (value.type) { switch (value.type) {
case 'string': case 'string':

View File

@ -186,6 +186,12 @@ describe('introspection', () => {
// (we'd need more complex assertion to check the actual format) // (we'd need more complex assertion to check the actual format)
await expect(`type (inspect 'hello')`).toEvaluateTo('string', globals) await expect(`type (inspect 'hello')`).toEvaluateTo('string', globals)
}) })
test('describe describes values', async () => {
// Just test that inspect returns something for now
// (we'd need more complex assertion to check the actual format)
await expect(`describe 'hello'`).toEvaluateTo({ string: "\u001b[32m'hello\u001b[32m'\u001b[0m" }, globals)
})
}) })
describe('collections', () => { describe('collections', () => {