-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from sjbarag/add-global-scope-assignments
Add global scope assignments
- Loading branch information
Showing
11 changed files
with
307 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Token, Literal as TokenLiteral } from "../Token"; | ||
import { Lexeme } from "../Lexeme"; | ||
import * as BrsError from "../Error"; | ||
|
||
export default class Environment { | ||
private values = new Map<string, TokenLiteral>(); | ||
|
||
public define(name: string, value: TokenLiteral): void { | ||
this.values.set(name, value); | ||
} | ||
|
||
public get(name: Token): TokenLiteral { | ||
if (this.values.has(name.text!)) { | ||
return this.values.get(name.text!); | ||
} | ||
|
||
throw BrsError.runtime(`Undefined variable ${name.text}`, name.line); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const BrsError = require("../../lib/Error"); | ||
const Expr = require("../../lib/parser/Expression"); | ||
const Stmt = require("../../lib/parser/Statement"); | ||
const { identifier, token } = require("../parser/ParserTests"); | ||
const { Lexeme } = require("../../lib/Lexeme"); | ||
const { Executioner } = require("../../lib/visitor/Executioner"); | ||
|
||
let executioner; | ||
|
||
describe("executioner", () => { | ||
beforeEach(() => { | ||
BrsError.reset(); | ||
executioner = new Executioner(); | ||
}); | ||
|
||
it("returns 'invalid' for assignments", () => { | ||
let ast = new Stmt.Assignment( | ||
identifier("foo"), | ||
new Expr.Literal(5) | ||
); | ||
|
||
let result = executioner.exec([ast]); | ||
expect(result).toEqual([undefined]); | ||
}); | ||
|
||
it("stores assigned values in global scope", () => { | ||
let ast = new Stmt.Assignment( | ||
identifier("bar"), | ||
new Expr.Literal(6) | ||
); | ||
executioner.exec([ast]); | ||
expect( | ||
executioner.environment.get( | ||
identifier("bar") | ||
) | ||
).toBe(6); | ||
}); | ||
|
||
it("retrieves variables from global scope", () => { | ||
let assign = new Stmt.Assignment( | ||
identifier("baz"), | ||
new Expr.Literal(7) | ||
); | ||
let retrieve = new Stmt.Expression( | ||
new Expr.Variable( | ||
identifier("baz") | ||
) | ||
); | ||
let results = executioner.exec([ assign, retrieve ]); | ||
expect(results).toEqual([undefined, 7]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.