Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
creating:operators [2025/06/06 17:39] – Add variable shadowing error checking ahelwer | creating:operators [2025/06/13 21:25] (current) – Removed PrintStream parameter from Interpreter constructor ahelwer | ||
---|---|---|---|
Line 109: | Line 109: | ||
import java.util.Map; | import java.util.Map; | ||
import java.util.HashMap; | import java.util.HashMap; | ||
- | import java.io.PrintStream; | ||
</ | </ | ||
Line 242: | Line 241: | ||
We've already implemented all our native TLA⁺ functions in the // | We've already implemented all our native TLA⁺ functions in the // | ||
Due to the '' | Due to the '' | ||
- | <code java [highlight_lines_extra=" | + | <code java [highlight_lines_extra=" |
class Interpreter implements Expr.Visitor< | class Interpreter implements Expr.Visitor< | ||
| | ||
final Environment globals; | final Environment globals; | ||
private Environment environment; | private Environment environment; | ||
- | private final PrintStream out; | ||
- | public Interpreter(PrintStream out, boolean replMode) { | + | public Interpreter(boolean replMode) { |
this.globals = new Environment(replMode); | this.globals = new Environment(replMode); | ||
this.environment = this.globals; | this.environment = this.globals; | ||
- | this.out = out; | ||
} | } | ||
</ | </ | ||
Line 444: | Line 441: | ||
Additional TLA⁺-specific validation is necessary here. | Additional TLA⁺-specific validation is necessary here. | ||
- | TLA⁺ does not allow shadowing existing | + | While we allow redefining operators // |
So, if an operator of name '' | So, if an operator of name '' | ||
We should also disallow operator definitions with duplicate parameter names like '' | We should also disallow operator definitions with duplicate parameter names like '' | ||
Line 452: | Line 449: | ||
for (Token name : names) { | for (Token name : names) { | ||
if (environment.isDefined(name)) { | if (environment.isDefined(name)) { | ||
- | throw new RuntimeError(name, | + | throw new RuntimeError(name, |
} | } | ||
} | } | ||
Line 459: | Line 456: | ||
for (int j = i + 1; j < names.size(); | for (int j = i + 1; j < names.size(); | ||
if (names.get(i).lexeme.equals(names.get(j).lexeme)) { | if (names.get(i).lexeme.equals(names.get(j).lexeme)) { | ||
- | throw new RuntimeError(names.get(i), | + | throw new RuntimeError(names.get(i), |
} | } | ||
} | } | ||
Line 474: | Line 471: | ||
</ | </ | ||
- | Add a call to '' | + | Add a '' |
<code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
public Void visitOpDefStmt(Stmt.OpDef stmt) { | public Void visitOpDefStmt(Stmt.OpDef stmt) { | ||
Line 480: | Line 477: | ||
for (Token param : stmt.params) { | for (Token param : stmt.params) { | ||
if (param.lexeme.equals(stmt.name.lexeme)) { | if (param.lexeme.equals(stmt.name.lexeme)) { | ||
- | throw new RuntimeError(param, | + | throw new RuntimeError(param, |
} | } | ||
} | } |