ferNANDo

ferNANDo is an esoteric programming language by User:Whtspc. It's based on NAND-logic and uses no other syntax-elements than variable names. When variables names are chosen carefully, programs may sound quite poetic.

At initialization every possible variable is set to 0. (Actually every time the program encounters an undefined variable, it creates this variable with the value 0.) The exception to this is ?, which acts as a PRNG bit.

The amount of words (variables) in a sentence determine what type of command will be executed. There are 4 commands.

The NAND command

Note: 2 variable NAND is new in version 0.5

A B C

or

A B

The NAND command is executed when there are 2 or 3 variables in a sentence. If there are 3 variables, it sets the value of the first variable to the NAND of the second and third; if there are 2, it sets the first variable to the NAND of itself and the second. In the above examples, it sets 'A' to 'B NAND C', and 'A' to 'A NAND B' respectively. Since the variables are set to 0 by default, A will be 0 NAND 0, which is 1.

The output command

A B C D E F G H

The output command is executed when there are 8 variables in a sentence. The value of the binary digit represented by the values of the variables in the sentence together is displayed as an ascii character.

The input command

New in version 0.5

R A B C D E F G H

The input command is executed when there are 9 variables in a sentence, and reads a single byte from STDIN. The first variable will be set to 1 if the operation was successful, or 0 otherwise (usually indicating EOF). The rest of the variables are set to bits of the byte read, most significant bit first. In the event of EOF, only the value of the first variable will be changed.

A sample program which reads the contents of STDIN, and echos them back to STDOUT:

R A B C D E F G H
R
A B C D E F G H
R A B C D E F G H
R

The conditional loop command

loop
loop loop loop
loop

When there's only one variable in a sentence and this variable has value 1, program points back to the line right after the previous sentence that's exactly the same. If there isn't a correspondenting sentence back in the program, the command is ignored and program continues without any further action. The same happens when the value of the variable is 0.

So, walking through the example above:

-the word 'loop' is unknown, and undefined therefore. The variable 'loop' is created with value 0.

-Since the value is 0 we ignore the command in line 1.

-In the second line the variable 'loop' is set to 'loop NAND loop', or 0 NAND 0, which is 1. The variable 'loop' now has value 1.

-In the third line we have one variable with value 1, so we have to go back to the line after the previous sentence that's exactly the same. In this case line 1 is exactly the same, so the program points back to line 2.

-In line 2 'loop' is set to 'loop NAND loop', loop is 1 by now, 1 NAND 1 = 0, loop is set to 0.

-We encounter line 3 again, loop is now 0, so the command is ignored. Since there are no other lines left, program execution is stopped.

? The PRNG Bit

New in version 0.5

? is a pre-defined variable, which is set to a function that returns a random bit each time it is accessed. ? may also be used in a write context, but afterwards, it will act as a normal variable.

The following prints out a random digit between 0 and 7:

1 1 1
0 0 1 1 0 ? ? ?

This functionality may be disabled by use of the --no-prng command line option, in which case ? will act as a normal variable, defaulting to value 0.

Other logic gates

To place the value of logic gate(A,B) into O:

NOT:

O A A

AND:

O A B
O O O

OR:

C A A
D B B
O C D

NOR:

C A A
D B B
O C D
O O O

XOR:

C A A C A B
D B B or D A C
E A D E B C
F C B O D E
O E F

NXOR:

C A A C A B
D B B or D A C
E A B E B C
F C D O D E
O E F O O O

A IMPLIES B:

C B B
O A C

Arithmetic circuits

HALF-ADDER

Single bit incrementer with carry.

Ci = Carry-in; Co = Carry-out; S = Bit

Co Ci S
t1 Ci Co
t2 Co S
S t1 t2
Co Co Co

N-BIT INCREMENTER

Implemented by chaining N half-adders. The following is a 5-Bit incrementer (on bits a, b, c, d, e), which loops 26 times (until b, d, and e are all set), printing out the corresponding letter.

Rock, Paper, Scissors

The following plays a game of Rock, Paper, Scissors against the user. Input is accepted as a single character from STDIN: r ⇒ rock, p ⇒ paper, s ⇒ scissors. It then displays the computer's move, chosen randomly, as well as the result from the computer's perspective - Win!, Lose or Draw. The script may be run interactively, by typing your move followed by Enter.