From 4936178ecfbb43acef26e506158f22971f69ee40 Mon Sep 17 00:00:00 2001 From: Shen Yi Hong Date: Tue, 9 Apr 2024 22:10:57 +0800 Subject: [PATCH] refactor: simplify ece entrypoint (`evaluate`) --- src/go-slang/ece.ts | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/go-slang/ece.ts b/src/go-slang/ece.ts index c9a9f17f3..33c2ecbbb 100644 --- a/src/go-slang/ece.ts +++ b/src/go-slang/ece.ts @@ -9,11 +9,12 @@ import { PREDECLARED_FUNCTIONS, PREDECLARED_IDENTIFIERS } from './lib/predeclare import { Scheduler } from './scheduler' import { BuiltinOp, CallExpression, Instruction, NodeType, SourceFile } from './types' -function initMainGoRoutineCtx(program: SourceFile, slangContext: SlangContext): Context { +export function evaluate(program: SourceFile, slangContext: SlangContext): Value { + const scheduler = new Scheduler(slangContext) + const C = new Stack() const S = new Stack() const E = new Environment({ ...PREDECLARED_IDENTIFIERS }) - // `SourceFile` is the root node of the AST which has latest (monotonically increasing) uid of all AST nodes // Therefore, the next uid to be used to track AST nodes is the uid of SourceFile + 1 const A = new AstMap((program.uid as number) + 1) @@ -41,14 +42,7 @@ function initMainGoRoutineCtx(program: SourceFile, slangContext: SlangContext): } C.pushR(H.alloc(program), H.alloc(CALL_MAIN)) - return { C, S, E, B, H, A } as Context -} - -export function evaluate(program: SourceFile, slangContext: SlangContext): Value { - const scheduler = new Scheduler(slangContext) - const mainRoutineCtx = initMainGoRoutineCtx(program, slangContext) - - scheduler.spawn(mainRoutineCtx, true) + scheduler.spawn({ C, S, E, B, H, A } as Context, true) scheduler.run() return 'Program exited'