Skip to content

Instantly share code, notes, and snippets.

@DetachHead
Last active March 1, 2023 12:59
Show Gist options
  • Save DetachHead/863b9ae20222d6c28a8283178efb4b77 to your computer and use it in GitHub Desktop.
Save DetachHead/863b9ae20222d6c28a8283178efb4b77 to your computer and use it in GitHub Desktop.
compile-time a=b runtime written in typescript
// i made this to troll my friend
// https://store.steampowered.com/app/1720850/AB/
type Contains<T extends string, Substring extends string> = T extends `${string}${Substring}${string}` ? true : false
type ReplaceOne<T extends string, Find extends string, ReplaceWith extends string> =
T extends `${infer Start}${Find}${infer End}`
? `${Start}${ReplaceWith}${End}`
: T
interface Line {
left: string
right: string
return?: boolean
}
interface RecurseResult {
done: boolean
result: string
}
type Iterate<T extends string, Code extends Line[], OriginalString extends string> =
Code extends []
? { result: T, done: T extends OriginalString ? true : false }
: Code extends [infer CurrentLine extends Line, ...infer Rest extends Line[]]
? Contains<T, CurrentLine['left']> extends infer HasMatch
? CurrentLine['return'] extends true
? HasMatch extends true
? { result: CurrentLine['right'], done: true }
: Iterate<T, Rest, OriginalString>
: HasMatch extends true
? { result: ReplaceOne<T, CurrentLine['left'], CurrentLine['right']>, done: false }
: Iterate<T, Rest, OriginalString>
: Iterate<T, Rest, OriginalString>
: never
type Execute<T extends string, Code extends Line[]> =
Iterate<T, Code, T> extends infer Result extends RecurseResult
? Result['done'] extends true
? Result['result']
: Execute<Result['result'], Code>
: never
type SplitLines<T extends string> = T extends `${infer CurrentLine}\n${infer Rest}` ? [CurrentLine, ...SplitLines<Rest>] : [T]
type ReturnStatement<T extends string> = T extends `(return)${infer Result}` ? Result : never
type ParseLine<T extends string> = T extends `${infer Left}=${infer Right}`
? {
left: Left,
right: ReturnStatement<Right> extends never
? Right
: ReturnStatement<Right>
return: ReturnStatement<Right> extends never ? false : true
}
: never
type ParseLines<Lines extends string[]> = Lines extends [infer CurrentLine extends string, ...infer Rest extends string[]]
? [...(CurrentLine extends '' ? [] : [ParseLine<CurrentLine>]), ...ParseLines<Rest>]
: []
type ParseCode<T extends string> = ParseLines<SplitLines<T>>
type AB<Input extends string, Code extends string> = Execute<Input, ParseCode<Code>>
//tests
import '@detachhead/ts-helpers' // for ts playground
import {exactly} from '@detachhead/ts-helpers/dist/functions/misc'
type Level1Code = 'a=b'
exactly<'bbc', AB<'abc', Level1Code>>()
exactly<'bbbb', AB<'aabb', Level1Code>>
declare type Level2Code = `
ca=ac
cb=bc
ba=ab
`
exactly<'aabc', AB<'caba', Level2Code>>()
exactly<'aabbcc', AB<'abccba', Level2Code>>()
type Level3Code = `
cb=bc
ca=ac
ba=ab
aaa=a
aa=(return)false
bbb=b
bb=(return)false
ccc=c
cc=(return)false
=(return)true
`
exactly<'true', AB<'abc', Level3Code>>()
exactly<'false', AB<'abac', Level3Code>>()
exactly<'true', AB<'abaca', Level3Code>>()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment