cli: don't re-implement stuff
This commit is contained in:
parent
bae0da31c2
commit
9f45252522
50
bin/shrimp
50
bin/shrimp
|
|
@ -1,53 +1,14 @@
|
||||||
#!/usr/bin/env bun
|
#!/usr/bin/env bun
|
||||||
|
|
||||||
import { Compiler } from '../src/compiler/compiler'
|
import { colors } from '../src/prelude'
|
||||||
import { colors, globals } from '../src/prelude'
|
|
||||||
import { parser } from '../src/parser/shrimp'
|
|
||||||
import { setGlobals } from '../src/parser/tokenizer'
|
|
||||||
import { treeToString } from '../src/utils/tree'
|
import { treeToString } from '../src/utils/tree'
|
||||||
import { VM, fromValue, bytecodeToString } from 'reefvm'
|
import { runFile, compileFile, parseCode } from '../src'
|
||||||
|
import { bytecodeToString } from 'reefvm'
|
||||||
import { readFileSync, writeFileSync, mkdirSync } from 'fs'
|
import { readFileSync, writeFileSync, mkdirSync } from 'fs'
|
||||||
import { randomUUID } from 'crypto'
|
import { randomUUID } from 'crypto'
|
||||||
import { spawn } from 'child_process'
|
import { spawn } from 'child_process'
|
||||||
import { join } from 'path'
|
import { join } from 'path'
|
||||||
|
|
||||||
async function runFile(filePath: string) {
|
|
||||||
try {
|
|
||||||
const code = readFileSync(filePath, 'utf-8')
|
|
||||||
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) {
|
|
||||||
console.error(`${colors.red}Error:${colors.reset} ${error.message}`)
|
|
||||||
process.exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function compileFile(filePath: string) {
|
|
||||||
try {
|
|
||||||
setGlobals(Object.keys(globals))
|
|
||||||
const code = readFileSync(filePath, 'utf-8')
|
|
||||||
const compiler = new Compiler(code)
|
|
||||||
return bytecodeToString(compiler.bytecode)
|
|
||||||
} catch (error: any) {
|
|
||||||
console.error(`${colors.red}Error:${colors.reset} ${error.message}`)
|
|
||||||
process.exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function parseFile(filePath: string) {
|
|
||||||
try {
|
|
||||||
setGlobals(Object.keys(globals))
|
|
||||||
const code = readFileSync(filePath, 'utf-8')
|
|
||||||
const tree = parser.parse(code)
|
|
||||||
return treeToString(tree, code)
|
|
||||||
} catch (error: any) {
|
|
||||||
console.error(`${colors.red}Error:${colors.reset} ${error.message}`)
|
|
||||||
process.exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function showHelp() {
|
function showHelp() {
|
||||||
console.log(`${colors.bright}${colors.magenta}🦐 Shrimp${colors.reset} is a scripting language in a shell.
|
console.log(`${colors.bright}${colors.magenta}🦐 Shrimp${colors.reset} is a scripting language in a shell.
|
||||||
|
|
||||||
|
|
@ -115,7 +76,7 @@ async function main() {
|
||||||
console.log(`${colors.bright}usage: shrimp bytecode <file>${colors.reset}`)
|
console.log(`${colors.bright}usage: shrimp bytecode <file>${colors.reset}`)
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
console.log(await compileFile(file))
|
console.log(bytecodeToString(compileFile(file)))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -125,7 +86,8 @@ async function main() {
|
||||||
console.log(`${colors.bright}usage: shrimp parse <file>${colors.reset}`)
|
console.log(`${colors.bright}usage: shrimp parse <file>${colors.reset}`)
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
console.log(await parseFile(file))
|
const input = readFileSync(file, 'utf-8')
|
||||||
|
console.log(treeToString(parseCode(input), input))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
16
src/index.ts
16
src/index.ts
|
|
@ -1,6 +1,9 @@
|
||||||
import { readFileSync } from 'fs'
|
import { glob, readFileSync } from 'fs'
|
||||||
import { VM, fromValue, type Bytecode } from 'reefvm'
|
import { VM, fromValue, type Bytecode } from 'reefvm'
|
||||||
|
import { type Tree } from '@lezer/common'
|
||||||
import { Compiler } from '#compiler/compiler'
|
import { Compiler } from '#compiler/compiler'
|
||||||
|
import { parser } from '#parser/shrimp'
|
||||||
|
import { setGlobals } from '#parser/tokenizer'
|
||||||
import { globals as shrimpGlobals, colors } from '#prelude'
|
import { globals as shrimpGlobals, colors } from '#prelude'
|
||||||
|
|
||||||
export { Compiler } from '#compiler/compiler'
|
export { Compiler } from '#compiler/compiler'
|
||||||
|
|
@ -70,4 +73,15 @@ export function compileCode(code: string, globals?: Record<string, any>): Byteco
|
||||||
const globalNames = [...Object.keys(shrimpGlobals), ...(globals ? Object.keys(globals) : [])]
|
const globalNames = [...Object.keys(shrimpGlobals), ...(globals ? Object.keys(globals) : [])]
|
||||||
const compiler = new Compiler(code, globalNames)
|
const compiler = new Compiler(code, globalNames)
|
||||||
return compiler.bytecode
|
return compiler.bytecode
|
||||||
|
}
|
||||||
|
|
||||||
|
export function parseFile(path: string, globals?: Record<string, any>): Tree {
|
||||||
|
const code = readFileSync(path, 'utf-8')
|
||||||
|
return parseCode(code, globals)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function parseCode(code: string, globals?: Record<string, any>): Tree {
|
||||||
|
const globalNames = [...Object.keys(shrimpGlobals), ...(globals ? Object.keys(globals) : [])]
|
||||||
|
setGlobals(globalNames)
|
||||||
|
return parser.parse(code)
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user