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
83 comments
[ 5.9 ms ] story [ 160 ms ] thread1) 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
Have you tried teaching Crumb to GPT4? I bet it could surprise you with what it can do.
@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.
It’s not usually a great idea for production code anymore, but for learning it’s fantastic.
Is lambda calculus or the PDP instructions set a closer match to which language?
> several better options these days
Every other option requires you to buy into some additional paradigm-of-the-day.
You can easily lose decades going down the wrong programming rabbit holes.
> You can easily lose decades going down the wrong programming rabbit holes
total rubbish.
Look at how much time we lost with OO. I am only recently coming to grips with how bad it is. C++ was the "thing you are interested in today" back then. And it wasn't well thought through.
What were people doing before Rust?
C is great.
> Why do you think the industry keep circling back on old stuff?
What does this even mean? Can you give some examples?
> Look at how much time we lost with OO
Okay, what do you mean by 'lost' because I find OO to be extremely useful (if used appropriately). Ditto functional, ditto logic.
Are you suggesting we should scrap all of these and just use code procedurally and nothing else? Because that would be a massive step backwards in my view, and I do have decades of experience so I can't understand why you're so negative about anything else but procedural. And maybe C++ was just a bad implementation in hindsight. But it took us a long way.
There is a lot of good wisdom from the 60s, 70s, 80s that people forget and end up re-inventing. I don't think I'm the first person to make this comment.
> OO
It's very subjective I'll admit. But I don't like OO, and it took me along time to realize why. There are many people who feel the same way. The difficulty is in explaining it. Because you have to look at large systems, explore the evolution of these systems, and pluck enough examples where things don't work.
I can't learn from that.
You've given me nothing to work on, nothing to learn from, nothing I can even oppose because your objection is so nebulous. I can't even knock down your claims because you haven't really made any except "muh, OOP baaad". I haven't learnt anything, equally you haven't learnt anything... what was the bloody point in you saying anything at all?
> I think knowing that this viewpoint exists is a good lesson in itself
I'll skip the sarcastic reply and just say I draw rather different conclusions.
OP is at a stage in life where fluid intelligence is still very high and crystallized intelligence is growing rapidly. This is the time in your life where you are perhaps most able to absorb new ideas and ways of thinking, before you get set in your ways and less receptive to different ways of thinking.
If that's the case at the Uni OP is studying at, then he should be come fluent in C - because from experience as a TA, it's not necessarily the CS knowledge/fundamentals that tend to be lacking when people struggle with classes, but rather that they're fighting a new language. Even if they have years of previous experience...especially in these days, when Python or JS tend to be the first (and only) language to many freshmen.
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.
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.
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.
Regarding parents, what's made me cringe lately has been the shutting down of opportunities for their kids to form new bonds and play. There's a park I go to regularly that seems to be attended by well-off parents who go in groups. Last time I overheard a kid who must have been 5 playing with some other kid she just met on the swings. She ran over to her mother and said "This is ___ she's my new friend" and the mother basically wrote it off and said that's great but the other one has to go and then they left. The same outgoing kid had earlier wanted to try climbing this artificial rock I was doing, and the parent just showed absolutely no enthusiasm for letting their kid do it, she had somewhere else to be. I get that people are busy, but let your fucking kids play.
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/) was also super helpful. Getting to see the syntax definition visually made writing it so much easier :).
Hasn’t hurt me from finding interesting jobs that pay well!
Make sure you foster that curiosity continuously until the day you die, because it'll serve you really well
you should be studying: lisp ocaml haskell, interpreters (SICP), compilers, type systems, transaction processing, effect systems, FRP, concurrency NOT java guis python SQL databases webdev gamedev .. whatever
(Serious question. I’m not being snarky)
Covers all of the major programming languages in a deep way.
> interpreters (SICP), compilers, type systems, transaction processing, effect systems, FRP, concurrency
These subjects are the core of almost any system that you'll encounter, in practice - academic or industrial.
https://www.youtube.com/watch?v=4SiFgB1lGxw
I now see actual CS exposure in industry as rare broadly (you'd have to work in a research org, which is both rare and also requires a PhD or other credential for one to be selected for the opportunity). Furthermore, the bulk of the CS literature & papers I encountered is embedded in those three programming languages. Now editorializing: I think Haskell is like "the periodic table of computation" as well as basically "math notation for computational structures." These deep science-y topics are hard to learn outside of school, the material is dense and there's no clear and accessible trajectory to get there, and to even identify such at trajectory you need role models and teachers of which kind industrial programmers aren't exposed to.
In conclusion, I'd likely have gotten to where I am today at age 30 instead of 38 and regret the lost time wandering in the swamp of silicon valley arrogance. FWIW my startup is a CRUD Spreadsheet, we apply functional programming research to user interfaces and web development as per https://github.com/hyperfiddle/electric
At least that was my experience as a self-thought programmer. The first weeks were super boring for me but also lulled me into being complacent until I suddenly found myself in deep trouble. Just because you understand the practical side does not mean you can will automatically grok the academic side of things.
I went into mechanical engineering because I had a great natural understanding of basics physics, materials, design, mechanics, blah blah. Even UC Berkeley engineering basically taught me things I knew or would have easily understood it I had to lookup that specific thing. I learned very little from my mechanic engineering degree.
I ALMOST think people shouldn't go into a major based on their strongest aptitude, but I'm not sure yet.
But definitely 100% add another focus since you'll probably breeze through the main course work.
But I dont think it should be philosophy or art or calligraphy like Jobs would recommend. I wouldn't pick a second major that would be a foolish main major.
Pick something that is a hard skill that is useful to learn and understand.
Maybe electrical engineering. Maybe industrial design. Hell, I think tons of people would be well served with what they learned with a second major in accounting.
For example, I had a second major in theatre, but I had no delusion of working a living in theatre that could compare with the kind of career that a CS degree typically delivers. It was and is a personal passion of mine, and to be able to deepen my understanding of it in an academic setting was valuable and rewarding to me personally.
Studying theatre paid dividends in other ways as well. Managing a production is a beast, and you learn a lot about how to manage people and get things done. Dramaturgy, often called literary analysis, was quoted by my professor (a scholar on the topic) as "what are the parts, and how do they go together?" -- a shorthand that I still use to this day when thinking about a new system.
If it wasn't clear already, I had a very positive experience in my studies at a Liberal Arts school, where the connections between fields, and the value they can offer each other, are a big focus.
Just taking on a second major won't necessarily pay off, but keeping in mind the holistic point of your education may create an outcome where the sum is substantially larger than its parts.
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.
You may start with reversing compiled-code, but interpreter is much-much more.. interesting. Especialy if of unknown (simple) language.
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-reliabil... if you're curious about more practical implications.
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.
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.