From 6f531a2ebfac99a87063fcb82a451016eac15a7a Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Sat, 1 Nov 2025 11:17:39 -0700 Subject: [PATCH] ./bin/shrimp parse file --- bin/shrimp | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/bin/shrimp b/bin/shrimp index 49cd7f3..5706ab9 100755 --- a/bin/shrimp +++ b/bin/shrimp @@ -2,9 +2,11 @@ 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 { VM, fromValue, bytecodeToString } from 'reefvm' import { readFileSync, writeFileSync, mkdirSync } from 'fs' -import { randomUUID } from "crypto" +import { randomUUID } from 'crypto' import { spawn } from 'child_process' import { join } from 'path' @@ -32,6 +34,17 @@ async function compileFile(filePath: string) { } } +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. @@ -39,6 +52,7 @@ ${colors.bright}Usage:${colors.reset} shrimp [...args] ${colors.bright}Commands:${colors.reset} ${colors.cyan}run ${colors.yellow}./my-file.sh${colors.reset} Execute a file with Shrimp + ${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}repl${colors.reset} Start REPL @@ -102,6 +116,16 @@ async function main() { return } + if (['parse', '-parse', '--parse', '-p'].includes(command)) { + const file = args[1] + if (!file) { + console.log(`${colors.bright}usage: shrimp parse ${colors.reset}`) + process.exit(1) + } + console.log(await parseFile(file)) + return + } + if (['run', '-run', '--run', '-r'].includes(command)) { const file = args[1] if (!file) {