Skip to content

Instantly share code, notes, and snippets.

@Kuniwak
Created July 13, 2015 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kuniwak/b3e3efe75f344147b451 to your computer and use it in GitHub Desktop.
Save Kuniwak/b3e3efe75f344147b451 to your computer and use it in GitHub Desktop.
Compile-Time 4-bit adder on TypeScript
// Compile-Time 4-bit Adder for TypeScript
// Author: https://github.com/Kuniwak
// INTPUT ////////////////////////////////
// O: Low
// I: High
let inputA1: I;
let inputA2: I;
let inputA3: O;
let inputA4: O;
let inputB1: O;
let inputB2: I;
let inputB3: O;
let inputB4: O;
// OUTPUT ///////////////////////////////
// Hover the outputN to read the result
let output1: typeof halfByteAdder.sum1;
let output2: typeof halfByteAdder.sum2;
let output3: typeof halfByteAdder.sum3;
let output4: typeof halfByteAdder.sum4;
// //////////////////////////////////////
interface O { O: void }
interface I { I: void }
interface And {
(a: O, b: O): O;
(a: O, b: I): O;
(a: I, b: O): O;
(a: I, b: I): I;
}
interface Or {
(a: O, b: O): O;
(a: O, b: I): I;
(a: I, b: O): I;
(a: I, b: I): I;
}
interface Xor {
(a: O, b: O): O;
(a: O, b: I): I;
(a: I, b: O): I;
(a: I, b: I): O;
}
interface Not {
(a: O): I;
(a: I): O;
}
let o: O;
let i: I;
let and: And;
let or: Or;
let xor: Xor;
let not: Not;
module halfByteAdder {
let _a1: typeof inputA1;
let _b1: typeof inputB1;
let _x1: O;
module fullAdder1 {
let __a1: typeof _a1;
let __b1: typeof _b1;
export module _halfAdder1 {
export const sum = xor(__a1, __b1);
export const carry = and(__a1, __b1);
}
let __a2: typeof _halfAdder1.sum;
let __b2: typeof _x1;
export module _halfAdder2 {
export const sum = xor(__a2, __b2);
export const carry = and(__a2, __b2);
}
export const sum = _halfAdder2.sum;
export const carry = or(_halfAdder1.carry, _halfAdder2.carry);
}
let _a2: typeof inputA2;
let _b2: typeof inputB2;
let _x2: typeof fullAdder1.carry;
module fullAdder2 {
let __a1: typeof _a2;
let __b1: typeof _b2;
export module _halfAdder1 {
export const sum = xor(__a1, __b1);
export const carry = and(__a1, __b1);
}
let __a2: typeof _halfAdder1.sum;
let __b2: typeof _x2;
export module _halfAdder2 {
export const sum = xor(__a2, __b2);
export const carry = and(__a2, __b2);
}
export const sum = _halfAdder2.sum;
export const carry = or(_halfAdder1.carry, _halfAdder2.carry);
}
let _a3: typeof inputA3;
let _b3: typeof inputB3;
let _x3: typeof fullAdder2.carry;
module fullAdder3 {
let __a1: typeof _a3;
let __b1: typeof _b3;
export module _halfAdder1 {
export const sum = xor(__a1, __b1);
export const carry = and(__a1, __b1);
}
let __a2: typeof _halfAdder1.sum;
let __b2: typeof _x3;
export module _halfAdder2 {
export const sum = xor(__a2, __b2);
export const carry = and(__a2, __b2);
}
export const sum = _halfAdder2.sum;
export const carry = or(_halfAdder1.carry, _halfAdder2.carry);
}
let _a4: typeof inputA4;
let _b4: typeof inputB4;
let _x4: typeof fullAdder3.carry;
module fullAdder4 {
let __a1: typeof _a4;
let __b1: typeof _b4;
export module _halfAdder1 {
export const sum = xor(__a1, __b1);
export const carry = and(__a1, __b1);
}
let __a2: typeof _halfAdder1.sum;
let __b2: typeof _x4;
export module _halfAdder2 {
export const sum = xor(__a2, __b2);
export const carry = and(__a2, __b2);
}
export const sum = _halfAdder2.sum;
export const carry = or(_halfAdder1.carry, _halfAdder2.carry);
}
export let sum1: typeof fullAdder1.sum;
export let sum2: typeof fullAdder2.sum;
export let sum3: typeof fullAdder3.sum;
export let sum4: typeof fullAdder4.sum;
export let carry: typeof fullAdder4.carry;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment