Transpilation: PatLang to idiomatic Ruby
A self-hosted Ruby transpiler (lib/transpile_ruby.patlang) walking the PRE-LOWERING AST directly (real if/while/def already present, unlike the flattened jump-based IR the ordinary compiler produces) to emit idiomatic Ruby โ not an embedded PatLang VM ported to Ruby. Unsupported constructs (networking, the facts/logic engine, contracts, meta-compilation) fail the transpile with a named error rather than emitting silently-broken output. Reflection (lib/reflect.patlang, source โ AST โ JSON) is built the same way, reusing the existing self-hosted lexer/parser.
PatLang source:
make a function called fib takes n returns r
if n < 2 then
return n
else
return fib(n - 1) + fib(n - 2)
end
end
let i = 0
while i < 8 do
print(fib(i))
let i = i + 1
end
print(10 / 3)
print(sqrt(-4))
new("Counter", "c1")
send("c1", "set", "v", 5)
print(get("c1", "v"))Transpiled Ruby:
#!/usr/bin/env ruby
# Auto-generated by PatLang's self-hosted Ruby transpiler
# (self_hosting/lib/transpile_ruby.patlang). Do not edit by hand.
require_relative "patlang_runtime"
def fib(n)
if (n < 2)
return n
else
return (fib((n - 1)) + fib((n - 2)))
end
end
i = 0
while (i < 8)
puts(PL.display(fib(i)))
i = (i + 1)
end
puts(PL.display(PL.div(10, 3)))
puts(PL.display(PL.sqrt((-4))))
PL.new_obj("Counter", "c1")
PL.send_obj("c1", "set", "v", 5)
puts(PL.display(PL.get_obj("c1", "v")))
Native run on the build machine (via a real `ruby` subprocess, output byte-identical to the interpreter's own):
0 1 1 2 3 5 8 13 10/3 0+2i 5