-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathprogram.ts
42 lines (38 loc) · 1.27 KB
/
program.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import type { Instruction, selectInstruction } from "./instructions/instructions"
import type { IHalt } from "./instructions/synthetic"
import type { State } from "./state"
import type { ProgramState } from "./types"
import type { evaluate } from "ts-type-math"
export type executeInstruction<
state extends ProgramState,
debugMode extends boolean = false,
stopAt extends number = number,
> =
state['instructions'] extends [
infer instruction extends Instruction,
...infer remainingInstructions extends Instruction[]
]
// `Halt` is a special instruction that tells the program to stop for debugging
? instruction extends IHalt
? state
: stopAt extends state['count']
? state
: executeInstruction<
selectInstruction<
State.GarbageCollection.collect<
State.Count.increment<state>
>,
remainingInstructions,
instruction
>,
debugMode,
stopAt
>
// program execution is complete. yay.
// this is the base case of the main loop's recursion
: debugMode extends true
? State.GarbageCollection.collect<
evaluate<state>, // can't finish, because reading from memory requires access to the whole thing
'force'
>
: State.Result.finish<state>