How to implement arrays in an interpreter?

Posted by Ray on Stack Overflow See other posts from Stack Overflow or by Ray
Published on 2010-05-11T17:30:10Z Indexed on 2010/05/11 17:34 UTC
Read the original article Hit count: 256

Filed under:
|

I have managed to write several interpreters including

  • Tokenizing
  • Parsing, including more complicated expressions such as ((x+y)*z)/2
  • Building bytecode from syntax trees
  • Actual bytecode execution

What I didn't manage: Implementation of dictionaries/lists/arrays.

I always got stuck with getting multiple values into one variable.

My value structure (used for all values passed around, including variables) looks like this, for example:

class Value
{
public:
 ValueType type;

 int integerValue;
 string stringValue;
}

Works fine with integers and strings, but how could I implement arrays?

(From now on with array I mean arrays in my experimental language, not in C++)

  • How can I fit the array concept into the Value class above? Is it possible?

  • How should I make arrays able to be passed around just as you could pass around integers and strings in my language, using the class above?

Accessing array elements or memory allocation wouldn't be the problem, I just don't know how to store them.

© Stack Overflow or respective owner

Related posts about interpreter

Related posts about c++