|
|
|
|
@ -4,7 +4,7 @@ import { Num, Zero, ge, ifz, pred, succ } from './num'; |
|
|
|
|
import { x00 } from './byte'; |
|
|
|
|
import { Char, fromChar as _c, dec as cdec, inc as cinc, eq, ifn } from './char'; |
|
|
|
|
import { Pair, first, fst, second, snd, uncurry } from './pair'; |
|
|
|
|
import { List, Nil, cons, head, repeat, set, singleton, tail, update } from './list'; |
|
|
|
|
import { List, cons, head, repeat, set, singleton, tail, update } from './list'; |
|
|
|
|
import { Empty, String, get, len, snoc } from './string'; |
|
|
|
|
|
|
|
|
|
type Program = Pair<List<Num>, String>; |
|
|
|
|
@ -57,8 +57,8 @@ const prev: L<(t: State) => State> |
|
|
|
|
const jump: L<(d: Num) => L<(s: State) => State>> |
|
|
|
|
= _(d => _(s => |
|
|
|
|
iif(command(_c('['))(s))(jump(succ(d))) |
|
|
|
|
(iif(command(_c(']'))(s))(ifz(pred(d))(cnst(s))(jump(pred(d)))) |
|
|
|
|
(jump(d)))(advance(s)))); |
|
|
|
|
(iif(command(_c(']'))(s))(ifz(pred(d))(cnst(s))(jump(pred(d)))) |
|
|
|
|
(jump(d)))(advance(s)))); |
|
|
|
|
|
|
|
|
|
const whlnz: L<(t: State) => State> |
|
|
|
|
= _(s => ifn(read(s))(jump(Zero))(save)(s)); |
|
|
|
|
@ -68,15 +68,15 @@ const endwhl: L<(t: State) => State> |
|
|
|
|
|
|
|
|
|
const exec: L<(s: State) => State> |
|
|
|
|
= _(s => iif<State>(done(s))(s)(exec(advance |
|
|
|
|
(iif(command(_c('>'))(s))(next) |
|
|
|
|
(iif(command(_c('<'))(s))(prev) |
|
|
|
|
(iif(command(_c('+'))(s))(inc) |
|
|
|
|
(iif(command(_c('-'))(s))(dec) |
|
|
|
|
(iif(command(_c('.'))(s))(output) |
|
|
|
|
(iif(command(_c(','))(s))(input) |
|
|
|
|
(iif(command(_c('['))(s))(whlnz) |
|
|
|
|
(iif(command(_c(']'))(s))(endwhl) |
|
|
|
|
(id))))))))(s))))); |
|
|
|
|
(iif(command(_c('>'))(s))(next) |
|
|
|
|
(iif(command(_c('<'))(s))(prev) |
|
|
|
|
(iif(command(_c('+'))(s))(inc) |
|
|
|
|
(iif(command(_c('-'))(s))(dec) |
|
|
|
|
(iif(command(_c('.'))(s))(output) |
|
|
|
|
(iif(command(_c(','))(s))(input) |
|
|
|
|
(iif(command(_c('['))(s))(whlnz) |
|
|
|
|
(iif(command(_c(']'))(s))(endwhl) |
|
|
|
|
(id))))))))(s))))); |
|
|
|
|
|
|
|
|
|
export const run: L<(p: String) => L<(i: String) => String>> |
|
|
|
|
= _(p => _(i => snd(snd(snd(exec(Pair(Pair(singleton(Zero))(p))(Pair(Pair(Zero)(repeat(x00)))(Pair(i)(Empty))))))))); |
|
|
|
|
|