Last active
September 19, 2019 11:51
-
-
Save acutmore/a9e09be531f565ba3415200400a0ce31 to your computer and use it in GitHub Desktop.
logic in typescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ASHEMBLER | |
| type B = 1 | 0; | |
| // note: 'assert' interfaces function as unit tests | |
| interface assert { | |
| true: 1; | |
| false: 0; | |
| } | |
| interface assert { true: 0 } // expected error | |
| interface assert { false: 1 } // expected error | |
| interface assert { true: any } // expected error | |
| interface assert { false: any } // expected error | |
| interface assert { true: 1 } // good: no error | |
| interface assert { false: 0 } // good: no error | |
| type ADD_8< | |
| regA extends [B, B, B, B, B, B, B, B], | |
| regB extends [B, B, B, B, B, B, B, B], | |
| /* variables: */ | |
| a extends [B, B] = FULL_ADDER<0, regA[0], regB[0]>, | |
| b extends [B, B] = FULL_ADDER<a[1], regA[1], regB[1]>, | |
| c extends [B, B] = FULL_ADDER<b[1], regA[2], regB[2]>, | |
| d extends [B, B] = FULL_ADDER<c[1], regA[3], regB[3]>, | |
| e extends [B, B] = FULL_ADDER<d[1], regA[4], regB[4]>, | |
| f extends [B, B] = FULL_ADDER<e[1], regA[5], regB[5]>, | |
| g extends [B, B] = FULL_ADDER<f[1], regA[6], regB[6]>, | |
| h extends [B, B] = FULL_ADDER<g[1], regA[7], regB[7]> | |
| > = [a[0], b[0], c[0], d[0], e[0], f[0], g[0], h[0]]; | |
| type N_34 = [0, 1, 0, 0, 0, 1, 0, 0]; | |
| type N_101 = [1, 0, 1, 0, 0, 1, 1, 0]; | |
| type N_135 = [1, 1, 1, 0, 0, 0, 0, 1]; | |
| interface assert { true: equals<ADD_8<N_34, N_101>, /* expected: */ N_135> } | |
| type FULL_ADDER< | |
| carry extends B, | |
| a extends B, | |
| b extends B, | |
| /* variables: */ | |
| adder1 extends [B, B] = HALF_ADDER<a, b>, | |
| adder2 extends [B, B] = HALF_ADDER<carry, adder1[0]> | |
| > = [adder2[0], OR<adder2[1], adder1[1]>]; | |
| interface assert { true: equals<FULL_ADDER<0, 0, 0>, [0, 0]> } | |
| interface assert { true: equals<FULL_ADDER<1, 0, 0>, [1, 0]> } | |
| interface assert { true: equals<FULL_ADDER<0, 1, 0>, [1, 0]> } | |
| interface assert { true: equals<FULL_ADDER<0, 0, 1>, [1, 0]> } | |
| interface assert { true: equals<FULL_ADDER<1, 1, 0>, [0, 1]> } | |
| interface assert { true: equals<FULL_ADDER<1, 0, 1>, [0, 1]> } | |
| interface assert { true: equals<FULL_ADDER<0, 1, 1>, [0, 1]> } | |
| interface assert { true: equals<FULL_ADDER<1, 1, 1>, [1, 1]> } | |
| type HALF_ADDER<a extends B, b extends B> = [XOR<a, b>, AND<a, b>]; | |
| interface assert { true: equals<HALF_ADDER<0, 0>, [0, 0]> } | |
| interface assert { true: equals<HALF_ADDER<1, 0>, [1, 0]> } | |
| interface assert { true: equals<HALF_ADDER<0, 1>, [1, 0]> } | |
| interface assert { true: equals<HALF_ADDER<1, 1>, [0, 1]> } | |
| type equals<a extends B[], b extends B[]> = a extends b ? 1 : 0; | |
| interface assert { true: equals<[0, 1], [0, 1]> } | |
| interface assert { false: equals<[0, 1], [1, 1]> } | |
| type XOR<a extends B, b extends B> = AND<NAND[a][b], OR<a, b>>; | |
| interface assert { true: XOR<1, 0> } | |
| interface assert { true: XOR<0, 1> } | |
| interface assert { false: XOR<1, 1> } | |
| interface assert { false: XOR<0, 0> } | |
| type AND<a extends B, b extends B> = NOT[NAND[a][b]]; | |
| interface assert { true: AND<1, 1> } | |
| interface assert { false: AND<1, 0> } | |
| interface assert { false: AND<0, 1> } | |
| interface assert { false: AND<0, 0> } | |
| type OR<a extends B, b extends B> = NAND[NOT[a]][NOT[b]]; | |
| interface assert { true: OR<1, 1> } | |
| interface assert { true: OR<1, 0> } | |
| interface assert { true: OR<0, 1> } | |
| interface assert { false: OR<0, 0> } | |
| type NOT = { | |
| 1: 0; | |
| 0: 1; | |
| }; | |
| interface assert { true: NOT[0] } | |
| interface assert { false: NOT[1] } | |
| type NAND = { | |
| 1: { | |
| 1: 0; | |
| 0: 1; | |
| }; | |
| 0: { | |
| 1: 1; | |
| 0: 1; | |
| }; | |
| }; | |
| interface assert { true: NAND[0][0] } | |
| interface assert { true: NAND[1][0] } | |
| interface assert { true: NAND[0][1] } | |
| interface assert { false: NAND[1][1] } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment