One day, at school, I overheard some people talking about math homework, and getting problems wrong on said homework. One of them suggested they say that the number they got as an answer was actually a variable that stood for the correct answer... which gave me an idea.
Statements are entered one-per-line. Each statement consists of some expression (described below). Some expressions are special:
while
loop.
-1
at compile time end a loop.
42
will cause the current answer to be displayed. The answer is always 9
(or the value of the variable 9
).
-42
will prompt the user for input. User input is stored in the variable 5
.
In order of precedence:
π
, ∞
, Ø
:
), it will be replaced with its value. The replacement only happens once, when the number is directly in the code (i.e., not a result of an expression); for example, if the variable 5
has the value 6
and the variable 6
has the value 7
, typing 5
will give the value 6
, not 7
; likewise, typing 2+3
will give the value 5
, not 6
or 7
. π
, ∞
, and Ø
(uppercase O with slash) respectively give pi, infinity, and not-a-number.
∅
(empty set) for Ø
, and τ for 2π.
[expression]
[5]
would give 7
, and [2+3]
would give 6
. If the variable has not been assigned, the brackets don't do anything.
-
-2
negates the value of the variable 2
, rather than finding the value of the variable -2
.
^
*
, /
, %
/
, 1
is used instead. If the prefix form of /
is used after an operator of equal or higher precedence, the result is undefined.
+
, -
=
, ≠
, <
, ≤
, >
, ≥
1
for true and 0
for false.
&
, |
, !
&
and |
is only evaluated if necessary.
:
:
operator is a number or a bracketed expression, one less level of indirection is used than normal. Reassigning 0
or 1
will change how math works; assigning 0
and 1
to the same value is not recommended.
Any character not mentioned above (including all letters, apostrophe, and comma) is treated as whitespace.
Programs written in Brainfuck can be translated into 42 as follows:
Brainfuck | 42 |
---|---|
(start of the program) | 10:.5
|
. (output) | 9:[10]
|
, (input) | -42
|
< (move left) | 10:10-1
|
> (move right) | 10:10+1
|
+ (add 1) | [10]:[10]+1
|
- (subtract 1) | [10]:[10]-1
|
[ (start loop) | [10]≠0
|
] (end loop) | -1
|
This has cell sizes up to the largest integer that can be stored in a double-precision floating-point number, memory up to the largest number ending in .5 that can be stored, and memory only extends to the right (to make memory extend to the left, copy the code at the end of > to <; this code is needed to initialize the memory to 0). Variable 10 stores the current position in memory; the actual memory values are at n.5 for integers n.
Insert:
Output:
Input:
Old Java version: Applet, Download (125 KB), Download source (19 KB) (downloads also include Try and CLWPA)