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/04/28 20:27] – Implemented jlist flattening ahelwercreating: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 short-circuit. +Similar to the logical operators covered in the book, conjunction lists short-circuit. 
-This means juncts are evaluated in order and, if a conjunct is false or a disjunct is true, evaluation stops and immediately returns false or true respectively.+That means conjuncts are evaluated in order and, if a single conjunct is false, evaluation immediately stops and returns false
 +In an odd contrast, disjunction lists do //not// short-circuit; this is because they are used to express nondeterminism, as you will learn several chapters from now.
  
 Add conjunction list evaluation logic to ''visitVariadicExpr()'' in the ''Interpreter'' class, below the set constructor logic: Add conjunction list evaluation logic to ''visitVariadicExpr()'' in the ''Interpreter'' class, below the set constructor logic:
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="2,3,4,5,6,7,8"]>+<code java [highlight_lines_extra="2,3,4,5,6,7,8,9"]>
         return true;         return true;
       case OR:       case OR:
 +        boolean result = false;
         for (Expr disjunct : expr.parameters) {         for (Expr disjunct : expr.parameters) {
-          Object result = evaluate(disjunct); +          Object junctResult = evaluate(disjunct); 
-          checkBooleanOperand(expr.operator, result); +          checkBooleanOperand(expr.operator, junctResult); 
-          if ((boolean)result) return true;+          result |= (boolean)junctResult;
         }         }
-        return false;+        return result;
       default:       default:
         // Unreachable.         // Unreachable.
Line 353: Line 355:
     List<Expr> flattened = new ArrayList<>();     List<Expr> flattened = new ArrayList<>();
     for (Expr junct : juncts) {     for (Expr junct : juncts) {
-      Expr.Variadic vjunct; +      if (junct instanceof Expr.Variadic vjunct 
-      if ((vjunct = asVariadicOp(op, junct)) !null) {+          && vjunct.operator.type == op.type) {
         flattened.addAll(vjunct.parameters);         flattened.addAll(vjunct.parameters);
       } else {       } else {
Line 362: Line 364:
  
     return new Expr.Variadic(op, flattened);     return new Expr.Variadic(op, flattened);
-  } 
-</code> 
- 
-This uses Java's conditional-assign syntax along with the ''asVariadicOp()'' helper, which returns an ''Expr.Variadic'' instance if the given expression is a jlist of the given type: 
-<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; 
   }   }
 </code> </code>
  • creating/jlists.1745872051.txt.gz
  • Last modified: 2025/04/28 20:27
  • by ahelwer