2 ms·
My suggestion to you is simple: go read about the brainfuck language, and implement an interpreter for it. You'll probably get to the end and find that your fi
by pdpi 10y ago
My suggestion to you is simple: go read about the brainfuck language, and implement an interpreter for it.
You'll probably get to the end and find that your first implementation "cheats", whatever that means. That's fine, improve it.
Write the memory model as a contained module, and have the interpreter core call into it.
Build an internal representation of a program, then make reading the source code a separate step from running it. Once you have that representation, some opportunities arise: e.g. represent increment and decrement as "add 1" and "add -1" instead.
Then you can write your first optimiser: turn long runs of increment and decrement instructions in the source code into a single "add n" instruction.
At this point, you have modules that turn source code into an internal representation, optimise the internal repr, and execute it. Start thinking of commands you can add to the language. Start thinking of how to handle data in the source code, rather than just instructions. How to build functions.
When you get to this point, you'll realise that you're no longer thinking "I wonder how an interpreter works", but rather "I wonder how you implement this particular part of an interpreter", and that is a much easier question to find answers for.