Skip to content

Commit 0047b9a

Browse files
committed
reduced examples to show Anders
1 parent afb418c commit 0047b9a

File tree

4 files changed

+221
-4
lines changed

4 files changed

+221
-4
lines changed

Diff for: packages/conformance-tests/from-wat/memory-grow.wat

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
(func $entry (export "entry") (result i32 i32 i32)
55
memory.size ;; pushes 42 to the stack
66
i32.const 27 ;; we will grow by 27 pages
7-
memory.grow ;; grows and pushes 69 to the stack
7+
memory.grow ;; grows and pushes 42 to the stack (the previous value)
88
memory.size ;; pushes 69 to the stack
99
)
1010
)

Diff for: packages/playground/evaluate/start.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import type { funcs } from "../doom/doom";
2-
import type { entry } from "../doom/doom";
3-
export type NextResult = entry<[], true, 0>;
1+
import type { funcs } from "../../conformance-tests/from-c/conway";
2+
import type { entry } from "../../conformance-tests/from-c/conway";
3+
export type NextResult = entry<[0], true, 0>;
+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/** tells the machine to add a string to the current */
2+
type AddInstruction = {
3+
kind: 'add';
4+
value: string;
5+
}
6+
7+
type HandleAddInstruction<
8+
instruction extends AddInstruction,
9+
state extends ProgramState
10+
> = {
11+
instructions: state['instructions'];
12+
result: `${state['result']}${instruction['value']}`;
13+
}
14+
15+
/** tells the machine to uppercase the whole string */
16+
type UppercaseInstruction = {
17+
kind: 'uppercase';
18+
}
19+
20+
type HandleUppercaseInstruction<
21+
instruction extends UppercaseInstruction, // unused, but the instruction itself is always passed first to an instruction handler for consistency
22+
state extends ProgramState
23+
> = {
24+
instructions: state['instructions'];
25+
result: Uppercase<state['result']>;
26+
}
27+
28+
type Instruction = AddInstruction | UppercaseInstruction;
29+
30+
type selectInstruction<
31+
initialState extends ProgramState,
32+
remainingInstructions extends Instruction[],
33+
instruction extends Instruction,
34+
state extends ProgramState = {
35+
instructions: remainingInstructions;
36+
result: initialState['result'];
37+
}
38+
> =
39+
instruction extends AddInstruction
40+
? HandleAddInstruction<instruction, state>
41+
42+
: instruction extends UppercaseInstruction
43+
? HandleUppercaseInstruction<instruction, state>
44+
45+
: never
46+
47+
48+
type ProgramState = {
49+
instructions: Instruction[];
50+
result: string;
51+
}
52+
53+
type executeInstruction<
54+
state extends ProgramState
55+
> =
56+
state['instructions'] extends [
57+
infer instruction extends Instruction,
58+
...infer remainingInstructions extends Instruction[]
59+
]
60+
61+
? executeInstruction<
62+
selectInstruction<
63+
state,
64+
remainingInstructions,
65+
instruction
66+
>
67+
>
68+
69+
: state['result'];
70+
71+
type Bootstrap = {
72+
instructions: [
73+
{
74+
kind: 'add',
75+
value: 'hello'
76+
},
77+
{
78+
kind: 'add',
79+
value: ' world'
80+
},
81+
{
82+
kind: 'uppercase'
83+
},
84+
{
85+
kind: 'add',
86+
value: '!'
87+
},
88+
];
89+
result: '';
90+
}
91+
92+
type Execution = executeInstruction<Bootstrap>;
93+
// ^?

Diff for: packages/playground/toy-examples/1-with-stopping.ts

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/** tells the machine to add a string to the current */
2+
export type AddInstruction = {
3+
kind: 'add';
4+
value: string;
5+
}
6+
7+
export type HandleAddInstruction<
8+
instruction extends AddInstruction,
9+
state extends ProgramState
10+
> = {
11+
count: state['count'];
12+
instructions: state['instructions'];
13+
result: `${state['result']}${instruction['value']}`;
14+
}
15+
16+
/** tells the machine to uppercase the whole string */
17+
export type UppercaseInstruction = {
18+
kind: 'uppercase';
19+
}
20+
21+
export type HandleUppercaseInstruction<
22+
instruction extends UppercaseInstruction, // unused, but the instruction itself is always passed first to an instruction handler for consistency
23+
state extends ProgramState
24+
> = {
25+
count: state['count'];
26+
instructions: state['instructions'];
27+
result: Uppercase<state['result']>;
28+
}
29+
30+
export type Instruction = AddInstruction | UppercaseInstruction;
31+
32+
export type selectInstruction<
33+
initialState extends ProgramState,
34+
remainingInstructions extends Instruction[],
35+
instruction extends Instruction,
36+
state extends ProgramState = {
37+
count: [...initialState['count'], 1];
38+
instructions: remainingInstructions;
39+
result: initialState['result'];
40+
}
41+
> =
42+
instruction extends AddInstruction
43+
? HandleAddInstruction<instruction, state>
44+
45+
: instruction extends UppercaseInstruction
46+
? HandleUppercaseInstruction<instruction, state>
47+
48+
: never
49+
50+
51+
export type ProgramState = {
52+
count: 1[];
53+
instructions: Instruction[];
54+
result: string;
55+
}
56+
57+
export type executeInstruction<
58+
state extends ProgramState,
59+
stopAt extends number = 1000,
60+
> =
61+
state['instructions'] extends [
62+
infer instruction extends Instruction,
63+
...infer remainingInstructions extends Instruction[]
64+
]
65+
66+
? stopAt extends state['count']['length']
67+
? state
68+
: executeInstruction<
69+
selectInstruction<
70+
state,
71+
remainingInstructions,
72+
instruction
73+
>,
74+
stopAt
75+
>
76+
77+
: state['result'];
78+
79+
80+
export type Bootstrap = {
81+
count: [];
82+
instructions: [
83+
{
84+
kind: 'add',
85+
value: 'hello'
86+
},
87+
{
88+
kind: 'add',
89+
value: ' world'
90+
},
91+
{
92+
kind: 'uppercase'
93+
},
94+
{
95+
kind: 'add',
96+
value: '!'
97+
},
98+
];
99+
result: '';
100+
}
101+
102+
// with no explicit `stopAt`, the program will run until it's done
103+
export type Execution = executeInstruction<Bootstrap>;
104+
// ^?
105+
106+
export type Execution0 = executeInstruction<Bootstrap, 0>;
107+
export type Execution0Result = Execution0['result'];
108+
// ^?
109+
110+
export type Execution1 = executeInstruction<Bootstrap, 1>;
111+
export type Execution1Result = Execution1['result']
112+
// ^?
113+
114+
export type Execution2 = executeInstruction<Bootstrap, 2>;
115+
export type Execution2Result = Execution2['result']
116+
// ^?
117+
118+
export type Execution3 = executeInstruction<Bootstrap, 3>;
119+
export type Execution3Result = Execution3['result']
120+
// ^?
121+
122+
export type Execution4 = executeInstruction<Bootstrap, 4>;
123+
export type Execution4Result = Execution4
124+
// ^?

0 commit comments

Comments
 (0)