diff --git a/src/bf.ts b/src/bf.ts new file mode 100644 index 0000000..8895c41 --- /dev/null +++ b/src/bf.ts @@ -0,0 +1,85 @@ +import { L, _, fromNative as __ } from './core'; +import { Bool, iif } from './bool'; +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, tail, update } from './list'; +import { Empty, String, get, len, snoc } from './string'; + +type Program = Pair, String>; + +type Tape = Pair>; + +type IO = Pair; + +type State = Pair>; + +const pcommand: L<(p: Program) => Char> + = uncurry._(_(is => get._(head._(is)))); + +const padvance: L<(p: Program) => Program> + = first._(update._(succ)._(Zero)); + +const pjump: L<(d: Num) => L<(p: Program) => Program>> + = _(d => _(p => + iif._(eq._(_c('['))._(pcommand._(p)))._(pjump._(succ._(d))._(padvance._(p))) + ._(iif._(eq._(_c(']'))._(pcommand._(p)))._(ifz._(pred._(d))._(p)._(pjump._(pred._(d))._(padvance._(p)))) + ._(pjump._(d)._(padvance._(p)))))); + +const done: L<(s: State) => Bool> + = _(s => uncurry._(_(is => _(cs => ge._(head._(is))._(len._(cs)))))._(fst._(s))); + +const command: L<(s: State) => Char> + = _(s => pcommand._(fst._(s))); + +const advance: L<(s: State) => State> + = first._(padvance); + +const jump: L<(s: State) => State> + = first._(pjump._(Zero)); + +const save: L<(s: State) => State> + = first._(first._(_(is => cons._(head._(is))._(is)))); + +const restore: L<(s: State) => State> + = first._(first._(tail)); + +const reset: L<(s: State) => State> + = first._(first._(_(is => cons._(head._(is))._(tail._(tail._(is)))))); + +const inc: L<(t: State) => State> + = second._(first._(_(t => second._(update._(cinc)._(fst._(t)))._(t)))); + +const dec: L<(t: State) => State> + = second._(first._(_(t => second._(update._(cdec)._(fst._(t)))._(t)))); + +const read: L<(s: State) => Char> + = _(s => uncurry._(get)._(fst._(snd._(s)))); + +const input: L<(s: State) => State> + = second._(uncurry._(_(t => _(io => Pair._(second._(set._(head._(fst._(io)))._(fst._(t)))._(t))._(first._(tail)._(io)))))); + +const output: L<(s: State) => State> + = second._(uncurry._(_(t => _(io => Pair._(t)._(second._(_(os => snoc._(os)._(uncurry._(get)._(t))))._(io)))))); + +const next: L<(t: State) => State> + = second._(first._(first._(succ))); + +const prev: L<(t: State) => State> + = second._(first._(first._(pred))); + +const exec: L<(s: State) => State> + = _(s => iif._(done._(s))._(s)._(exec._(advance + ._(iif._(eq._(_c('>'))._(command._(s)))._(next._(s)) + ._(iif._(eq._(_c('<'))._(command._(s)))._(prev._(s)) + ._(iif._(eq._(_c('+'))._(command._(s)))._(inc._(s)) + ._(iif._(eq._(_c('-'))._(command._(s)))._(dec._(s)) + ._(iif._(eq._(_c('.'))._(command._(s)))._(output._(s)) + ._(iif._(eq._(_c(','))._(command._(s)))._(input._(s)) + ._(iif._(eq._(_c('['))._(command._(s)))._(ifn._(read._(s))._(jump._(s))._(save._(s))) + ._(iif._(eq._(_c(']'))._(command._(s)))._(ifn._(read._(s))._(reset._(s))._(save._(restore._(s)))) + ._(s)))))))))))); + +export const run: L<(p: String) => L<(i: String) => String>> + = _(p => _(i => snd._(snd._(snd._(exec._(Pair._(Pair._(cons._(Zero)._(Nil))._(p))._(Pair._(Pair._(Zero)._(repeat._(x00)))._(Pair._(i)._(Empty))))))))); diff --git a/src/byte.ts b/src/byte.ts new file mode 100644 index 0000000..fbcebc9 --- /dev/null +++ b/src/byte.ts @@ -0,0 +1,34 @@ +import { L, _, id } from './core'; +import { iif } from './bool'; +import { Num, Succ, Zero, fromNumber as _n, eq } from './num'; + +export type Byte = Num; + +export const x00: Byte + = Zero; + +export const xFF: Num + = _n(255); + +export const Inc: L<(p: Byte) => Byte> + = _(p => iif._(eq._(xFF)._(p))._(x00)._(Succ._(p))); + +export { ifz, cmp, lt, le, eq, ge, gt, even, odd, div, mod, show, toNumber } from './num'; + +export const inc: L<(n: Byte) => Byte> + = Inc; + +export const dec: L<(n: Byte) => Byte> + = _(n => n._(xFF)._(id)); + +export const sum: L<(l: Byte) => L<(r: Byte) => Byte>> + = _(l => _(r => r._(l)._(_(pr => sum._(inc._(l))._(pr))))); + +export const sub: L<(l: Byte) => L<(r: Byte) => Byte>> + = _(l => _(r => r._(l)._(_(pr => sub._(dec._(l))._(pr))))); + +export const mul: L<(l: Byte) => L<(r: Byte) => Byte>> + = _(l => _(r => l._(x00)._(_(pl => sum._(r)._(mul._(pl)._(r)))))); + +export const fromNumber: (n: number) => Byte + = n => _n(n % 256); diff --git a/src/index.ts b/src/index.ts index e4c7cd7..59543ed 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,8 @@ export * from './core'; export * from './bool'; export * from './num'; -export * from './list'; +export * from './byte'; export * from './char'; +export * from './list'; export * from './pair'; +export * from './bf'; diff --git a/src/list.ts b/src/list.ts index c86f3c0..8e67651 100644 --- a/src/list.ts +++ b/src/list.ts @@ -31,6 +31,9 @@ export const append: L<(l: List) => L<(r: List) => List>> export const concat: L<(ls: List>) => List> = _(ls => ls._(Nil)._(_(x => _(xs => append._(x)._(concat._(xs)))))); +export const repeat: L<(v: T) => List> + = _(v => cons._(v)._(repeat._(v))); + export const head: L<(xs: List) => T> = _(xs => xs._(undef)._(_(x => _(_xs => x)))); diff --git a/test/bf.spec.ts b/test/bf.spec.ts new file mode 100644 index 0000000..ae77ce8 --- /dev/null +++ b/test/bf.spec.ts @@ -0,0 +1,48 @@ +import { describe, expect, it } from '@jest/globals'; +import { _ } from '../src/core'; +import { fromChar as _c } from '../src/char'; +import { toString as $s, fromString as _s } from '../src/string'; +import { run } from '../src/bf'; + +describe('BF', () => { + describe('run', () => { + it('handles output command', () => { + expect($s(run._(_s('.'))._(_s('')))).toBe(String.fromCharCode(0)); + }); + + it('handles input command', () => { + expect($s(run._(_s(',.'))._(_s('a')))).toBe('a'); + }); + + it('handles inc command', () => { + expect($s(run._(_s('+.'))._(_s('')))).toBe(String.fromCharCode(1)); + }); + + it('handles dec command', () => { + expect($s(run._(_s('-.'))._(_s('')))).toBe(String.fromCharCode(255)); + }); + + it('handles next command', () => { + expect($s(run._(_s('+++++>++.'))._(_s('')))).toBe(String.fromCharCode(2)); + }); + + it('handles prev command', () => { + expect($s(run._(_s('+++++>++<+++++.'))._(_s('')))).toBe(String.fromCharCode(10)); + }); + + it('skips loop block on zero', () => { + expect($s(run._(_s('[+++++].'))._(_s('')))).toBe(String.fromCharCode(0)); + }); + + it('repeats loop block while not zero', () => { + expect($s(run._(_s('+++++[-].'))._(_s('')))).toBe(String.fromCharCode(0)); + }); + + it('prints "Hello World!"', () => { + expect($s(run + ._(_s('++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.')) + ._(_s('')))) + .toBe('Hello World!\n'); + }); + }); +}); diff --git a/test/byte.spec.ts b/test/byte.spec.ts new file mode 100644 index 0000000..cf74475 --- /dev/null +++ b/test/byte.spec.ts @@ -0,0 +1,475 @@ +import { describe, expect, it } from '@jest/globals'; +import { undef } from '../src/core'; +import { toNOrd as $o, NOrd } from '../src/ord'; +import { toBoolean as $b, fromBoolean as _b } from '../src/bool'; +import { toNumber as $n, Inc, fromNumber as _n, cmp, dec, div, eq, ge, gt, ifz, inc, le, lt, mod, mul, show, sub, sum, x00 } from '../src/byte'; +import { toString as $s } from '../src/string'; + +describe('Byte', () => { + describe('toNumber', () => { + it('converts 0', () => { + expect($n(x00)).toBe(0); + }); + + it('converts 1', () => { + expect($n(Inc._(x00))).toBe(1); + }); + + it('converts 2', () => { + expect($n(Inc._(Inc._(x00)))).toBe(2); + }); + }); + + describe('fromNumber', () => { + it('converts 0', () => { + expect($n(_n(0))).toBe(0); + }); + + it('converts 1', () => { + expect($n(_n(1))).toBe(1); + }); + + it('converts 2', () => { + expect($n(_n(2))).toBe(2); + }); + + it('converts 300', () => { + expect($n(_n(300))).toBe(44); + }); + }); + + describe('ifz', () => { + it('returns zero branch', () => { + expect($b(ifz._(_n(0))._(_b(true))._(_b(false)))).toBe(true); + }); + + it('returns non-zero branch', () => { + expect($b(ifz._(_n(1))._(_b(true))._(_b(false)))).toBe(false); + }); + + it('returns non-zero branch', () => { + expect($b(ifz._(_n(10))._(_b(true))._(_b(false)))).toBe(false); + }); + + it('skips other branch', () => { + expect($b(ifz._(_n(0))._(_b(true))._(undef))).toBe(true); + expect($b(ifz._(_n(1))._(undef)._(_b(false)))).toBe(false); + }); + }); + + describe('inc', () => { + it('inc 0 is 1', () => { + expect($n(inc._(_n(0)))).toBe(1); + }); + + it('inc 1 is 2', () => { + expect($n(inc._(_n(1)))).toBe(2); + }); + + it('inc 10 is 11', () => { + expect($n(inc._(_n(10)))).toBe(11); + }); + + it('inc 255 is 0', () => { + expect($n(inc._(_n(255)))).toBe(0); + }); + }); + + describe('dec', () => { + it('dec 0 is 255', () => { + expect($n(dec._(_n(0)))).toBe(255); + }); + + it('dec 1 is 0', () => { + expect($n(dec._(_n(1)))).toBe(0); + }); + + it('dec 10 is 9', () => { + expect($n(dec._(_n(10)))).toBe(9); + }); + + it('dec 255 is 254', () => { + expect($n(dec._(_n(255)))).toBe(254); + }); + }); + + describe('cmp', () => { + it('0 cmp 0 is EQ', () => { + expect($o(cmp._(_n(0))._(_n(0)))).toBe(NOrd.EQ); + }); + + it('0 cmp 1 is LT', () => { + expect($o(cmp._(_n(0))._(_n(1)))).toBe(NOrd.LT); + }); + + it('1 cmp 0 is GT', () => { + expect($o(cmp._(_n(1))._(_n(0)))).toBe(NOrd.GT); + }); + + it('3 cmp 3 is EQ', () => { + expect($o(cmp._(_n(3))._(_n(3)))).toBe(NOrd.EQ); + }); + + it('3 cmp 7 is LT', () => { + expect($o(cmp._(_n(3))._(_n(7)))).toBe(NOrd.LT); + }); + + it('7 cmp 3 is GT', () => { + expect($o(cmp._(_n(7))._(_n(3)))).toBe(NOrd.GT); + }); + + it('0 cmp 255 is LT', () => { + expect($o(cmp._(_n(0))._(_n(255)))).toBe(NOrd.LT); + }); + + it('255 cmp 0 is GT', () => { + expect($o(cmp._(_n(255))._(_n(0)))).toBe(NOrd.GT); + }); + + it('255 cmp 255 is EQ', () => { + expect($o(cmp._(_n(255))._(_n(255)))).toBe(NOrd.EQ); + }); + }); + + describe('lt', () => { + it('0 lt 0 is False', () => { + expect($b(lt._(_n(0))._(_n(0)))).toBe(false); + }); + + it('0 lt 1 is True', () => { + expect($b(lt._(_n(0))._(_n(1)))).toBe(true); + }); + + it('1 lt 0 is False', () => { + expect($b(lt._(_n(1))._(_n(0)))).toBe(false); + }); + + it('3 lt 3 is False', () => { + expect($b(lt._(_n(3))._(_n(3)))).toBe(false); + }); + + it('3 lt 7 is True', () => { + expect($b(lt._(_n(3))._(_n(7)))).toBe(true); + }); + + it('7 lt 3 is False', () => { + expect($b(lt._(_n(7))._(_n(3)))).toBe(false); + }); + + it('0 lt 255 is True', () => { + expect($b(lt._(_n(0))._(_n(255)))).toBe(true); + }); + + it('255 lt 0 is False', () => { + expect($b(lt._(_n(255))._(_n(0)))).toBe(false); + }); + + it('255 lt 255 is False', () => { + expect($b(lt._(_n(255))._(_n(255)))).toBe(false); + }); + }); + + describe('le', () => { + it('0 le 0 is True', () => { + expect($b(le._(_n(0))._(_n(0)))).toBe(true); + }); + + it('0 le 1 is True', () => { + expect($b(le._(_n(0))._(_n(1)))).toBe(true); + }); + + it('1 le 0 is False', () => { + expect($b(le._(_n(1))._(_n(0)))).toBe(false); + }); + + it('3 le 3 is True', () => { + expect($b(le._(_n(3))._(_n(3)))).toBe(true); + }); + + it('3 le 7 is True', () => { + expect($b(le._(_n(3))._(_n(7)))).toBe(true); + }); + + it('7 le 3 is False', () => { + expect($b(le._(_n(7))._(_n(3)))).toBe(false); + }); + + it('0 le 255 is True', () => { + expect($b(le._(_n(0))._(_n(255)))).toBe(true); + }); + + it('255 le 0 is False', () => { + expect($b(le._(_n(255))._(_n(0)))).toBe(false); + }); + + it('255 le 255 is True', () => { + expect($b(le._(_n(255))._(_n(255)))).toBe(true); + }); + + it('0 le skips second argument', () => { + expect($b(le._(_n(0))._(undef))).toBe(true); + }); + }); + + describe('eq', () => { + it('0 eq 0 is True', () => { + expect($b(eq._(_n(0))._(_n(0)))).toBe(true); + }); + + it('0 eq 1 is False', () => { + expect($b(eq._(_n(0))._(_n(1)))).toBe(false); + }); + + it('1 eq 0 is False', () => { + expect($b(eq._(_n(1))._(_n(0)))).toBe(false); + }); + + it('3 eq 3 is True', () => { + expect($b(eq._(_n(3))._(_n(3)))).toBe(true); + }); + + it('3 eq 7 is False', () => { + expect($b(eq._(_n(3))._(_n(7)))).toBe(false); + }); + + it('7 eq 3 is False', () => { + expect($b(eq._(_n(7))._(_n(3)))).toBe(false); + }); + + it('0 eq 255 is False', () => { + expect($b(eq._(_n(0))._(_n(255)))).toBe(false); + }); + + it('255 eq 0 is False', () => { + expect($b(eq._(_n(255))._(_n(0)))).toBe(false); + }); + + it('255 eq 255 is True', () => { + expect($b(eq._(_n(255))._(_n(255)))).toBe(true); + }); + }); + + describe('ge', () => { + it('0 ge 0 is True', () => { + expect($b(ge._(_n(0))._(_n(0)))).toBe(true); + }); + + it('0 ge 1 is False', () => { + expect($b(ge._(_n(0))._(_n(1)))).toBe(false); + }); + + it('1 ge 0 is True', () => { + expect($b(ge._(_n(1))._(_n(0)))).toBe(true); + }); + + it('3 ge 3 is True', () => { + expect($b(ge._(_n(3))._(_n(3)))).toBe(true); + }); + + it('3 ge 7 is False', () => { + expect($b(ge._(_n(3))._(_n(7)))).toBe(false); + }); + + it('7 ge 3 is True', () => { + expect($b(ge._(_n(7))._(_n(3)))).toBe(true); + }); + + it('0 ge 255 is False', () => { + expect($b(ge._(_n(0))._(_n(255)))).toBe(false); + }); + + it('255 ge 0 is True', () => { + expect($b(ge._(_n(255))._(_n(0)))).toBe(true); + }); + + it('255 ge 255 is True', () => { + expect($b(ge._(_n(255))._(_n(255)))).toBe(true); + }); + + it('ge 0 skips first argument', () => { + expect($b(ge._(undef)._(_n(0)))).toBe(true); + }); + }); + + describe('gt', () => { + it('0 gt 0 is False', () => { + expect($b(gt._(_n(0))._(_n(0)))).toBe(false); + }); + + it('0 gt 1 is False', () => { + expect($b(gt._(_n(0))._(_n(1)))).toBe(false); + }); + + it('1 gt 0 is True', () => { + expect($b(gt._(_n(1))._(_n(0)))).toBe(true); + }); + + it('3 gt 3 is False', () => { + expect($b(gt._(_n(3))._(_n(3)))).toBe(false); + }); + + it('3 gt 7 is False', () => { + expect($b(gt._(_n(3))._(_n(7)))).toBe(false); + }); + + it('7 gt 3 is True', () => { + expect($b(gt._(_n(7))._(_n(3)))).toBe(true); + }); + + it('0 gt 255 is False', () => { + expect($b(gt._(_n(0))._(_n(255)))).toBe(false); + }); + + it('255 gt 0 is True', () => { + expect($b(gt._(_n(255))._(_n(0)))).toBe(true); + }); + + it('255 gt 255 is False', () => { + expect($b(gt._(_n(255))._(_n(255)))).toBe(false); + }); + }); + + describe('sum', () => { + it('0 sum 0 is 0', () => { + expect($n(sum._(_n(0))._(_n(0)))).toBe(0); + }); + + it('0 sum 1 is 1', () => { + expect($n(sum._(_n(0))._(_n(1)))).toBe(1); + }); + + it('1 sum 0 is 1', () => { + expect($n(sum._(_n(1))._(_n(0)))).toBe(1); + }); + + it('3 sum 4 is 7', () => { + expect($n(sum._(_n(3))._(_n(4)))).toBe(7); + }); + + it('200 sum 200 is 144', () => { + expect($n(sum._(_n(200))._(_n(200)))).toBe(144); + }); + }); + + describe('sub', () => { + it('0 sub 0 is 0', () => { + expect($n(sub._(_n(0))._(_n(0)))).toBe(0); + }); + + it('0 sub 1 is 255', () => { + expect($n(sub._(_n(0))._(_n(1)))).toBe(255); + }); + + it('1 sub 0 is 1', () => { + expect($n(sub._(_n(1))._(_n(0)))).toBe(1); + }); + + it('7 sub 4 is 3', () => { + expect($n(sub._(_n(7))._(_n(4)))).toBe(3); + }); + + it('4 sub 7 is 253', () => { + expect($n(sub._(_n(4))._(_n(7)))).toBe(253); + }); + }); + + describe('mul', () => { + it('0 mul 0 is 0', () => { + expect($n(mul._(_n(0))._(_n(0)))).toBe(0); + }); + + it('0 mul 10 is 0', () => { + expect($n(mul._(_n(0))._(_n(10)))).toBe(0); + }); + + it('10 mul 0 is 0', () => { + expect($n(mul._(_n(1))._(_n(0)))).toBe(0); + }); + + it('1 mul 10 is 10', () => { + expect($n(mul._(_n(1))._(_n(10)))).toBe(10); + }); + + it('10 mul 1 is 10', () => { + expect($n(mul._(_n(10))._(_n(1)))).toBe(10); + }); + + it('3 mul 4 is 12', () => { + expect($n(mul._(_n(3))._(_n(4)))).toBe(12); + }); + + it('100 mul 100 is 12', () => { + expect($n(mul._(_n(3))._(_n(4)))).toBe(12); + }); + + it('0 mul ignores second argument', () => { + expect($n(mul._(_n(0))._(undef))).toBe(0); + }); + }); + + describe('div', () => { + it('0 div 10 is 0', () => { + expect($n(div._(_n(0))._(_n(10)))).toBe(0); + }); + + it('1 div 10 is 0', () => { + expect($n(div._(_n(1))._(_n(10)))).toBe(0); + }); + + it('10 div 1 is 10', () => { + expect($n(div._(_n(10))._(_n(1)))).toBe(10); + }); + + it('10 div 5 is 2', () => { + expect($n(div._(_n(10))._(_n(5)))).toBe(2); + }); + + it('10 div 3 is 3', () => { + expect($n(div._(_n(10))._(_n(3)))).toBe(3); + }); + + it.failing('0 div ignores second argument', () => { + expect($n(div._(_n(0))._(undef))).toBe(0); + }); + }); + + describe('mod', () => { + it('0 mod 10 is 0', () => { + expect($n(mod._(_n(0))._(_n(10)))).toBe(0); + }); + + it('1 mod 10 is 1', () => { + expect($n(mod._(_n(1))._(_n(10)))).toBe(1); + }); + + it('10 mod 1 is 0', () => { + expect($n(mod._(_n(10))._(_n(1)))).toBe(0); + }); + + it('10 mod 5 is 0', () => { + expect($n(mod._(_n(10))._(_n(5)))).toBe(0); + }); + + it('10 mod 3 is 1', () => { + expect($n(mod._(_n(10))._(_n(3)))).toBe(1); + }); + + it.failing('0 mod ignores second argument', () => { + expect($n(mod._(_n(0))._(undef))).toBe(0); + }); + }); + + describe('show', () => { + it('show 0 is "0"', () => { + expect($s(show._(_n(0)))).toBe('0'); + }); + + it('show 1 is "1"', () => { + expect($s(show._(_n(1)))).toBe('1'); + }); + + it('show 255 is "255"', () => { + expect($s(show._(_n(255)))).toBe('255'); + }); + }); +}); diff --git a/test/list.spec.ts b/test/list.spec.ts index 36ff778..ec9a982 100644 --- a/test/list.spec.ts +++ b/test/list.spec.ts @@ -154,6 +154,15 @@ describe('List', () => { }); }); + // describe('repeat', () => { + // it('repeat 0 is [0, 0, 0, ...]', () => { + // expect($na( + // repeat._(_n(0))._>(_((x0: Num) => _((x1s: List) => + // x1s._>(_((x1: Num) => _((x2s: List) => + // x2s._>(_((x2: Num) => _((_xrs: List): List => cons._(x0)._(cons._(x1)._(cons._(x2)._(Nil)))))))))))))).toEqual([0, 0, 0]); + // }); + // }); + describe('head', () => { it('returns nothing from Nil', () => { expect(() => $$(head._(Nil))).toThrow(); diff --git a/tsconfig.json b/tsconfig.json index f281384..1555b2d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,8 +13,6 @@ "noImplicitOverride": true, "noImplicitReturns": true, "noImplicitThis": true, - "noUnusedLocals": true, - "noUnusedParameters": true, "strictNullChecks": true, "strictPropertyInitialization": true, "declaration": false,