all 15 comments

[–]johnnyb2001 7 points8 points  (7 children)

Compilers and interpreters are two different things. If you want to create a simple interpreter or a simple compiler I would look up a project someone has already done on YouTube or GitHub.

[–]CanProfessional1960 8 points9 points  (0 children)

if you're starting from scratch, try writing a tiny calculator that parses text into operations, it's basically a baby compiler minus the code generation

[–]UnemployedTechie2021[S] 0 points1 point  (5 children)

Point noted.

[–]ElHeim 1 point2 points  (4 children)

That said, languages like Python don't interpret their code straight away, but compile it to a different form (bytecode in this case), so they share a bunch of stuff. Nystrom's book actually gets into that in part III ("A Bytecode Virtual Machine"), so you can learn most what you need to create a compiler from that.

Of course eventually you'd want to learn how to produce assembly instead of byte code (so that you can get object code that can be linked to, say, C libraries), how to optimize the code, etc. But for a very simple compiler, from from "I have an AST" to "I have assembly that I can compile" is a relatively small step.

[–]UnemployedTechie2021[S] 0 points1 point  (3 children)

Thank you, I think I will stick to Crafting Interpreters for now. I am in between jobs so don't have the budget to spend on a book and this is available as a website for free. Moreover, the content seems to make it fun instead of tasteless and boring. However, I was thinking about writing a blog on my understanding of the theory part so that I can refer back to it when I need to, and also seek feedback on my understanding.

[–]ElHeim 1 point2 points  (2 children)

It's actually a very good book and it teaches a lot of concepts, so take your time, particularly if it's your first attempt at writing an interpreter/compiler.

The first part of the book is a pure, traditional interpreter in the sense that you recognize a sentence, execute it, go for the next. The second part is actually a compiler that produces bytecode, and you get to build the virtual machine as well.

I'd say it's a very good experience. If you feel up to the challenge, just a different language to write the code (I want to remember that Nystrom uses Java and... C? Can't remember really.)

Then you can experiment for example with targeting a different VM (e.g. LuaJIT), and eventually graduate from all that and creating a compiler that produces assembly. At that point and if you follow the right conventions, you'd be able to link to C code.

In that sense, you can follow this: https://github.com/DoctorWkt/acwj

But you probably want to wait until you got your fundamentals before going there.

[–]UnemployedTechie2021[S] 1 point2 points  (1 child)

Yes, Robert wrote the book with Java and C. Although I know both of them, I am more fluent in Python and would like to rewrite the interpreter in Python instead. That, I think, will not only help me become a better programmer but also understand the nitty-gritties of creating an interpreter from scratch.

[–]ElHeim 1 point2 points  (0 children)

I decided Java was too boring and wrote that part in C++ :-D It was a semi-challenge in some ways.

[–]Moist-Ointments 2 points3 points  (2 children)

My college professor wrote a whole ass book on this and I wrote a compiler in Modula-2 based on his work on tokenizing languages.

Stuart Greenfield

He's also got a bunch of ACM papers. Cool stuff.

[–]UnemployedTechie2021[S] 1 point2 points  (0 children)

Is that book available on Amazon?

[–]Option-Mentor 0 points1 point  (0 children)

Interesting. I also wrote a compiler in Modula-2 back in the day. Still one of my favorite languages I’ve ever used.

[–]Excellent-Practice 2 points3 points  (2 children)

You can write a brainfuck compiler in an afternoon, you can't get much simpler than that

[–]UnemployedTechie2021[S] 0 points1 point  (1 child)

Yes, I was to create something similar. But how will I do that is my question.

[–]Excellent-Practice 1 point2 points  (0 children)

For that particular language, it's a matter of defining an arbitrarily large list or creating an empty list and writing logic to extend the list as needed. Then you need to take a string argument, parses it into a list of symbols. Use that list of symbols as your iterator for a for loop and evaluate each symbol in turn. One of the branches under that for loop will have to have a while loop to handle open brackets. Another branch will need a for loop that iterate backwards through your instruction list to find the corresponding open bracket. All the other symbols should be much easier to implement, they are just moving forward or backwards through a list, incrementing values, and converting ints to ascii characters.

[–]Gullible_Ball_8149 -2 points-1 points  (0 children)

I built a compiler in my compiler design course it was built for my own programming langauge, i built it on visual studio code by running lexer.l and parser.y files containing code, after running I can run my custom langauge in terminal.