Crab

From Esolang
Jump to navigation Jump to search

Crab (carriage returns are bountiful) is a new language based on an idea i was playing around with some time ago, CRalphabet. Crab uses some fairly unique language constructs(see Below). Version 1.0 of the Crab interpreter is currently under development.

Variables

Variables in Crab are defined in separate files (*.vdf) and are passed to the interpreter via calls in the source file. i.e.

<REAL1.VDF>
<TYPE/REAL>
REAL1 = REAL(10.0)

Line 1: declares the variable name. This must match the file name. i.e.: variable name REAL1 must be declared in file REAL1.vdf.

Line 2: Sets the Variable type to REAL(floating-point number). The built in types are;

REAL e.g. 10.0
STRING e.g. "Hello, World"
BOOL e.g. true/false
STORAGE see Storage Variables.
REAL1 = REAL(10.0)

This is the actual variable assignment. It must be in the form:

name = type(value)

Calling Variables

Once variables are created in .vdf files, they can be loaded and manipulated by the source file.

Call<REAL1>

This is similar to C #include or Java import, except instead of loading libraries, you just load variables.

Commands/Operators

Crab is similar to most non-esoteric languages in that it makes use of the common mathematical operators. Crab can not handle modulo(%) calculations(at least not explicitly). Here is a list of Crab operators:

+   addition e.g. 2+2
-   subtraction e.g. 2-2
*   multiplication e.g. 2*2
/   division e.g. 2/2
++  increment e.g. 2++
--  decrement e.g. 2--
^^  square e.g. ^^2 (or 2^2)
^   power e.g. 2^10
^^/ square root e.g. ^^/4(=2)
^/  root e.g. 4^/2(=2)
&e  e
&pi Pi
%   percentage e.g. 75%100(=75)
AND &&(logical AND) e.g. REAL1 = 10.0 AND REAL1 > 0
OR  ||(Logical OR) e.g. REAL1 = 10.0 OR REAL1 > 0
IS  equality test e.g. REAL1 IS 10.0(returns true if variable REAL1 == 10.0)
NOT equality test e.g. REAL1 NOT 0.0(returns true if REAL1 != 0.0)
>   greater than test e.g. REAL1 > 9.9
<   less than test e.g. REAL1 < 10.1

Crab v1.0 has very few commands. These are:

Q    Quine command. Prints source code to the prompt
E    Prints e(transient number) to the prompt.
PI   Prints Pi to the Prompt.
t    Gets user input from the prompt.
r    Flushes the prompt with a carriage return.
s    Prints a Space to the prompt.

Note that any expression or not assigned to a variable or declared inside a function is printed to the prompt. i.e.:

2+2 prints 4.
var = 2+2 assigns var the value of 4 and does not print.

The maximum number of operands in an expression must not be greater than two, and must not contain white-space between the operands and the operator.

2+2     Legal.
2 + 2   illegal.
2+2+2   illegal.
2+ 2 +2 illegal.
etc.

STORAGE Variables

Because of the 2 operand limitaton in expressions, Crab needs a way to combat this. It does so with STORAGE variables. These are a special variable type that can hold the result of any mathematical computation. The following code demonstrates their usage:

## .VDF file ##
<BOX.VDF>
<TYPE/STORAGE>
BOX = #STORAGE

This creates the variable named BOX and assigns its type to STORAGE.

## .CR file(source code) ##
Call<BOX>              ## Loads BOX.VDF ##
BOX = expression   ## Assigns the value of BOX to the result of expression i.e. 2+2##
BOX = BOX+BOX          ## BOX now equals expression+expression ##
BOX                    ## Prints the value of BOX to the prompt ##

Strings

Crab has a bizarre way of printing Strings. Instead of:

"Hello"

Strings must be declared 1 character per line. And instead of using actual characters, you use varying numbers of '\n' characters. i.e.:

\n
\n\n
\n\n\n
\n\n\n\n

Prints "abcd" to the prompt.

\N
\N\N
\N\N\N
\N\N\N\N

Prints "ABCD". Use a single '\n' for a, 2 for b 3 for c etc., up to 26 for z.

Functions

Crab functions take the form: F[x]. Where F is the function name and x is a list of any arguments to be passed to F

printString[STRING s]: s

This example creates a function called printString. It takes a predefined STRING variable[s] its only argument. Functions are called like so:

Call<STRING1>
printString[STRING s]: s
printString[STRING1]

This example assumes you have created a .VDF of type STRING. In Crab v1.1, functions will declarable in .VDF variable files.

Loops

Instead of the usual If, While, For and Do loops, Crab has a single LOOP[x][n] type. [x] is replaced with a list of expressions to be evaluated.[n] must be replaced with an Integer(-1 -> positive_infinity). This number sets the number of loop iterations. e.g.:

LOOP[10.0 IS 10.0][1] ## This is an IF loop ##
LOOP[10.0 IS 10.0][-1] ## This is a WHILE loop ##
LOOP[i|i < 10.0|i++][i] ## This is a FOR loop ##

Crab does not currently support SWITCH/CASE looping, although future versions may.