Skip to content

Commit 15f1ac1

Browse files
JJtan2002hyizhak
andauthored
Fixed resolver and translator errors (#21)
* scoping rules in resolver implemented * Uncommented newline handling * Added display to environment for debugging * Remove test code from index.ts --------- Co-authored-by: yizhak <hyz0235@gmail.com>
1 parent 979f54d commit 15f1ac1

File tree

4 files changed

+56
-20
lines changed

4 files changed

+56
-20
lines changed

src/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,6 @@ export function parsePythonToEstreeAst(code: string,
150150
return translator.resolve(ast) as unknown as Program
151151
}
152152

153-
const text = `
154-
-1
155-
`
156-
console.dir(parsePythonToEstreeAst(text, 1, false));
157153
export * from './errors';
158154

159155
// import {ParserErrors, ResolverErrors, TokenizerErrors} from "./errors";

src/parser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ export class Parser {
141141
const startToken = this.peek();
142142
const statements: Stmt[] = [];
143143
while (!this.isAtEnd()) {
144-
// if (this.match(TokenType.NEWLINE)) {
145-
// continue;
146-
// }
144+
if (this.match(TokenType.NEWLINE)) {
145+
continue;
146+
}
147147
statements.push(this.stmt());
148148
}
149149
const endToken = this.peek();

src/resolver.ts

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,18 @@ export class Resolver implements StmtNS.Visitor<void>, ExprNS.Visitor<void> {
127127
ast: Stmt;
128128
// change the environment to be suite scope as in python
129129
environment: Environment | null;
130+
functionScope: Environment | null;
130131
constructor(source: string, ast: Stmt) {
131132
this.source = source;
132133
this.ast = ast;
133134
// The global environment
134135
this.environment = new Environment(source, null, new Map([
135136
["range", new Token(TokenType.NAME, "range", 0, 0, 0)],
136-
["display", new Token(TokenType.NAME, "display", 0, 0, 0)],
137+
["print", new Token(TokenType.NAME, "print", 0, 0, 0)],
137138
["stringify", new Token(TokenType.NAME, "stringify", 0, 0, 0)],
138139
// @TODO add all the source pre-declared names here
139140
]));
141+
this.functionScope = null;
140142
}
141143
resolve(stmt: Stmt[] | Stmt | Expr[] | Expr | null) {
142144
if (stmt === null) {
@@ -163,6 +165,27 @@ export class Resolver implements StmtNS.Visitor<void>, ExprNS.Visitor<void> {
163165
return res.length === 0 ? null : res;
164166
}
165167

168+
functionVarConstraint(identifier: Token): void {
169+
if (this.functionScope == null) {
170+
return;
171+
}
172+
let curr = this.environment;
173+
while (curr !== this.functionScope) {
174+
if (curr !== null && curr.names.has(identifier.lexeme)) {
175+
const token = curr.names.get(identifier.lexeme);
176+
if (token === undefined) {
177+
throw new Error("placeholder error")
178+
}
179+
throw new ResolverErrors.NameReassignmentError(identifier.line, identifier.col,
180+
this.source,
181+
identifier.indexInSource,
182+
identifier.indexInSource + identifier.lexeme.length,
183+
token);
184+
}
185+
curr = curr?.enclosing ?? null;
186+
}
187+
}
188+
166189
//// STATEMENTS
167190
visitFileInputStmt(stmt: StmtNS.FileInput): void {
168191
// Create a new environment.
@@ -175,8 +198,7 @@ export class Resolver implements StmtNS.Visitor<void>, ExprNS.Visitor<void> {
175198
}
176199

177200
visitIndentCreation(stmt: StmtNS.Indent): void {
178-
// Create a new environment.
179-
const oldEnv = this.environment;
201+
// Create a new environment
180202
this.environment = new Environment(this.source, this.environment, new Map());
181203
}
182204

@@ -190,19 +212,20 @@ export class Resolver implements StmtNS.Visitor<void>, ExprNS.Visitor<void> {
190212
visitFunctionDefStmt(stmt: StmtNS.FunctionDef) {
191213
this.environment?.declareName(stmt.name);
192214
this.environment?.functions.add(stmt.name.lexeme);
193-
// // Create a new environment.
215+
// Create a new environment.
194216
// const oldEnv = this.environment;
195-
// // Assign the parameters to the new environment.
196-
// const newEnv = new Map(
197-
// stmt.parameters.map(param => [param.lexeme, param])
198-
// );
199-
// this.environment = new Environment(this.source, this.environment, newEnv);
200-
const params = new Map(
217+
// Assign the parameters to the new environment.
218+
const newEnv = new Map(
201219
stmt.parameters.map(param => [param.lexeme, param])
202220
);
203-
if (this.environment !== null) {
204-
this.environment.names = params;
205-
}
221+
this.environment = new Environment(this.source, this.environment, newEnv);
222+
// const params = new Map(
223+
// stmt.parameters.map(param => [param.lexeme, param])
224+
// );
225+
// if (this.environment !== null) {
226+
// this.environment.names = params;
227+
// }
228+
this.functionScope = this.environment;
206229
this.resolve(stmt.body);
207230
// Grab identifiers from that new environment. That are NOT functions.
208231
// stmt.varDecls = this.varDeclNames(this.environment.names)
@@ -213,11 +236,13 @@ export class Resolver implements StmtNS.Visitor<void>, ExprNS.Visitor<void> {
213236
visitAnnAssignStmt(stmt: StmtNS.AnnAssign): void {
214237
this.resolve(stmt.ann);
215238
this.resolve(stmt.value);
239+
this.functionVarConstraint(stmt.name);
216240
this.environment?.declareName(stmt.name);
217241
}
218242

219243
visitAssignStmt(stmt: StmtNS.Assign): void {
220244
this.resolve(stmt.value);
245+
this.functionVarConstraint(stmt.name);
221246
this.environment?.declareName(stmt.name);
222247
}
223248

src/translator.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
WhileStatement
4242
} from "estree";
4343
import { TranslatorErrors } from "./errors";
44+
// import { isEmpty } from "lodash";
4445

4546
export interface EstreePosition {
4647
line: number;
@@ -193,6 +194,20 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
193194
};
194195
}
195196

197+
visitIndentCreation(stmt: StmtNS.Indent): EmptyStatement {
198+
return {
199+
type: 'EmptyStatement',
200+
loc: this.toEstreeLocation(stmt),
201+
};
202+
}
203+
204+
visitDedentCreation(stmt: StmtNS.Dedent): EmptyStatement {
205+
return {
206+
type: 'EmptyStatement',
207+
loc: this.toEstreeLocation(stmt),
208+
};
209+
}
210+
196211
visitFunctionDefStmt(stmt: StmtNS.FunctionDef): FunctionDeclaration {
197212
const newBody = this.resolveManyStmt(stmt.body);
198213
// if (stmt.varDecls !== null && stmt.varDecls.length > 0) {

0 commit comments

Comments
 (0)