I'm a fairly new programmer as I've only been doing this for about five or so years now. Which in my book is still just early phases of programming. I'm still a baby when it comes to programming. So take what I say with that context in mind.
As I have been working on the compiler for about two months now, I've come to realize that most of the components of the compiler are just Arrays. The stack, symbol tables, and instructions. Which was quite a surprise for me if I'm honest. I was waiting for something complex to come up but it never came. Today I came across some of it, but that was mostly because I'm a noob when it comes to managing memory. That's besides the point, as it is not really part of the compiler. Now mind you, it's a very simple compiler so that could also be the reason why. But it does give me a strong understanding of what really goes on when I ask Go to build the byte code and then run it. At least at a basic level. It's all quite fascinating to me, don't get me wrong. The point is that, the complexity was in my head. It's still complex but compared to what I imagined it to be, it was nothing. Is this what learned helplessness looks like? Maybe. I might have just found the cure. Find a book and build the simplest version of something. I'm still not sure how I would build it from scratch if I had to do it all over again without the book but that's a problem for another day.
Also, just as I was typing the above I realized one of the skills is to break a problem down. I guess something that you can do, once you understand how the problem really can be solved. It's a whole chicken and egg problem. So that statement does not make any sense either. So I guess my struggle is just everyone else's struggle too? Sure, you might have some innate abilities but that is quite the useless sentiment for the rest of us. The solution is I guess to just sit down and do the work. Then I suppose the thing to do is, figure out if there is any part of this that you could do and then do that.
Let me try this with the compiler. If I were to start over on my own. How would I do it? Let's say you want to be able to do the simplest thing. Accept an input, say 5;
and then print the output, which would be 5
. I'd start by checking if the input is a number (however you would do that in your language of choice), then create a representation of it ,a token. Which is just a representation of the number 5
, can be whatever you want really. Ideally it'll have a token type
and a value of sorts. This can just be a string for now. Then I would turn the token in to an AST. Then the compiler would go through this tree and turn it back into a an array of instructions and constants. Finally the virtual machine would see an instruction and decide what to do with it based on what you want it to do.
I'm quite surprised at that myself. Turns out I can build it on my own if I wish to. I guess this is what learned helplessness looks like.
Also, a small note on Arrays. I read this incredible paper about memory written for programmers a while ago. Arrays are truly one of the best ways to handle a lot of things when it comes to computers, if you understand even a little bit about how memory works. There are caveats ofcourse. This is programming after all. If an explanation does not include the words, "It depends", is it even a proper explanation?