Interpreters
Could someone explain how an interpreter for a programming language works? Such as Ruby?
Does it use some sort of emulating, virtual machine to execute code on? Or does it execute the instructions as it reads them, or they are inputted, and if so, how? How can you just translate the code to a low level and tell the computer to run it, without putting it into a file of sorts, or a location in memory, dealing with the operating system to allocate it and such? How does an interpreter work, specifically? Any resources for constructing one?
Do you believe interpreted languages have a place, in the real-time world, or no? Thoughts? Comments? Thanks!
Interpreted means the source is executed in place. Typically the source is lexed and parsed into an AST, which is short for abstract syntax tree. The AST is then walked along with an environment structure and this constitutes execution. Here''s an example of such a function.
It''s in Ocaml, but you get the picture. It interprets simple arithmetic instructions with only constants.
let rec eval ast = match ast with Constant(c) -> c | Plus(e1, e2) -> let v1 = eval e1 in let v2 = eval e2 in v1 + v2 | Minus(e1, e2) -> ... | Multiply(e1, e2) -> .. | Divide(e1, e2) -> let v1 = eval e1 in let v2 = eval e2 in if v2 <> 0 then v1/v2 else raise DivideByZero | _ -> raise UnknownASTNode
It''s in Ocaml, but you get the picture. It interprets simple arithmetic instructions with only constants.
Then do they just insert opcodes into memory whilst the script is being read, and it executes them? Or what?
Help please, thanks.
Help please, thanks.
No, nothing so low-level. Generally each opcode represents a function that gets called.
As for whether interpreted languages have a place in the ''real-time'' world, that depends on what your definition of real-time is.
[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
As for whether interpreted languages have a place in the ''real-time'' world, that depends on what your definition of real-time is.
[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
quote:Original post by Khaos
Does it use some sort of emulating, virtual machine to execute code on? Or does it execute the instructions as it reads them, or they are inputted, and if so, how?
There are lots of different schemes for how the instructions will be despatched. All of those you mention are valid ways of doing it. In fact, there is a very blurred distinction between the meanings of `interpreter' and `compiler'.
For a simple scheme, you could write a primitive interpreter which despatches commands off to be executed as soon as a complete command (including its operands) has been found in the input. Suppose you invent a language which has commands `set' for setting the value of a variable, `add' for adding to the value of a variable, and `print' to print the value of a variable. We need to specify a grammar for how these statements might look. Here's something suitably primitive and easy to write an interpreter for...
program := statement+statement := set-statement | add-statement | print-statementset-statement := "set" var valadd-statement := "add" var valprint-statement := "print" var
This means that a program consists of a series of statements. A statement is either a set-statement, add-statement or print-statement. Each of those statements begins with a command which identifies what type of statement it is. That makes it very easy to write a parser since we don't need to look at any of the tokens after the command to figure out what type of command we are parsing.
So, here's some C++ code (rather crude, no error-recovery) which will interpret programs which adhere to the above grammar:
#include <vector>#include <iostream>#include <string>#include <sstream>#include <fstream>#include <map>std::map<std::string, int> vars;void despatch_set(const std::string& var, int val){ vars[var] = val;}void despatch_add(const std::string& var, int val){ vars[var] += val;}void despatch_print(const std::string& var){ std::cout << vars[var] << "\n";}bool despatcher(std::istream& input){ std::string command; input >> command; if(input) { if(command == "set") { std::string var; int val; input >> var >> val; despatch_set(var, val); } else if(command == "add") { std::string var; int val; input >> var >> val; despatch_add(var, val); } else if(command == "print") { std::string var; input >> var; despatch_print(var); } else throw "Syntax error"; return true; } else return false;}int main(int argc, char *argv[]){ if(argc < 2) { std::cerr << "Usage is " << argv[0] << " <script-name>\n"; return EXIT_FAILURE; } std::ifstream ifs(argv[1]); while(despatcher(ifs)) ;}
Compile this program, then create a script file for it to run. For example, create a file called script.txt with the following contents:
set i 0print iadd i 5print iadd i 7print i
Now invoke your program to run the script (if your program is runscript.exe, then type "runscript script.txt" at the command prompt). The contents of script.txt will be intepreted and executed as the program runs.
quote:
Do you believe interpreted languages have a place, in the real-time world, or no?
Obviously yes.
[edited by - SabreMan on October 17, 2003 9:45:47 AM]
quote:Original post by SabreMan
In fact, there is a very blurred distinction between the meanings of `interpreter'' and `compiler''.
Sorry, that isn''t true. An interpreter performs evaluation of a program while a compiler translates a program into a different language, most often times assembly. You could try to argue that an interpreter transforms a program into the value domain specified by the language, but that''s reaching. Interpretation and compilation are two very different things.
quote:Original post by Anonymous Posterquote:Original post by SabreMan
In fact, there is a very blurred distinction between the meanings of `interpreter'' and `compiler''.
Sorry, that isn''t true. An interpreter performs evaluation of a program while a compiler translates a program into a different language, most often times assembly. You could try to argue that an interpreter transforms a program into the value domain specified by the language, but that''s reaching. Interpretation and compilation are two very different things.
Actually that depends. Ever heard of DRs (dynamic-recompilers)?
quote:Original post by Kylotan
No, nothing so low-level. Generally each opcode represents a function that gets called.
As for whether interpreted languages have a place in the ''real-time'' world, that depends on what your definition of real-time is.
It depends on whether or not you can bound the execution time of your interpreter, and that it''s fast enough to do the job. You could also say that a CPU is a RT interpreter, unless we should restrict the term interpreter to human-readable languages.
quote:Anonymous Poster
An interpreter performs evaluation of a program while a compiler translates a program into a different language, most often times assembly. You could try to argue that an interpreter transforms a program into the value domain specified by the language, but that''s reaching. Interpretation and compilation are two very different things.
I concur; While they both perform translation, an interpreter executes the program, a compiler does not. I don''t see any ambiguity.
quote:Original post by Khaos
Or does it execute the instructions as it reads them, or they are inputted, and if so, how?
The typical interpreter executes each instruction as it reads it. BASIC worked this way, among many others. Pre-compiling to byte-code is much more efficent (compiling is hard work), but less flexible. With an interpreter, you can make changes and run them right-there-and-then. With byte-code, you have to recompile.
quote:
How can you just translate the code to a low level and tell the computer to run it, without putting it into a file of sorts, or a location in memory, dealing with the operating system to allocate it and such?
Well you can't; you have to do all that. You don't have to actually create a PE or ELF, but you have to translate the commands to function your interpreter provides. Think of it as a data file on steriods. You open the text file, read in a line and start creating a program-state from what you read in. You make a symbol-table to look-up variables (where they are in memory), and have functions to add numbers and index arrays etc...
quote:
Do you believe interpreted languages have a place, in the real-time world, or no? Thoughts? Comments? Thanks!
No, I don't. For real-time code, you should use proven tools and produce machine code.
Most code does not need to be real-time though. Games are not real-time programs. The appropriate term, on-line, has fallen out of use, due to the confusion with the use of the term and the Internet. It’s on-line processing, as opposed to off-line processing. Can we get the job done and show it on the screen as it happens? Real-time means you *must* get the job done on time, or something bad happens (the missile misses the target, the Mars Lander becomes a scrap heap). With a game, you can tolerate an inconsistent fps. Real-time is more about consistency than throughput.
[edited by - Magmai Kai Holmlor on October 17, 2003 10:46:05 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement