r/Compilers • u/MD90__ • 17d ago
Feedback on a transpiler project
I'm thinking about making a small scripting language with a domain focus in transpiling to rust code. The overall goal of the language is to have easier to use types and structures that will transpile to more complex rust code. Probably a dumb and difficult project but it was just an idea to give me something to do to write code again. I'm curious from you all with experience in the compiler field, if you think this project is a fun idea and what issues I could face and also if it's actually a big waste of time. Thank you all for commenting! :)
5
u/FransFaase 17d ago
Great idea, but try not to focus on the parsing part. Keep the grammar easy to parse and/or use a flexible tool for parsing. A recursive decent parser with back-tracking is not a big deal (if you a bit of caching) nowadays with respect to performance.
There are several online tools that can help you with developing the grammar and that can interactively parse input on the grammar.
Transpiling, generating code that can be parsed by another compiler, is at least as difficult as generating assembly. You have a different set of problems to solve. Do you want the code to be readable? How are you going to generate names for identifier? How are going to debug the transpiler? (The generated code often has not direct connection to the input.) Doing anything more than a one-to-one translation often requires complex semantic analyses and/or transformations. There are always more edge cases than you expect. In the past half year, I spend some time generating flow-charts from C programs.
5
u/simon_the_detective 17d ago
Be careful to avoid the problems of Powershell. It tried to support objects and types in scripting and made a terrible mess of things.
A small scripting language should have few types, mostly strings, and make everything fit together, IMO.
4
u/whizzter 17d ago
My thesis project (and some other experiments) were JS (and others) to C transpilers to investigate what kind of runtime semantics were best (ie, I left the C compiler do to classic compiler things like register-allocations,etc).
Back then LLVM wasn't as advanced in terms of platform support, it's a good target and I probably would've picked it today iirc, last I read Clang leaves fairly "unoptimized" code to LLVM and lets it do the job of deciding what variables can be moved to registers. So you don't really need to do all advanced analysis just to create a LLVM backend (it's a moving target though).
I meantion this because even if the C targeting transpilers went fairly well for their purposes, I've gotten more mileage out of just generating JVM bytecode as a middle level target and LLVM is kinda like that, not being stuck with the higher level semantics, also if you want any kind of decent debugging experience generating LLVM,etc code will be far better since it probably have facilities to receive debug-info.
Tl;Dr; Transpilers are fine, quite nice but leaves you with some baggage of the underlying target language, options today are good for dropping one level (LLVM, MIR, JVM BC, MSIL,etc) without having to go down all the level to native codegen.
6
u/CommandExact1304 17d ago
This will be great as long as it isn't just yet anther claude project with 8 commits that'll never see the light of day again after the first week
3
u/mamcx 17d ago
The actual challenge is match the semantic of the target (Rust) from what you write in scripting lang.
This means, what about ownership? How prove the execution model? etc.
A probably good idea is to use
bridgeswriting in Rust, so you transpile to known solutions instead of try to match directly (ie: You make a DSL in Rust that you check in Rust is fine and know could be targeted by your language).So, for example you make wrapers with Rc/Arc/Arenas, etc that encode the memory model in Rust and go with that.
The other option is that you impl the borrow checker on your transpiler so you can do "zero-cost" abstractions.
2
u/pthierry 17d ago
That's not dumb at all, it's a very typical thing to do in languages like Lisp or Haskell, called a Domain-Specific Language (DSL).
I don't know Rust enough to be sure, but I guess you could implement a macro to use your language directly from Rust, without needing to transpile into a Rust file. The pitfall with the latter is that you need to be careful about retranspiling when the source file changed.
2
u/zlfn 17d ago
Maybe you might be interested in Rhai
2
u/EggplantExtra4946 17d ago edited 16d ago
It's a waste of your time. Transpilers are a dead end and a reliable and useful one will be as hard to make as a compiler front end, so you'd be better off making your own language and generate LLVM IR (or use an other backend) like someone else already said.
2
u/Discordbothost 15d ago
One small semantics test to add early: f(next(), next()), where next changes a counter. Decide argument evaluation order and make sure each call happens once in the generated Rust. It catches code-generation mistakes that arithmetic-only examples hide.