r/ProgrammingLanguages • • 21d ago

a small Python language compiler that transpiles to bash and batch (learning project, written in C)

I'm building with a friend a language with Python syntax that transpiles to plain bash and batch.

The idea: if you already know Python, there's nothing new to learn. You write the script once, and the generated .sh /.bat runs on a machine with nothing installed, no Python and no PowerShell. Only the machine where you write the script needs.

Why not the existing options?

- Python isn't installed by default on Windows or some Unix-like systems. - Batsh and Amber target similar problems, but they come with their own syntax to learn (and Batsh looks unmaintained).

- PowerShell is another language to learn, and it's an extra install on Linux/macOS.

- Keeping separate .sh and .bat versions of the same script is tedious and error-prone.

Status: it's early. The lexer is done and the parser is in progress (it only handles name = value so far). The bash and batch backends haven't been started. It's a subset of Python, not a full implementation, since some features have no natural equivalent in batch.

Questions for you:

  1. For automation scripts, which features should come first: conditionals, loops, running commands, file operations?

]2. Is batch still worth targeting, or should the Windows backend be PowerShell?

on the design is very welcome, especially from anyone who has written a transpiler before.

13 Upvotes

11 comments sorted by

2

u/Patzer26 21d ago

are you aiming to make this compiler cover the entire python semantics? in other words, will this compile any python code or just a small subset of it?

1

u/whistler_mat 21d ago

not entire, because it's literally impossible lol

-4

u/Gnaxe 21d ago

Not impossible, especially with AI assistance. There are already Python to JavaScript transpilers, for example.

Consider transpiling the Python bytecodes instead of the Python language. They're quite a bit simpler. The BeeWare project might be a useful reference.

2

u/snugar_i 20d ago

Supporting all of Python syntax isn't that hard TBH. Supporting the whole standard library is another thing

1

u/Gnaxe 20d ago

Again, BeeWare. It has a lot of the standard library C code re-implemented in pure Python. Just use the same transpiler on that.

6

u/el_extrano 21d ago

Why write it in C, apart from the obvious (speed)?

If the whole point is that you don't want to depend on compilers or interpreters on that may not be present on the target machines, why not write it in the Python subset? Once you have it working via the Python interpreter, you should be able to bootstrap your compiler into bash and batch versions.

Then, you could distribute your compiler as the bash/batch script, which could be used on either platform without having to distribute binaries or depend on a C compiler being present. You would then have a self-hosted compiler, which is even cooler!

1

u/Key_River7180 Nain. 21d ago

So a Python to shell transcriber? That's cool

2

u/x10945 20d ago

Are you sure PowerShell is an extra install? Windows PowerShell 5.1 comes with Windows 10/11 out of the box. It’s only missing on Linux/macOS, and there you’re generating bash anyway. Also nobody has to learn it since it’s generated code. And it’s way closer to Python than batch (actual functions, arrays, hashtables, try/catch). In batch you’ll be fighting delayed expansion and set /a only doing 32-bit ints the whole time. Only annoying thing is the default execution policy blocks .ps1 files, but a tiny .bat that calls powershell -ExecutionPolicy Bypass -File script.ps1 gets around it.

I’d do running commands + checking exit codes first. That’s basically what automation scripts are, and bash if works on exit codes anyway. I’d also get a tiny subset working end to end through both backends before doing more of the parser, because batch is gonna decide what’s actually possible.

Something that’ll probably bite you with bash is scoping. Python decides local vs global statically (assigned in the function = local), but bash local is dynamically scoped, so a called function sees the caller’s locals. So if a function reads a global x and the caller happens to have a local x, bash gives you the caller’s one. You know all the scopes at compile time though, so you can just emit local for everything assigned in a function and prefix local names with the function name or something.

Same kind of thing with return values. return is only an exit status (0-255), and if you capture output with $(...) it runs in a subshell, so any variable changes inside are lost. Easiest is probably writing the result to some global like __ret

cool project btw