8 ms·
Show HN: Going into freshman year, figured I should build an interpreter
Hi all!
I'm going into my freshman year, and figured that the best way to prepare for the intro to programming Racket course would be to implement my own garbage-collected, dynamically typed, functional programming language in C ;)
Anyways... here's the repo:
https://github.com/liam-ilan/crumb
I started learning C over the summer, so I still have a whole lot to learn... Any feedback would be greatly appreciated! :D
- victorsup 3y agoAmazing!
- cod1r 3y agoI wish I was this talented when I was going into college. Nice job though. :)
- withtheprogram 3y agoclickable link to repo: https://github.com/liam-ilan/crumb https://github.com/liam-ilan/crumb
- gus_massa 3y agoDid you wrote your own garbage colector? Is it a moving one or a not moving one?
- liamilan 3y agoCrumb is garbage collected (there is no need to manually allocated/deallocate memory)... though there is no background "garbage collector" process running... The interpreter for Crumb is a tree-walk interpreter, and it just frees memory whenever it can... Crumb frees memory in the following cases: 1) When a function is finished, all memory related to the scope of that runtime is freed. 2) When an value is not returned out of a statement, or assigned to a variable, said value is freed. 3) When a function is applied, if an argument has no reference (it is not stored in a variable), it is freed. 4) Additionally, if the function itself has no reference (such as in the case of an immediately invoked function), it is freed. Hope that clarifies things a bit :D
- gus_massa 3y agoSorry if I got the syntax wrong, but in something like f = { x = (list 1 2 3) y = (list x x) z = (get x 1) <- y } How does the compiler decides if it must free the memory used by x?
- s_Hogg 3y agoAll lists are passed by value and X isn't the return value, would be my guess
- yeputons 3y agoIt should. Everything is copied and by-value, there are no references/pointers or even closures, cycles are impossible. Think Pascal that has garbage collection for strings and dynamic arrays.
- _a_a_a_ 3y agoIt's an interpreter rather than a compiler, and looking at the code it seems to use ref counting
- gus_massa 3y agoRef counting makes a lot of sense with the description of the OP, and if there are no cycles it's good.
- deleted 3y ago[deleted]
- mindvirus 3y agoLove it! I learned so much from building projects like this! None of mine had cool logos like yours - this looks really pro. Have you tried teaching Crumb to GPT4? I bet it could surprise you with what it can do.
- xrd 3y agoMan, your blog (https://www.liamilan.com/ https://www.liamilan.com/) is wonderful. I wish you had an RSS feed, hint, hint.
- gus_massa 3y agoI agree. @OP: I recommend to submit your posts here when you have new ones, and post the older ones slowly (like one per week) so people don't get annoyed.
- s_Hogg 3y agoCongratulations, this is great. Have you thought about spending some time writing down what you had to learn and how you went about figuring out what to learn in order to do this? I think it'd be even more interesting than the language itself.
- liamilan 3y agoYeah actually, stay tuned! :D
- 38 3y ago[flagged]
- ta-sfghjkk 3y ago[flagged]
- Snarwin 3y agoI'd say C is actually a pretty good choice for an educational project like this. Having to write out all of your data structures by hand and manage your memory manually is a good learning experience, and since you're not writing serious production code, you don't have to worry too much about making mistakes.
- convolvatron 3y ago_someone_ has to learn to build runtimes
- kristofferg 3y ago_someone_ “else” :)
- slashdev 3y agoC is like a high level assembly language. It maps very closely to how the computer actually runs programs. That, and it’s simplicity, make it a great way to learn. It’s not usually a great idea for production code anymore, but for learning it’s fantastic.
- the_biot 3y agoThat old chestnut. C is nothing remotely like an assembly language, of any kind. It is a low-level programming language, but only compared to most languages. Disassemble some compiled C code sometime -- it's another world entirely.
- 3y ago
- zerojames 3y agoI love this! Amazing work!
- liamilan 3y agoThanks!
- delta_p_delta_x 3y agoNicely done. You're a freshman, picked up C over the summer, and have already built an interpreter for a language that you haven't officially been taught yet, and have a blog with a couple dozen posts. I'll be honest, people with achievements like yours make me feel pretty worthless.
- brailsafe 3y agoYa, but would you rather spend the summer enjoying summer or writing C
- soligern 3y agoIt depends. When I was young I could spend hours coding and it felt amazing and like a great use of my time. Now in my 30s? No way, writing detailed code and spending all my time in front of the screen feels like I’m throwing away my life. All that minutia, whether useful or useless, is repulsive.
- brailsafe 3y agoThat's pretty much how I feel as well. In my 30s now, but unemployed and working a bit day to day on a project while sending out the odd job application in an abysmal market (in the same city as the author), but it's a gradual process and only after enjoying the sunny days as much as possible, socializing, fitness etc.. It's not that there's no time for coding in a hobbyist manner, but I'm careful about moderating the amount of time I allocate to it. That said, I wasn't at even remotely the same level of execution ability as the author, and are probably still not, but I was starting to work with freelance clients in PHP and Angular.js, building some small technical experiments end to end, and relentlessly trying to get better at technical stuff. I could definitely still do that, just not at the expense of other things.
- adithyassekhar 3y agoI'm in my 20s and I'm disappointed in myself for not spending time with my friends during school instead of wasting time making hacked together programs. I came to realise I don't even like "real" programming only random quickly put together scripts. When my friends explored all kind of things I was so stuck on coding and web dev and php. I didn't check out any other fields of interest or professions. When I read all the threads here about parents teaching their 4 year old BASIC to "indoctrinate them into programming", I cringe hard. They're stealing their kids' childhood and their drive to explore what they'd actually love.
- brailsafe 3y agoAmbitious! What were some of the educational resources you used or that inspired you before and throughout the project? Textbooks, tutorials etc..?
- liamilan 3y agoRobert Nystrom's Crafting Interpreters (https://craftinginterpreters.com/ https://craftinginterpreters.com/) is an awesome resource :D Though I didn't implement his language exactly, a lot of what he talks about carries over anywhere. It especially helped me figure out how to handle the memory management and scoping. Vincent Jacques' DrawGrammar (https://jacquev6.github.io/DrawGrammar/ https://jacquev6.github.io/DrawGrammar/) was also super helpful. Getting to see the syntax definition visually made writing it so much easier :).
- _lvbh 3y agoThis is seriously impressive. I’m also just about to enter college for software engineering and projects like this make me concerned about how I’m even gonna compete with such skilled peers.
- actionfromafar 3y agoThe skills of work are only partially overlapping with coding skills, so you’ll have new kinds of angst later.
- zarroboogs 3y agoIf it helps I would absolutely struggle to do this as a dev with 9 years experience. You’ll never totally shake imposter syndrome in a field where you can easily see such excellent projects routinely. Hasn’t hurt me from finding interesting jobs that pay well!
- tonfreed 3y agoWell done, I had a degree and 5 years professional experience under my belt before doing that. Make sure you foster that curiosity continuously until the day you die, because it'll serve you really well
- Cheetah26 3y agoSince you're going into your freshman year I'll offer my college advice as a current senior. The moment that you feel a class will be covering something you already know, reach out to your advisor and the professor. I personally wasted far too much time in intro classes that could've been easily bypassed. While easy A's are great for the GPA, it's better to spend your time and money actually learning.
- deleted 3y ago[deleted]
- poulsbohemian 3y agoHere's perhaps some unexpected and unsolicited advice... so pretty clearly you aren't going to have a hard time with the CS curriculum (assuming that's what you intend to study) so my $0.02 is - find a second or even third major to augment your skill set. If you are already able to get to this level on your own, you'll likely breeze right through a typical undergrad CS course of study. So consider other courses of study that might be a bit more of a challenge / might give you an even broader skill set four years from now when you enter the workforce or consider grad school.
- eru 3y agoThe part of the advice that suggests acquiring more skills is good. The other part, that suggests doing more courses of study is somewhat questionable. Uni courses are designed partially to help you learn skills. But also for other requirements. If OP wants to learn more skills, they should go for that. Whether in uni or outside. If OP wants to relax and have fun, that would also be a good use of time in your twenties.
- katella 3y agoTake the grad courses.
- sam0x17 3y agoCan't agree more with this. I ended up double-majoring Philosophy and it was fantastic and has surprisingly helped several times in my career
- Serenacula 3y agoI still honestly believe coding should be a requirement in all philosophy courses. A system for layout out logical structures efficiently was something philosophy to this day sorely lacks, with formal logic being practically unreadable. And computer science has literally spent 50 years working on how to do that well.
- sam0x17 3y ago
- svilen_dobrev 3y agoFor furthering your skills, i can suggest a related topic (that is never taught officialy though): debugging/reverse-engineering an interpreter. Does not have to be a JVM-size, most games have lots of interpreters in them too.. (although there might be multiple layers of interpreters - which is magnitude harder) You may start with reversing compiled-code, but interpreter is much-much more.. interesting. Especialy if of unknown (simple) language.
- yeputons 3y agoMy main advice: never trust C, C++, or their compilers. Undefined behavior bites. Even if you just care about the latest 64-bit Ubuntu Linux LTS, compiler updates or flag changes bite as well. That's the main difference between C and C++ and almost any other language. Not the syntax, neither the perceived "low-levelness", nor manual memory management. In any other language, if you make a mistake, the program behaves badly, but in a predictable way. In C and C++ it may behave reasonably, it may crash in another file altogether few seconds after the erroneous line is executed, it may produce the correct answer. Or it may crash only sometimes. Or it may silently produce an incorrect answer. Different behaviors on different systems, and even on the same system with different compiler flags (e.g. optimization level), of course. That makes reliable experiments with C and C++ impossible. Even if you have just five lines of code, you almost never know if they're valid C or C++ for sure (separately, whether they do what you want with modern/legacy compilers). It's still fun and everything (congrats on making your own language, that should've been fun!), but you never truly know whether what you wrote is not going to break in a few years (or months) with zero code modifications just because there was another UB lurking around. See https://evan.nemerson.com/2021/05/04/portability-is-reliability/ https://evan.nemerson.com/2021/05/04/portability-is-reliabil... if you're curious about more practical implications.
- fisherjeff 3y agoI suppose this is one of those things where people’s experiences vary widely, but FWIW after 20 years of writing C/C++, I can count the number of times this has bitten me on one finger. And even then, I still think it was more likely just an optimizer bug in the god-awful compiler.
- yeputons 3y agoYes, I think it heavily depends on what you're doing and your mindset. E.g. if you work with a single compiler on a single platform all the time and expect the compiler to "just compile the code", then you're likely to learn all its quirks, avoid them automatically, use proper abstractions/contracts/invariants to guard against incorrect code, etc. Moreover, you likely know how exactly the most popular UB shows itself with your compiler. If you juggle compilers all the time and work with not-so-high-quality code (legacy or not), especially the one with little tests, I expect UB to pop up a lot. Or even if you just test on one platform and do final runs on another, that happens all the time in competitive programming or home assignment grading, especially with beginners.
- dazhbog 3y agoLearning C controlling some shitty robot in my first year was soo much fun. I was waking up and going to sleep thinking about it. So I hope your first year is also fun. Obviously you are much more advanced, so my advice would be to keep tinkering with everything u can get your hands on.., and keep spreading to adjacent disciplines (electronics, cad, startupy stuff, and even non tech stuff) until u take over the world haha.
- wxce 3y agoThis is so good for your age! I don't think I made anything beyond a calculator or a website before entering college.