Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| creating:operators [2025/06/06 17:39] – Add variable shadowing error checking ahelwer | creating:operators [2025/06/19 14:50] (current) – ALL_MAP_TO interpretation splits out Object value = 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 300: | Line 297: | ||
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| defineAst(outputDir, | defineAst(outputDir, | ||
| - | " | + | " |
| " | " | ||
| )); | )); | ||
| Line 307: | Line 304: | ||
| We can now parse operators with parameters! | We can now parse operators with parameters! | ||
| On to functions. | On to functions. | ||
| - | This requires defining a new expression type in the '' | + | This requires defining a new expression type in the '' |
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| defineAst(outputDir, | defineAst(outputDir, | ||
| 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, |
| } | } | ||
| } | } | ||
| Line 572: | Line 569: | ||
| One important thing to note is that unlike in '' | One important thing to note is that unlike in '' | ||
| <code haskell> | <code haskell> | ||
| - | op(x) == \A y \in 0 .. 2 : y < x | + | op(x) == \A y \in 0 .. 2 : y < x |
| </ | </ | ||
| Here's how we implement the '' | Here's how we implement the '' | ||
| Line 587: | Line 584: | ||
| </ | </ | ||
| Finally, here's how we implement the '' | Finally, here's how we implement the '' | ||
| - | <code java [highlight_lines_extra=" | + | <code java [highlight_lines_extra=" |
| case ALL_MAP_TO: { | case ALL_MAP_TO: { | ||
| Token param = expr.params.get(0); | Token param = expr.params.get(0); | ||
| Map< | Map< | ||
| for (Environment binding : bindings) { | for (Environment binding : bindings) { | ||
| - | function.put(binding.get(param), | + | |
| + | | ||
| } | } | ||
| return function; | return function; | ||