Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: source-academy/py-slang
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: d5f42ea50dd9b1c62f0ad580f17dda8609bc2520
Choose a base ref
...
head repository: source-academy/py-slang
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 9a3eceb6b03b077b6c187b068f80b74136aa5c53
Choose a head ref
  • 4 commits
  • 5 files changed
  • 3 contributors

Commits on May 9, 2024

  1. Fixed indentStack (#43)

    JJtan2002 authored May 9, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    c9f94dc View commit details

Commits on May 12, 2024

  1. Bump @babel/traverse from 7.20.13 to 7.24.5 (#44)

    Bumps [@babel/traverse](https://github.com/babel/babel/tree/HEAD/packages/babel-traverse) from 7.20.13 to 7.24.5.
    - [Release notes](https://github.com/babel/babel/releases)
    - [Changelog](https://github.com/babel/babel/blob/main/CHANGELOG.md)
    - [Commits](https://github.com/babel/babel/commits/v7.24.5/packages/babel-traverse)
    
    ---
    updated-dependencies:
    - dependency-name: "@babel/traverse"
      dependency-type: indirect
    ...
    
    Signed-off-by: dependabot[bot] <[email protected]>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored May 12, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    8299d75 View commit details

Commits on Jun 1, 2024

  1. "Hoist" functions in resolver (#45)

    Closes #35
    Fidget-Spinner authored Jun 1, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    ab7af0c View commit details

Commits on Jul 14, 2024

  1. Bump braces from 3.0.2 to 3.0.3 (#46)

    Bumps [braces](https://github.com/micromatch/braces) from 3.0.2 to 3.0.3.
    - [Changelog](https://github.com/micromatch/braces/blob/master/CHANGELOG.md)
    - [Commits](micromatch/braces@3.0.2...3.0.3)
    
    ---
    updated-dependencies:
    - dependency-name: braces
      dependency-type: indirect
    ...
    
    Signed-off-by: dependabot[bot] <[email protected]>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Jul 14, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    9a3eceb View commit details
Showing with 225 additions and 164 deletions.
  1. +187 −161 package-lock.json
  2. +23 −1 src/resolver.ts
  3. +12 −1 src/tests/regression.test.ts
  4. +1 −0 src/tests/utils.ts
  5. +2 −1 src/tokenizer.ts
348 changes: 187 additions & 161 deletions package-lock.json

Large diffs are not rendered by default.

24 changes: 23 additions & 1 deletion src/resolver.ts
Original file line number Diff line number Diff line change
@@ -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
@@ -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,
@@ -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;
@@ -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);
}
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', () => {
@@ -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
@@ -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);
}
3 changes: 2 additions & 1 deletion src/tokenizer.ts
Original file line number Diff line number Diff line change
@@ -450,12 +450,13 @@ export class Tokenizer {
if (this.indentStack.length == 0) {
throw new TokenizerErrors.InconsistentIndentError(this.line, this.col, this.source, this.current);
}
const prev = this.indentStack.pop();
const prev = this.indentStack[this.indentStack.length - 1];
if (prev === undefined || prev === null) {
throw new TokenizerErrors.InconsistentIndentError(this.line, this.col, this.source, this.current);
}
const indents = Math.floor((prev - accLeadingWhiteSpace) / 4);
for (let i = 0; i < indents; ++i) {
this.indentStack.pop();
this.addToken(TokenType.DEDENT);
}
}