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
# spinel app.rb -c - generate C only (app.c)# 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)## Options:# -o FILE Output file (binary or C depending on -c)# -S Print generated C to stdout# -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)DIR="$(cd "$(dirname "$(readlink -f "$0" 2>/dev/null || realpath "$0" 2>/dev/null || echo "$0")")" && pwd)"PARSE_BIN="$DIR/spinel_parse"CC_CMD="cc"EXTRA_FLAGS=""C_TMP=""EVAL_SCRIPT=""EVAL_USED=0EVAL_TMP=""while [ $# -gt 0 ]; do case "$1" in --cc=*) CC_CMD="${1#--cc=}" ;; -o) shift; OUTPUT="$1" ;; -O) shift; OPT_LEVEL="$1" ;; -e) shift if [ "$EVAL_USED" -eq 0 ]; then EVAL_SCRIPT="$1" else EVAL_SCRIPT="$EVAL_SCRIPT$1" fi EVAL_USED=1 ;; -c) C_ONLY=1 ;; -S) STDOUT_MODE=1 ;; *.rb) [ -z "$SOURCE" ] && SOURCE="$1" ;; shiftdone# `-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# .rb file were given, -e wins (consistent with `ruby -e ... script.rb`# where the .rb file becomes ARGV — Spinel only consumes the source).if [ "$EVAL_USED" -eq 1 ]; then EVAL_TMP="$(mktemp /tmp/spinel_eval.XXXXXX).rb" printf '%s\n' "$EVAL_SCRIPT" > "$EVAL_TMP" SOURCE="$EVAL_TMP"fiif [ -z "$SOURCE" ]; then echo "Spinel AOT Compiler" >&2 echo "" >&2 echo " spinel app.rb -o myapp - compile to ./myapp" >&2 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 "" >&2 echo "Options:" >&2 echo " -o FILE Output file" >&2 echo " -S Print C to stdout" >&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)" >&2 exit 1fi exit 1fiBASENAME="$(basename "$SOURCE" .rb)"if [ "$EVAL_USED" -eq 1 ]; then BASENAME="a"else BASENAME="$(basename "$SOURCE" .rb)"fiAST_TMP="$(mktemp /tmp/spinel_ast.XXXXXX)"trap 'rm -f "$AST_TMP" "$C_TMP"' EXITtrap 'rm -f "$AST_TMP" "$C_TMP" "$EVAL_TMP"' EXIT# Step 1: Parse. spinel_parse is C-only; build it via `make parse`# (or `make all`) before invoking spinel.