/** Preprocessor **/
-Dk="_nZZiaeY" // This is a lookup string; it corresponds to "ixddskoc", with 10 deducted
// with 10 deducted from each character. Upon compilation,
// all occurences of the string literal
are replaced with a
// are replaced with a // pointer to its location in memory.
/** Source **/
x;f(c){ // x: 32-bit accumulator, c: local variable for read character
for(printf(">>"); // Start for-loop and print prompt.
c=getchar()-10; // Read a character from STDIN,.
break loop // Loop breaks if it is '\n'.
// ThisThe below happens at the end of each iteration of the for-loop.
x+=c--?c--?c--? // Find
the correct operation by testing c; if it is 0, operation
// Find the correct operation by testing c and post-
// was found,decrementing addwith themultiple valueternary-ifs. ofIf thec ternary-elseis (<test>?:<else>)0,
// operation was found, add the else-value to the
// to the accumulator x.
// accumulator x.
// // If the character is invalid, the value is very large,of andc willis
// very large, and //will not reach 0 with 3 decrements.
c||printf("%i\n",x),0
// If c-3 is 0, print accumulator, else do nothing.
// Returns 0 regardless of what happens. (No change to x)
// Returns 0 regardless of what:x*x-x happens.
:x*x-x // Square. Results in x=x+x*x-x, and is shorter than :(x*=x)
:-1:1,
// Decrement, incrementIncrement.
x*=~x&&x^256
// Because -1==0xffffffff, ~x==0 when x==-1. Likewise,
// x^256==0 only when x==256. The logical-AND coerces the result
// to boolean 1 (no match) or 0 (match). Multiplication resets
// the accumulator as appropriate.
)
// This is the actual body of the for-loop
c=strchr(k,c)-k>>1;
// FindFinds the index of the read character in the lookup string,
// then "divide""divides" it by two.
// Because strchr() returns NULL (0) when character is not found,
deducting k from
// deducting k from it results in a very negative number.
// The right-shift results in division by 2 for positive numbers,
// positive numbers, // while the negative numbers become very large positive numbers
// (c >= 0x70000000)
because of the f();2's complement representation.
// RecurseFinally, recurse until forceful termination.
f();
}
/** Preprocessor **/
-Dk="_nZZiaeY" // This is a lookup string; it corresponds to "ixddskoc", with 10 deducted
// from each character. Upon compilation, all occurences of the string literal
// are replaced with a pointer to its location in memory.
/** Source **/
x;f(c){ // x: 32-bit accumulator, c: local variable for read character
for(printf(">>"); // Start for-loop and print prompt.
c=getchar()-10; // Read a character from STDIN, break loop if it is '\n'.
// This happens at the end of each iteration of the for-loop.
x+=c--?c--?c--? // Find the correct operation by testing c; if it is 0, operation
// was found, add the value of the ternary-else (<test>?:<else>)
// to the accumulator x.
// If the character is invalid, the value is very large, and will
// not reach 0 with 3 decrements.
c||printf("%i\n",x),0 // If c is 0, print accumulator, else do nothing.
// Returns 0 regardless of what happens.
:x*x-x // Square. Results in x=x+x*x-x, and is shorter than :(x*=x)
:-1:1, // Decrement, increment.
x*=~x&&x^256 // Because -1==0xffffffff, ~x==0 when x==-1. Likewise,
// x^256==0 only when x==256. The logical-AND coerces the result
// to boolean 1 (no match) or 0 (match). Multiplication resets
// the accumulator as appropriate.
)
// This is the actual body of the for-loop
c=strchr(k,c)-k>>1; // Find the index of the read character in the lookup string,
// then "divide" it by two.
// Because strchr() returns NULL (0) when character is not found, deducting k from
// it results in a very negative number. The right-shift results in division by 2 for
// positive numbers, while the negative numbers become very large positive numbers
// (c >= 0x70000000)
f(); // Recurse until termination.
}
/** Preprocessor **/
-Dk="_nZZiaeY" // This is a lookup string; it corresponds to "ixddskoc",
// with 10 deducted from each character. Upon compilation,
// all occurences of the string literal are replaced with a
// pointer to its location in memory.
/** Source **/
x;f(c){ // x: 32-bit accumulator, c: local variable for read character
for(printf(">>"); // Start for-loop and print prompt.
c=getchar()-10; // Read a character from STDIN.
// Loop breaks if it is '\n'.
// The below happens at the end of each iteration.
x+=c--?c--?c--?
// Find the correct operation by testing c and post-
// decrementing with multiple ternary-ifs. If c is 0,
// operation was found, add the else-value to the
// accumulator x.
// If the character is invalid, the value of c is
// very large, and will not reach 0 with 3 decrements.
c||printf("%i\n",x),0
// If c-3 is 0, print accumulator, else do nothing.
// Returns 0 regardless of what happens. (No change to x)
:x*x-x
// Square. Results in x=x+x*x-x, and is shorter than (x*=x)
:-1:1,
// Decrement, Increment.
x*=~x&&x^256
// Because -1==0xffffffff, ~x==0 when x==-1. Likewise,
// x^256==0 only when x==256. The logical-AND coerces the result
// to boolean 1 (no match) or 0 (match). Multiplication resets
// the accumulator as appropriate.
)
// This is the actual body of the for-loop
c=strchr(k,c)-k>>1;
// Finds the index of the read character in the lookup string,
// then "divides" it by two.
// Because strchr() returns NULL (0) when character is not found,
// deducting k from it results in a very negative number.
// The right-shift results in division by 2 for positive numbers,
// while the negative numbers become very large positive numbers
// (c >= 0x70000000) because of the 2's complement representation.
// Finally, recurse until forceful termination.
f();
}
C (gcc), 139 bytes
Compile with -Dk="_nZZiaeY"
(included in byte count). -2 bytes if prompt >>\n
is allowed.
x;f(c){for(printf(">>");c=getchar()-10;x+=c--?c--?c--?c||printf("%i\n",x),0:x*x-x:-1:1,x*=~x&&x^256)c=strchr(k,c)-k>>1;f();}
Degolf
/** Preprocessor **/
-Dk="_nZZiaeY" // This is a lookup string; it corresponds to "ixddskoc", with 10 deducted
// from each character. Upon compilation, all occurences of the string literal
// are replaced with a pointer to its location in memory.
/** Source **/
x;f(c){ // x: 32-bit accumulator, c: local variable for read character
for(printf(">>"); // Start for-loop and print prompt.
c=getchar()-10; // Read a character from STDIN, break loop if it is '\n'.
// This happens at the end of each iteration of the for-loop.
x+=c--?c--?c--? // Find the correct operation by testing c; if it is 0, operation
// was found, add the value of the ternary-else (<test>?:<else>)
// to the accumulator x.
// If the character is invalid, the value is very large, and will
// not reach 0 with 3 decrements.
c||printf("%i\n",x),0 // If c is 0, print accumulator, else do nothing.
// Returns 0 regardless of what happens.
:x*x-x // Square. Results in x=x+x*x-x, and is shorter than :(x*=x)
:-1:1, // Decrement, increment.
x*=~x&&x^256 // Because -1==0xffffffff, ~x==0 when x==-1. Likewise,
// x^256==0 only when x==256. The logical-AND coerces the result
// to boolean 1 (no match) or 0 (match). Multiplication resets
// the accumulator as appropriate.
)
// This is the actual body of the for-loop
c=strchr(k,c)-k>>1; // Find the index of the read character in the lookup string,
// then "divide" it by two.
// Because strchr() returns NULL (0) when character is not found, deducting k from
// it results in a very negative number. The right-shift results in division by 2 for
// positive numbers, while the negative numbers become very large positive numbers
// (c >= 0x70000000)
f(); // Recurse until termination.
}