Skip to content

Commit

Permalink
refactor: simplify ece entrypoint (evaluate)
Browse files Browse the repository at this point in the history
  • Loading branch information
shenyih0ng committed Apr 9, 2024
1 parent 31e533b commit 4936178
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions src/go-slang/ece.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Instruction | HeapAddress>()
const S = new Stack<any>()
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)
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit 4936178

Please sign in to comment.