Skip to content

Commit

Permalink
Move specialised FlatClosure classes into their only user, 'interpreter'
Browse files Browse the repository at this point in the history
  • Loading branch information
rrthomas committed Aug 6, 2024
1 parent 3f6a8de commit d2e618a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
16 changes: 2 additions & 14 deletions src/ark/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
import {Interval} from 'ohm-js'

import {
ArkCallable, ArkClosure, ArkNull, ArkRef, ArkVal, ArkValRef,
ArkCallable, ArkNull, ArkVal,
} from './data.js'
import {type ArkInst} from './flatten.js'
import {type ArkState, callFlat} from './interpreter.js'
import {type ArkState} from './interpreter.js'

export class ArkDebugInfo {
uid: number | undefined
Expand Down Expand Up @@ -69,17 +68,6 @@ export class ArkReturn extends ArkExp {

export class ArkYield extends ArkReturn {}

export class ArkFlatClosure extends ArkClosure {
constructor(params: string[], captures: ArkRef[], public body: ArkInst) {
super(params, captures)
}

async call(locals: ArkValRef[]) {
return callFlat(this, locals)
}
}
export class ArkFlatGeneratorClosure extends ArkFlatClosure {}

export class ArkContinuation extends ArkCallable {
public done = false

Expand Down
18 changes: 14 additions & 4 deletions src/ark/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ import {
import {
ArkAbstractObjectBase, ArkBoolean, ArkList, ArkMap, ArkNull, ArkNullVal,
ArkObject, ArkOperation, ArkUndefined, ArkVal, NativeAsyncFn, NativeFn,
NativeOperation, ArkRef, ArkValRef, ArkCallable,
NativeOperation, ArkRef, ArkValRef, ArkCallable, ArkClosure,
} from './data.js'
import {
ArkCapture, ArkContinuation, ArkFlatClosure, ArkFlatGeneratorClosure,
ArkLocal, ArkNamedLoc,
ArkCapture, ArkContinuation, ArkLocal, ArkNamedLoc,
} from './code.js'
import {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down Expand Up @@ -86,6 +85,17 @@ export class ArkRuntimeError extends Error {
}
}

class ArkFlatClosure extends ArkClosure {
constructor(params: string[], captures: ArkRef[], public body: ArkInst) {
super(params, captures)
}

async call(locals: ArkValRef[]) {
return callFlat(this, locals)
}
}
class ArkFlatGeneratorClosure extends ArkFlatClosure {}

function evalRef(frame: ArkFrame, lexp: ArkNamedLoc): ArkRef {
if (lexp instanceof ArkLocal) {
return frame.locals[lexp.index]
Expand Down Expand Up @@ -403,7 +413,7 @@ export async function pushLets(ark: ArkState, boundVars: [string, ArkInst][]) {
return lets.length
}

export async function callFlat(callable: ArkFlatClosure, locals: ArkValRef[]): Promise<ArkVal> {
async function callFlat(callable: ArkFlatClosure, locals: ArkValRef[]): Promise<ArkVal> {
const ark = new ArkState(callable.body, new ArkFrame(locals, callable.captures))
return evalFlat(ark)
}

0 comments on commit d2e618a

Please sign in to comment.