Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| creating:evaluation [2025/04/27 17:32] – Removed infix conjunction & disjunction operators ahelwer | creating:evaluation [2025/06/19 16:31] (current) – LEFT_BRACE interpretation splits out Object value = ahelwer | ||
|---|---|---|---|
| Line 65: | Line 65: | ||
| ==== Evaluating Unary Operators ==== | ==== Evaluating Unary Operators ==== | ||
| - | Our first real difference is in the '' | + | Our first real difference |
| Recall that since our '' | Recall that since our '' | ||
| - | Here's how we define | + | We also don't pre-emptively evaluate |
| - | + | The method is structured as a switch statement handling all possible unary operators: | |
| - | <code java [highlight_lines_extra=" | + | <code java> |
| @Override | @Override | ||
| public Object visitUnaryExpr(Expr.Unary expr) { | public Object visitUnaryExpr(Expr.Unary expr) { | ||
| - | Object operand = evaluate(expr.expr); | ||
| - | |||
| switch (expr.operator.type) { | switch (expr.operator.type) { | ||
| - | case MINUS: | + | case PRIME: { |
| - | return -(int)operand; | + | |
| - | } | + | |
| - | | + | } case ENABLED: { |
| - | return null; | + | |
| + | } case NOT: { | ||
| + | |||
| + | } case MINUS: { | ||
| + | |||
| + | } default: { | ||
| + | | ||
| + | return null; | ||
| + | } | ||
| + | } | ||
| } | } | ||
| + | </ | ||
| + | |||
| + | Here's how we define the negative prefix operator; while the book casts the operand to a '' | ||
| + | <code java [highlight_lines_extra=" | ||
| + | } case MINUS: { | ||
| + | Object operand = evaluate(expr.expr); | ||
| + | return -(int)operand; | ||
| + | } default: { | ||
| </ | </ | ||
| Line 90: | Line 103: | ||
| Our logical-not prefix operator is denoted by the '' | Our logical-not prefix operator is denoted by the '' | ||
| - | We also have no use for the '' | + | We also have no use for the '' |
| - | Add this code to the '' | + | Add this code to '' |
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| - | switch | + | } case NOT: { |
| - | case NOT: | + | Object operand = evaluate(expr.expr); |
| return !(boolean)operand; | return !(boolean)operand; | ||
| - | case MINUS: | + | |
| </ | </ | ||
| We still have two more unary operators to define: '' | We still have two more unary operators to define: '' | ||
| Both are trivial in this domain but will become much more complicated later on. | Both are trivial in this domain but will become much more complicated later on. | ||
| - | For constant expressions, | + | For constant expressions, |
| - | Add the highlighted code to the '' | + | Add the highlighted code to '' |
| - | + | <code java [highlight_lines_extra=" | |
| - | <code java [highlight_lines_extra=" | + | case PRIME: { |
| - | switch (expr.operator.type) { | + | return |
| - | case ENABLED: | + | |
| - | return (boolean)operand; | + | return |
| - | case NOT: | + | |
| - | </ | + | |
| - | + | ||
| - | Priming a constant expression has no effect on the value of that expression, so just return the operand' | + | |
| - | + | ||
| - | <code java [highlight_lines_extra=" | + | |
| - | switch (expr.operator.type) | + | |
| - | case PRIME: | + | |
| - | return | + | |
| - | case ENABLED: | + | |
| </ | </ | ||
| Line 124: | Line 127: | ||
| Unlike Lox, addition in TLA⁺ is only defined between two numbers (at least in its standard '' | Unlike Lox, addition in TLA⁺ is only defined between two numbers (at least in its standard '' | ||
| - | Here's how we define our basic binary arithmetic & comparison operators; add a '' | + | Here's how we define our basic binary arithmetic & comparison operators; add a '' |
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| @Override | @Override | ||
| Line 226: | Line 228: | ||
| Set< | Set< | ||
| for (Expr parameter : expr.parameters) { | for (Expr parameter : expr.parameters) { | ||
| - | | + | |
| + | set.add(value); | ||
| } | } | ||
| return set; | return set; | ||
| Line 240: | Line 243: | ||
| In [[https:// | In [[https:// | ||
| - | Here's our variant of the '' | + | Here's our variant of the '' |
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| Line 292: | Line 295: | ||
| Now add these checks to our interpreter code. | Now add these checks to our interpreter code. | ||
| Here's negative: | Here's negative: | ||
| - | <code java [highlight_lines_extra=" | + | <code java [highlight_lines_extra=" |
| - | case MINUS: | + | |
| + | Object operand = evaluate(expr.expr); | ||
| checkNumberOperand(expr.operator, | checkNumberOperand(expr.operator, | ||
| return -(int)operand; | return -(int)operand; | ||
| + | } default: { | ||
| </ | </ | ||
| Logical not: | Logical not: | ||
| - | <code java [highlight_lines_extra=" | + | <code java [highlight_lines_extra=" |
| - | case NOT: | + | |
| + | Object operand = evaluate(expr.expr); | ||
| checkBooleanOperand(expr.operator, | checkBooleanOperand(expr.operator, | ||
| return !(boolean)operand; | return !(boolean)operand; | ||
| - | </ | + | } case MINUS: { |
| - | Enabled: | + | |
| - | <code java [highlight_lines_extra=" | + | |
| - | | + | |
| - | checkBooleanOperand(expr.operator, | + | |
| - | return (boolean)operand; | + | |
| </ | </ | ||
| Subtraction: | Subtraction: | ||