Created
September 23, 2012 16:09
-
-
Save arianvp/3772160 to your computer and use it in GitHub Desktop.
This file contains 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
0 _ = require 'underscore' | |
1 TRUE = (a,b) -> a | |
2 FALSE = (a,b) -> b | |
3 NAND = (a,b) -> a(b,a)(FALSE, TRUE) | |
4 NOT = (a) -> NAND(a,a) | |
5 AND = (a,b) -> NOT(NAND(a,b)) | |
6 OR = (a,b) -> NAND(NOT(a),NOT(b)) | |
7 NOR = (a,b) -> NOT(OR(a,b)) | |
8 XOR = (a,b) -> NOR(AND(a,b),NOR(a,b)) | |
9 XNOR = (a,b) -> NOT(XOR(a,b)) | |
10 FULL_ADD = (a,b,c,p) -> p(XOR(XOR(a,b),c),NAND(NAND(XOR(a,b),c),NAND(a, c))) | |
11 HALF_ADD = (a,b,p) -> p(XOR(a,b), OR(a,b)) | |
12 | |
13 | |
14 ADD_SUB = (as,bs,d) -> | |
15 results = [] | |
16 callback = (res, c_out) -> | |
17 results.unshift res | |
18 a = as.pop() | |
19 b = bs.pop() | |
20 if a? and b? | |
21 FULL_ADD XOR(a,d), b, c_out, callback | |
22 HALF_ADD as.pop(), bs.pop(), callback | |
23 results | |
24 | |
25 ADD = (as,bs) -> ADD_SUB as, bs, FALSE | |
26 SUB = (as,bs) -> ADD_SUB as, bs, TRUE | |
27 TRUE.toString = -> "TRUE" | |
28 FALSE.toString = -> "FALSE" | |
29 | |
30 | |
31 from_bools = (bools...) -> (bools.map (bool) -> bool.toString()) | |
32 console.log from_bools SUB [FALSE,FALSE,FALSE,TRUE], [FALSE,FALSE,TRUE,TRUE] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment