From 10e1986fe22344134846e6d170bfdb6ae9e6a7b6 Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Sun, 9 Nov 2025 19:52:59 -0800 Subject: [PATCH] cli: add print/-E --- bin/shrimp | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/bin/shrimp b/bin/shrimp index 49d1d78..fe856b3 100755 --- a/bin/shrimp +++ b/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 ${colors.reset}`) + process.exit(1) + } + + console.log(await evalCode(code, imports)) return }