Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| creating:jlists [2025/04/28 20:27] – Implemented jlist flattening ahelwer | creating:jlists [2025/11/04 17:32] (current) – Further simplified instanceof code ahelwer | ||
|---|---|---|---|
| Line 253: | Line 253: | ||
| Now that we can parse jlists, let's interpret them. | Now that we can parse jlists, let's interpret them. | ||
| - | Similar to the logical operators covered in the book, jlists | + | Similar to the logical operators covered in the book, conjunction lists short-circuit. |
| - | This means juncts | + | That means conjuncts |
| + | In an odd contrast, disjunction lists do //not// short-circuit; | ||
| Add conjunction list evaluation logic to '' | Add conjunction list evaluation logic to '' | ||
| Line 272: | Line 273: | ||
| Then add the disjunction list logic right below that: | Then add the disjunction list logic right below that: | ||
| - | <code java [highlight_lines_extra=" | + | <code java [highlight_lines_extra=" |
| return true; | return true; | ||
| case OR: | case OR: | ||
| + | boolean result = false; | ||
| for (Expr disjunct : expr.parameters) { | for (Expr disjunct : expr.parameters) { | ||
| - | Object | + | Object |
| - | checkBooleanOperand(expr.operator, | + | checkBooleanOperand(expr.operator, |
| - | | + | |
| } | } | ||
| - | return | + | return |
| default: | default: | ||
| // Unreachable. | // Unreachable. | ||
| Line 353: | Line 355: | ||
| List< | List< | ||
| for (Expr junct : juncts) { | for (Expr junct : juncts) { | ||
| - | Expr.Variadic vjunct; | + | |
| - | if ((vjunct = asVariadicOp(op, | + | && |
| flattened.addAll(vjunct.parameters); | flattened.addAll(vjunct.parameters); | ||
| } else { | } else { | ||
| Line 362: | Line 364: | ||
| return new Expr.Variadic(op, | return new Expr.Variadic(op, | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | This uses Java's conditional-assign syntax along with the '' | ||
| - | <code java> | ||
| - | private Expr.Variadic asVariadicOp(Token op, Expr expr) { | ||
| - | if (expr instanceof Expr.Variadic) { | ||
| - | Expr.Variadic vExpr = (Expr.Variadic)expr; | ||
| - | if (vExpr.operator.type == op.type) return vExpr; | ||
| - | } | ||
| - | |||
| - | return null; | ||
| } | } | ||
| </ | </ | ||