Compare commits
2 Commits
4fb58483f0
...
a21ba54ad7
| Author | SHA1 | Date | |
|---|---|---|---|
| a21ba54ad7 | |||
| df3d483de5 |
8
bin/repl
8
bin/repl
|
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/env bun
|
||||
|
||||
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 * as readline from 'readline'
|
||||
import { readFileSync, writeFileSync } from 'fs'
|
||||
|
|
@ -48,7 +48,7 @@ async function repl() {
|
|||
return
|
||||
}
|
||||
|
||||
vm ||= new VM({ instructions: [], constants: [] }, globalFunctions)
|
||||
vm ||= new VM({ instructions: [], constants: [] }, globals)
|
||||
|
||||
if (['/exit', 'exit', '/quit', 'quit'].includes(trimmed)) {
|
||||
console.log(`\n${colors.yellow}Goodbye!${colors.reset}`)
|
||||
|
|
@ -147,7 +147,7 @@ async function repl() {
|
|||
codeHistory.push(trimmed)
|
||||
|
||||
try {
|
||||
const compiler = new Compiler(trimmed)
|
||||
const compiler = new Compiler(trimmed, Object.keys(globals))
|
||||
|
||||
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}`)
|
||||
|
||||
const vm = new VM({ instructions: [], constants: [] }, globalFunctions)
|
||||
const vm = new VM({ instructions: [], constants: [] }, globals)
|
||||
await vm.run()
|
||||
|
||||
const codeHistory: string[] = []
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/env bun
|
||||
|
||||
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 { readFileSync, writeFileSync, mkdirSync } from 'fs'
|
||||
import { randomUUID } from "crypto"
|
||||
|
|
@ -11,8 +11,8 @@ import { join } from 'path'
|
|||
async function runFile(filePath: string) {
|
||||
try {
|
||||
const code = readFileSync(filePath, 'utf-8')
|
||||
const compiler = new Compiler(code)
|
||||
const vm = new VM(compiler.bytecode, globalFunctions)
|
||||
const compiler = new Compiler(code, Object.keys(globals))
|
||||
const vm = new VM(compiler.bytecode, globals)
|
||||
await vm.run()
|
||||
return vm.stack.length ? fromValue(vm.stack[vm.stack.length - 1]) : null
|
||||
} catch (error: any) {
|
||||
|
|
|
|||
|
|
@ -11,19 +11,6 @@ import { list } from './list'
|
|||
import { math } from './math'
|
||||
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 = {
|
||||
dict,
|
||||
load,
|
||||
|
|
@ -43,6 +30,10 @@ export const globals = {
|
|||
// info
|
||||
type: (v: any) => toValue(v).type,
|
||||
inspect: (v: any) => formatValue(toValue(v)),
|
||||
describe: (v: any) => {
|
||||
const val = toValue(v)
|
||||
return { [val.type]: formatValue(toValue(v)) }
|
||||
},
|
||||
length: (v: any) => {
|
||||
const value = toValue(v)
|
||||
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 {
|
||||
switch (value.type) {
|
||||
case 'string':
|
||||
|
|
|
|||
|
|
@ -186,6 +186,12 @@ describe('introspection', () => {
|
|||
// (we'd need more complex assertion to check the actual format)
|
||||
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', () => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user