creating:jlists

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
creating:jlists [2025/11/04 17:32] – Further simplified instanceof code ahelwercreating:jlists [2026/11/19 03:00] (current) – Minor change to interpretation code of OR ahelwer
Line 3: Line 3:
 [[https://craftinginterpreters.com/control-flow.html|Chapter 9]] of //Crafting Interpreters// covers control flow - ''if'', ''while'', ''for'', and all that. [[https://craftinginterpreters.com/control-flow.html|Chapter 9]] of //Crafting Interpreters// covers control flow - ''if'', ''while'', ''for'', and all that.
 TLA⁺ doesn't really have that, because TLA⁺ specifications aren't imperative programs. TLA⁺ doesn't really have that, because TLA⁺ specifications aren't imperative programs.
-However, TLA⁺ //does// have something similar.+However, TLA⁺ //does// have something somewhat analogous.
 When specifying a system's actions, we commonly need to do two things: write preconditions or "guards" controlling whether an action can be taken, and specify the set of possible next-state transitions. When specifying a system's actions, we commonly need to do two things: write preconditions or "guards" controlling whether an action can be taken, and specify the set of possible next-state transitions.
 For the former we use conjunction (aka logical "and"), and for the latter we use disjunction (aka logical "or"). For the former we use conjunction (aka logical "and"), and for the latter we use disjunction (aka logical "or").
Line 64: Line 64:
  
 ''matchBullet()'' consumes a ''/\'' or ''\/'' token only if it is of the expected type and column. ''matchBullet()'' consumes a ''/\'' or ''\/'' token only if it is of the expected type and column.
-In this way our jlist parsing logic will only add another expression if it finds another vertically-aligned ''/\'' or ''\/'' token on the next line.+In this way our jlist parsing logic will only add another expression if it finds another vertically-aligned ''/\'' or ''\/'' token on a subsequent line.
 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 ''\/'' and ''/\'' as //infix operators//. Here's the proverbial wrench: TLA⁺ also allows ''\/'' and ''/\'' as //infix operators//.
-We skipped defining them in the expressions chapter where we learned how to parse operators of different precedences; let's add them to ''operators'' table in our ''Token'' class now:+We skipped defining them in the expressions chapter where we learned how to parse operators of different precedences; let's add them to the ''operators'' table in our ''Parser'' class now:
  
 <code java [highlight_lines_extra="5,6"]> <code java [highlight_lines_extra="5,6"]>
-private static final Operator[] operators = new Operator[] { +  static final Operator[] operators = new Operator[] { 
-    new Operator(Fix.PREFIX,  NOT,        true,   4,  4 ),+    new Operator(Fix.PREFIX,  NOT,        false,  4,  4 ),
     new Operator(Fix.PREFIX,  ENABLED,    false,  4,  15),     new Operator(Fix.PREFIX,  ENABLED,    false,  4,  15),
-    new Operator(Fix.PREFIX,  MINUS,      true,   12, 12),+    new Operator(Fix.PREFIX,  MINUS,      false,  12, 12),
     new Operator(Fix.INFIX,   AND,        true,   3,  3 ),     new Operator(Fix.INFIX,   AND,        true,   3,  3 ),
     new Operator(Fix.INFIX,   OR,         true,   3,  3 ),     new Operator(Fix.INFIX,   OR,         true,   3,  3 ),
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://craftinginterpreters.com/representing-code.html|Chapter 5: Representing Code]]. First, it means we cannot easily write the formal TLA⁺ grammar in Backus-Naur Form (BNF) as we learned to do in [[https://craftinginterpreters.com/representing-code.html|Chapter 5: Representing Code]].
-Although grammar notation [[https://en.wikipedia.org/wiki/Context-sensitive_grammar|does exist]] for context-sensitive languages, it is unintuitive and more of a hindrance to langage implementers than a help.+Although grammar notation [[https://en.wikipedia.org/wiki/Context-sensitive_grammar|does exist]] for context-sensitive languages, it is unintuitive and more of a hindrance to language implementers than a help.
 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 ''Parser'' class carries some additional mutable state in a class variable, and its methods change their behavior based on that state.+Second - returning to our parser implementation here - it means that our ''Parser'' class carries some additional mutable state in an instance field, and its methods change their behavior based on that state.
  
 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 ''[[https://docs.oracle.com/javase/8/docs/api/java/util/ArrayDeque.html|ArrayDeque]]'' class as a stack. We'll be using Java's ''[[https://docs.oracle.com/javase/8/docs/api/java/util/ArrayDeque.html|ArrayDeque]]'' class as a stack.
 It only needs to hold the jlist column. It only needs to hold the jlist column.
-Define a new class variable near the top of the ''Parser'' class:+Define a new instance field near the top of the ''Parser'' class:
 <code java [highlight_lines_extra="4"]> <code java [highlight_lines_extra="4"]>
   private final List<Token> tokens;   private final List<Token> tokens;
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 ''operatorExpression()'':+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 ''operatorExpression()'', presented below without change:
 <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, operator, right);       expr = new Expr.Binary(expr, operator, right);
       if (!op.assoc) return expr;       if (!op.assoc) return expr;
Line 269: Line 270:
       default:       default:
         // Unreachable.         // Unreachable.
-        return null+        return null;
 </code> </code>
  
 Then add the disjunction list logic right below that: Then add the disjunction list logic right below that:
-<code java [highlight_lines_extra="2,3,4,5,6,7,8,9"]>+<code java [highlight_lines_extra="2,3,4,5,6,7,8,9,10,11"]>
         return true;         return true;
       case OR:       case OR:
Line 280: Line 281:
           Object junctResult = evaluate(disjunct);           Object junctResult = evaluate(disjunct);
           checkBooleanOperand(expr.operator, junctResult);           checkBooleanOperand(expr.operator, junctResult);
-          result |= (boolean)junctResult;+          if ((boolean)junctResult) { 
 +            result = true; 
 +          }
         }         }
         return result;         return result;
Line 295: Line 298:
  
 In the ''Parser'' class, modify the infix operator parsing loop in ''operatorExpression()'' by replacing the ''expr ='' line with a call to a new helper, ''flattenInfix()'': In the ''Parser'' class, modify the infix operator parsing loop in ''operatorExpression()'' by replacing the ''expr ='' line with a call to a new helper, ''flattenInfix()'':
-<code java [highlight_lines_extra="5"]>+<code java [highlight_lines_extra="6"]>
     Expr expr = operatorExpression(prec + 1);     Expr expr = operatorExpression(prec + 1);
     while ((op = matchOp(Fix.INFIX, prec)) != null) {     while ((op = matchOp(Fix.INFIX, prec)) != null) {
       Token operator = previous();       Token operator = previous();
       Expr right = operatorExpression(op.highPrec + 1);       Expr right = operatorExpression(op.highPrec + 1);
 +      checkPrecOverlap(op);
       expr = flattenInfix(expr, operator, right);       expr = flattenInfix(expr, operator, right);
       if (!op.assoc) return expr;       if (!op.assoc) return expr;
Line 418: Line 422:
  
 [[creating:statements|< Previous Page]] | [[creating:start#table_of_contents|Table of Contents]] | [[creating:operators|Next Page >]] [[creating:statements|< Previous Page]] | [[creating:start#table_of_contents|Table of Contents]] | [[creating:operators|Next Page >]]
- 
  • creating/jlists.1762277569.txt.gz
  • Last modified: 2025/11/04 17:32
  • by ahelwer