Commit 8bcde2a7
8bcde2a736cfMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
feat(cli): -E flag compiles and runs the source in one step
./spinel -E foo.rb a b c parses + analyzes + codegens + compiles
to a fresh /tmp/spinel_run.* directory, then runs the produced
binary with a b c as ARGV. The leftover positionals after the
first .rb token (or after the last spinel-recognized flag in
-e mode) are passed straight to the child. The child's exit code
is propagated; the EXIT trap reaps the temp directory so the cwd
stays clean.
Rejects combinations that don't make sense alongside run mode: -E + -o / -c / -S are surfaced as a clear error before the parse step. -E + -e works (inline + run); ARGV after the inline source flows through.
The Windows .bat wrapper still lags on -e and now -E both; same parity gap as before.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 8bcde2a736cf
- Parent
- 22367b9826c4
spinelmodifiedView file
# spinel app.rb -c -o app.c - generate C to specific file# spinel app.rb -S - print C to stdout# spinel -e 'puts 42' - compile inline source (default name: a)# spinel -E app.rb a b c - compile to /tmp, run, ARGV=[a, b, c]## Options:# -o FILE Output file (binary or C depending on -c)# -c Generate C source only, don't compile# -S Print generated C to stdout# -E Run the compiled binary instead of saving it; the# first .rb file ends spinel-flag parsing, and the rest# of the command line becomes the binary's ARGV.# -O LEVEL Optimization level for cc (default: 2)# --cc=CMD C compiler command (default: cc)# -e STR Inline Ruby source (repeatable; multiple -e join with \n)OUTPUT=""C_ONLY=0STDOUT_MODE=0RUN_MODE=0OPT_LEVEL="2"CC_CMD="cc"EXTRA_FLAGS=""C_TMP=""C_TMP_DIR=""RUN_TMP_DIR=""EVAL_SCRIPT=""EVAL_USED=0EVAL_TMP=""RBS_DIR=""SEED_TMP=""# Parse spinel-side flags. In `-E` (run) mode, the first positional# `.rb` arg marks the boundary between spinel flags and the script's# ARGV: everything after that point is passed straight to the# compiled binary. The same boundary trip fires on the first# unrecognized token in run mode so `spinel -E -e '...' arg1 arg2`# also hands the trailing positionals through.while [ $# -gt 0 ]; do case "$1" in --source=*) SOURCE="${1#--source=}" ;; --output=*) OUTPUT="${1#--output=}" ;; --cc=*) CC_CMD="${1#--cc=}" ;; --rbs=*) RBS_DIR="${1#--rbs=}" ;; --rbs) shift; RBS_DIR="$1" ;; -o) shift; OUTPUT="$1" ;; -O) shift; OPT_LEVEL="$1" ;; --source=*) SOURCE="${1#--source=}"; shift ;; --output=*) OUTPUT="${1#--output=}"; shift ;; --cc=*) CC_CMD="${1#--cc=}"; shift ;; --rbs=*) RBS_DIR="${1#--rbs=}"; shift ;; --rbs) shift; RBS_DIR="$1"; shift ;; -o) shift; OUTPUT="$1"; shift ;; -O) shift; OPT_LEVEL="$1"; shift ;; -e) shift if [ "$EVAL_USED" -eq 0 ]; then EVAL_SCRIPT="$1" EVAL_SCRIPT="$EVAL_SCRIPT$1" fi EVAL_USED=1 ;; -c) C_ONLY=1 ;; -S) STDOUT_MODE=1 ;; *.rb) [ -z "$SOURCE" ] && SOURCE="$1" ;; EVAL_USED=1 shift ;; -c) C_ONLY=1; shift ;; -S) STDOUT_MODE=1; shift ;; -E) RUN_MODE=1; shift ;; *.rb) if [ -z "$SOURCE" ]; then SOURCE="$1" shift [ "$RUN_MODE" -eq 1 ] && break else shift fi ;; *) if [ "$RUN_MODE" -eq 1 ]; then break fi shift ;; esac shiftdone# Mode-conflict checks: `-E` short-circuits to "run the produced# binary" so output-saving flags (`-o`, `-c`, `-S`) don't make sense# alongside it.if [ "$RUN_MODE" -eq 1 ]; then if [ -n "$OUTPUT" ]; then echo "spinel: -E (run) cannot be combined with -o" >&2 exit 2 fi if [ "$C_ONLY" -eq 1 ]; then echo "spinel: -E (run) cannot be combined with -c" >&2 exit 2 fi if [ "$STDOUT_MODE" -eq 1 ]; then echo "spinel: -E (run) cannot be combined with -S" >&2 exit 2 fifi# `-e` mode: write the accumulated inline script to a temp file and# treat it as the source. Default output name is `a` since the# inline form has no .rb basename to derive from. If both -e and a echo " spinel app.rb -c - generate app.c only" >&2 echo " spinel app.rb -S - print C to stdout" >&2 echo " spinel -e 'puts 42' - compile inline source" >&2 echo " spinel -E app.rb a b c - compile + run with ARGV=[a, b, c]" >&2 echo "" >&2 echo "Options:" >&2 echo " -o FILE Output file" >&2 echo " -c C source only (don't compile)" >&2 echo " -S Print C to stdout" >&2 echo " -E Run the compiled binary; leftover args become its ARGV" >&2 echo " -O LEVEL Optimization level (default: 2)" >&2 echo " --cc=CMD C compiler (default: cc)" >&2 echo " -e STR Inline Ruby source (repeatable; joined with newlines)" >&2fiAST_TMP="$(mktemp /tmp/spinel_ast.XXXXXX)"IR_TMP="$(mktemp /tmp/spinel_ir.XXXXXX)"trap 'rm -f "$AST_TMP" "$IR_TMP" "$EVAL_TMP" "$SEED_TMP"; [ -n "$C_TMP_DIR" ] && rm -rf "$C_TMP_DIR"' EXITtrap 'rm -f "$AST_TMP" "$IR_TMP" "$EVAL_TMP" "$SEED_TMP"; [ -n "$C_TMP_DIR" ] && rm -rf "$C_TMP_DIR"; [ -n "$RUN_TMP_DIR" ] && rm -rf "$RUN_TMP_DIR"' EXIT# Step 1: Parse. spinel_parse is C-only; build it via `make parse`# (or `make all`) before invoking spinel.fi# Step 3: Compile C -> binaryBIN_FILE="${OUTPUT:-$BASENAME}"# In -E (run) mode the binary is short-lived: drop it into a fresh# temp dir so the cwd stays clean and the trap reaps the file when# the script exits (including via signal).if [ "$RUN_MODE" -eq 1 ]; then RUN_TMP_DIR="$(mktemp -d /tmp/spinel_run.XXXXXX)" || { echo "spinel: mktemp -d failed" >&2; exit 1; } BIN_FILE="$RUN_TMP_DIR/$BASENAME"else BIN_FILE="${OUTPUT:-$BASENAME}"fi# Runtime library (bigint + regexp)SP_RT_LIB="$DIR/lib/libspinel_rt.a"INCLUDE_FLAGS="-I$DIR/lib" exit 1fiif [ "$RUN_MODE" -eq 1 ]; then # Hand off to the freshly compiled binary, forwarding the # leftover positionals as its ARGV. Run as a subprocess (not # exec) so the EXIT trap reaps RUN_TMP_DIR after the child # returns. Propagate the child's exit code. "$BIN_FILE" "$@" exit $?fiecho "$SOURCE -> $BIN_FILE" >&2