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/11/04 17:32] – Further simplified instanceof code ahelwer | creating:jlists [2026/11/19 03:00] (current) – Minor change to interpretation code of OR ahelwer | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| [[https:// | [[https:// | ||
| TLA⁺ doesn' | TLA⁺ doesn' | ||
| - | However, TLA⁺ //does// have something | + | However, TLA⁺ //does// have something |
| When specifying a system' | When specifying a system' | ||
| For the former we use conjunction (aka logical " | For the former we use conjunction (aka logical " | ||
| Line 64: | Line 64: | ||
| '' | '' | ||
| - | In this way our jlist parsing logic will only add another expression if it finds another vertically-aligned ''/ | + | In this way our jlist parsing logic will only add another expression if it finds another vertically-aligned ''/ |
| This all... kind of works? | This all... kind of works? | ||
| You can parse a surprisingly broad set of jlists with just our simple logic! | You can parse a surprisingly broad set of jlists with just our simple logic! | ||
| Line 88: | Line 88: | ||
| Unfortunately not. | Unfortunately not. | ||
| Here's the proverbial wrench: TLA⁺ also allows '' | Here's the proverbial wrench: TLA⁺ also allows '' | ||
| - | We skipped defining them in the expressions chapter where we learned how to parse operators of different precedences; | + | We skipped defining them in the expressions chapter where we learned how to parse operators of different precedences; |
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| - | private | + | |
| - | new Operator(Fix.PREFIX, | + | new Operator(Fix.PREFIX, |
| new Operator(Fix.PREFIX, | new Operator(Fix.PREFIX, | ||
| - | new Operator(Fix.PREFIX, | + | new Operator(Fix.PREFIX, |
| new Operator(Fix.INFIX, | new Operator(Fix.INFIX, | ||
| new Operator(Fix.INFIX, | new Operator(Fix.INFIX, | ||
| Line 126: | Line 126: | ||
| What does this change mean in practical terms? | What does this change mean in practical terms? | ||
| First, it means we cannot easily write the formal TLA⁺ grammar in Backus-Naur Form (BNF) as we learned to do in [[https:// | First, it means we cannot easily write the formal TLA⁺ grammar in Backus-Naur Form (BNF) as we learned to do in [[https:// | ||
| - | Although grammar notation [[https:// | + | Although grammar notation [[https:// |
| Thus formal TLA⁺ grammars exclude jlists and use BNF to define the non-jlist parts of the language, then use plain language to describe how jlists work. | Thus formal TLA⁺ grammars exclude jlists and use BNF to define the non-jlist parts of the language, then use plain language to describe how jlists work. | ||
| - | Second - returning to our parser implementation here - it means that our '' | + | Second - returning to our parser implementation here - it means that our '' |
| Ultimately the shape of this state is a stack of nested jlists. | Ultimately the shape of this state is a stack of nested jlists. | ||
| Line 138: | Line 138: | ||
| We'll be using Java's '' | We'll be using Java's '' | ||
| It only needs to hold the jlist column. | It only needs to hold the jlist column. | ||
| - | Define a new class variable | + | Define a new instance field near the top of the '' |
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| private final List< | private final List< | ||
| Line 184: | Line 184: | ||
| This works! | This works! | ||
| We can parse jlists again! | We can parse jlists again! | ||
| - | It's a bit tricky to figure out how this changes things in practice, so let's take a close look at our infix operator parsing code in '' | + | It's a bit tricky to figure out how this changes things in practice, so let's take a close look at our infix operator parsing code in '' |
| <code java> | <code java> | ||
| Expr expr = operatorExpression(prec + 1); | Expr expr = operatorExpression(prec + 1); | ||
| Line 190: | Line 190: | ||
| Token operator = previous(); | Token operator = previous(); | ||
| Expr right = operatorExpression(op.highPrec + 1); | Expr right = operatorExpression(op.highPrec + 1); | ||
| + | checkPrecOverlap(op); | ||
| expr = new Expr.Binary(expr, | expr = new Expr.Binary(expr, | ||
| if (!op.assoc) return expr; | if (!op.assoc) return expr; | ||
| Line 269: | Line 270: | ||
| default: | default: | ||
| // Unreachable. | // Unreachable. | ||
| - | return null | + | return null; |
| </ | </ | ||
| 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: | ||
| Line 280: | Line 281: | ||
| Object junctResult = evaluate(disjunct); | Object junctResult = evaluate(disjunct); | ||
| checkBooleanOperand(expr.operator, | checkBooleanOperand(expr.operator, | ||
| - | | + | |
| + | result = true; | ||
| + | } | ||
| } | } | ||
| return result; | return result; | ||
| Line 295: | Line 298: | ||
| In the '' | In the '' | ||
| - | <code java [highlight_lines_extra=" | + | <code java [highlight_lines_extra=" |
| Expr expr = operatorExpression(prec + 1); | Expr expr = operatorExpression(prec + 1); | ||
| while ((op = matchOp(Fix.INFIX, | while ((op = matchOp(Fix.INFIX, | ||
| Token operator = previous(); | Token operator = previous(); | ||
| Expr right = operatorExpression(op.highPrec + 1); | Expr right = operatorExpression(op.highPrec + 1); | ||
| + | checkPrecOverlap(op); | ||
| expr = flattenInfix(expr, | expr = flattenInfix(expr, | ||
| if (!op.assoc) return expr; | if (!op.assoc) return expr; | ||
| Line 418: | Line 422: | ||
| [[creating: | [[creating: | ||
| - | |||