An interpreter for P-code (and videos?)
I just returned to writing, after a long Christmas holiday and a very busy time at work, which included a trip to Colorado, and I have decided that the next thing I want to tackle at Computer programming — and so can you! is going to be an interpreter for P-code. I want to do that for two reasons:
- I miss coding for fun.
- A P-code interpreter would make the examples a little (a lot?) more fun I think. Then I could say, hey, go and try to run it in the P-code interpreter.
So I am going to start writing it — it should be relatively simple, since P-code is such a simple language.
I see that my posts are actually starting to be followed by a few people and several tens are clapping for some articles, so that is very motivating for me. Thank you!
Thoughts on the process
So the first question is, should I make some videos about it? I thought I could talk a bit about my thoughts on designing the interpreter, maybe show some live-coding (super sexy) and let you all enjoy my sexy voice. What do you think? Let me know in the comments.
Apart from that, writing the interpreter is going to be the biggest coding project I have undertaken in a long time by myself, so naturally there are a lot of things to decide.
First, should I write a formal grammar, or just let the parser be the specification? I am leaning towards the latter, maybe out of laziness.
Which lexing/parsing system should I use? I have pretty good experience with Parboiled, so I am leaning towards reusing that.
The next question is which programming language, and here the choices are between Java and Scala currently, at least if I want to use Parboiled. I think the choice will be Scala, because it is functional, which I enjoy more.
Requirements — aka features I want
This list is still in progress, but so far I think I would like the following:
- Should be able to run P-code. I am not sure that P-code is defined well enough to be able to write an interpreter, but that will be an added bonus of this project then, a more stringent definition of legal P-code.
- Should give error messages suitable for beginners. Strangely enough GHC is my model here — Haskell is probably the most difficult mainstream language I have tried (no Prolog is not mainstream), but the error messages are really good. I imagine something like: “Error: Found a reference to an uninitialized variable ‘counter’ on line 34. This means that the variable ‘counter’ did not have a value and the program could not continue. Please change your code so that ‘counter’ is set to a value before line 34.
- Support for debugging of some kind — I think this will have to wait, because I would like a nice graphical debugger and integrating this in IntelliJ/Visual Studio Code/Whatever seems difficult.
- Some kind of system for namespacing. I am thinking that each file just declares a package in the top and then all referencing is local to that package. If no package is declared I will just let the file be part of the root package. I am thinking of resolving references like this:
1) Package local
2) Root
3) Standard library - I can’t decide if I want shadowing to be allowed. I think not — but it needs a very good error message explaining what went wrong, as this may be confusing for beginners.
- I would like a simple system for grouping files that should be compiled together, maybe just a simple text file called a project file, which has paths to all files being compiled.
- Support for GUI programming would be nice at some point, but I do not think it is realistic at this point.
- A small standard library, preferably small enough that you can memorize most methods in a week of classes.
- Tail-call optimization would be nice, but again this can wait.