The Java projects which implements the interpreter for the simple language.
Language rules:
The interpreter interprets the input. Any numerical
value is put on the data stack. Any text that is not a number, is an
operator. The language operators work on data stack. They can take the operands
from the stack and will put the results back on the stack.
Operators:
- '.' - take a value from the stack and print it,
- '+' - add the values on the stack,
- '-' - subtract the values on the stack,
- '*' - multiply the values on the stack,
- '/' - divide the values on the stack (the top-1 value is divided by the top value).
- 'DUP' - duplicate the value on the stack,
- 'SWAP' - swap the 2 values on the stack,
- '<' - the 'less-than' operator,
- '>' - the 'more-than' operator.
- 'if else
then' - control structure (can be
nested).
- 'do LOOP' - takes the initial counter
from the top of the stack and executes the loop that many times.
The implementation of interpreter is based on ANTLR library. This library is a powerful parser generator for reading and processing any text formats. From the grammar file, it generates a parser that can build and walk parse trees. The grammar file defines the syntax rules for the interpreted language.
The grammar file is - Expr.g4
Application entry point - App.java