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 ruby1+
#!/bin/sh22
# Spinel AOT Compiler - Ruby to native binary33
#44
# Usage:5-
# spinel app.rb → compiles to ./app6-
# spinel app.rb -o myapp → compiles to ./myapp7-
# spinel app.rb -c → generate C only (app.c)8-
# spinel app.rb -c -o app.c → generate C to specific file9-
# spinel app.rb -S → print C to stdout5+
# spinel app.rb - compiles to ./app6+
# spinel app.rb -o myapp - compiles to ./myapp7+
# spinel app.rb -c - generate C only (app.c)8+
# spinel app.rb -c -o app.c - generate C to specific file9+
# spinel app.rb -S - print C to stdout1010
#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 binary25+
SOURCE=""26+
OUTPUT=""27+
C_ONLY=028+
STDOUT_MODE=029+
OPT_LEVEL="2"30+
CC_CMD="cc"31+
EXTRA_FLAGS=""2632
27-
source = nil28-
output = nil29-
c_only = false30-
stdout_mode = false31-
opt_level = "2"32-
cc_cmd = "cc"33-
extra_flags = []33+
while [ $# -gt 0 ]; do34+
case "$1" in35+
--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+
esac45+
shift46+
done3447
35-
i = 036-
while i < ARGV.length37-
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.length45-
i += 146-
output = ARGV[i]47-
elsif arg == "-O" && i + 1 < ARGV.length48-
i += 149-
opt_level = ARGV[i]50-
elsif arg == "-c"51-
c_only = true52-
elsif arg == "-S"53-
stdout_mode = true54-
elsif arg == "--lonig"55-
extra_flags << "-lonig"56-
elsif arg.end_with?(".rb") && source.nil?57-
source = arg58-
end59-
i += 160-
end61-
62-
unless source63-
$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" ]; then49+
echo "Spinel AOT Compiler" >&250+
echo "" >&251+
echo "Usage: spinel app.rb - compile to ./app" >&252+
echo " spinel app.rb -o myapp - compile to ./myapp" >&253+
echo " spinel app.rb -c - generate app.c only" >&254+
echo " spinel app.rb -S - print C to stdout" >&255+
echo "" >&256+
echo "Options:" >&257+
echo " -o FILE Output file" >&258+
echo " -c C source only (don't compile)" >&259+
echo " -S Print C to stdout" >&260+
echo " -O LEVEL Optimization level (default: 2)" >&261+
echo " --cc=CMD C compiler (default: cc)" >&262+
echo " --lonig Link with oniguruma (regexp)" >&27763
exit 178-
end64+
fi7965
80-
unless File.exist?(source)81-
$stderr.puts "spinel: #{source}: No such file"66+
if [ ! -f "$SOURCE" ]; then67+
echo "spinel: $SOURCE: No such file" >&28268
exit 183-
end69+
fi8470
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"' EXIT8674
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" ]; then77+
"$PARSE_BIN" "$SOURCE" "$AST_TMP"9178
else92-
system("ruby", parse_rb, source, out: ast_tmp.path)93-
end94-
unless $?.success?95-
$stderr.puts "spinel: parse failed"96-
ast_tmp.close; ast_tmp.unlink79+
ruby "$PARSE_RB" "$SOURCE" > "$AST_TMP"80+
fi81+
if [ $? -ne 0 ]; then82+
echo "spinel: parse failed" >&29783
exit 198-
end84+
fi9985
100-
# Step 2: Codegen (use compiled binary if available, else CRuby)101-
if stdout_mode102-
c_file = nil103-
if File.executable?(codegen_bin)104-
system(codegen_bin, ast_tmp.path)86+
# Step 2: Codegen87+
if [ $STDOUT_MODE -eq 1 ]; then88+
if [ -x "$CODEGEN_BIN" ]; then89+
"$CODEGEN_BIN" "$AST_TMP"10590
else106-
system("ruby", codegen_rb, ast_tmp.path)107-
end108-
ast_tmp.close; ast_tmp.unlink109-
exit($?.success? ? 0 : 1)110-
end91+
ruby "$CODEGEN_RB" "$AST_TMP"92+
fi93+
exit $?94+
fi11195
112-
c_file = c_only ? (output || "#{basename}.c") : Tempfile.new(["spinel_out", ".c"]).path113-
if File.executable?(codegen_bin)114-
system(codegen_bin, ast_tmp.path, c_file)96+
if [ $C_ONLY -eq 1 ]; then97+
C_FILE="${OUTPUT:-$BASENAME.c}"11598
else116-
system("ruby", codegen_rb, ast_tmp.path, c_file)117-
end118-
ast_tmp.close; ast_tmp.unlink99+
C_TMP="$(mktemp /tmp/spinel_out.XXXXXX.c)"100+
C_FILE="$C_TMP"101+
fi119102
120-
unless $?.success?121-
$stderr.puts "spinel: codegen failed"103+
if [ -x "$CODEGEN_BIN" ]; then104+
"$CODEGEN_BIN" "$AST_TMP" "$C_FILE"105+
else106+
ruby "$CODEGEN_RB" "$AST_TMP" "$C_FILE"107+
fi108+
if [ $? -ne 0 ]; then109+
echo "spinel: codegen failed" >&2122110
exit 1123-
end111+
fi124112
125-
if c_only126-
$stderr.puts "Wrote #{c_file}"113+
if [ $C_ONLY -eq 1 ]; then114+
echo "Wrote $C_FILE" >&2127115
exit 0128-
end129-
130-
# Step 3: Compile C → binary131-
bin_file = output || basename132-
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_only116+
fi135117
136-
unless $?.success?137-
$stderr.puts "spinel: C compilation failed"118+
# Step 3: Compile C -> binary119+
BIN_FILE="${OUTPUT:-$BASENAME}"120+
$CC_CMD -O${OPT_LEVEL} -Wno-all "$C_FILE" -lm $EXTRA_FLAGS -o "$BIN_FILE"121+
if [ $? -ne 0 ]; then122+
echo "spinel: C compilation failed" >&2138123
exit 1139-
end124+
fi140125
141-
$stderr.puts "#{source} → #{bin_file}"126+
echo "$SOURCE -> $BIN_FILE" >&2