Calcutape

From Esolang
Jump to: navigation, search

Calcutape is a programming language designed to calculate mathematical expressions on a tape, designed (along with the interpreter) by Darkrifts (talk). It is interpreted, with no compiler made as of yet. The interpreter can be found here, and has only been tested on Windows with .NET 4.0.

Documentation

The language is stack-based and has only 1-character commands.

Math

  • 0 - 9 - A single digit number pushes the respective number onto the stack.
  • + - Pulls the two top numbers off of the stack, and pushes their sum on the stack.
  • - - Pulls the two top numbers off of the stack, and pushes their difference on the stack. (1st - 2nd)
  • * - Pulls the two top numbers off of the stack, and pushes their product on the stack.
  • / - Pulls the two top numbers off of the stack, and pushes their quotient on the stack. (1st / 2nd)
  • : - Pushes a random number from 1 to 999 onto the stack.

Output

  • % - Pulls the top number from the stack and outputs it as a number
  • @ - Pulls the top number from the stack and outputs it as a character

Stack control

  • | - Swaps the position of the top two numbers on the stack. (1st is 2nd, and 2nd is 1st)
  • _ - Duplicates the top number on the stack.
  • & - Pulls the top number off of the stack (N) and pushes a copy of the Nth number to the stack.
  • $ - Pulls the top number off of the stack and throws it away.
  • ^ - Pulls the top number off of the stack (N) and waits for N milliseconds.
  • = - Clears the screen.
  • ? - Closes the application immediately.
  • # - One of the most powerful commands. It reads the number of the top value on the stack, leaves it where it is, and performs an operation based on it (Stored as N). If N is 0, it reverses the direction of the interpretation (turns around properly on the far left end). If N is greater than 0, it skips N commands. If N is less than 0, it passes by with no operation occurring.

In the below example, the number the # statement lands on is 6:

5#1234567890

The stack (From bottom to top) is {5, 6, 7, 8, 9, 0} at the end of that program. (The 5 from before the # is not removed)

Input

  • V - Reads a single character to the stack (does not display on the user's screen) and moves on. This is the only way of getting input, and waits until a key is pressed.

Comments

Comments are done by opening brackets in some form, such as (, [, or {, and are closed by the opposite kind, such as ), ], or }. Any kind of comment opener can be closed with any kind of comment closer. (((((() is a closed comment, and (()) has an extra closer, which does nothing.

Strings

Strings in Calcutape are somewhat complicated. As you cannot store string or character literals, you have to push multiple numbers onto the stack (in reverse order, since the last one in is the first one out) based on ASCII values.
A string could be made by creating and pushing all numbers, and outputting them all at the end, or by outputting each number as it is created.
It is recommended that instead of creating each character number from 0, that you create one instance of a median number and use _ commands to copy it for each letter.
A good base to copy repeatedly for strings is 100, as it is simple to compute (52*_*)

Sample programs

Extra math

Exponents can be handled in this way:

_* (squared) __** (cubed) ___*** (4th) {And the pattern continues a^b = (b-1 underscores and b-1 *s)

You can invert a sign or turn a cell to 0 in this fashion:

_- (turn to 0) _-- (to negative)

"Hello World" program

As outputting with % or @ does not create a new line automatically (requires outputting a newline character with @), you can string sentences and words together, like this:

48*1+(!)52*_*(d)52*_*8+(l)52*_*9+5+(r)52*_*9+2+(o)99*6+(W)48*( )52*_*9+2+(o)52*_*8+_(ll)52*_*1+(e)89*(H) @@@@@@@@@@@@(Output area: Outputs "Hello World!")

Parenthesis were added after each group of numbers to show what each of them equated to (Such as 52*_*1+ (101) being the same as "e")

If you need help in finding the number value for a character, you can use this ASCII table or the 2nd cat program below

Cat program

V@ (Only outputs once)

or the alternative version of the Cat program:

V% (Outputs the number value of the input)

Key to number

To obtain a number press on the keyboard as its respective number, use this:

V86*|- (Input comes before the 86* calculation)

or

86*V- (Input comes after the 86* calculation)

The number at the top of the stack will be dependent on the number of the keypress (Pressing a number key with this gives the number exactly)

Infinite Loop

To create an infinite loop (similar to a "while(true)" in 'better' programming languages) do:

1##0$$0#

This works because placing 1 on the stack skips the first # command, jumping to the 0. Having 2 numbers on the stack, we want to throw them both away, and use 2 $ commands.
Pushing a 0 onto the stack causes the program to reverse itself and grab a second 0 as well, meaning we can throw away 2 more numbers, and the cycle continues.

Quine program

No Quine program (or a program that prints its source code) has been found yet, and there may not be one. If anyone finds a quine program, please edit this page and let me know :P Darkrifts (talk)

Implementations

A haskell implementation can be found here. Note: The # instruction is not implemented, and will throw the error "No.".

A C# implementation can be found here. This does have the # instruction implemented.