r/Compilers • • Sep 18 '26

Looking for feedback to my compiler

Hi everyone

I’ve been working on my own programming language and compiler in C++, mostly as a learning project.
The main idea is to experiment with compiler architecture while also trying to make the language’s syntax more convenient for mathematical expressions, but now I am doing programming language.
After this I will add equations.

Currently works:
Lexer
Parser
AST
IR generation

I am still relatively new to compiler development, so I’m sure there are things I am doing in non ideal ways.

I’d especially appreciate feedback on:

compiler architecture
parser design
C++ implementation
things that I should learn next
mistakes or questionable design decision

Github: https://github.com/0sewter0/Compiler.git
Thanks for an attention!

21 Upvotes

41 comments sorted by

10

u/NYU_VM Sep 18 '26 edited Sep 18 '26

Documentation is a good starting point here, your project seems to have none, not even sparse comments. And what about language grammar? You have leaned on LLVM for backend which for a learning project I would say is a missed opportunity on learning so many things about the backend by delegating to LLVM, plus it's a massive dependency for such a lean project. I didn't go through much but your main.cpp. I instantly noticed that you included the heavy filesystem header for just one filepath extraction. It could be done with simple string handling, so filepath is redundant if not used for other purposes. And, '__argv[]' is platform specific extension, not the standard.

1

u/Sewter0 Sep 19 '26

Thanks a lot for taking the time to look at ⁠main.cpp⁠ and for the constructive feedback!
You’re absolutely right.

Since this is my first compiler project, I chose LLVM to focus on the frontend architecture without getting bogged down in target specific assembly generation. But writing a custom light backend is definitely on my learning roadmap!

Replacing ⁠<filesystem>⁠ with simple string parsing and fixing ⁠__argv⁠ to standard ⁠argv⁠ will be my immediate refactoring task today.

3

u/Patzer26 Sep 19 '26

Compiler Backend is orders of magnitude difficult than frontend. LLVM can be a perfect choice when starting out cause you can focus on learning and getting the frontend right. Then slowly shift to backend once you are comfortable with AST and IR generation.

1

u/whizzter Sep 19 '26

Depends on the goals, the absolute easiest is almost always a primitive interpreter, but yes, once generating native code you’re immediately in another level of possible complexity.

5

u/GenericPointer Sep 18 '26

I mean... nice you're building a compiler, but PLEASE Clean up your repo:

  1. Source into `src/`
  2. Headers into `include/`
  3. CMakelists.txt or at least a Makefile
  4. Don't commit object files
  5. Create a `README.md` and a syntax specification so someone understands what you are building without looking throgh every file
  6. Create tests and examples
    Once you did that others will actually try to understand what you did, because most people just close the tab when there's no docs.
    But nice that you're doing that.

3

u/Sewter0 Sep 19 '26

I accidentally commited the obj files? I am sorry about that.
Thank you, I will soon improve the architecture

2

u/GenericPointer Sep 18 '26

About your Parser:
- It has no precedence rules
- You use a unique pointer class hierarchy instead of actual recursive AST.
- Your look ahead has potential out-of-bounds
- You have only very few error recovery, I'd recommend using a unified error system.

2

u/Sewter0 Sep 19 '26

oh, okay, thank you!

1

u/AeroForger Sep 21 '26

Hey!

I'm a compiler developer as well, and I looked through your repo. Overall, it's pretty decent, especially considering it's still a learning project.

There are a few things I didn't really like:

  • The README doesn't give any basic overview of the language. It would be useful to explain what zH is, what it currently supports, how the compiler works at a high level, and how to build/run it.
  • Some parts of the language syntax feel a little strange or inconsistent to me. This is subjective, though, and mostly comes down to language design preferences.
  • main is currently treated like a normal int function, so examples such as return x + y; directly become the program's exit status. That's technically valid, but I think it would be clearer to encourage main to return intentional exit codes rather than arbitrary computation results.

The project itself has a pretty recognizable compiler structure already, though, and I like that you're actually implementing things like structs, loops, functions, arrays, and LLVM codegen instead of keeping it at calculator-language level.

Feel free to open an issue or DM me if you have any questions. I'd be happy to help you out!

1

u/Sewter0 Sep 22 '26

Thank you for the feedback, I appreciate it.
Okay, I will improve README and I will add special exit codes.

1

u/recursion_is_love Sep 19 '26

Take a look at some documentation system. Maybe start with doxygen

https://www.doxygen.nl/

-1

u/Mean-Decision-3502 Sep 18 '26

Show examples from your language.

5

u/Sewter0 Sep 18 '26

My language is similar to C:
int x = 5;
int foo(a, b) {
…
}

int y = 10;
foo(x, y);

