Compare commits
No commits in common. "9f45252522e59d1afa6634fa4e9834372ece7133" and "68ec6f9f3e9fcc6a6551710d9f5a5459ac59bb8f" have entirely different histories.
9f45252522
...
68ec6f9f3e
47
bin/shrimp
47
bin/shrimp
|
|
@ -1,14 +1,50 @@
|
|||
#!/usr/bin/env bun
|
||||
|
||||
import { colors } from '../src/prelude'
|
||||
import { Compiler } from '../src/compiler/compiler'
|
||||
import { colors, globals } from '../src/prelude'
|
||||
import { parser } from '../src/parser/shrimp'
|
||||
import { treeToString } from '../src/utils/tree'
|
||||
import { runFile, compileFile, parseCode } from '../src'
|
||||
import { bytecodeToString } from 'reefvm'
|
||||
import { VM, fromValue, bytecodeToString } from 'reefvm'
|
||||
import { readFileSync, writeFileSync, mkdirSync } from 'fs'
|
||||
import { randomUUID } from 'crypto'
|
||||
import { spawn } from 'child_process'
|
||||
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 {
|
||||
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 {
|
||||
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() {
|
||||
console.log(`${colors.bright}${colors.magenta}🦐 Shrimp${colors.reset} is a scripting language in a shell.
|
||||
|
||||
|
|
@ -76,7 +112,7 @@ async function main() {
|
|||
console.log(`${colors.bright}usage: shrimp bytecode <file>${colors.reset}`)
|
||||
process.exit(1)
|
||||
}
|
||||
console.log(bytecodeToString(compileFile(file)))
|
||||
console.log(await compileFile(file))
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -86,8 +122,7 @@ async function main() {
|
|||
console.log(`${colors.bright}usage: shrimp parse <file>${colors.reset}`)
|
||||
process.exit(1)
|
||||
}
|
||||
const input = readFileSync(file, 'utf-8')
|
||||
console.log(treeToString(parseCode(input), input))
|
||||
console.log(await parseFile(file))
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -58,9 +58,9 @@ export class Compiler {
|
|||
bytecode: Bytecode
|
||||
pipeCounter = 0
|
||||
|
||||
constructor(public input: string, globals?: string[] | Record<string, any>) {
|
||||
constructor(public input: string, globals?: string[]) {
|
||||
try {
|
||||
if (globals) setGlobals(Array.isArray(globals) ? globals : Object.keys(globals))
|
||||
if (globals) setGlobals(globals)
|
||||
const cst = parser.parse(input)
|
||||
const errors = checkTreeForErrors(cst)
|
||||
|
||||
|
|
|
|||
16
src/index.ts
16
src/index.ts
|
|
@ -1,9 +1,6 @@
|
|||
import { glob, readFileSync } from 'fs'
|
||||
import { readFileSync } from 'fs'
|
||||
import { VM, fromValue, type Bytecode } from 'reefvm'
|
||||
import { type Tree } from '@lezer/common'
|
||||
import { Compiler } from '#compiler/compiler'
|
||||
import { parser } from '#parser/shrimp'
|
||||
import { setGlobals } from '#parser/tokenizer'
|
||||
import { globals as shrimpGlobals, colors } from '#prelude'
|
||||
|
||||
export { Compiler } from '#compiler/compiler'
|
||||
|
|
@ -73,15 +70,4 @@ export function compileCode(code: string, globals?: Record<string, any>): Byteco
|
|||
const globalNames = [...Object.keys(shrimpGlobals), ...(globals ? Object.keys(globals) : [])]
|
||||
const compiler = new Compiler(code, globalNames)
|
||||
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)
|
||||
}
|
||||
|
|
@ -8,9 +8,9 @@ export function specializeKeyword(ident: string) {
|
|||
|
||||
// tell the dotGet searcher about builtin globals
|
||||
export const globals: string[] = []
|
||||
export const setGlobals = (newGlobals: string[] | Record<string, any>) => {
|
||||
export const setGlobals = (newGlobals: string[]) => {
|
||||
globals.length = 0
|
||||
globals.push(...(Array.isArray(newGlobals) ? newGlobals : Object.keys(newGlobals)))
|
||||
globals.push(...newGlobals)
|
||||
}
|
||||
|
||||
// The only chars that can't be words are whitespace, apostrophes, closing parens, and EOF.
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user