./bin/repl <file>
This commit is contained in:
parent
e95c8e5018
commit
0ff0dd5380
50
bin/repl
50
bin/repl
|
|
@ -3,7 +3,8 @@
|
|||
import { Compiler } from '../src/compiler/compiler'
|
||||
import { VM, type Value, Scope, bytecodeToString } from 'reefvm'
|
||||
import * as readline from 'readline'
|
||||
import * as fs from 'fs'
|
||||
import { readFileSync, writeFileSync } from 'fs'
|
||||
import { basename } from 'path'
|
||||
|
||||
const colors = {
|
||||
reset: '\x1b[0m',
|
||||
|
|
@ -39,6 +40,14 @@ async function repl() {
|
|||
let codeHistory: string[] = []
|
||||
let vm: VM | null = null
|
||||
|
||||
// Load file if provided as argument
|
||||
const filePath = process.argv[2]
|
||||
if (filePath) {
|
||||
const loaded = await loadFile(filePath)
|
||||
vm = loaded.vm
|
||||
codeHistory = loaded.codeHistory
|
||||
}
|
||||
|
||||
showWelcome()
|
||||
|
||||
rl.prompt()
|
||||
|
|
@ -137,7 +146,7 @@ async function repl() {
|
|||
const content = codeHistory.join('\n') + '\n'
|
||||
|
||||
try {
|
||||
fs.writeFileSync(finalFilename, content, 'utf-8')
|
||||
writeFileSync(finalFilename, content, 'utf-8')
|
||||
console.log(`\n${colors.green}✓${colors.reset} Saved ${codeHistory.length} line${codeHistory.length === 1 ? '' : 's'} to ${colors.bright}${finalFilename}${colors.reset}`)
|
||||
} catch (error: any) {
|
||||
console.log(`\n${colors.red}Error:${colors.reset} Failed to save file: ${error.message}`)
|
||||
|
|
@ -241,6 +250,42 @@ function formatVariables(scope: Scope, onlyFunctions = false): string {
|
|||
return vars.join('\n')
|
||||
}
|
||||
|
||||
async function loadFile(filePath: string): Promise<{ vm: VM; codeHistory: string[] }> {
|
||||
try {
|
||||
const fileContent = readFileSync(filePath, 'utf-8')
|
||||
const lines = fileContent.trim().split('\n')
|
||||
|
||||
console.log(`${colors.dim}Loading ${basename(filePath)}...${colors.reset}`)
|
||||
|
||||
const vm = new VM({ instructions: [], constants: [] }, nativeFunctions)
|
||||
await vm.run()
|
||||
|
||||
const codeHistory: string[] = []
|
||||
|
||||
for (const line of lines) {
|
||||
const trimmed = line.trim()
|
||||
if (!trimmed) continue
|
||||
|
||||
try {
|
||||
const compiler = new Compiler(trimmed)
|
||||
vm.appendBytecode(compiler.bytecode)
|
||||
await vm.continue()
|
||||
codeHistory.push(trimmed)
|
||||
} catch (error: any) {
|
||||
console.log(`${colors.red}Error in ${basename(filePath)}:${colors.reset} ${error.message}`)
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`${colors.green}✓${colors.reset} Loaded ${codeHistory.length} line${codeHistory.length === 1 ? '' : 's'}\n`)
|
||||
|
||||
return { vm, codeHistory }
|
||||
} catch (error: any) {
|
||||
console.log(`${colors.red}Error:${colors.reset} Could not load file: ${error.message}`)
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
function showWelcome() {
|
||||
console.log(
|
||||
`${colors.pink}═══════════════════════════════════════════════════════════════${colors.reset}`
|
||||
|
|
@ -250,6 +295,7 @@ function showWelcome() {
|
|||
`${colors.pink}═══════════════════════════════════════════════════════════════${colors.reset}`
|
||||
)
|
||||
console.log(`\nType Shrimp expressions. Press ${colors.bright}Ctrl+D${colors.reset} to exit.`)
|
||||
console.log(`${colors.dim}Usage: bun bin/repl [file.shrimp]${colors.reset}`)
|
||||
console.log(`\nCommands:`)
|
||||
console.log(` ${colors.bright}/clear${colors.reset} - Clear screen and reset state`)
|
||||
console.log(` ${colors.bright}/reset${colors.reset} - Reset state (keep history visible)`)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user