Skip to content

Commit

Permalink
Redo Ark*Inst class hierarchy
Browse files Browse the repository at this point in the history
In some cases different instructions inherited from one another;
insert a common parent in such cases.
  • Loading branch information
rrthomas committed Jul 24, 2024
1 parent 902238b commit 55bcb47
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
20 changes: 12 additions & 8 deletions src/ark/flatten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class ArkIfBlockOpenInst extends ArkBlockOpenInst {
}
}

export class ArkFnBlockOpenInst extends ArkBlockOpenInst {
export class ArkCallableBlockOpenInst extends ArkBlockOpenInst {
constructor(
sourceLoc: Interval | undefined,
public params: string[],
Expand All @@ -84,7 +84,8 @@ export class ArkFnBlockOpenInst extends ArkBlockOpenInst {
super(sourceLoc)
}
}
export class ArkGeneratorBlockOpenInst extends ArkFnBlockOpenInst {}
export class ArkFnBlockOpenInst extends ArkCallableBlockOpenInst {}
export class ArkGeneratorBlockOpenInst extends ArkCallableBlockOpenInst {}

export class ArkLetBlockOpenInst extends ArkBlockOpenInst {
constructor(sourceLoc: Interval | undefined, public vars: string[], public valIds: symbol[]) {
Expand Down Expand Up @@ -208,7 +209,7 @@ export class ArkBreakInst extends ArkInst {
}
}

export class ArkReturnInst extends ArkInst {
class ArkResultInst extends ArkInst {
constructor(
sourceLoc: Interval | undefined,
public argId: symbol,
Expand All @@ -217,7 +218,8 @@ export class ArkReturnInst extends ArkInst {
super(sourceLoc)
}
}
export class ArkYieldInst extends ArkReturnInst {}
export class ArkReturnInst extends ArkResultInst {}
export class ArkYieldInst extends ArkResultInst {}

export class ArkCallInst extends ArkInst {
constructor(
Expand All @@ -235,7 +237,7 @@ export class ArkSetInst extends ArkInst {
super(sourceLoc)
}
}
export class ArkSetLocalInst extends ArkSetInst {
export class ArkSetNamedLocInst extends ArkSetInst {
constructor(
sourceLoc: Interval | undefined,
lexpId: symbol,
Expand All @@ -245,7 +247,8 @@ export class ArkSetLocalInst extends ArkSetInst {
super(sourceLoc, lexpId, valId)
}
}
export class ArkSetCaptureInst extends ArkSetLocalInst {}
export class ArkSetLocalInst extends ArkSetNamedLocInst {}
export class ArkSetCaptureInst extends ArkSetNamedLocInst {}

export class ArkSetPropertyInst extends ArkInst {
constructor(
Expand Down Expand Up @@ -276,12 +279,13 @@ export class ArkMapLiteralInst extends ArkInst {
}
}

export class ArkLocalInst extends ArkInst {
class ArkNamedLocInst extends ArkInst {
constructor(sourceLoc: Interval | undefined, public index: number, public name: string) {
super(sourceLoc)
}
}
export class ArkCaptureInst extends ArkLocalInst {}
export class ArkLocalInst extends ArkNamedLocInst {}
export class ArkCaptureInst extends ArkNamedLocInst {}

export class ArkPropertyInst extends ArkInst {
constructor(sourceLoc: Interval | undefined, public objId: symbol, public prop: string) {
Expand Down
24 changes: 16 additions & 8 deletions src/ark/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import {Interval} from 'ohm-js'

import {
ArkAwaitInst, ArkBlockCloseInst, ArkBlockOpenInst, ArkBreakInst, ArkCallInst,
ArkCaptureInst, ArkContinueInst, ArkElseBlockCloseInst, ArkElseBlockInst,
ArkAwaitInst, ArkBlockCloseInst, ArkBlockOpenInst, ArkBreakInst, ArkCallableBlockOpenInst,
ArkCallInst, ArkCaptureInst, ArkContinueInst, ArkElseBlockCloseInst, ArkElseBlockInst,
ArkFnBlockOpenInst, ArkGeneratorBlockOpenInst, ArkIfBlockOpenInst, ArkInst,
ArkLaunchBlockCloseInst, ArkLaunchBlockOpenInst, ArkLetBlockCloseInst, ArkLetBlockOpenInst,
ArkLetCopyInst, ArkListLiteralInst, ArkLiteralInst, ArkLocalInst, ArkLoopBlockCloseInst,
ArkLoopBlockOpenInst, ArkMapLiteralInst, ArkObjectLiteralInst, ArkPropertyInst, ArkReturnInst,
ArkSetCaptureInst, ArkSetLocalInst, ArkSetPropertyInst, ArkYieldInst,
ArkSetCaptureInst, ArkSetLocalInst, ArkSetNamedLocInst, ArkSetPropertyInst, ArkYieldInst,
} from './flatten.js'
import {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down Expand Up @@ -158,13 +158,19 @@ async function evalFlat(outerArk: ArkState): Promise<ArkVal> {
// The Promise becomes the result of the entire block.
mem.set(inst.matchingClose.id, result)
inst = inst.matchingClose.next
} else if (inst instanceof ArkFnBlockOpenInst) {
} else if (inst instanceof ArkCallableBlockOpenInst) {
const captures = []
for (const v of inst.capturedVars) {
captures.push(evalRef(ark.frame, v))
}
const Constructor = inst instanceof ArkGeneratorBlockOpenInst
? ArkGeneratorClosure : ArkClosure
let Constructor
if (inst instanceof ArkFnBlockOpenInst) {
Constructor = ArkClosure
} else if (inst instanceof ArkGeneratorBlockOpenInst) {
Constructor = ArkGeneratorClosure
} else {
throw new Error('invalid ArkCallableBlockOpenInst')
}
const result = new Constructor(inst.params, captures, inst.next!)
mem.set(inst.matchingClose.id, result)
inst = inst.matchingClose.next
Expand Down Expand Up @@ -272,13 +278,15 @@ async function evalFlat(outerArk: ArkState): Promise<ArkVal> {
} else {
throw new ArkRuntimeError(ark, 'Invalid call', inst.sourceLoc)
}
} else if (inst instanceof ArkSetLocalInst) {
} else if (inst instanceof ArkSetNamedLocInst) {
const result = mem.get(inst.valId)!
let ref: ArkRef
if (inst instanceof ArkSetCaptureInst) {
ref = ark.frame.captures[inst.lexpIndex]
} else {
} else if (inst instanceof ArkSetLocalInst) {
ref = ark.frame.locals[inst.lexpIndex]
} else {
throw new Error('invalid ArkSetNamedLocInst')
}
const oldVal = ref.get()
if (
Expand Down

0 comments on commit 55bcb47

Please sign in to comment.