How long would creating an interpreted language in C be
Posted by RageNutXD@reddit | learnprogramming | View on Reddit | 12 comments
I think I would call myself a beginner in low level programming, most of my experience has been in python, making games with godot, and some java. I feel like doing something like this on python would be fun but I feel like doing it in C will be a really good way of learning about data structures and other stuff. If this is too ambitious, I am fine with doing it in C++.
Skusci@reddit
Depends on how complex you want to make it.
Like if you want to write the whitespace interpreter that's like 300 lines of code.
Guido van Rossum started working on Python initially as a hobby project and Python 1.0 was released like 14 years later.
bravopapa99@reddit
strtok/strtok_r for a quick win for a DEAD SIMPLE lexer
after that it gets a bit slower, simplest would be a stack based FORTH approach (currently writing one for raylib and my own simple drawing language) but scan a number, goes on the stack, scan a "string", goes on the stack, scan a 'word', pull of stack and execute.
The details, as they say, are left as an exercise to the reader!
michael0x2a@reddit
It depends on what tutorial you follow and how complex your interpreted language is.
Following along something like https://norvig.com/lispy.html might take only a day or two (even if you implement everything in C instead of Python).
Following along something like https://craftinginterpreters.com/ might take a couple of weeks to a couple of months, mostly depending on how much time you have to work through everything.
RageNutXD@reddit (OP)
These resources were so helpful thanks! Would love to sit down and read through the whole thing
iOSCaleb@reddit
It’s too ambitious. If you’re not familiar with either C or data structures, writing a compiler or an interpreter is not a good project to start with.
Using C++ isn’t going to make it easier.
RageNutXD@reddit (OP)
I understand your first point, but what do you mean using C++ isn't going to make it easier? I feel like having most of the basic data structures (dynamic arrays, maps) included in the standard library makes it easier because you don't have to rebuild everything from scratch.
plastikmissile@reddit
It's impossible to tell. I would say since it's a project that interests you, just go for it. It may or may not be too ambitious for you, and you might end up not finishing it, but I guarantee you'll learn a lot along the way.
RageNutXD@reddit (OP)
That is very reassuring! I have had multiple unfinished projects but still learned alot from them. This time I want to finish this so that I have something to show for after the summer break ends.
UdPropheticCatgirl@reddit
It depends. Did you pick a sane language to interpret (scheme or PL0 or forth are the ones you should look at since you can’t really fuck them up). Do you plan on just making a tree walker, that’s easy enough, can be done in like a weekend. some bytecode vm? that’s a bit harder probably a couple of weeks. Straight up JIT compiler? that’s actually a lot of effort. C is nicer imo, but be prepared to reinvent the wheel a couple more times than you would have to in C++.
RageNutXD@reddit (OP)
Yeahhh i am trying to make a straight up JIT compiler, my own estimate is that it'll take at least 3 months, but will probably be a fun summer project.
This is why I asked this question in the first place, I don't know if doing everything from scratch will be worth it, even dynamic arrays got me scratching my head but it was definitely worth it.
tanay0907@reddit
took me 2 weeks, depends on what you’re going for
high_throughput@reddit
If you're already familiar with parsing and know other languages, I would guess you could write a simple parser and AST based interpreter for a toy language with global variables and basic control flow in 2-3 days.