Last active
September 19, 2019 11:51
-
-
Save acutmore/a9e09be531f565ba3415200400a0ce31 to your computer and use it in GitHub Desktop.
Revisions
-
acutmore revised this gist
Sep 19, 2019 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -18,13 +18,13 @@ interface assert { false: 0 } // good: no error type DIV_4< numerator extends BITS_4, divisor extends BITS_4, /* variables: */ count extends BITS_4 = N_0, remainder extends BITS_4 = numerator > = { // remainder < divisor 1: {count:count, remainder:remainder}, // remainder >= divisor 0: DIV_4<numerator, divisor, ADD_4<count, N_1>, SUB_4<remainder, divisor>> }[ COMP_4<remainder, divisor>['less'] extends 1 ? 1 : 0 @@ -161,7 +161,7 @@ interface assert { true: equals< type COMP_4< a extends BITS_4, b extends BITS_4, /* variables: */ c0 extends COMP_RESULT = COMP<a[0], b[0]>, c1 extends COMP_RESULT = COMP<a[1], b[1]>, c2 extends COMP_RESULT = COMP<a[2], b[2]>, @@ -188,7 +188,7 @@ interface assert { true: equals<COMP_4<N_11, N_3>, {less:0, eq:0, grt:1}> } type COMP_RESULT = {less: B, eq: B, grt: B}; type COMP<a extends B, b extends B, /* variables */ aLessThanB extends B = AND<NOT<b>, a>, aGreaterThanB extends B = AND<b, NOT<a>>, equal extends B = NOR<aLessThanB, aGreaterThanB> -
acutmore revised this gist
Sep 19, 2019 . 1 changed file with 89 additions and 79 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ // ASHEMBLER type B = 1 | 0; type BITS_4 = [B,B,B,B]; // note: 'assert' interfaces function as unit tests interface assert { @@ -23,16 +23,14 @@ type DIV_4< remainder extends BITS_4 = numerator > = { // remainder < divisor 1: {count:count, remainder:remainder}, // remainder >= dicisor 0: DIV_4<numerator, divisor, ADD_4<count, N_1>, SUB_4<remainder, divisor>> }[ COMP_4<remainder, divisor>['less'] extends 1 ? 1 : 0 ]; interface assert { true: equals<DIV_4<N_4, N_2>, {count:N_2, remainder:N_0}> } interface assert { true: equals<DIV_4<N_9, N_2>, {count:N_4, remainder: N_1}> } type MUL_4< regA extends BITS_4, @@ -52,22 +50,22 @@ type SUB_4< regA extends BITS_4, regB extends BITS_4, /* variables: */ a extends SUBTRACT_RESULT = FULL_SUBTRACTOR<0, regA[0], regB[0]>, b extends SUBTRACT_RESULT = FULL_SUBTRACTOR<a['borrow'], regA[1], regB[1]>, c extends SUBTRACT_RESULT = FULL_SUBTRACTOR<b['borrow'], regA[2], regB[2]>, d extends SUBTRACT_RESULT = FULL_SUBTRACTOR<c['borrow'], regA[3], regB[3]>, > = [a['diff'], b['diff'], c['diff'], d['diff']]; interface assert { true: equals<SUB_4<N_5, N_2>, N_3> } type ADD_4< regA extends BITS_4, regB extends BITS_4, /* variables: */ a extends ADDER_RESULT = FULL_ADDER<0, regA[0], regB[0]>, b extends ADDER_RESULT = FULL_ADDER<a['carry'], regA[1], regB[1]>, c extends ADDER_RESULT = FULL_ADDER<b['carry'], regA[2], regB[2]>, d extends ADDER_RESULT = FULL_ADDER<c['carry'], regA[3], regB[3]>, > = [a['sum'], b['sum'], c['sum'], d['sum']]; interface assert { true: equals<ADD_4<N_2, N_3>, N_5> } type N_0 = [0, 0, 0, 0]; @@ -92,49 +90,59 @@ type FULL_SUBTRACTOR< a extends B, b extends B, /* variables: */ subtractor1 extends SUBTRACT_RESULT = HALF_SUBTRACTOR<a, b>, subtractor2 extends SUBTRACT_RESULT = HALF_SUBTRACTOR<subtractor1['diff'], borrow>, > = { diff:subtractor2['diff'], borrow:OR<subtractor1['borrow'], subtractor2['borrow']> }; interface assert { true: equals<FULL_SUBTRACTOR<0, 0, 0>, {diff:0, borrow:0}> } interface assert { true: equals<FULL_SUBTRACTOR<1, 0, 0>, {diff:1, borrow:1}> } interface assert { true: equals<FULL_SUBTRACTOR<0, 1, 0>, {diff:1, borrow:0}> } interface assert { true: equals<FULL_SUBTRACTOR<0, 0, 1>, {diff:1, borrow:1}> } interface assert { true: equals<FULL_SUBTRACTOR<1, 1, 0>, {diff:0, borrow:0}> } interface assert { true: equals<FULL_SUBTRACTOR<1, 0, 1>, {diff:0, borrow:1}> } interface assert { true: equals<FULL_SUBTRACTOR<0, 1, 1>, {diff:0, borrow:0}> } interface assert { true: equals<FULL_SUBTRACTOR<1, 1, 1>, {diff:1, borrow:1}> } type FULL_ADDER< carry extends B, a extends B, b extends B, /* variables: */ adder1 extends ADDER_RESULT = HALF_ADDER<a, b>, adder2 extends ADDER_RESULT = HALF_ADDER<carry, adder1['sum']> > = { sum:adder2['sum'], carry:OR<adder2['carry'], adder1['carry']> }; interface assert { true: equals<FULL_ADDER<0, 0, 0>, {sum:0, carry:0}> } interface assert { true: equals<FULL_ADDER<1, 0, 0>, {sum:1, carry:0}> } interface assert { true: equals<FULL_ADDER<0, 1, 0>, {sum:1, carry:0}> } interface assert { true: equals<FULL_ADDER<0, 0, 1>, {sum:1, carry:0}> } interface assert { true: equals<FULL_ADDER<1, 1, 0>, {sum:0, carry:1}> } interface assert { true: equals<FULL_ADDER<1, 0, 1>, {sum:0, carry:1}> } interface assert { true: equals<FULL_ADDER<0, 1, 1>, {sum:0, carry:1}> } interface assert { true: equals<FULL_ADDER<1, 1, 1>, {sum:1, carry:1}> } type SUBTRACT_RESULT = {diff: B, borrow: B}; type ADDER_RESULT = {sum: B, carry: B}; type HALF_SUBTRACTOR<a extends B, b extends B> = { diff: XOR<a, b>, borrow: AND<NOT<a>, b> }; interface assert { true: equals<HALF_SUBTRACTOR<0, 0>, {diff:0, borrow:0}> } interface assert { true: equals<HALF_SUBTRACTOR<1, 0>, {diff:1, borrow:0}> } interface assert { true: equals<HALF_SUBTRACTOR<0, 1>, {diff:1, borrow:1}> } interface assert { true: equals<HALF_SUBTRACTOR<1, 1>, {diff:0, borrow:0}> } type HALF_ADDER<a extends B, b extends B> = {sum:XOR<a, b>, carry:AND<a, b>}; interface assert { true: equals<HALF_ADDER<0, 0>, {sum:0, carry:0}> } interface assert { true: equals<HALF_ADDER<1, 0>, {sum:1, carry:0}> } interface assert { true: equals<HALF_ADDER<0, 1>, {sum:1, carry:0}> } interface assert { true: equals<HALF_ADDER<1, 1>, {sum:0, carry:1}> } type BITWISE_AND<a extends BITS_4, b extends BITS_4> = [ AND<a[0], b[0]>, @@ -154,41 +162,43 @@ type COMP_4< a extends BITS_4, b extends BITS_4, /* parameters: */ c0 extends COMP_RESULT = COMP<a[0], b[0]>, c1 extends COMP_RESULT = COMP<a[1], b[1]>, c2 extends COMP_RESULT = COMP<a[2], b[2]>, c3 extends COMP_RESULT = COMP<a[3], b[3]>, > = { less: OR_4< c3['grt'], AND<c3['eq'], c2['grt']>, AND_4<1, c3['eq'], c2['eq'], c1['grt']>, AND_4<c3['eq'], c2['eq'], c1['eq'], c0['grt']> >, eq: AND_4<c0['eq'], c1['eq'], c2['eq'], c3['eq']>, grt: OR_4< c3['less'], AND<c3['eq'], c2['less']>, AND_4<1, c3['eq'], c2['eq'], c1['less']>, AND_4<c3['eq'], c2['eq'], c1['eq'], c0['less']> >, }; interface assert { true: equals<COMP_4<N_1, N_5>, {less:1, eq:0, grt:0}> } interface assert { true: equals<COMP_4<N_9, N_9>, {less:0, eq:1, grt:0}> } interface assert { true: equals<COMP_4<N_11, N_3>, {less:0, eq:0, grt:1}> } type COMP_RESULT = {less: B, eq: B, grt: B}; type COMP<a extends B, b extends B, /* parameters */ aLessThanB extends B = AND<NOT<b>, a>, aGreaterThanB extends B = AND<b, NOT<a>>, equal extends B = NOR<aLessThanB, aGreaterThanB> > = {less: aLessThanB, eq: equal, grt: aGreaterThanB}; interface assert { true: equals<COMP<1, 0>, {less:1, eq:0, grt:0}> } interface assert { true: equals<COMP<0, 0>, {less:0, eq:1, grt:0}> } interface assert { true: equals<COMP<1, 1>, {less:0, eq:1, grt:0}> } interface assert { true: equals<COMP<0, 1>, {less:0, eq:0, grt:1}> } type equals<a extends any, b extends any> = a extends b ? 1 : 0; interface assert { true: equals<[0, 1], [0, 1]> } interface assert { false: equals<[0, 1], [1, 1]> } -
acutmore revised this gist
Sep 18, 2019 . 1 changed file with 19 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -15,6 +15,25 @@ interface assert { false: any } // expected error interface assert { true: 1 } // good: no error interface assert { false: 0 } // good: no error type DIV_4< numerator extends BITS_4, divisor extends BITS_4, /* variables */ count extends BITS_4 = N_0, remainder extends BITS_4 = numerator > = { // remainder < divisor 1: [count, remainder], // remainder >= dicisor 0: DIV_4<numerator, divisor, ADD_4<count, N_1>, SUB_4<remainder, divisor>> }[ COMP_4<remainder, divisor>[0] extends 1 ? 1 : 0 ]; interface assert { true: equals<DIV_4<N_4, N_2>[0], N_2> } interface assert { true: equals<DIV_4<N_4, N_2>[1], N_0> } interface assert { true: equals<DIV_4<N_9, N_2>[0], N_4> } interface assert { true: equals<DIV_4<N_9, N_2>[1], N_1> } type MUL_4< regA extends BITS_4, regB extends BITS_4, -
acutmore revised this gist
Sep 18, 2019 . 1 changed file with 59 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -19,7 +19,7 @@ type MUL_4< regA extends BITS_4, regB extends BITS_4, /* variables: */ sum0 extends BITS_4 = BITWISE_AND<[regA[0], regA[0], regA[0], regA[0]], regB>, sum1 extends BITS_4 = ADD_4<[0, AND<regA[1],regB[0]>, AND<regA[1],regB[1]>, AND<regA[1],regB[2]>], sum0>, sum2 extends BITS_4 = ADD_4<[0, 0, AND<regA[2],regB[0]>, AND<regA[2],regB[1]>], sum1>, sum3 extends BITS_4 = ADD_4<[0, 0, 0, AND<regA[3], regB[0]>], sum2> @@ -117,20 +117,58 @@ 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 BITWISE_AND<a extends BITS_4, b extends BITS_4> = [ AND<a[0], b[0]>, AND<a[1], b[1]>, AND<a[2], b[2]>, AND<a[3], b[3]>, ]; interface assert { true: equals< BITWISE_AND< [0, 0, 1, 1], [1, 0, 1, 0] >, [0, 0, 1, 0]> } type COMP_4< a extends BITS_4, b extends BITS_4, /* parameters: */ c0 extends [B, B, B] = COMP<a[0], b[0]>, c1 extends [B, B, B] = COMP<a[1], b[1]>, c2 extends [B, B, B] = COMP<a[2], b[2]>, c3 extends [B, B, B] = COMP<a[3], b[3]>, > = [ OR_4< c3[2], AND<c3[1], c2[2]>, AND_4<1, c3[1], c2[1], c1[2]>, AND_4<c3[1], c2[1], c1[1], c0[2]> >, AND_4<c0[1], c1[1], c2[1], c3[1]>, OR_4< c3[0], AND<c3[1], c2[0]>, AND_4<1, c3[1], c2[1], c1[0]>, AND_4<c3[1], c2[1], c1[1], c0[0]> >, ]; interface assert { true: equals<COMP_4<N_1, N_5>, [1, 0, 0]> } interface assert { true: equals<COMP_4<N_9, N_9>, [0, 1, 0]> } interface assert { true: equals<COMP_4<N_11, N_3>, [0, 0, 1]> } type COMP<a extends B, b extends B, /* parameters */ aLessThanB extends B = AND<NOT<b>, a>, aGreaterThanB extends B = AND<b, NOT<a>>, equal extends B = NOR<aLessThanB, aGreaterThanB> > = [aLessThanB, equal, aGreaterThanB]; interface assert { true: equals<COMP<1, 0>, [1, 0, 0]> } interface assert { true: equals<COMP<0, 0>, [0, 1, 0]> } interface assert { true: equals<COMP<1, 1>, [0, 1, 0]> } interface assert { true: equals<COMP<0, 1>, [0, 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]> } @@ -141,12 +179,30 @@ interface assert { true: XOR<0, 1> } interface assert { false: XOR<1, 1> } interface assert { false: XOR<0, 0> } type AND_4<a extends B, b extends B, c extends B, d extends B> = AND<AND<a, b>, AND<c, d>>; interface assert { true: AND_4<1, 1, 1, 1> } interface assert { false: AND_4<1, 1, 1, 0> } interface assert { false: AND_4<0, 1, 0, 1> } interface assert { false: AND_4<0, 0, 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 NOR<a extends B, b extends B> = NOT<OR<a, b>>; interface assert { true: NOR<0, 0> } interface assert { false: NOR<1, 0> } interface assert { false: NOR<0, 1> } interface assert { false: NOR<1, 1> } type OR_4<a extends B, b extends B, c extends B, d extends B> = OR<OR<a, b>, OR<c, d>>; interface assert { true: OR_4<1, 1, 1, 1> } interface assert { true: OR_4<1, 1, 1, 0> } interface assert { true: OR_4<0, 0, 0, 1> } interface assert { false: OR_4<0, 0, 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> } -
acutmore revised this gist
Sep 18, 2019 . 1 changed file with 10 additions and 10 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -15,19 +15,19 @@ interface assert { false: any } // expected error interface assert { true: 1 } // good: no error interface assert { false: 0 } // good: no error type MUL_4< regA extends BITS_4, regB extends BITS_4, /* variables: */ sum0 extends BITS_4 = AND_4<[regA[0], regA[0], regA[0], regA[0]], regB>, sum1 extends BITS_4 = ADD_4<[0, AND<regA[1],regB[0]>, AND<regA[1],regB[1]>, AND<regA[1],regB[2]>], sum0>, sum2 extends BITS_4 = ADD_4<[0, 0, AND<regA[2],regB[0]>, AND<regA[2],regB[1]>], sum1>, sum3 extends BITS_4 = ADD_4<[0, 0, 0, AND<regA[3], regB[0]>], sum2> > = sum3; interface assert { true: equals<MUL_4<N_5, N_0>, N_0> } interface assert { true: equals<MUL_4<N_2, N_3>, N_6> } interface assert { true: equals<MUL_4<N_3, N_3>, N_9> } interface assert { true: equals<MUL_4<N_3, N_5>, N_15> } type SUB_4< regA extends BITS_4, -
acutmore revised this gist
Sep 18, 2019 . 1 changed file with 65 additions and 41 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,6 @@ // ASHEMBLER type B = 1 | 0; type BITS_4 = [B, B, B, B]; // note: 'assert' interfaces function as unit tests interface assert { @@ -14,52 +15,66 @@ interface assert { false: any } // expected error interface assert { true: 1 } // good: no error interface assert { false: 0 } // good: no error type MUL_8< regA extends BITS_4, regB extends BITS_4, /* variables: */ s0 extends BITS_4 = AND_4<[regA[0], regA[0], regA[0], regA[0]], regB>, s1 extends BITS_4 = ADD_4<[0, AND<regA[1],regB[0]>, AND<regA[1],regB[1]>, AND<regA[1],regB[2]>], s0>, s2 extends BITS_4 = ADD_4<[0, 0, AND<regA[2],regB[0]>, AND<regA[2],regB[1]>], s1>, s3 extends BITS_4 = ADD_4<[0, 0, 0, AND<regA[3], regB[0]>], s2> > = s3; interface assert { true: equals<MUL_8<N_5, N_0>, N_0> } interface assert { true: equals<MUL_8<N_2, N_3>, N_6> } interface assert { true: equals<MUL_8<N_3, N_3>, N_9> } interface assert { true: equals<MUL_8<N_3, N_5>, N_15> } type SUB_4< regA extends BITS_4, regB extends BITS_4, /* variables: */ a extends [B, B] = FULL_SUBTRACTOR<0, regA[0], regB[0]>, b extends [B, B] = FULL_SUBTRACTOR<a[1], regA[1], regB[1]>, c extends [B, B] = FULL_SUBTRACTOR<b[1], regA[2], regB[2]>, d extends [B, B] = FULL_SUBTRACTOR<c[1], regA[3], regB[3]>, > = [a[0], b[0], c[0], d[0]]; interface assert { true: equals<SUB_4<N_5, N_2>, N_3> } type ADD_4< regA extends BITS_4, regB extends BITS_4, /* 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]>, > = [a[0], b[0], c[0], d[0]]; interface assert { true: equals<ADD_4<N_2, N_3>, N_5> } type N_0 = [0, 0, 0, 0]; type N_1 = [1, 0, 0, 0]; type N_2 = [0, 1, 0, 0]; type N_3 = [1, 1, 0, 0]; type N_4 = [0, 0, 1, 0]; type N_5 = [1, 0, 1, 0]; type N_6 = [0, 1, 1, 0]; type N_7 = [1, 1, 1, 0]; type N_8 = [0, 0, 0, 1]; type N_9 = [1, 0, 0, 1]; type N_10 = [0, 1, 0, 1]; type N_11 = [1, 1, 0, 1]; type N_12 = [0, 0, 1, 1]; type N_13 = [1, 0, 1, 1]; type N_14 = [0, 1, 1, 1]; type N_15 = [1, 1, 1, 1]; type FULL_SUBTRACTOR< borrow extends B, a extends B, b extends B, /* variables: */ subtractor1 extends [B, B] = HALF_SUBTRACTOR<a, b>, subtractor2 extends [B, B] = HALF_SUBTRACTOR<subtractor1[0], borrow>, > = [subtractor2[0], OR<subtractor1[1], subtractor2[1]>]; interface assert { true: equals<FULL_SUBTRACTOR<0, 0, 0>, [0, 0]> } interface assert { true: equals<FULL_SUBTRACTOR<1, 0, 0>, [1, 1]> } @@ -89,25 +104,37 @@ interface assert { true: equals<FULL_ADDER<1, 1, 1>, [1, 1]> } type HALF_SUBTRACTOR<a extends B, b extends B> = [ XOR<a, b>, AND<NOT<a>, b> ]; interface assert { true: equals<HALF_SUBTRACTOR<0, 0>, [0, 0]> } interface assert { true: equals<HALF_SUBTRACTOR<1, 0>, [1, 0]> } interface assert { true: equals<HALF_SUBTRACTOR<0, 1>, [1, 1]> } interface assert { true: equals<HALF_SUBTRACTOR<1, 1>, [0, 0]> } 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 AND_4<a extends BITS_4, b extends BITS_4> = [ AND<a[0], b[0]>, AND<a[1], b[1]>, AND<a[2], b[2]>, AND<a[3], b[3]>, ]; interface assert { true: equals< AND_4< [0, 0, 1, 1], [1, 0, 1, 0] >, [0, 0, 1, 0]> } 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> } @@ -126,10 +153,7 @@ interface assert { true: OR<1, 0> } interface assert { true: OR<0, 1> } interface assert { false: OR<0, 0> } type NOT<a extends B> = NAND<a, 1>; interface assert { true: NOT<0> } interface assert { false: NOT<1> } -
acutmore revised this gist
Sep 18, 2019 . 1 changed file with 14 additions and 13 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -107,32 +107,33 @@ 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<a extends B> = { 1: 0; 0: 1; }[a]; interface assert { true: NOT<0> } interface assert { false: NOT<1> } type NAND<a extends B, b extends B> = { 1: { 1: 0; 0: 1; @@ -141,9 +142,9 @@ type NAND = { 1: 1; 0: 1; }; }[a][b]; interface assert { true: NAND<0, 0> } interface assert { true: NAND<1, 0> } interface assert { true: NAND<0, 1> } interface assert { false: NAND<1, 1> } -
acutmore revised this gist
Sep 18, 2019 . 1 changed file with 50 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -14,9 +14,26 @@ interface assert { false: any } // expected error interface assert { true: 1 } // good: no error interface assert { false: 0 } // good: no error type BITS_8 = [B, B, B, B, B, B, B, B]; type SUB_8< regA extends BITS_8, regB extends BITS_8, /* variables: */ a extends [B, B] = FULL_SUBTRACTOR<0, regA[0], regB[0]>, b extends [B, B] = FULL_SUBTRACTOR<a[1], regA[1], regB[1]>, c extends [B, B] = FULL_SUBTRACTOR<b[1], regA[2], regB[2]>, d extends [B, B] = FULL_SUBTRACTOR<c[1], regA[3], regB[3]>, e extends [B, B] = FULL_SUBTRACTOR<d[1], regA[4], regB[4]>, f extends [B, B] = FULL_SUBTRACTOR<e[1], regA[5], regB[5]>, g extends [B, B] = FULL_SUBTRACTOR<f[1], regA[6], regB[6]>, h extends [B, B] = FULL_SUBTRACTOR<g[1], regA[7], regB[7]> > = [a[0], b[0], c[0], d[0], e[0], f[0], g[0], h[0]]; interface assert { true: equals<SUB_8<N_101, N_34>, N_67> } type ADD_8< regA extends BITS_8, regB extends BITS_8, /* variables: */ a extends [B, B] = FULL_ADDER<0, regA[0], regB[0]>, b extends [B, B] = FULL_ADDER<a[1], regA[1], regB[1]>, @@ -28,10 +45,30 @@ type ADD_8< 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_0 = [0, 0, 0, 0, 0, 0, 0, 0]; type N_1 = [0, 0, 0, 0, 0, 0, 0, 1]; type N_34 = [0, 1, 0, 0, 0, 1, 0, 0]; type N_67 = [1, 1, 0, 0, 0, 0, 1, 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>, N_135> } type FULL_SUBTRACTOR< borrow extends B, a extends B, b extends B, /* variables: */ subtractor1 extends [B, B] = HALF_SUBTRACTOR<a, b>, subtractor2 extends [B, B] = HALF_SUBTRACTOR<subtractor1[0], borrow>, > = [subtractor2[0], OR<subtractor1[1], subtractor2[1]>]; interface assert { true: equals<FULL_SUBTRACTOR<0, 0, 0>, [0, 0]> } interface assert { true: equals<FULL_SUBTRACTOR<1, 0, 0>, [1, 1]> } interface assert { true: equals<FULL_SUBTRACTOR<0, 1, 0>, [1, 0]> } interface assert { true: equals<FULL_SUBTRACTOR<0, 0, 1>, [1, 1]> } interface assert { true: equals<FULL_SUBTRACTOR<1, 1, 0>, [0, 0]> } interface assert { true: equals<FULL_SUBTRACTOR<1, 0, 1>, [0, 1]> } interface assert { true: equals<FULL_SUBTRACTOR<0, 1, 1>, [0, 0]> } interface assert { true: equals<FULL_SUBTRACTOR<1, 1, 1>, [1, 1]> } type FULL_ADDER< carry extends B, @@ -50,6 +87,16 @@ 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_SUBTRACTOR<a extends B, b extends B> = [ XOR<a, b>, AND<NOT[a], b> ]; interface assert { true: equals<HALF_SUBTRACTOR<0, 0>, [0, 0]> } interface assert { true: equals<HALF_SUBTRACTOR<1, 0>, [1, 0]> } interface assert { true: equals<HALF_SUBTRACTOR<0, 1>, [1, 1]> } interface assert { true: equals<HALF_SUBTRACTOR<1, 1>, [0, 0]> } 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]> } -
acutmore revised this gist
Sep 15, 2019 . 1 changed file with 49 additions and 37 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,18 @@ // 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], @@ -17,7 +31,7 @@ type ADD_8< 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, @@ -27,45 +41,49 @@ type FULL_ADDER< 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: { @@ -77,14 +95,8 @@ type NAND = { 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] } -
acutmore revised this gist
Sep 15, 2019 . 1 changed file with 12 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -27,8 +27,20 @@ type FULL_ADDER< adder1 extends [B, B] = HALF_ADDER<a, b>, adder2 extends [B, B] = HALF_ADDER<carry, adder1[0]> > = [adder2[0], OR<adder2[1], adder1[1]>]; assertTrue<equals<FULL_ADDER<0, 0, 0>, [0, 0]>>(); assertTrue<equals<FULL_ADDER<1, 0, 0>, [1, 0]>>(); assertTrue<equals<FULL_ADDER<0, 1, 0>, [1, 0]>>(); assertTrue<equals<FULL_ADDER<0, 0, 1>, [1, 0]>>(); assertTrue<equals<FULL_ADDER<1, 1, 0>, [0, 1]>>(); assertTrue<equals<FULL_ADDER<1, 0, 1>, [0, 1]>>(); assertTrue<equals<FULL_ADDER<0, 1, 1>, [0, 1]>>(); assertTrue<equals<FULL_ADDER<1, 1, 1>, [1, 1]>>(); type HALF_ADDER<a extends B, b extends B> = [XOR<a, b>, AND<a, b>]; assertTrue<equals<HALF_ADDER<0, 0>, [0, 0]>>(); assertTrue<equals<HALF_ADDER<1, 0>, [1, 0]>>(); assertTrue<equals<HALF_ADDER<0, 1>, [1, 0]>>(); assertTrue<equals<HALF_ADDER<1, 1>, [0, 1]>>(); type XOR<a extends B, b extends B> = AND<NAND[a][b], OR<a, b>>; assertTrue<XOR<1, 0>>(); -
acutmore revised this gist
Sep 15, 2019 . 1 changed file with 31 additions and 56 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -19,19 +19,6 @@ type N_101 = [1, 0, 1, 0, 0, 1, 1, 0]; type N_135 = [1, 1, 1, 0, 0, 0, 0, 1]; assertTrue<equals<ADD_8<N_34, N_101>, /* expected: */ N_135>>(); type FULL_ADDER< carry extends B, a extends B, @@ -40,64 +27,52 @@ type FULL_ADDER< adder1 extends [B, B] = HALF_ADDER<a, b>, adder2 extends [B, B] = HALF_ADDER<carry, adder1[0]> > = [adder2[0], OR<adder2[1], adder1[1]>]; type HALF_ADDER<a extends B, b extends B> = [XOR<a, b>, AND<a, b>]; type XOR<a extends B, b extends B> = AND<NAND[a][b], OR<a, b>>; assertTrue<XOR<1, 0>>(); assertTrue<XOR<0, 1>>(); assertFalse<XOR<1, 1>>(); assertFalse<XOR<0, 0>>(); type AND<a extends B, b extends B> = NOT[NAND[a][b]]; assertTrue<AND<1, 1>>(); assertFalse<AND<1, 0>>(); assertFalse<AND<0, 1>>(); assertFalse<AND<0, 0>>(); type OR<a extends B, b extends B> = NAND[NOT[a]][NOT[b]]; assertTrue<OR<1, 1>>(); assertTrue<OR<1, 0>>(); assertTrue<OR<0, 1>>(); assertFalse<OR<0, 0>>(); type NOT = { 1: 0; 0: 1; }; assertTrue<NOT[0]>(); assertFalse<NOT[1]>(); type NAND = { 1: { 1: 0; 0: 1; }; 0: { 1: 1; 0: 1; }; }; assertTrue<NAND[0][0]>(); assertTrue<NAND[1][0]>(); assertTrue<NAND[0][1]>(); assertFalse<NAND[1][1]>(); type B = 1 | 0; declare function assertFalse<B extends 0>(): void; declare function assertTrue<B extends 1>(): void; type equals<a extends B[], b extends B[]> = a extends b ? 1 : 0; assertTrue<equals<[0, 1], [0, 1]>>(); assertFalse<equals<[0, 1], [1, 1]>>(); -
acutmore created this gist
Sep 15, 2019 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,103 @@ // ASHEMBLER 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]; assertTrue<equals<ADD_8<N_34, N_101>, /* expected: */ N_135>>(); type ADD_2< regA extends [B, B], regB extends [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]> > = [a[0], b[0]]; assertTrue<equals<ADD_2<[0, 0], [0, 0]>, /* expected: */ [0, 0]>>(); assertTrue<equals<ADD_2<[1, 0], [1, 0]>, /* expected: */ [0, 1]>>(); assertTrue<equals<ADD_2<[0, 1], [1, 0]>, /* expected: */ [1, 1]>>(); assertTrue<equals<ADD_2<[1, 1], [1, 0]>, /* expected: */ [0, 0]>>(); // overflows 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]>]; assertTrue<equals<FULL_ADDER<0, 0, 0>, [0, 0]>>(); assertTrue<equals<FULL_ADDER<1, 0, 0>, [1, 0]>>(); assertTrue<equals<FULL_ADDER<0, 1, 0>, [1, 0]>>(); assertTrue<equals<FULL_ADDER<0, 0, 1>, [1, 0]>>(); assertTrue<equals<FULL_ADDER<1, 1, 0>, [0, 1]>>(); assertTrue<equals<FULL_ADDER<1, 0, 1>, [0, 1]>>(); assertTrue<equals<FULL_ADDER<0, 1, 1>, [0, 1]>>(); assertTrue<equals<FULL_ADDER<1, 1, 1>, [1, 1]>>(); type HALF_ADDER<a extends B, b extends B> = [XOR<a, b>, AND<a, b>]; assertTrue<equals<HALF_ADDER<0, 0>, [0, 0]>>(); assertTrue<equals<HALF_ADDER<1, 0>, [1, 0]>>(); assertTrue<equals<HALF_ADDER<0, 1>, [1, 0]>>(); assertTrue<equals<HALF_ADDER<1, 1>, [0, 1]>>(); type equals<a extends B[], b extends B[]> = a extends b ? 1 : 0; assertTrue<equals<[0, 1], [0, 1]>>(); assertFalse<equals<[0, 1], [1, 1]>>(); type XOR<a extends B, b extends B> = AND<NAND<a, b>, OR<a, b>>; assertTrue<XOR<1, 0>>(); assertTrue<XOR<0, 1>>(); assertFalse<XOR<1, 1>>(); assertFalse<XOR<0, 0>>(); type NOR<a extends B, b extends B> = NOT<OR<a, b>>; assertTrue<NOR<0, 0>>(); assertFalse<NOR<1, 0>>(); assertFalse<NOR<0, 1>>(); assertFalse<NOR<1, 1>>(); type OR<a extends B, b extends B> = NAND<NOT<a>, NOT<b>>; assertTrue<OR<1, 1>>(); assertTrue<OR<1, 0>>(); assertTrue<OR<0, 1>>(); assertFalse<OR<0, 0>>(); type AND<a extends B, b extends B> = NOT<NAND<a, b>>; assertTrue<AND<1, 1>>(); assertFalse<AND<1, 0>>(); assertFalse<AND<0, 1>>(); assertFalse<AND<0, 0>>(); type NOT<a extends B> = NAND<a, a>; assertTrue<NOT<0>>(); assertFalse<NOT<1>>(); type NAND<b1 extends B, b2 extends B> = b1 extends 1 ? b2 extends 1 ? 0 : 1 : 1; assertTrue<NAND<0, 0>>(); assertTrue<NAND<1, 0>>(); assertTrue<NAND<0, 1>>(); assertFalse<NAND<1, 1>>(); declare function assertFalse<B extends 0>(): void; declare function assertTrue<B extends 1>(): void; assertFalse<0>(); assertTrue<1>(); type B = 1 | 0;