Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| creating:actions [2025/11/03 23:05] – Use Java's instanceof-cast syntax ahelwer | creating:actions [2026/11/19 02:03] (current) – Fixed input handling ahelwer | ||
|---|---|---|---|
| Line 151: | Line 151: | ||
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| public Object visitVariableExpr(Expr.Variable expr) { | public Object visitVariableExpr(Expr.Variable expr) { | ||
| - | Object | + | Object |
| variables.containsKey(expr.name.lexeme) | variables.containsKey(expr.name.lexeme) | ||
| ? (primed ? next : current).get(expr.name.lexeme) | ? (primed ? next : current).get(expr.name.lexeme) | ||
| : environment.get(expr.name); | : environment.get(expr.name); | ||
| - | if (!(referent | + | if (!(callee |
| </ | </ | ||
| Line 189: | Line 189: | ||
| </ | </ | ||
| - | Double-priming an expression or priming an expression | + | Double-priming an expression or - in the initial state - priming an expression at all are both fatal errors in TLA⁺. |
| The meaning of priming an expression is that all state variables within that expression are primed, so the way the expression is evaluated is fundamentally changed. | The meaning of priming an expression is that all state variables within that expression are primed, so the way the expression is evaluated is fundamentally changed. | ||
| This is why the prime operator somewhat uniquely modifies the state of the '' | This is why the prime operator somewhat uniquely modifies the state of the '' | ||
| Line 256: | Line 256: | ||
| Set< | Set< | ||
| clearNext(); | clearNext(); | ||
| - | possibleNext.add(new HashMap<> | + | possibleNext.add(Map.copyOf(next)); |
| try { | try { | ||
| while (!possibleNext.isEmpty()) { | while (!possibleNext.isEmpty()) { | ||
| Line 270: | Line 270: | ||
| We use the familiar '' | We use the familiar '' | ||
| - | We also use the '' | + | We also use '' |
| For more on this peculiar issue, consult the Java '' | For more on this peculiar issue, consult the Java '' | ||
| Line 285: | Line 285: | ||
| Now we can write the central '' | Now we can write the central '' | ||
| - | <code java [highlight_lines_extra=" | + | <code java [highlight_lines_extra=" |
| while (!possibleNext.isEmpty()) { | while (!possibleNext.isEmpty()) { | ||
| Map< | Map< | ||
| Line 291: | Line 291: | ||
| Object satisfied = evaluate(action); | Object satisfied = evaluate(action); | ||
| checkBooleanOperand(location, | checkBooleanOperand(location, | ||
| - | if ((boolean)satisfied && isComplete()) confirmedNext.add(next); | + | if ((boolean)satisfied && isComplete()) |
| + | | ||
| + | } | ||
| possibleNext.remove(trunk); | possibleNext.remove(trunk); | ||
| } | } | ||
| Line 316: | Line 318: | ||
| Let's get concrete. | Let's get concrete. | ||
| How do we add states to '' | How do we add states to '' | ||
| - | For each disjunct, we use a fresh copy of the '' | + | For each disjunct, we use a fresh copy of the '' |
| As described above, this state will either already be in '' | As described above, this state will either already be in '' | ||
| We need to evaluate every disjunct to ensure we find all possible next states, which is why TLA⁺ disjunction does not short-circuit. | We need to evaluate every disjunct to ensure we find all possible next states, which is why TLA⁺ disjunction does not short-circuit. | ||
| + | We also need to ensure that at the end of evaluation, '' | ||
| Modify the '' | Modify the '' | ||
| - | <code java [highlight_lines_extra=" | + | <code java [highlight_lines_extra=" |
| case OR: | case OR: | ||
| boolean result = false; | boolean result = false; | ||
| Map< | Map< | ||
| + | Map< | ||
| for (Expr disjunct : expr.parameters) { | for (Expr disjunct : expr.parameters) { | ||
| next = new HashMap<> | next = new HashMap<> | ||
| Object junctResult = evaluate(disjunct); | Object junctResult = evaluate(disjunct); | ||
| checkBooleanOperand(expr.operator, | checkBooleanOperand(expr.operator, | ||
| - | possibleNext.add(new HashMap<> | + | |
| - | result | + | |
| + | | ||
| + | | ||
| + | } | ||
| } | } | ||
| + | next = lastValidNext; | ||
| return result; | return result; | ||
| default: | default: | ||
| Line 336: | Line 344: | ||
| There are two other operators to support for binding state variables: the set membership '' | There are two other operators to support for binding state variables: the set membership '' | ||
| - | Actions like '' | + | Actions like '' |
| Add the following code to the '' | Add the following code to the '' | ||
| - | <code java [highlight_lines_extra=" | + | <code java [highlight_lines_extra=" |
| case IN: | case IN: | ||
| checkSetOperand(expr.operator, | checkSetOperand(expr.operator, | ||
| Line 347: | Line 355: | ||
| next.put(var.name().lexeme, | next.put(var.name().lexeme, | ||
| left = element; | left = element; | ||
| - | possibleNext.add(new HashMap<> | + | possibleNext.add(Map.copyOf(next)); |
| } | } | ||
| - | return true; | ||
| } | } | ||
| return ((Set<?> | return ((Set<?> | ||
| Line 355: | Line 362: | ||
| Then update the '' | Then update the '' | ||
| - | <code java [highlight_lines_extra=" | + | <code java [highlight_lines_extra=" |
| } case EXISTS: { | } case EXISTS: { | ||
| boolean result = false; | boolean result = false; | ||
| - | State current = state; | ||
| Map< | Map< | ||
| + | Map< | ||
| for (Environment binding : bindings) { | for (Environment binding : bindings) { | ||
| next = new HashMap<> | next = new HashMap<> | ||
| Object junctResult = executeBlock(expr.body, | Object junctResult = executeBlock(expr.body, | ||
| checkBooleanOperand(expr.op, | checkBooleanOperand(expr.op, | ||
| - | possibleNext.add(new HashMap<> | + | |
| - | result | + | |
| + | | ||
| + | | ||
| + | } | ||
| } | } | ||
| + | next = lastValidNext; | ||
| return result; | return result; | ||
| </ | </ | ||
| Line 379: | Line 390: | ||
| Well, not quite - the '' | Well, not quite - the '' | ||
| This means we need some extra ceremony to stash the current values of '' | This means we need some extra ceremony to stash the current values of '' | ||
| + | '' | ||
| Add the highlighted code to the '' | Add the highlighted code to the '' | ||
| - | <code java [highlight_lines_extra=" | + | <code java [highlight_lines_extra=" |
| } case ENABLED: { | } case ENABLED: { | ||
| Map< | Map< | ||
| Set< | Set< | ||
| try { | try { | ||
| - | | + | return !getNextStates(expr.operator, |
| - | | + | |
| } finally { | } finally { | ||
| next = oldNext; | next = oldNext; | ||
| Line 393: | Line 404: | ||
| } case NOT: { | } case NOT: { | ||
| </ | </ | ||
| + | |||
| + | Then modify '' | ||
| + | <code java [highlight_lines_extra=" | ||
| + | List< | ||
| + | Set< | ||
| + | clearNext(); | ||
| + | possibleNext.add(Map.copyOf(next)); | ||
| + | try { | ||
| + | while (!possibleNext.isEmpty()) { | ||
| + | Map< | ||
| + | next = new HashMap<> | ||
| + | Object satisfied = evaluate(action); | ||
| + | checkBooleanOperand(location, | ||
| + | if ((boolean)satisfied && (isComplete() || acceptIncompleteStates)) { | ||
| + | confirmedNext.add(Map.copyOf(next)); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | This change lets '' | ||
| This is a big milestone: we now have fully defined // | This is a big milestone: we now have fully defined // | ||
| Line 415: | Line 445: | ||
| </ | </ | ||
| - | In our '' | + | In our '' |
| Also, two values can only be compared for equality if they are both defined: | Also, two values can only be compared for equality if they are both defined: | ||
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| Line 428: | Line 458: | ||
| </ | </ | ||
| - | A null check must also must be added to the '' | + | A null check must also be added to the '' |
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| case LEFT_BRACE: | case LEFT_BRACE: | ||
| Line 467: | Line 497: | ||
| One decent approach is to hijack '' | One decent approach is to hijack '' | ||
| For this, add the following code to the bottom of the '' | For this, add the following code to the bottom of the '' | ||
| - | <code java [highlight_lines_extra=" | + | <code java [highlight_lines_extra=" |
| // Stop if there was a syntax error. | // Stop if there was a syntax error. | ||
| if (hadError) return; | if (hadError) return; | ||
| if (replMode && statements.size() == 1 | if (replMode && statements.size() == 1 | ||
| - | && statements.get(0) instanceof Stmt.Print) { | + | && statements.get(0) instanceof Stmt.Print |
| - | tryStep((Stmt.Print)statements.get(0)); | + | |
| + | | ||
| + | } catch (RuntimeError error) { | ||
| + | runtimeError(error); | ||
| + | } | ||
| } else { | } else { | ||
| interpreter.interpret(statements); | interpreter.interpret(statements); | ||
| Line 493: | Line 527: | ||
| List< | List< | ||
| - | interpreter.getNextStates(action.location, | + | interpreter.getNextStates(action.location, |
| if (nextStates.isEmpty()) { | if (nextStates.isEmpty()) { | ||
| action.accept(interpreter); | action.accept(interpreter); | ||
| Line 513: | Line 547: | ||
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| List< | List< | ||
| - | interpreter.getNextStates(action.location, | + | interpreter.getNextStates(action.location, |
| if (nextStates.isEmpty()) { | if (nextStates.isEmpty()) { | ||
| - | | + | |
| return; | return; | ||
| } | } | ||
| Line 527: | Line 561: | ||
| We also print out '' | We also print out '' | ||
| - | This called | + | This calls two methods; one, '' |
| <code java> | <code java> | ||
| void goToState(Map< | void goToState(Map< | ||
| Line 539: | Line 573: | ||
| If there are multiple possible next states, the user is prompted to choose one: | If there are multiple possible next states, the user is prompted to choose one: | ||
| <code java> | <code java> | ||
| - | private static Map< | + | private static Map< |
| if (nextStates.size() == 1) { | if (nextStates.size() == 1) { | ||
| return nextStates.get(0); | return nextStates.get(0); | ||
| Line 549: | Line 583: | ||
| System.out.print("> | System.out.print("> | ||
| - | | + | return nextStates.get(Integer.parseInt(reader.readLine())); |
| - | | + | |
| - | } | + | |
| } | } | ||
| } | } | ||
| + | </ | ||
| + | |||
| + | Since we are reading from '' | ||
| + | <code java [highlight_lines_extra=" | ||
| + | private static void tryStep(Stmt.Print action) throws IOException { | ||
| + | Object result = interpreter.executeBlock(action.expression, | ||
| + | </ | ||
| + | <code java [highlight_lines_extra=" | ||
| + | private static void run(String source, boolean replMode) throws IOException { | ||
| + | Scanner scanner = new Scanner(source); | ||
| </ | </ | ||
| Line 568: | Line 610: | ||
| [[creating: | [[creating: | ||
| - | |||