Skip to content

Prettier formatting for CSEC machine component #73

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ node_modules
dist

src/ast
src/ec-evaluator
124 changes: 62 additions & 62 deletions src/ec-evaluator/components.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { UnannType } from "../ast/types/classes";
import { DECLARED_BUT_NOT_YET_ASSIGNED, GLOBAL_FRAME, OBJECT_CLASS } from "./constants";
import * as errors from "./errors";
import * as struct from "./structCreator";
import { UnannType } from '../ast/types/classes'
import { DECLARED_BUT_NOT_YET_ASSIGNED, GLOBAL_FRAME, OBJECT_CLASS } from './constants'
import * as errors from './errors'
import * as struct from './structCreator'
import {
Class,
Closure,
Expand All @@ -11,181 +11,181 @@ import {
StashItem,
Value,
VarValue,
Variable,
} from "./types";
import { Stack } from "./utils";
Variable
} from './types'
import { Stack } from './utils'

/**
* Components of CSE Machine.
*/
export class Control extends Stack<ControlItem> {};
export class Stash extends Stack<StashItem> {};
export class Control extends Stack<ControlItem> {}
export class Stash extends Stack<StashItem> {}

export class Environment {
private _global: EnvNode;
private _current: EnvNode;
private _objects: Object[] = [];
private _global: EnvNode
private _current: EnvNode
private _objects: Object[] = []

constructor() {
const node = new EnvNode(GLOBAL_FRAME);
this._global = node;
this._current = node;
const node = new EnvNode(GLOBAL_FRAME)
this._global = node
this._current = node
}

get global() {
return this._global;
return this._global
}

get current() {
return this._current;
return this._current
}

get objects() {
return this._objects;
return this._objects
}

set current(node: EnvNode) {
this._current = node;
this._current = node
}

extendEnv(fromEnv: EnvNode, name: string) {
// Create new environemnt.
const node = new EnvNode(name);
node.parent = fromEnv;
fromEnv.addChild(node);
const node = new EnvNode(name)
node.parent = fromEnv
fromEnv.addChild(node)

// Set current environment.
this._current = node;
this._current = node
}

createObj(c: Class): Object {
// Create new environment.
const node = new EnvNode(OBJECT_CLASS);
const node = new EnvNode(OBJECT_CLASS)

// Create new object.
const obj = struct.objStruct(node, c);
const obj = struct.objStruct(node, c)

// Add to objects arr.
this._objects.push(obj);
this._objects.push(obj)

// Set current environment.
this._current = node;
this._current = node

return obj;
return obj
}

restoreEnv(toEnv: EnvNode) {
this._current = toEnv;
this._current = toEnv
}

declareVariable(name: Name, type: UnannType) {
const variable = struct.varStruct(type, name, DECLARED_BUT_NOT_YET_ASSIGNED);
this._current.setVariable(name, variable);
const variable = struct.varStruct(type, name, DECLARED_BUT_NOT_YET_ASSIGNED)
this._current.setVariable(name, variable)
}

defineVariable(name: Name, type: UnannType, value: VarValue) {
const variable = struct.varStruct(type, name, value);
this._current.setVariable(name, variable);
const variable = struct.varStruct(type, name, value)
this._current.setVariable(name, variable)
}

getName(name: Name): Variable | Class {
return this._current.getName(name);
return this._current.getName(name)
}

getVariable(name: Name): Variable {
return this._current.getVariable(name);
return this._current.getVariable(name)
}

defineMtdOrCon(name: Name, method: Closure) {
this._current.setMtdOrCon(name, method);
this._current.setMtdOrCon(name, method)
}

defineClass(name: Name, c: Class) {
this._global.setClass(name, c);
this._global.setClass(name, c)
}

getClass(name: Name): Class {
return this._global.getClass(name);
return this._global.getClass(name)
}
};
}

// Frame with metadata, e.g., parent/children, name.
export class EnvNode {
private _frame: Frame = new Frame();
private _parent: EnvNode;
private _children: EnvNode[] = [];
private _frame: Frame = new Frame()
private _parent: EnvNode
private _children: EnvNode[] = []

constructor(readonly name: string) {}

get frame() {
return this._frame;
return this._frame
}

get parent(): EnvNode {
return this._parent;
return this._parent
}

set parent(parent: EnvNode) {
this._parent = parent;
this._parent = parent
}

get children() {
return this._children;
return this._children
}

addChild(child: EnvNode) {
this._children.push(child);
this._children.push(child)
}

setVariable(name: Name, value: Variable) {
if (this._frame.has(name)) {
throw new errors.VariableRedeclarationError(name);
throw new errors.VariableRedeclarationError(name)
}
this._frame.set(name, value);
this._frame.set(name, value)
}

getName(name: Name): Variable | Class {
if (this._frame.has(name)) {
return this._frame.get(name) as Variable | Class;
return this._frame.get(name) as Variable | Class
}
if (this._parent) {
return this._parent.getName(name);
return this._parent.getName(name)
}
throw new errors.UndeclaredNameError(name);
throw new errors.UndeclaredNameError(name)
}

getVariable(name: Name): Variable {
if (this._frame.has(name)) {
return this._frame.get(name) as Variable;
return this._frame.get(name) as Variable
}
if (this._parent) {
return this._parent.getVariable(name);
return this._parent.getVariable(name)
}
throw new errors.UndeclaredVariableError(name);
throw new errors.UndeclaredVariableError(name)
}

setMtdOrCon(name: Name, value: Closure) {
if (this._frame.has(name)) {
throw new errors.MtdOrConRedeclarationError(name);
throw new errors.MtdOrConRedeclarationError(name)
}
this._frame.set(name, value);
this._frame.set(name, value)
}

setClass(name: Name, value: Class) {
if (this._frame.has(name)) {
throw new errors.ClassRedeclarationError(name);
throw new errors.ClassRedeclarationError(name)
}
this._frame.set(name, value);
this._frame.set(name, value)
}

getClass(name: Name): Class {
if (this._frame.has(name)) {
return this._frame.get(name) as Class;
return this._frame.get(name) as Class
}
if (this._parent) {
return this._parent.getClass(name);
return this._parent.getClass(name)
}
throw new errors.UndeclaredClassError(name);
throw new errors.UndeclaredClassError(name)
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/ec-evaluator/constants.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { symStruct } from "./structCreator";
import { symStruct } from './structCreator'

export const STEP_LIMIT = 100000;
export const DECLARED_BUT_NOT_YET_ASSIGNED = symStruct("Used to implement block scope");
export const STEP_LIMIT = 100000
export const DECLARED_BUT_NOT_YET_ASSIGNED = symStruct('Used to implement block scope')

export const THIS_KEYWORD = "this";
export const SUPER_KEYWORD = "super";
export const THIS_KEYWORD = 'this'
export const SUPER_KEYWORD = 'super'

export const OBJECT_CLASS = "Object";
export const OBJECT_CLASS = 'Object'

export const GLOBAL_FRAME = "global";
export const BLOCK_FRAME = "block";
export const GLOBAL_FRAME = 'global'
export const BLOCK_FRAME = 'block'
Loading