-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod.ts
214 lines (209 loc) · 6.25 KB
/
mod.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import type {
ASCIICharMap,
ASCIICodeMap,
U8DecMap,
U8IncMap,
} from "./_maps.ts";
type SkipCountedPair<
T extends string,
Open extends string,
Close extends string,
$Opened extends 0[] = [],
> = T extends `${infer $Before}${Open}${infer $After}`
? $Before extends `${string}${Close}${infer $After2}`
? $Opened extends [0, ...infer $Rest] ? SkipCountedPair<
`${$After2}${Open}${$After}`,
Open,
Close,
$Rest extends 0[] ? $Rest : never
>
: T
: SkipCountedPair<
$After,
Open,
Close,
[...$Opened, 0]
>
: T extends `${string}${Close}${infer $After}`
? $Opened extends [0, ...infer $Rest] ? SkipCountedPair<
$After,
Open,
Close,
$Rest extends 0[] ? $Rest : never
>
: never
: $Opened["length"] extends 0 ? T
: never;
type Locked<L extends number, T, $Draft extends T[] = []> =
//
$Draft["length"] extends L ? $Draft
: $Draft["length"] extends 999 ? { length: L } & T[]
: Locked<L, T, [...$Draft, T]>;
type MapCharCodesToASCII<T extends number[], $Draft extends string = ""> =
T extends [infer $F, ...infer $R]
? $F extends keyof ASCIICodeMap ? MapCharCodesToASCII<
$R extends number[] ? $R : never,
`${$Draft}${ASCIICodeMap[$F]}`
>
: MapCharCodesToASCII<$R extends number[] ? $R : never, `${$Draft}\0`>
: $Draft;
type BFOutputType = "buffer" | "ascii";
/**
* The type for running the Brainf*ck interpreter. The `program` is your
* specified Brainf*ck code. You can also specify the output encoding as either
* `ascii` or `buffer` depending on how you want your output to display.
*
* @template Init The settings for how to run the Brainf*ck program.
*/
export type BF<
Init extends {
program: string;
input?: string;
memory?: number[];
output?: number[];
outputType?: BFOutputType;
blocks?: string[];
},
T extends string = Init["program"],
$Input extends string = Init["input"] extends string ? Init["input"] : "",
$Memory extends number[] = Init["memory"] extends number[] ? Init["memory"]
: Locked<256, 0>,
$Blocks extends string[] = Init["blocks"] extends string[] ? Init["blocks"]
: [],
$Output extends number[] = Init["output"] extends number[] ? Init["output"]
: [],
$OutputType extends BFOutputType = Init["outputType"] extends BFOutputType
? Init["outputType"]
: "buffer",
> =
//
T extends `>${infer $Rest}` ? BF<
{
program: $Rest;
input: $Input;
memory: $Memory extends [...infer $R, infer $L] ? [$L, ...$R]
: $Memory;
output: $Output;
blocks: $Blocks;
outputType: $OutputType;
}
>
: T extends `<${infer $Rest}` ? BF<{
program: $Rest;
input: $Input;
memory: $Memory extends [infer $F, ...infer $R] ? [...$R, $F]
: $Memory;
output: $Output;
blocks: $Blocks;
outputType: $OutputType;
}>
: T extends `+${infer $Rest}`
? $Memory extends [infer $F, ...infer $R] ? BF<{
program: $Rest;
input: $Input;
memory: [
$F extends keyof U8IncMap ? U8IncMap[$F] : never,
...($R extends number[] ? $R : never),
];
output: $Output;
blocks: $Blocks;
outputType: $OutputType;
}>
: `Error: Failed to access memory pointer at slot. Did you provide an initial memory size?`
: T extends `-${infer $Rest}`
? $Memory extends [infer $Data, ...infer $R] ? BF<
{
program: $Rest;
input: $Input;
memory: $R extends number[]
? [$Data extends keyof U8DecMap ? U8DecMap[$Data] : never, ...$R]
: never;
output: $Output;
blocks: $Blocks;
outputType: $OutputType;
}
>
: `Error: Failed to access memory pointer at slot. Did you provide an initial memory size?`
: T extends `,${infer $Rest}`
? $Memory extends [number, ...infer $R]
? $Input extends `${infer $Ch}${infer $RestI}` ? BF<{
program: $Rest;
input: $RestI;
memory: [
$Ch extends keyof ASCIICharMap ? ASCIICharMap[$Ch] : 69,
...($R extends number[] ? $R : never),
];
output: $Output;
blocks: $Blocks;
outputType: $OutputType;
}>
: BF<{
program: $Rest;
input: $Input;
memory: $Memory extends [number, ...infer $R]
? [ASCIICodeMap[0], ...($R extends number[] ? $R : never)]
: never;
output: $Output;
blocks: $Blocks;
outputType: $OutputType;
}>
: "Error: Failed to access memory pointer at slot. Did you provide an initial memory size?"
: T extends `.${infer $Rest}` ? BF<{
program: $Rest;
input: $Input;
memory: Init["memory"];
output: [
...$Output,
$Memory extends [infer $F, ...number[]] ? ($F extends number ? $F
: never)
: never,
];
blocks: $Blocks;
outputType: $OutputType;
}>
: T extends `[${infer $Rest}` ? $Memory extends [0, ...number[]] ? BF<{
program: SkipCountedPair<T, "[", "]">;
input: $Input;
memory: $Memory;
output: $Output;
blocks: $Blocks;
outputType: $OutputType;
}>
: BF<{
program: $Rest;
input: $Input;
memory: $Memory;
output: $Output;
blocks: [$Rest, ...$Blocks];
outputType: $OutputType;
}>
: T extends `]${infer $Rest}`
? $Blocks["length"] extends 0 ? "Unexpected ']' without a matching '['."
: $Memory extends [0, ...number[]] ? BF<{
program: $Rest;
input: $Input;
memory: $Memory;
output: $Output;
blocks: $Blocks extends [string, ...infer $RestBlocks]
? $RestBlocks extends string[] ? $RestBlocks : never
: never;
outputType: $OutputType;
}>
: BF<{
program: $Blocks[0];
input: $Input;
memory: $Memory;
output: $Output;
blocks: $Blocks;
outputType: $OutputType;
}>
: T extends `${string}${infer $Rest}` ? BF<{
program: $Rest;
input: $Input;
memory: $Memory;
output: $Output;
blocks: $Blocks;
outputType: $OutputType;
}>
: "buffer" extends $OutputType ? $Output
: MapCharCodesToASCII<$Output>;