Created
March 11, 2022 04:51
-
-
Save ProfAvery/5a3216077c8914c7b977214b9f01f8ce to your computer and use it in GitHub Desktop.
CPSC 439 - Spring 2022 - Week 6 - List-of-tuples Representation
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
| #include <iostream> | |
| /* | |
| C++ version of | |
| https://nbviewer.org/github/boazbk/tcscode/blob/master/Chap_04_Syntactic_Sugar.ipynb#Even-more-sugar | |
| */ | |
| using namespace std; | |
| string NAND(string a, string b) { | |
| static auto counter = 1; | |
| auto v = "temp_" + to_string(counter++); | |
| cout << "auto " << v << " = NAND(" << a << "," << b << ");" << endl; | |
| return v; | |
| } | |
| string NOT(string a) { return NAND(a, a); } | |
| string AND(string a, string b) { | |
| auto temp = NAND(a, b); | |
| return NOT(temp); | |
| } | |
| string OR(string a, string b) { | |
| auto temp1 = NOT(a); | |
| auto temp2 = NOT(b); | |
| return NAND(temp1, temp2); | |
| } | |
| string MAJ(string a, string b, string c) { | |
| auto and1 = AND(a, b); | |
| auto and2 = AND(a, c); | |
| auto and3 = AND(b, c); | |
| auto or1 = OR(and1, and2); | |
| return OR(or1, and3); | |
| } | |
| string IF(string cond, string a, string b) { | |
| auto notcond = NAND(cond, cond); | |
| auto temp = NAND(b, notcond); | |
| auto temp1 = NAND(a, cond); | |
| return NAND(temp, temp1); | |
| } | |
| string LOOKUP1(string X0, string X1, string X2) { return IF(X2, X1, X0); } | |
| string LOOKUP2(string X0, string X1, string X2, string X3, string i0, | |
| string i1) { | |
| auto a = LOOKUP1(X2, X3, i1); | |
| auto b = LOOKUP1(X0, X1, i1); | |
| return IF(i0, a, b); | |
| } | |
| string LOOKUP3(string X0, string X1, string X2, string X3, string X4, string X5, | |
| string X6, string X7, string i0, string i1, string i2) { | |
| auto a = LOOKUP2(X4, X5, X6, X7, i1, i2); | |
| auto b = LOOKUP2(X0, X1, X2, X3, i1, i2); | |
| return IF(i0, a, b); | |
| } | |
| int main() { | |
| auto output = LOOKUP3("X[0]", "X[1]", "X[2]", "X[3]", "X[4]", "X[5]", "X[6]", | |
| "X[7]", "i[0]", "i[1]", "i[2]"); | |
| cout << "return " << output << ";" << endl; | |
| return EXIT_SUCCESS; | |
| } |
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
| CXXFLAGS = -g -std=c++17 -Wall -Wextra -Wpedantic -Werror | |
| .PHONY: clean | |
| run: test | |
| ./test | |
| test: test.cpp nand-circ.h | |
| $(CXX) $(CXXFLAGS) $< -o $@ | |
| nand-circ.h: desugar | |
| ./desugar > nand-circ.h | |
| clean: | |
| rm -f *.o *.gch nand-circ.h desugar test |
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
| #include <iostream> | |
| #include <vector> | |
| using namespace std; | |
| int NAND(int a, int b) { return !(a && b); } | |
| int LOOKUP3(vector<int> X, vector<int> i) { | |
| #include "nand-circ.h" | |
| } | |
| int main() { | |
| vector<int> F = {0, 0, 1, 1, 0, 0, 1, 0}; | |
| for (auto i = 0; i <= 1; i++) { | |
| for (auto j = 0; j <= 1; j++) { | |
| for (auto k = 0; k <= 1; k++) { | |
| cout << "LOOKUP3(" << F[0] << "," << F[1] << "," << F[2] << "," << F[3] | |
| << "," << F[4] << "," << F[5] << "," << F[6] << "," << F[7] << "," | |
| << i << "," << j << "," << k << ") = " << LOOKUP3(F, {i, j, k}) | |
| << endl; | |
| } | |
| } | |
| } | |
| return EXIT_SUCCESS; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment