Skip to content

Commit

Permalink
Evaluate Assignment
Browse files Browse the repository at this point in the history
- Rename Agenda to Control
- Simplify Environment
- Evaluate until target step
- Support Assignment
- Break down LocalVariableDeclarationStatement with VariableInitializer
- Return Promise<Result>
  • Loading branch information
xyliew25 committed Jan 29, 2024
1 parent 71e28ad commit a002131
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 308 deletions.
72 changes: 0 additions & 72 deletions src/ec-evaluator/createContext.ts

This file was deleted.

36 changes: 36 additions & 0 deletions src/ec-evaluator/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { parse } from "../ast/parser";
import { Control, Environment, evaluate, Stash } from "./interpreter";
import { Context, Error, Finished, Result } from "./types";

export const runECEvaluator = (
files: Partial<Record<string, string>>,
entrypointFilePath: string,
context: Context,
targetStep: number = Infinity,
): Promise<Result> => {
try {
const code = files[entrypointFilePath];
const compilationUnit = parse(code!);

context.control.push(compilationUnit!);
const value = evaluate(context, targetStep);

return new Promise((resolve, _) => {
resolve({ status: 'finished', context, value } as Finished);
});
} catch {
return new Promise((resolve, _) => {
resolve({ status: 'error' } as Error);
});
}
}

export const createContext = (): Context => ({
errors: [],

control: new Control(),
stash: new Stash(),
environment: new Environment(),

totalSteps: Infinity,
});
8 changes: 2 additions & 6 deletions src/ec-evaluator/instrCreator.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { Node } from "../ast/types/ast"
import { AssmtInstr, BinOpInstr, Instr, InstrType } from "./types"
import { Node } from "../ast/types/ast";
import { AssmtInstr, BinOpInstr, Instr, InstrType } from "./types";

export const assmtInstr = (
symbol: string,
constant: boolean,
declaration: boolean,
srcNode: Node
): AssmtInstr => ({
instrType: InstrType.ASSIGNMENT,
symbol,
constant,
declaration,
srcNode
})

Expand Down
Loading

0 comments on commit a002131

Please sign in to comment.