Skip to content

Commit

Permalink
use evaluated type instead
Browse files Browse the repository at this point in the history
with some minor testing code refactoring
  • Loading branch information
wcho21 committed Dec 21, 2023
1 parent b79225c commit b16c075
Show file tree
Hide file tree
Showing 6 changed files with 337 additions and 146 deletions.
31 changes: 17 additions & 14 deletions src/evaluator/environment/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,52 @@
import type { Evaluated } from "../evaluated";
import Environment from "./";

describe("set()", () => {
it("set name and value", () => {
const env = new Environment();
const varName = "foo";
const varValue = {} as Evaluated;

expect(() => env.set("foo", 42)).not.toThrow();
expect(() => env.set(varName, varValue)).not.toThrow();
});
});

describe("get()", () => {
it("get value after setting the value", () => {
const env = new Environment();
const varName = "foo";
const varValue = {} as Evaluated;

env.set("foo", 42);
const value = env.get("foo");
env.set(varName, varValue);

expect(value).toBe(42);
expect(env.get(varName)).toBe(varValue);
});

it("get null if not found", () => {
const env = new Environment();
const varNameNotSet = "foo";

const value = env.get("foo");

expect(value).toBe(null);
expect(env.get(varNameNotSet)).toBe(null);
});
});

describe("linked environment", () => {
it("set super environment and get via sub environment", () => {
const varNameInSuper = "foo";
const varValueInSuper = {} as Evaluated;

const superEnv = new Environment();
superEnv.set("foo", 42);
superEnv.set(varNameInSuper, varValueInSuper);
const subEnv = new Environment(superEnv);

const value = subEnv.get("foo");

expect(value).toBe(42);
expect(subEnv.get(varNameInSuper)).toBe(varValueInSuper);
});

it("get null if not found even in super environment", () => {
const superEnv = new Environment();
const subEnv = new Environment(superEnv);

const value = subEnv.get("foo");

expect(value).toBe(null);
const varNameSetNowhere = "foo";
expect(subEnv.get(varNameSetNowhere)).toBe(null);
});
});
12 changes: 6 additions & 6 deletions src/evaluator/environment/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { Evaluated } from "../evaluated";

export interface EnvironmentType {
get: (name: string) => unknown;
set: (name: string, value: any) => unknown;
get: (name: string) => Evaluated | null;
set: (name: string, value: Evaluated) => void;
}

export default class Environment implements EnvironmentType {
Expand All @@ -12,7 +14,7 @@ export default class Environment implements EnvironmentType {
this.table = new Map<string, any>;
}

get(name: string): unknown {
get(name: string): Evaluated | null {
// return if found in current environment
const fetched = this.table.get(name);
if (fetched !== undefined) {
Expand All @@ -26,9 +28,7 @@ export default class Environment implements EnvironmentType {
return this.superEnvironment.get(name);
}

set(name: string, value: any): unknown {
set(name: string, value: Evaluated): void {
this.table.set(name, value);

return value;
}
}
Loading

0 comments on commit b16c075

Please sign in to comment.