-
Notifications
You must be signed in to change notification settings - Fork 0
EarScript Code & Tokens
You can try the code snippets in this page in Try EarScript online!
Since EarScript is not as good at dealing with strings, instead of greeting the world, our first program will instead answer what the meaning of life, the universe and everything is.
# The meaning of life, the universe, and everything.
=42 .
- In EarScript, you can write single line comments by using
#
, which will make the machine ignore anything after the#
until the end of the line. There are no multi-line comments in EarScript. -
=42
is a token whose head is=
, meaning this token assigns a value to the current cell, and whose tail is42
, meaning the value that is assigned is the number42
. -
.
is another token, which corresponds to the default output.
Other than comments, an EarScript source file should only contain tokens. Each token starts with an operator character which is a symbol in the following list:
+ - * / % ! & ? = > < : ^ ` ; $ @ ' " ~ \ . , | ( ) [ ] { }
Then, a token can have any alphanumeric characters like lowercase letters, upper case letters, digits, or underscores.
Tokens cannot have any spaces inside them. Whitespace can be used to format your code, and to separate tokens from one another, but tokens can also be directly next to each other.
# The following two lines are equivalent =42+3[4-.] =42 +3 [4 - . ] # The following line has errors! Tokens can't have spaces inside them = 42 + 3 [ 4 - . ]
Let's look at an example to calculate Fibonacci numbers
# Fibonacci numbers
\ncol2 =1>=1>
[10 .+l> ]
The tokens would be \ncol2
, =1
, >
, =1
, >
, [10
, .
, +l
, >
and ]
.
Each token has a head and a tail, the head always contains the operator character, but some tokens can have larger heads.
- The head of a token indicates what type of instruction it is.
- The tail of a token indicates the value which affects the instruction.
For example,
+2
adds2
to the current cell, whereas>5
moves the pen5
cells to the right.
If the operator character is one of \ . , { [ (
, then the head contains everything until the first number or underscore.
For example, in \nrow2
the head is \nrow
and the tail is 2
, and in {r0
, the head is {r
while the tail is 0
.
The tail of a token can be a number like in +4
, but there's a few other things it can be.
- Negative numbers use an underscore instead of a minus; for example
*_1
multiplies by-1
. - An empty tail is equal to 1, for example
+
is the same as+1
. - You can refer to the current cell of a different table by using a table reference,
like
+other
which adds the value from the tableother
to the current cell. - You can use relative references like
+l
to use the values from cells in the same row or column as your current cell.
Next: Operators & I/O
Prev: EarScript Machines
Back to Wiki Home