int
expressions.
The expression has some similarities in the syntax to the C programming language.
Numbers
Numbers are 32-bit signed integers in two's-complement notation (like
values of int
type in Java and C#, int32_t
type in C23,
and so on). They can represent integers from −2147483648 to
2147483647.
Expressions
A simple expression is just an integer constant.
Note that the minimum integer (−2147483648) cannot be constant
because the expression -2147483648
is a unary -
operator
followed by 2147483648
, and the integer is greater than the maximum
integer (2147483647). So, you have to represent the minimum integer with
(-2147483647 - 1)
.
In the following descriptions of legal expressions, expr refers to a complete expression:
(
expr)
- The parentheses alter the standard precedence to force the evaluation of the expression to precede the operation outside of them.
- unary
+
expr - The result is the expression itself (that is, no operation).
- unary
-
expr - The result is the negation of the expression.
- unary
!
expr - The result is 1 if expr is 0, otherwise 0.
- unary
~
expr - The result is the logical negation on each bit, forming the ones' complement of the given binary value.
- expr
*
expr - The result is the product of the two expressions.
- expr
/
expr - The result is the quotient of the two expressions.
- expr
%
expr - The result is the remainder of the two expressions.
- expr
+
expr - The result is the sum of the two expressions.
- expr
-
expr - The result is the difference between the two expressions.
- expr1
<<
expr2 - The result is the left arithmetic shift of expr1 by expr2.
- expr
>>
expr - The result is the right arithmetic shift of expr1 by expr2.
- expr1
<
expr2 - The result is 1 if expr1 is strictly less than expr2, otherwise 0.
- expr1
<=
expr2 - The result is 1 if expr1 is less than or equal to expr2, otherwise 0.
- expr1
>
expr2 - The result is 1 if expr1 is strictly greater than expr2, otherwise 0.
- expr1
>=
expr2 - The result is 1 if expr1 is greater than or equal to expr2, otherwise 0.
- expr1
==
expr2 - The result is 1 if expr1 is equal to expr2, otherwise 0.
- expr1
!=
expr2 - The result is 1 if expr1 is not equal to expr2, otherwise 0.
- expr
&&
expr - The result is 1 if both expressions are non-zero, otherwise 0.
- expr
||
expr - The result is 1 if either expression is non-zero, otherwise 0.
The operator precedence is as follows:
Precedence | Operator | Associativity |
---|---|---|
highest |
unary + - ! ~ |
|
* / % |
Left-to-right | |
+ - |
||
<< >> |
||
< <= > >= |
||
== != |
||
& |
||
^ |
||
| |
||
&& |
||
lowest |
|| |
-
Method Summary
Modifier and TypeMethodDescriptionstatic int
Evaluates the specified string representing an expression and returns the evaluated value.static String
Returns the string to visualize the syntax tree representing the expression in Reverse Polish notation, which is equivalent to the specified expression.
-
Method Details
-
eval
Evaluates the specified string representing an expression and returns the evaluated value.The expression is of 32-bit signed integer in two's-complement notation.
For example, the invocation
IntExpr.eval("(1+2*3<<4)%5")
returns 2.- Parameters:
expr
- The expression to evaluate.- Returns:
- The evaluated value.
- Throws:
IllegalArgumentException
- If the specifiedexpr
has syntax errors such as a mismatched or missing parenthesis, a stray token, an unknown token.ArithmeticException
- If there is an attempt to divide an integer value by zero or to overflow.
-
toTree
Returns the string to visualize the syntax tree representing the expression in Reverse Polish notation, which is equivalent to the specified expression.The expression is of 32-bit signed integer in two's-complement notation.
For example, the invocation:
IntExpr.toTree("(1+2*3<<4)%5")
returns as follows:""" MOD ├ SHL │ ├ ADD │ │ ├ CONST 1 │ │ └ MUL │ │ ├ CONST 2 │ │ └ CONST 3 │ └ CONST 4 └ CONST 5 """
The opcodes that the tree includes (such asADD
,MUL
,CONST
, etc.) are defined inOpcode
.- Parameters:
expr
- The expression to evaluate.- Returns:
- The string to visualize the syntax tree.
- Throws:
IllegalArgumentException
- If the specifiedexpr
has syntax errors such as a mismatched or missing parenthesis, a stray token, an unknown token.
-