Skip to content
This repository has been archived by the owner on Sep 10, 2023. It is now read-only.

Commit

Permalink
fix: fix var can not be recover and redeclarat
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Mar 6, 2018
1 parent f046490 commit 04df8ef
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 68 deletions.
9 changes: 2 additions & 7 deletions src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as types from "babel-types";
import {ErrNotDefined, ErrNotSupport, ErrDuplicateDeclard} from "./error";
import {EvaluateFunc} from "./type";
import {Scope} from "./scope";
import {Var} from "./scope";
import {
_classCallCheck,
_createClass,
Expand Down Expand Up @@ -207,7 +206,6 @@ const evaluate_map = {
const func = evaluate_map.FunctionExpression(<any>node, scope);

const {name: func_name} = node.id;

// function declartion can be duplicate
scope.$var(func_name, func);
}
Expand Down Expand Up @@ -349,13 +347,10 @@ const evaluate_map = {
},
UpdateExpression(node: types.UpdateExpression, scope: Scope) {
const {prefix} = node;
let $var: {
$set(value: any): boolean;
$get(): any;
};
let $var;
if (types.isIdentifier(node.argument)) {
const {name} = node.argument;
$var = <Var>scope.$find(name);
$var = scope.$find(name);
if (!$var) throw `${name} 未定义`;
} else if (types.isMemberExpression(node.argument)) {
const argument = node.argument;
Expand Down
96 changes: 35 additions & 61 deletions src/scope.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,37 @@
import {ErrDuplicateDeclard} from "./error";

export type ScopeType = "function" | "loop" | "switch" | "block";

export type Kind = "const" | "var" | "let";

export interface Var {
$get(): any;
$set(value: any): boolean;
// $call($this: any, args: Array<any>): any
}

export class ScopeVar implements Var {
value: any;
kind: Kind;

constructor(kind: Kind, value: any) {
export class ScopeVar {
constructor(public kind: Kind, public value: any) {
this.value = value;
this.kind = kind;
}

$set(value: any): boolean {
if (this.value === "const") {
return false;
} else {
this.value = value;
return true;
}
this.value = value;
return true;
}

$get(): any {
return this.value;
}
}

export class PropVar implements Var {
object: any;
property: string;

constructor(object: any, property: string) {
this.object = object;
this.property = property;
}

$set(value: any) {
this.object[this.property] = value;
return true;
}
$get() {
return this.object[this.property];
}
$delete() {
delete this.object[this.property];
}
}

export class Scope {
private content: {[key: string]: Var};
private parent: Scope | null;
private content: {[key: string]: ScopeVar} = {};
// private parent: Scope | null;
private prefix: string = "@";

readonly type: ScopeType;

invasived: boolean;
public invasived: boolean = false;

constructor(type: ScopeType, parent?: Scope, label?: string) {
this.type = type;
this.parent = parent || null;
this.content = {};
this.invasived = false;
}
constructor(
public readonly type: ScopeType,
private parent: Scope | null = null,
label?: string
) {}

$all() {
const map = {};
Expand All @@ -78,49 +43,58 @@ export class Scope {
return map;
}

$find(raw_name: string): Var | null {
const name = this.prefix + raw_name;
$find(varName: string): ScopeVar | null {
const name = this.prefix + varName;
if (this.content.hasOwnProperty(name)) {
return this.content[name];
} else if (this.parent) {
return this.parent.$find(raw_name);
return this.parent.$find(varName);
} else {
return null;
}
}

$let(raw_name: string, value: any): boolean {
const name = this.prefix + raw_name;
$let(varName: string, value: any): boolean {
const name = this.prefix + varName;
const $var = this.content[name];
if (!$var) {
this.content[name] = new ScopeVar("let", value);
return true;
} else {
return false;
throw new ErrDuplicateDeclard(varName);
}
}

$const(raw_name: string, value: any): boolean {
const name = this.prefix + raw_name;
$const(varName: string, value: any): boolean {
const name = this.prefix + varName;
const $var = this.content[name];
if (!$var) {
this.content[name] = new ScopeVar("const", value);
return true;
} else {
return false;
throw new ErrDuplicateDeclard(varName);
}
}

$var(raw_name: string, value: any): boolean {
const name = this.prefix + raw_name;
$var(varName: string, value: any): boolean {
const name: string = this.prefix + varName;
let scope: Scope = this;

while (scope.parent !== null && scope.type !== "function") {
scope = scope.parent;
}

const $var = scope.content[name];
this.content[name] = new ScopeVar("var", value);
if ($var) {
if ($var.kind !== "var") {
// only recover var with var, not const and let
throw new ErrDuplicateDeclard(name);
} else {
this.content[name] = new ScopeVar("var", value);
}
} else {
this.content[name] = new ScopeVar("var", value);
}
return true;
}

Expand Down

0 comments on commit 04df8ef

Please sign in to comment.