java.lang.Object
com.maroontress.intexpr.impl.Compiler
This class provides a tiny compiler that translates code written in the
little language into
Instruction
s that the Interpreter
class can execute.
The code is of integer expressions specified in infix notation. The
expression has some similarities in the syntax to the C programming
language, containing operators and operands. The operators specified in
OperatorSpec
and parentheses are available in the expressions.
The numbers that the expression includes and represents are 32-bit signed
integers in two's-complement notation. The constants are non-negative
integers.
The compiled Instruction
s consist of Constant
s and
Operator
s in Reverse Polish notation to execute with the stack
machine.
-
Method Summary
Modifier and TypeMethodDescriptionstatic List<Instruction>
Creates a new list containingInstruction
objects from the expression that the specified reader provides.
-
Method Details
-
toRpn
Creates a new list containingInstruction
objects from the expression that the specified reader provides.The list contains the operators and operands in Reverse Polish notation, so it does not include any parentheses.
- Parameters:
reader
- The reader that provides the expression to evaluate.- Returns:
- The new list containing
Instruction
objects. - Throws:
IOException
- If an I/O error occurs with thereader
.IllegalArgumentException
- If the specified expression has syntax errors, unknown operators, illegal tokens, mismatched parentheses.
-