r/Compilers • • 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! :)

16 Upvotes

35 comments sorted by

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.

2

u/MD90__ 17d ago

Oh what online tools if you know any off hand? :)

4

u/FransFaase 17d ago

2

u/MD90__ 17d ago

Thank you for sharing this 😌

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.

1

u/MD90__ 17d ago

That's the part I was having trouble with but luckily rust likes their structs a bit more and using impl on them. Do you think this project is a good idea or not so good?

2

u/simon_the_detective 17d ago

Hard to say.

1

u/MD90__ 17d ago

Guess I'll find out

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.

1

u/MD90__ 17d ago

Maybe I can use llvm for it

2

u/Trader-One 17d ago

llvm is too much work and easy to do mistake. I compile to golang - 300 LOC backend.

1

u/MD90__ 17d ago

Do you think getting to rust is achievable?

2

u/Trader-One 17d ago

i had rust as original target but due to memory ownership rules you need 1 pass of quite complex decision tree solving to sort object hierarchy and some cases are probably not doable without unsafe to safe rust smart pointers.

So i decided to go for faster time to market and moved to golang which is very easy to generate.

1

u/MD90__ 17d ago

That challenge of rust sounds interesting

2

u/whizzter 17d ago

Superficially interesting, the ownership stuff sounds exactly like the type of thing I mentioned as "baggage of the underlying language" at the start of this thread (honestly, I was even thinking of the ownership system when writing that comment).

Regardless, a transpiler or "proper" compiler doesn't matter much, you're likely bound to write the lexer, parser and other frontend analysis regardless of your target being C, Rust, Go, JVM, LLVM, MIR, MSIL or native code. F.ex. Haxe started as a transpiler but later got it's own HaxeVM with a JIT.

A strong recommendation is to jot down an idea for a syntax with some corner-cases, then jot down a target program in your target env and consider what kind of analysis you'd need to perform on the AST to reproduce the correct semantics.

1

u/MD90__ 17d ago

Good idea! Rust is my goal but my inspiration was jakt language from serenity os that transpiles to C++

2

u/whizzter 17d ago

Oh Jakt looks quite nice, it definetly shares a bunch of semantics and syntax ideas with what I’m tinkering on right now.

1

u/MD90__ 17d ago

yeah ive been studying it because it is a good use case of transpiling and they did it well starting with rust til they self compiled

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

1

u/MD90__ 17d ago

I'm sure some AI will be involved but my goal is more me and less AI

2

u/CommandExact1304 17d ago

That's awesome then :D

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 bridges writing 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.

1

u/MD90__ 17d ago

This is a really good idea. What challenges can you face besides just ownership with rust?

2

u/mamcx 17d ago

In general a transpiler is "tied" to what the target do and expect. How much of that will affect you depends on what you lang wish to do. So try to offer in your front end as close as possible to what Rust do (ie: Do Vec not List, The same hashmap, btree, enums, basic types, etc)

1

u/MD90__ 16d ago

Do you think that's worth pursuing or just making my own language as some suggested?

2

u/mamcx 16d ago

Both are challenging by different reasons, if is worth or not depends on your own goals.

Not expect the project be a runaway success that will shake the industry (much less at the stage you are doing first steps) but I say making anything like this is a very rewarding learning experience (for example, I learn Rust doing https://tablam.org, and after a few years doing Rust professionally I wanna restart with more experience and more clear use cases for it)

1

u/MD90__ 16d ago

Yeah I just wanna learn compilers more after finishing crafting interpreters and reading source code to jakt from serenityOS

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.

1

u/MD90__ 17d ago

Yeah there are some trip ups but I don't mind the challenge :)

2

u/zlfn 17d ago

Maybe you might be interested in Rhai

1

u/MD90__ 17d ago

Yeah I didn't even know about this but I'll check it out

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.

1

u/MD90__ 16d ago

Yeah I guess you're right I could just make a low level language myself but I have to see what fits for it

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.

1

u/MD90__ 14d ago

Good to know :)