Commit f3719584

f371958428d5Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

feat(spinel): add -e for inline Ruby source

spinel -e 'puts 42' -> ./a spinel -e 'a = 10' -e 'puts a + 1' -o p -> ./p spinel -e 'p [1, 2, 3]' -S -> C to stdout

Mirrors ruby -e ...: each -e is a chunk of inline source, multiple -e flags join with a newline, and the result becomes the program. The accumulated text is written to a temporary .rb file so the rest of the pipeline (parser, codegen, cc) doesn't need to know about inline sources. Default output binary name is a since there's no .rb basename to derive from; -o overrides as usual.

If both -e and a positional .rb file are given, -e wins. The trap that cleans up the AST and C temp files now also removes the inline-source temp file.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
f371958428d5
Parent
e735e99a1233

spinelmodifiedView file

@@ -7,6 +7,7 @@
77 # spinel app.rb -c - generate C only (app.c)
88 # spinel app.rb -c -o app.c - generate C to specific file
99 # spinel app.rb -S - print C to stdout
10+# spinel -e 'puts 42' - compile inline source (default name: a)
1011 #
1112 # Options:
1213 # -o FILE Output file (binary or C depending on -c)
@@ -14,6 +15,7 @@
1415 # -S Print generated C to stdout
1516 # -O LEVEL Optimization level for cc (default: 2)
1617 # --cc=CMD C compiler command (default: cc)
18+# -e STR Inline Ruby source (repeatable; multiple -e join with \n)
1719
1820 DIR="$(cd "$(dirname "$(readlink -f "$0" 2>/dev/null || realpath "$0" 2>/dev/null || echo "$0")")" && pwd)"
1921 PARSE_BIN="$DIR/spinel_parse"
@@ -37,6 +39,9 @@
3739 CC_CMD="cc"
3840 EXTRA_FLAGS=""
3941 C_TMP=""
42+EVAL_SCRIPT=""
43+EVAL_USED=0
44+EVAL_TMP=""
4045
4146 while [ $# -gt 0 ]; do
4247 case "$1" in
@@ -45,6 +50,14 @@
4550 --cc=*) CC_CMD="${1#--cc=}" ;;
4651 -o) shift; OUTPUT="$1" ;;
4752 -O) shift; OPT_LEVEL="$1" ;;
53+ -e) shift
54+ if [ "$EVAL_USED" -eq 0 ]; then
55+ EVAL_SCRIPT="$1"
56+ else
57+ EVAL_SCRIPT="$EVAL_SCRIPT
58+$1"
59+ fi
60+ EVAL_USED=1 ;;
4861 -c) C_ONLY=1 ;;
4962 -S) STDOUT_MODE=1 ;;
5063 *.rb) [ -z "$SOURCE" ] && SOURCE="$1" ;;
@@ -52,6 +65,17 @@
5265 shift
5366 done
5467
68+# `-e` mode: write the accumulated inline script to a temp file and
69+# treat it as the source. Default output name is `a` since the
70+# inline form has no .rb basename to derive from. If both -e and a
71+# .rb file were given, -e wins (consistent with `ruby -e ... script.rb`
72+# where the .rb file becomes ARGV — Spinel only consumes the source).
73+if [ "$EVAL_USED" -eq 1 ]; then
74+ EVAL_TMP="$(mktemp /tmp/spinel_eval.XXXXXX).rb"
75+ printf '%s\n' "$EVAL_SCRIPT" > "$EVAL_TMP"
76+ SOURCE="$EVAL_TMP"
77+fi
78+
5579 if [ -z "$SOURCE" ]; then
5680 echo "Spinel AOT Compiler" >&2
5781 echo "" >&2
@@ -59,6 +83,7 @@
5983 echo " spinel app.rb -o myapp - compile to ./myapp" >&2
6084 echo " spinel app.rb -c - generate app.c only" >&2
6185 echo " spinel app.rb -S - print C to stdout" >&2
86+ echo " spinel -e 'puts 42' - compile inline source" >&2
6287 echo "" >&2
6388 echo "Options:" >&2
6489 echo " -o FILE Output file" >&2
@@ -66,6 +91,7 @@
6691 echo " -S Print C to stdout" >&2
6792 echo " -O LEVEL Optimization level (default: 2)" >&2
6893 echo " --cc=CMD C compiler (default: cc)" >&2
94+ echo " -e STR Inline Ruby source (repeatable; joined with newlines)" >&2
6995 exit 1
7096 fi
7197
@@ -74,9 +100,13 @@
74100 exit 1
75101 fi
76102
77-BASENAME="$(basename "$SOURCE" .rb)"
103+if [ "$EVAL_USED" -eq 1 ]; then
104+ BASENAME="a"
105+else
106+ BASENAME="$(basename "$SOURCE" .rb)"
107+fi
78108 AST_TMP="$(mktemp /tmp/spinel_ast.XXXXXX)"
79-trap 'rm -f "$AST_TMP" "$C_TMP"' EXIT
109+trap 'rm -f "$AST_TMP" "$C_TMP" "$EVAL_TMP"' EXIT
80110
81111 # Step 1: Parse. spinel_parse is C-only; build it via `make parse`
82112 # (or `make all`) before invoking spinel.