Reputation: 3411
As a self project I'm creating an interpreter for a programming language I "made up" (It's really just a tiny extension of the JavaScript programming language) but I'm a little lost as to how an interpreter really works and how I should execute the programs that are written in my language. My questions are:
1: Because this is an interpreter, should I be executing statements as I walk my parse tree, or should I instead be generating code in a different language - say, python - and then using a subprocess call to compile and run that generated python file?
2: Am I suppose to execute each statement as I read it? or should I be constructing the entire program's parse tree -> AST in memory before walking the tree and generating / executing code? (Depending on what the answer to question 1 is)
Upvotes: 3
Views: 613
Reputation: 3246
one intuitive way to implement an interpreter is to create an executable AST:
one way is to implement it on a top of the Java Truffle framework. if you implement your interpreter in the right way, you will get decent performance. there are also several language implementations available; a simple example language is also included that uses the Cocoa compiler generator. some papers (1) (2) explain, how you can implement things such as local variables or handle values of different types for dynamically typed languages.
i would recommend you to first have a look at the truffle.sl implementation (link how to get it) and then see where you can get from there.
Upvotes: 2