- START and FINISH blocks define different levels of scope.
- COM represents single line comments.
- VAR indicates a variable declaration (i.e. VAR myVariable). Variables and their values are stored in a hash table.
- PRINT Prints to the console either the value of a variable (e.g. PRINT myVariable) or the result of an expression (e.g. PRINT myVariable + 25).
- Expressions are limited unary and binary arithmetic, and values are integers.
- All tokens are separated by a space or newline.
- Operators include:
- Assignment " = " (e.g. myVariable = 15)
- Increment " ++ " (e.g. myVariable ++)
- Decrement " -- " (e.g. myVariable --)
- Addition " + "
- Subtraction " - "
- Multiplication " *"
- Division " / "
- Modulo " % "
- Power/Exponent " ^ "
- Assignment " = " (e.g. myVariable = 15)
I'm having trouble figuring out how I should handle tokens that operate in different ordering, like PRINT myVar vs. thirdVar = firstVar * secondVar (where the operators lie between the operands). Lately I've been trying to recursively execute a function (returns an int) that does a different operation depending on the token that is extracted, but I run into some problems of tracking what the operand was preceding an multiplication operator.
I also had the idea of pushing tokens that I extract onto 2 separate stacks - one for keywords/operators and the other for variable and literal values. Then after tokenizing the line of code, the program would continuously pop a keyword/operator off the stack and execute an "if" block that matched that token. I can't exactly remember what problems I encountered with this approach, because I obviously didn't use it.
Is there a better way to structure the interpreter, assuming that I cannot use 3rd-party libraries like boost? This problem has been plaguing me for the past 2 days...