r/C_Programming • u/CommercialStrike9439 • Sep 18 '26
First Ever major C Project: Wrote a statically-typed bytecode VM language from scratch in C
Hello everyone! I (19M) am just about to get into my first year of a Bsc in Computer Science. I've been coding a few years time and haven't ever really touched C before, but really wanted to get into it. I thought "why not start big" so I've been working on a hobby language called Oli-Nat, a full pipeline written in C with no external dependencies: scanner to Pratt parser to AST to type checker to bytecode compiler to a stack based VM, including my own mark and sweep garbage collector. ( I took a lot of inspiration from Writing Interprets by Robert Nystrom)
It compiles in two passes. The first pass just walks the source looking for function and class declarations and registers their signatures, so the second pass can type check calls and field accesses without needing forward declarations. That made mutual recursion and out of order function definitions work without a separate header or prototype step.
Typing is static and the type checker is meant to reject anything that would hit a bad type combination at runtime, so the VM's IS_INT, IS_STRING, and similar macros are only used for dispatch, like numeric promotion, rather than as safety checks. I'm still second guessing myself on whether that's too optimistic in a couple of edge cases. Generally haven't much used macros in the past given how strongly related to C they are. I tried to make my best out of my understanding but still.
Strings are interned using FNV-1a hashing, so identical strings share a pointer, which makes field lookups in OP_GET_FIELD and OP_SET_FIELD a pointer comparison instead of a memcmp. There's also closures with upvalue capture, single inheritance, and a small standard library brought in through #pullf io, math, chronos, fileIO, and so on.
The repo has the opcode list, a writeup of the memory model, and some notes on design decisions if anyone wants to look closer: https://github.com/NateTheGrappler/OliNat-Programming-Language
As my first major C project I wanted to really get some feedback on how it is and looks. I wanted to try my best to have this be my first real coding project. The things I struggled most with were closures/upvalues and anything that looks off in the garbage collector (it's tri color mark and sweep, tracing roots from the value stack, call frames, open upvalues, and the globals table), and general C style or structure critique, since this is my first project at this scale. Really would apperciate any feedback or interest, the project is obviously open-source as well!
•
u/github-guard Sep 18 '26
🔍 GitHub Guard: Trust Report
This project scored 3/6 on our safety audit.
Audit Breakdown:
13
u/71d1 Sep 18 '26
This is really impressive, it looks like a project I was working on not too long ago. I created my own CPU architecture, devised the primitive instructions (16 of those to be precise) and a 16 bit fixed instruction width, my plan was to map a region of memory in the VM for video frambuffer output, along with keyboard and mouse interrupts, but I got sidetracked with other hobbies.
Good job on your implementation.
8
u/[deleted] Sep 19 '26
it's very clean looking code
4
u/river-pepe Sep 19 '26
Honestly very nice. Only suggestion is to abuse C macros for debug prints. Then wrap your print macros with your debug ifdef
2
u/[deleted] Sep 18 '26
It's really impressive that you've done that in a month!
3
u/cflexer Sep 20 '26
We are the same age, and almost our language implementations are the same too. I looked at your project, gained new insights. Keep going, amazing !
1
u/umlcat Sep 22 '26
Ok, I think you are mixing two related things.
One is the human readable programming language like Java or C#.
Another is the VM such as JVM or .Net that can have their own simple assembler / bytecode alike programming language.
For you project you will need to work with both.
0
u/AutoModerator Sep 18 '26
Hi /u/CommercialStrike9439,
Your submission in r/C_Programming was filtered because it links to a git project.
You must edit the submission or respond to this comment with an explanation about how AI was involved in the creation of your project.
While AI-generated code is not disallowed, low-effort "slop" projects may be removed and it's likely that other users push back strongly on substantially AI-generated projects.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
0
u/james_56f Sep 21 '26
Por qué en al segunda pasada de la compilación verifica la sintaxis/type cheking? No debería ser acaso lo primero que debería fijarse el compilador?