import dict #45
30
bin/shrimp
30
bin/shrimp
|
|
@ -2,10 +2,9 @@
|
|||
|
||||
import { colors } from '../src/prelude'
|
||||
import { treeToString } from '../src/utils/tree'
|
||||
import { runFile, compileFile, parseCode } from '../src'
|
||||
import { runCode, runFile, compileFile, parseCode } from '../src'
|
||||
import { bytecodeToString } from 'reefvm'
|
||||
import { readFileSync, writeFileSync, mkdirSync } from 'fs'
|
||||
import { randomUUID } from 'crypto'
|
||||
import { readFileSync } from 'fs'
|
||||
import { spawn } from 'child_process'
|
||||
import { join } from 'path'
|
||||
|
||||
|
|
@ -19,6 +18,7 @@ ${colors.bright}Commands:${colors.reset}
|
|||
${colors.cyan}parse ${colors.yellow}./my-file.sh${colors.reset} Print parse tree for Shrimp file
|
||||
${colors.cyan}bytecode ${colors.yellow}./my-file.sh${colors.reset} Print bytecode for Shrimp file
|
||||
${colors.cyan}eval ${colors.yellow}'some code'${colors.reset} Evaluate a line of Shrimp code
|
||||
${colors.cyan}print ${colors.yellow}'some code'${colors.reset} Evaluate a line of Shrimp code and print the result
|
||||
${colors.cyan}repl${colors.reset} Start REPL
|
||||
${colors.cyan}help${colors.reset} Print this help message
|
||||
${colors.cyan}version${colors.reset} Print version
|
||||
|
|
@ -33,6 +33,12 @@ function showVersion() {
|
|||
console.log('🦐 v0.0.1')
|
||||
}
|
||||
|
||||
async function evalCode(code: string, imports: string[]) {
|
||||
const importStatement = imports.length > 0 ? `import ${imports.join(' ')}` : ''
|
||||
if (importStatement) code = `${importStatement}; ${code}`
|
||||
return await runCode(code)
|
||||
}
|
||||
|
||||
async function main() {
|
||||
let args = process.argv.slice(2)
|
||||
|
||||
|
|
@ -101,14 +107,18 @@ async function main() {
|
|||
process.exit(1)
|
||||
}
|
||||
|
||||
// Prepend import statement if -I flags were provided
|
||||
const importStatement = imports.length > 0 ? `import ${imports.join(' ')}` : ''
|
||||
const fullCode = importStatement ? `${importStatement}; ${code}` : code
|
||||
await evalCode(code, imports)
|
||||
return
|
||||
}
|
||||
|
||||
try { mkdirSync('/tmp/shrimp') } catch { }
|
||||
const path = `/tmp/shrimp/${randomUUID()}.sh`
|
||||
writeFileSync(path, fullCode)
|
||||
await runFile(path)
|
||||
if (['print', '-print', '--print', '-E'].includes(command)) {
|
||||
const code = args[1]
|
||||
if (!code) {
|
||||
console.log(`${colors.bright}usage: shrimp print <code>${colors.reset}`)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
console.log(await evalCode(code, imports))
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user