Commit 077af5f3

077af5f374bcMerged

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

Description

refactor: rewrite spinel wrapper from Ruby to POSIX shell

Eliminates the CRuby dependency for the wrapper script. With spinel_parse_bin and spinel_codegen compiled, the entire pipeline now runs without Ruby installed.

Falls back to CRuby for parse/codegen when binaries are not available.

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

Details

Commit
077af5f374bc
Parent
a5bb79c4a323

spinelmodifiedView file

@@ -1,12 +1,12 @@
1-#!/usr/bin/env ruby
1+#!/bin/sh
22 # Spinel AOT Compiler - Ruby to native binary
33 #
44 # Usage:
5-# spinel app.rb → compiles to ./app
6-# spinel app.rb -o myapp → compiles to ./myapp
7-# spinel app.rb -c → generate C only (app.c)
8-# spinel app.rb -c -o app.c → generate C to specific file
9-# spinel app.rb -S → print C to stdout
5+# spinel app.rb - compiles to ./app
6+# spinel app.rb -o myapp - compiles to ./myapp
7+# spinel app.rb -c - generate C only (app.c)
8+# spinel app.rb -c -o app.c - generate C to specific file
9+# spinel app.rb -S - print C to stdout
1010 #
1111 # Options:
1212 # -o FILE Output file (binary or C depending on -c)
@@ -16,126 +16,111 @@
1616 # --cc=CMD C compiler command (default: cc)
1717 # --lonig Link with oniguruma (for regexp programs)
1818
19-require "tempfile"
19+DIR="$(cd "$(dirname "$(readlink -f "$0" 2>/dev/null || realpath "$0" 2>/dev/null || echo "$0")")" && pwd)"
20+PARSE_RB="$DIR/spinel_parse.rb"
21+PARSE_BIN="$DIR/spinel_parse_bin"
22+CODEGEN_RB="$DIR/spinel_codegen.rb"
23+CODEGEN_BIN="$DIR/spinel_codegen"
2024
21-dir = File.dirname(File.realpath(__FILE__))
22-parse_rb = File.join(dir, "spinel_parse.rb")
23-parse_bin = File.join(dir, "spinel_parse_bin") # compiled parser (C + libprism)
24-codegen_rb = File.join(dir, "spinel_codegen.rb")
25-codegen_bin = File.join(dir, "spinel_codegen") # compiled binary
25+SOURCE=""
26+OUTPUT=""
27+C_ONLY=0
28+STDOUT_MODE=0
29+OPT_LEVEL="2"
30+CC_CMD="cc"
31+EXTRA_FLAGS=""
2632
27-source = nil
28-output = nil
29-c_only = false
30-stdout_mode = false
31-opt_level = "2"
32-cc_cmd = "cc"
33-extra_flags = []
33+while [ $# -gt 0 ]; do
34+ case "$1" in
35+ --source=*) SOURCE="${1#--source=}" ;;
36+ --output=*) OUTPUT="${1#--output=}" ;;
37+ --cc=*) CC_CMD="${1#--cc=}" ;;
38+ -o) shift; OUTPUT="$1" ;;
39+ -O) shift; OPT_LEVEL="$1" ;;
40+ -c) C_ONLY=1 ;;
41+ -S) STDOUT_MODE=1 ;;
42+ --lonig) EXTRA_FLAGS="$EXTRA_FLAGS -lonig" ;;
43+ *.rb) [ -z "$SOURCE" ] && SOURCE="$1" ;;
44+ esac
45+ shift
46+done
3447
35-i = 0
36-while i < ARGV.length
37- arg = ARGV[i]
38- if arg.start_with?("--source=")
39- source = arg.sub("--source=", "")
40- elsif arg.start_with?("--output=")
41- output = arg.sub("--output=", "")
42- elsif arg.start_with?("--cc=")
43- cc_cmd = arg.sub("--cc=", "")
44- elsif arg == "-o" && i + 1 < ARGV.length
45- i += 1
46- output = ARGV[i]
47- elsif arg == "-O" && i + 1 < ARGV.length
48- i += 1
49- opt_level = ARGV[i]
50- elsif arg == "-c"
51- c_only = true
52- elsif arg == "-S"
53- stdout_mode = true
54- elsif arg == "--lonig"
55- extra_flags << "-lonig"
56- elsif arg.end_with?(".rb") && source.nil?
57- source = arg
58- end
59- i += 1
60-end
61-
62-unless source
63- $stderr.puts "Spinel AOT Compiler"
64- $stderr.puts ""
65- $stderr.puts "Usage: spinel app.rb → compile to ./app"
66- $stderr.puts " spinel app.rb -o myapp → compile to ./myapp"
67- $stderr.puts " spinel app.rb -c → generate app.c only"
68- $stderr.puts " spinel app.rb -S → print C to stdout"
69- $stderr.puts ""
70- $stderr.puts "Options:"
71- $stderr.puts " -o FILE Output file"
72- $stderr.puts " -c C source only (don't compile)"
73- $stderr.puts " -S Print C to stdout"
74- $stderr.puts " -O LEVEL Optimization level (default: 2)"
75- $stderr.puts " --cc=CMD C compiler (default: cc)"
76- $stderr.puts " --lonig Link with oniguruma (regexp)"
48+if [ -z "$SOURCE" ]; then
49+ echo "Spinel AOT Compiler" >&2
50+ echo "" >&2
51+ echo "Usage: spinel app.rb - compile to ./app" >&2
52+ echo " spinel app.rb -o myapp - compile to ./myapp" >&2
53+ echo " spinel app.rb -c - generate app.c only" >&2
54+ echo " spinel app.rb -S - print C to stdout" >&2
55+ echo "" >&2
56+ echo "Options:" >&2
57+ echo " -o FILE Output file" >&2
58+ echo " -c C source only (don't compile)" >&2
59+ echo " -S Print C to stdout" >&2
60+ echo " -O LEVEL Optimization level (default: 2)" >&2
61+ echo " --cc=CMD C compiler (default: cc)" >&2
62+ echo " --lonig Link with oniguruma (regexp)" >&2
7763 exit 1
78-end
64+fi
7965
80-unless File.exist?(source)
81- $stderr.puts "spinel: #{source}: No such file"
66+if [ ! -f "$SOURCE" ]; then
67+ echo "spinel: $SOURCE: No such file" >&2
8268 exit 1
83-end
69+fi
8470
85-basename = File.basename(source, ".rb")
71+BASENAME="$(basename "$SOURCE" .rb)"
72+AST_TMP="$(mktemp /tmp/spinel_ast.XXXXXX)"
73+trap 'rm -f "$AST_TMP" "$C_TMP"' EXIT
8674
8775 # Step 1: Parse (use compiled parser if available, else CRuby + Prism)
88-ast_tmp = Tempfile.new(["spinel_ast", ".txt"])
89-if File.executable?(parse_bin)
90- system(parse_bin, source, ast_tmp.path)
76+if [ -x "$PARSE_BIN" ]; then
77+ "$PARSE_BIN" "$SOURCE" "$AST_TMP"
9178 else
92- system("ruby", parse_rb, source, out: ast_tmp.path)
93-end
94-unless $?.success?
95- $stderr.puts "spinel: parse failed"
96- ast_tmp.close; ast_tmp.unlink
79+ ruby "$PARSE_RB" "$SOURCE" > "$AST_TMP"
80+fi
81+if [ $? -ne 0 ]; then
82+ echo "spinel: parse failed" >&2
9783 exit 1
98-end
84+fi
9985
100-# Step 2: Codegen (use compiled binary if available, else CRuby)
101-if stdout_mode
102- c_file = nil
103- if File.executable?(codegen_bin)
104- system(codegen_bin, ast_tmp.path)
86+# Step 2: Codegen
87+if [ $STDOUT_MODE -eq 1 ]; then
88+ if [ -x "$CODEGEN_BIN" ]; then
89+ "$CODEGEN_BIN" "$AST_TMP"
10590 else
106- system("ruby", codegen_rb, ast_tmp.path)
107- end
108- ast_tmp.close; ast_tmp.unlink
109- exit($?.success? ? 0 : 1)
110-end
91+ ruby "$CODEGEN_RB" "$AST_TMP"
92+ fi
93+ exit $?
94+fi
11195
112-c_file = c_only ? (output || "#{basename}.c") : Tempfile.new(["spinel_out", ".c"]).path
113-if File.executable?(codegen_bin)
114- system(codegen_bin, ast_tmp.path, c_file)
96+if [ $C_ONLY -eq 1 ]; then
97+ C_FILE="${OUTPUT:-$BASENAME.c}"
11598 else
116- system("ruby", codegen_rb, ast_tmp.path, c_file)
117-end
118-ast_tmp.close; ast_tmp.unlink
99+ C_TMP="$(mktemp /tmp/spinel_out.XXXXXX.c)"
100+ C_FILE="$C_TMP"
101+fi
119102
120-unless $?.success?
121- $stderr.puts "spinel: codegen failed"
103+if [ -x "$CODEGEN_BIN" ]; then
104+ "$CODEGEN_BIN" "$AST_TMP" "$C_FILE"
105+else
106+ ruby "$CODEGEN_RB" "$AST_TMP" "$C_FILE"
107+fi
108+if [ $? -ne 0 ]; then
109+ echo "spinel: codegen failed" >&2
122110 exit 1
123-end
111+fi
124112
125-if c_only
126- $stderr.puts "Wrote #{c_file}"
113+if [ $C_ONLY -eq 1 ]; then
114+ echo "Wrote $C_FILE" >&2
127115 exit 0
128-end
129-
130-# Step 3: Compile C → binary
131-bin_file = output || basename
132-cc_args = [cc_cmd, "-O#{opt_level}", "-Wno-all", c_file, "-lm"] + extra_flags + ["-o", bin_file]
133-system(*cc_args)
134-File.delete(c_file) if $?.success? && !c_only
116+fi
135117
136-unless $?.success?
137- $stderr.puts "spinel: C compilation failed"
118+# Step 3: Compile C -> binary
119+BIN_FILE="${OUTPUT:-$BASENAME}"
120+$CC_CMD -O${OPT_LEVEL} -Wno-all "$C_FILE" -lm $EXTRA_FLAGS -o "$BIN_FILE"
121+if [ $? -ne 0 ]; then
122+ echo "spinel: C compilation failed" >&2
138123 exit 1
139-end
124+fi
140125
141-$stderr.puts "#{source} → #{bin_file}"
126+echo "$SOURCE -> $BIN_FILE" >&2