Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Hoist" functions in resolver #45

Merged
merged 1 commit into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { ResolverErrors } from "./errors";

const levenshtein = require('fast-levenshtein');

const RedefineableTokenSentinel = new Token(TokenType.AT, "", 0, 0, 0);

class Environment {
source: string;
// The parent of this environment
Expand Down Expand Up @@ -72,7 +74,7 @@ class Environment {
}
declareName(identifier: Token) {
const lookup = this.lookupNameCurrentEnv(identifier);
if (lookup !== undefined) {
if (lookup !== undefined && lookup !== RedefineableTokenSentinel) {
throw new ResolverErrors.NameReassignmentError(identifier.line, identifier.col,
this.source,
identifier.indexInSource,
Expand All @@ -82,6 +84,19 @@ class Environment {
}
this.names.set(identifier.lexeme, identifier);
}
// Same as declareName but allowed to re-declare later.
declarePlaceholderName(identifier: Token) {
const lookup = this.lookupNameCurrentEnv(identifier);
if (lookup !== undefined) {
throw new ResolverErrors.NameReassignmentError(identifier.line, identifier.col,
this.source,
identifier.indexInSource,
identifier.indexInSource + identifier.lexeme.length,
lookup);

}
this.names.set(identifier.lexeme, RedefineableTokenSentinel);
}
suggestNameCurrentEnv(identifier: Token): string | null {
const name = identifier.lexeme;
let minDistance = Infinity;
Expand Down Expand Up @@ -203,6 +218,13 @@ export class Resolver implements StmtNS.Visitor<void>, ExprNS.Visitor<void> {
return;
}
if (stmt instanceof Array) {
// Resolve all top-level functions first. Python allows functions declared after
// another function to be used in that function.
for (const st of stmt) {
if (st instanceof StmtNS.FunctionDef) {
this.environment?.declarePlaceholderName(st.name);
}
}
for (const st of stmt) {
st.accept(this);
}
Expand Down
13 changes: 12 additions & 1 deletion src/tests/regression.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {toEstreeAST} from "./utils";
import { toEstreeAST, toEstreeAstAndResolve } from "./utils";

describe('Regression tests for py-slang', () => {
test('Issue #2', () => {
Expand Down Expand Up @@ -38,4 +38,15 @@ add_one = lambda : False
`;
toEstreeAST(text);
})

test('Issue #35', () => {
const text = `
def f():
return g()

def g():
return 3
`;
toEstreeAstAndResolve(text);
})
})
1 change: 1 addition & 0 deletions src/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ export function toEstreeAST(text: string): Expression | Statement {

export function toEstreeAstAndResolve(text: string): Expression | Statement {
const ast = toPythonAst(text);
new Resolver(text, ast).resolve(ast);
return new Translator(text).resolve(ast);
}
Loading