0

u/[deleted] Sep 18 '26

[deleted] — view deleted comment

17

u/Sewter0 Sep 18 '26

Why am I doing it? Please read the post again.
In future I am going to make complex math structures.

5

u/algerbrex Sep 18 '26

Don’t feel like you need to justify yourself dude. I can’t speak for everyone in the compiler creation hobby space, but I love studying and writing compilers and fine the process super fun. And that’s enough justification. If you enjoy doing it and are having fun, keep doing it.

0

u/[deleted] Sep 18 '26

[deleted] — view deleted comment

18

u/Sewter0 Sep 18 '26

No, I didn’t.
Are you kidding me? Your saying me like:
“Why should we learn math if we already have a calculator".
I am learning how compilers work.
Sorry for strange english, I am still studying it.

0

u/[deleted] Sep 18 '26

[deleted] — view deleted comment

10

u/Sewter0 Sep 18 '26

I am building this to master the underlying compiler mechanics myself, not to generate boilerplate. Wish you all the best with your project.

7

u/Slow-Mechanic-7427 Sep 18 '26

don't worry about it, keep doing your thing. They have tried to hijack other posts today too.

7

u/Kywim Sep 18 '26

I also started with projects like these to learn compilers and it got me my first compiler job, and I am now a senior compiler engineer specifically thanks so those projects being on my portfolio.
What you are doing works, you are on the right track. Ignore that guy

2

u/Mean-Decision-3502 Sep 18 '26

I'm sorry. I was not proper. I meant showing my compiler, that you might take some ideas from it. Also this operator specification might be interesting, especially if you are focusing on mathematics: https://nvitya.github.io/pluops/

Sorry again.

1

u/GenericPointer Sep 18 '26

You can't say ur sorry and advertise your own thing in the same comment, also ur other post says OP should respect the time of the people. Just be respectful yourself first. That's not meant to blame you, just don't do it.

→ More replies

3

u/Karyo_Ten Sep 18 '26

You're insufferable. And hypocritical too. Why do a mix of Pascal, Python and C/C++ when Nim exists?

1

u/GenericPointer Sep 18 '26

**criticizes a learning project for advertising his own thing**

0

u/[deleted] Sep 18 '26

[deleted] — view deleted comment

6

u/yeetes12 Sep 18 '26

You're weird

5

u/Slow-Mechanic-7427 Sep 18 '26

For sure, seen them in multiple threads already.

1

u/Dry-Status2983 Sep 18 '26

its the self-replicating bots that escaped OpenAI

0

u/EggplantExtra4946 Sep 18 '26

Them? Who are the others?

2

u/Slow-Mechanic-7427 Sep 18 '26

Mean-decision-3502.

They've been in a couple threads on here shitting on OPs project and promoting theirs.

0

u/EggplantExtra4946 Sep 18 '26

I asked what others. Or do you have trouble with that word too.

3

u/Slow-Mechanic-7427 Sep 18 '26

I told you who i was speaking about, so looks like you are one having the trouble.

0

u/EggplantExtra4946 Sep 18 '26

Your proselytizing brain autoconverts all plural marks to singular, got it.

Don't you think it's ironic for a guy interested in PLs to be so fond of grammatical ambiguity?

2

u/Slow-Mechanic-7427 Sep 19 '26 edited Sep 19 '26

Hate to break it to you, but 'them' can refer to a single indvidual whose gender you do not know / are not specifying.

In any case, what a weird thing to crash out on in a programming language community. You ok?

0

u/[deleted] Sep 18 '26

[deleted] — view deleted comment

1

u/EggplantExtra4946 Sep 19 '26 edited Sep 21 '26

Fair point. One is idiomatic, the other isn't.

→ More replies

4

u/envythekaleidoscope Sep 18 '26

Receiving feedback shouldn't be something one "unlocks" at a certain stage; from my perspective, quite the contrary - you should receive continuous feedback to make sure you don't blunder yourself 70% the way there - I'd say having a working Lexer, Parser, and IR Generation is quite the way forwards to receive feedback anyway. Working on programming languages and compiler architecture is quite esoteric enough as it is, don't contribute to it.

0

u/Mean-Decision-3502 Sep 18 '26

I got it. I'm sorry. It is harder to distinguish when one just want to make a compiler, and when one builds a tool, that might already exists.

2

u/ThrowRA-NFlamingo Sep 18 '26

Not everything written needs to be some novel, useful tool. You often learn programming by building things that already exist. That is how you learn. Get off this thread if you are just gonna be a hostile jerk to beginners trying to learn the fundamentals of compiler design. We should encourage people to seek feedback. You come across as the bad stereotype of the obtuse, rude comp-sci person.