Skip to content

Commit

Permalink
Prevent multiple assignments in the same function
Browse files Browse the repository at this point in the history
  • Loading branch information
hyizhak committed Mar 18, 2024
1 parent 20932b9 commit c8f4986
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
18 changes: 18 additions & 0 deletions src/ast-types.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ export class Parser {
const startToken = this.peek();
const statements: Stmt[] = [];
while (!this.isAtEnd()) {
if (this.match(TokenType.NEWLINE)) {
continue;
}
// if (this.match(TokenType.NEWLINE)) {
// continue;
// }
statements.push(this.stmt());
}
const endToken = this.peek();
Expand All @@ -154,7 +154,7 @@ export class Parser {
if (this.check(TokenType.DEF, TokenType.FOR, TokenType.IF, TokenType.WHILE)) {
return this.compound_stmt();
} else if (this.check(TokenType.NAME, ...PSEUD_NAMES, TokenType.NUMBER,
TokenType.PASS, TokenType.BREAK, TokenType.CONTINUE, TokenType.MINUS, TokenType.PLUS,
TokenType.PASS, TokenType.BREAK, TokenType.CONTINUE, TokenType.MINUS, TokenType.PLUS, TokenType.INDENT, TokenType.DEDENT,
TokenType.RETURN, TokenType.FROM, TokenType.GLOBAL, TokenType.NONLOCAL,
TokenType.ASSERT, TokenType.LPAR, TokenType.STRING, TokenType.BIGINT, ...SPECIAL_IDENTIFIER_TOKENS)) {
return this.simple_stmt();
Expand Down Expand Up @@ -239,6 +239,10 @@ export class Parser {
let res = null;
if (this.match(TokenType.NAME)) {
res = this.assign_stmt();
} else if (this.match(TokenType.INDENT)) {
res = new StmtNS.Indent(startToken, startToken);
} else if (this.match(TokenType.DEDENT)) {
res = new StmtNS.Dedent(startToken, startToken);
} else if (this.match(TokenType.PASS)) {
res = new StmtNS.Pass(startToken, startToken);
} else if (this.match(TokenType.BREAK)) {
Expand Down
32 changes: 26 additions & 6 deletions src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class Environment {
export class Resolver implements StmtNS.Visitor<void>, ExprNS.Visitor<void> {
source: string;
ast: Stmt;
// change the environment to be suite scope as in python
environment: Environment | null;
constructor(source: string, ast: Stmt) {
this.source = source;
Expand Down Expand Up @@ -173,21 +174,40 @@ export class Resolver implements StmtNS.Visitor<void>, ExprNS.Visitor<void> {
this.environment = oldEnv;
}

visitIndentCreation(stmt: StmtNS.Indent): void {
// Create a new environment.
const oldEnv = this.environment;
this.environment = new Environment(this.source, this.environment, new Map());
}

visitDedentCreation(stmt: StmtNS.Dedent): void {
// Switch to the previous environment.
if (this.environment?.enclosing !== undefined) {
this.environment = this.environment.enclosing;
}
}

visitFunctionDefStmt(stmt: StmtNS.FunctionDef) {
this.environment?.declareName(stmt.name);
this.environment?.functions.add(stmt.name.lexeme);
// Create a new environment.
const oldEnv = this.environment;
// Assign the parameters to the new environment.
const newEnv = new Map(
// // Create a new environment.
// const oldEnv = this.environment;
// // Assign the parameters to the new environment.
// const newEnv = new Map(
// stmt.parameters.map(param => [param.lexeme, param])
// );
// this.environment = new Environment(this.source, this.environment, newEnv);
const params = new Map(
stmt.parameters.map(param => [param.lexeme, param])
);
this.environment = new Environment(this.source, this.environment, newEnv);
if (this.environment !== null) {
this.environment.names = params;
}
this.resolve(stmt.body);
// Grab identifiers from that new environment. That are NOT functions.
// stmt.varDecls = this.varDeclNames(this.environment.names)
// Restore old environment
this.environment = oldEnv;
// this.environment = oldEnv;
}

visitAnnAssignStmt(stmt: StmtNS.AnnAssign): void {
Expand Down

0 comments on commit c8f4986

Please sign in to comment.