cli: add print/-E
This commit is contained in:
parent
7d14ef173b
commit
3a1e9e9250
30
bin/shrimp
30
bin/shrimp
|
|
@ -2,10 +2,9 @@
|
||||||
|
|
||||||
import { colors } from '../src/prelude'
|
import { colors } from '../src/prelude'
|
||||||
import { treeToString } from '../src/utils/tree'
|
import { treeToString } from '../src/utils/tree'
|
||||||
import { runFile, compileFile, parseCode } from '../src'
|
import { runCode, runFile, compileFile, parseCode } from '../src'
|
||||||
import { bytecodeToString } from 'reefvm'
|
import { bytecodeToString } from 'reefvm'
|
||||||
import { readFileSync, writeFileSync, mkdirSync } from 'fs'
|
import { readFileSync } from 'fs'
|
||||||
import { randomUUID } from 'crypto'
|
|
||||||
import { spawn } from 'child_process'
|
import { spawn } from 'child_process'
|
||||||
import { join } from 'path'
|
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}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}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}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}repl${colors.reset} Start REPL
|
||||||
${colors.cyan}help${colors.reset} Print this help message
|
${colors.cyan}help${colors.reset} Print this help message
|
||||||
${colors.cyan}version${colors.reset} Print version
|
${colors.cyan}version${colors.reset} Print version
|
||||||
|
|
@ -33,6 +33,12 @@ function showVersion() {
|
||||||
console.log('🦐 v0.0.1')
|
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() {
|
async function main() {
|
||||||
let args = process.argv.slice(2)
|
let args = process.argv.slice(2)
|
||||||
|
|
||||||
|
|
@ -101,14 +107,18 @@ async function main() {
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepend import statement if -I flags were provided
|
await evalCode(code, imports)
|
||||||
const importStatement = imports.length > 0 ? `import ${imports.join(' ')}` : ''
|
return
|
||||||
const fullCode = importStatement ? `${importStatement}; ${code}` : code
|
}
|
||||||
|
|
||||||
try { mkdirSync('/tmp/shrimp') } catch { }
|
if (['print', '-print', '--print', '-E'].includes(command)) {
|
||||||
const path = `/tmp/shrimp/${randomUUID()}.sh`
|
const code = args[1]
|
||||||
writeFileSync(path, fullCode)
|
if (!code) {
|
||||||
await runFile(path)
|
console.log(`${colors.bright}usage: shrimp print <code>${colors.reset}`)
|
||||||
|
process.exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(await evalCode(code, imports))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user