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

@@ -8,11 +8,15 @@
88 # spinel app.rb -c -o app.c - generate C to specific file
99 # spinel app.rb -S - print C to stdout
1010 # spinel -e 'puts 42' - compile inline source (default name: a)
11+# spinel -E app.rb a b c - compile to /tmp, run, ARGV=[a, b, c]
1112 #
1213 # Options:
1314 # -o FILE Output file (binary or C depending on -c)
1415 # -c Generate C source only, don't compile
1516 # -S Print generated C to stdout
17+# -E Run the compiled binary instead of saving it; the
18+# first .rb file ends spinel-flag parsing, and the rest
19+# of the command line becomes the binary's ARGV.
1620 # -O LEVEL Optimization level for cc (default: 2)
1721 # --cc=CMD C compiler command (default: cc)
1822 # -e STR Inline Ruby source (repeatable; multiple -e join with \n)
@@ -40,26 +44,34 @@
4044 OUTPUT=""
4145 C_ONLY=0
4246 STDOUT_MODE=0
47+RUN_MODE=0
4348 OPT_LEVEL="2"
4449 CC_CMD="cc"
4550 EXTRA_FLAGS=""
4651 C_TMP=""
4752 C_TMP_DIR=""
53+RUN_TMP_DIR=""
4854 EVAL_SCRIPT=""
4955 EVAL_USED=0
5056 EVAL_TMP=""
5157 RBS_DIR=""
5258 SEED_TMP=""
5359
60+# Parse spinel-side flags. In `-E` (run) mode, the first positional
61+# `.rb` arg marks the boundary between spinel flags and the script's
62+# ARGV: everything after that point is passed straight to the
63+# compiled binary. The same boundary trip fires on the first
64+# unrecognized token in run mode so `spinel -E -e '...' arg1 arg2`
65+# also hands the trailing positionals through.
5466 while [ $# -gt 0 ]; do
5567 case "$1" in
56- --source=*) SOURCE="${1#--source=}" ;;
57- --output=*) OUTPUT="${1#--output=}" ;;
58- --cc=*) CC_CMD="${1#--cc=}" ;;
59- --rbs=*) RBS_DIR="${1#--rbs=}" ;;
60- --rbs) shift; RBS_DIR="$1" ;;
61- -o) shift; OUTPUT="$1" ;;
62- -O) shift; OPT_LEVEL="$1" ;;
68+ --source=*) SOURCE="${1#--source=}"; shift ;;
69+ --output=*) OUTPUT="${1#--output=}"; shift ;;
70+ --cc=*) CC_CMD="${1#--cc=}"; shift ;;
71+ --rbs=*) RBS_DIR="${1#--rbs=}"; shift ;;
72+ --rbs) shift; RBS_DIR="$1"; shift ;;
73+ -o) shift; OUTPUT="$1"; shift ;;
74+ -O) shift; OPT_LEVEL="$1"; shift ;;
6375 -e) shift
6476 if [ "$EVAL_USED" -eq 0 ]; then
6577 EVAL_SCRIPT="$1"
@@ -67,14 +79,47 @@
6779 EVAL_SCRIPT="$EVAL_SCRIPT
6880 $1"
6981 fi
70- EVAL_USED=1 ;;
71- -c) C_ONLY=1 ;;
72- -S) STDOUT_MODE=1 ;;
73- *.rb) [ -z "$SOURCE" ] && SOURCE="$1" ;;
82+ EVAL_USED=1
83+ shift ;;
84+ -c) C_ONLY=1; shift ;;
85+ -S) STDOUT_MODE=1; shift ;;
86+ -E) RUN_MODE=1; shift ;;
87+ *.rb)
88+ if [ -z "$SOURCE" ]; then
89+ SOURCE="$1"
90+ shift
91+ [ "$RUN_MODE" -eq 1 ] && break
92+ else
93+ shift
94+ fi
95+ ;;
96+ *)
97+ if [ "$RUN_MODE" -eq 1 ]; then
98+ break
99+ fi
100+ shift
101+ ;;
74102 esac
75- shift
76103 done
77104
105+# Mode-conflict checks: `-E` short-circuits to "run the produced
106+# binary" so output-saving flags (`-o`, `-c`, `-S`) don't make sense
107+# alongside it.
108+if [ "$RUN_MODE" -eq 1 ]; then
109+ if [ -n "$OUTPUT" ]; then
110+ echo "spinel: -E (run) cannot be combined with -o" >&2
111+ exit 2
112+ fi
113+ if [ "$C_ONLY" -eq 1 ]; then
114+ echo "spinel: -E (run) cannot be combined with -c" >&2
115+ exit 2
116+ fi
117+ if [ "$STDOUT_MODE" -eq 1 ]; then
118+ echo "spinel: -E (run) cannot be combined with -S" >&2
119+ exit 2
120+ fi
121+fi
122+
78123 # `-e` mode: write the accumulated inline script to a temp file and
79124 # treat it as the source. Default output name is `a` since the
80125 # inline form has no .rb basename to derive from. If both -e and a
@@ -94,11 +139,13 @@
94139 echo " spinel app.rb -c - generate app.c only" >&2
95140 echo " spinel app.rb -S - print C to stdout" >&2
96141 echo " spinel -e 'puts 42' - compile inline source" >&2
142+ echo " spinel -E app.rb a b c - compile + run with ARGV=[a, b, c]" >&2
97143 echo "" >&2
98144 echo "Options:" >&2
99145 echo " -o FILE Output file" >&2
100146 echo " -c C source only (don't compile)" >&2
101147 echo " -S Print C to stdout" >&2
148+ echo " -E Run the compiled binary; leftover args become its ARGV" >&2
102149 echo " -O LEVEL Optimization level (default: 2)" >&2
103150 echo " --cc=CMD C compiler (default: cc)" >&2
104151 echo " -e STR Inline Ruby source (repeatable; joined with newlines)" >&2
@@ -118,7 +165,7 @@
118165 fi
119166 AST_TMP="$(mktemp /tmp/spinel_ast.XXXXXX)"
120167 IR_TMP="$(mktemp /tmp/spinel_ir.XXXXXX)"
121-trap 'rm -f "$AST_TMP" "$IR_TMP" "$EVAL_TMP" "$SEED_TMP"; [ -n "$C_TMP_DIR" ] && rm -rf "$C_TMP_DIR"' EXIT
168+trap '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
122169
123170 # Step 1: Parse. spinel_parse is C-only; build it via `make parse`
124171 # (or `make all`) before invoking spinel.
@@ -199,7 +246,15 @@
199246 fi
200247
201248 # Step 3: Compile C -> binary
202-BIN_FILE="${OUTPUT:-$BASENAME}"
249+# In -E (run) mode the binary is short-lived: drop it into a fresh
250+# temp dir so the cwd stays clean and the trap reaps the file when
251+# the script exits (including via signal).
252+if [ "$RUN_MODE" -eq 1 ]; then
253+ RUN_TMP_DIR="$(mktemp -d /tmp/spinel_run.XXXXXX)" || { echo "spinel: mktemp -d failed" >&2; exit 1; }
254+ BIN_FILE="$RUN_TMP_DIR/$BASENAME"
255+else
256+ BIN_FILE="${OUTPUT:-$BASENAME}"
257+fi
203258 # Runtime library (bigint + regexp)
204259 SP_RT_LIB="$DIR/lib/libspinel_rt.a"
205260 INCLUDE_FLAGS="-I$DIR/lib"
@@ -234,4 +289,13 @@
234289 exit 1
235290 fi
236291
292+if [ "$RUN_MODE" -eq 1 ]; then
293+ # Hand off to the freshly compiled binary, forwarding the
294+ # leftover positionals as its ARGV. Run as a subprocess (not
295+ # exec) so the EXIT trap reaps RUN_TMP_DIR after the child
296+ # returns. Propagate the child's exit code.
297+ "$BIN_FILE" "$@"
298+ exit $?
299+fi
300+
237301 echo "$SOURCE -> $BIN_FILE" >&2