Ferntape

From Esolang
Jump to navigation Jump to search

Instructions & Overview

Ferntape is a single queue Turing complete coding language made by user:RainbowDash on March 19th 2024. Ferntape consists of 1 register, and 1 queue. The queue can only be "Pulled" left, so that the last item in the queue is now the first item, like a circular tape with instructions on it. Every command here follows queue logic, First in first out. Ferntape however can read item 0 in the queue. The "tape" can be made longer or shorter at the user's will.

The Ferntape logo
pull Shifts the queue left
pop Pop the queue by 1 item and then set that output to the register
push Push the register to the queue
clr Clear the register
repl Replace last item with the register
inc Increment the register
dec Decrement the register
copy Copy item 0 from the queue to the register
deci Output item 0 in decimal
asci Output item 0 in ASSCI
del Delete last item in the queue
inp Get input from user then push each ASCII letter to the queue
[ Go to the instruction after the matching ] if the last cell is zero
] Go back to the instruction after the matching [ if the last cell is not zero

When the increment or decrement instruction is followed by a number, it will increment or decrement the register by that many times. If no number follows the instruction it will increment or decrement the register by 1.

Example Programs

Hello world!

Outputs "Hello world!"

push inc 72 push inc 101 push inc 108 push inc 108 push inc 111 push inc 32 push inc 119 push inc 111 push inc 114 push inc 108 push inc 100 push inc 33 push pull [ pull asci ]

Cat program

push inp pull [ pull asci ]

Explanation of code above

 push (Push a zero into the queue to mark a stopping point)
 inp (Ask for user input)
 pull (Make sure 0 is not at the end of the queue)
 [
  pull asci (Go through every character and print it out)
 ]

Addition program

This program asks the user for 2 numbers, 1-9 anything else and the program can crash. It adds both of the given numbers and outputs the sum.

inp copy dec 48 repl deci clr inp copy dec 48 pull repl pull deci clr copy push [ pull copy pull pull dec repl copy inc pull pull repl pull ] del del deci

Output text generator

This JavaScript code will write a Ferntape program for you that will output your string.

let text = "Hello World!"

let output = "";
output += ("push ");
for (const char of text) {
    output += ("inc " + char.charCodeAt(0) + " push ");
}
output += ("pull [ pull asci ]");

Disan Count

This program outputs all of the even numbers between 100 and 0. Changing the 100 in the program allows the program to find all the even numbers between that number and 0. The reason this program is so long is because it has to check if the given number is even or odd first.

inc 100 push copy push [ pull copy dec dec pull repl ] pull copy pull inc repl 
[ pull copy pull dec repl copy inc pull repl pull ] copy pull pull dec 
pull repl pull del [ copy deci dec dec repl ] clr repl deci

Interpreter in JavaScript

function ferntape(prgm){
    let reg = 0;
    let i = 0;
    let queue = [];
    let program = prgm.toLowerCase().split(" ");
    while (i <= program.length){
        let instruction = program[i];
        if (instruction == "]") {

            if ((queue[queue.length-1] || 0) == 0) {
                i++;
            } else {
                let depth = 1;
                while (depth > 0) {
                    i--;
                    if (program[i] === "]") {
                        depth++;
                    } else if (program[i] === "[") {
                        depth--;
                    }
                }
            }
        } else if (instruction == "[") {
            if ((queue[queue.length-1] || 0) == 0) {
                let depth = 1;
                while (depth > 0) {
                    i++;
                    if (program[i] === "[") {
                        depth++;
                    } else if (program[i] === "]") {
                        depth--;
                    }
                }
            } else {
                i++;
            }
        } else if (instruction == "inp"){
            var input = prompt("");
            var inputASSCI = input.split('');
            for (let h = 0; h < inputASSCI.length; h++) {
                 queue.splice(0, 0, inputASSCI[h].charCodeAt(0));
            }
            i++
          } else {
            switch(instruction) {
                case "del":
                    queue.splice(queue.length-1, 1);
                break;
                case "copy":
                    reg = queue[0];
                break;
                case "deci":
                    console.log(queue[0])
                break;
                case "asci":
                    console.log(String.fromCharCode(queue[0]));
                break;
                case "repl":
                    queue[queue.length-1] = reg;
                break;
                case "clr":
                    reg = 0;
                break;
                case "inc":
                    if (isNaN(Number(program[i+1])) || typeof program[i+1] === "undefined") {
                        reg += 1
                    } else {
                        reg += Number(program[i+1]);
                        i++
                    }
                break;
                case "dec":
                    if (isNaN(Number(program[i+1])) || typeof program[i+1] === "undefined") {
                        reg -= 1
                    } else {
                        reg -= Number(program[i+1]);
                        i++
                    }
                break;
                case "pop":
                    reg = queue[queue.length-1];
                    queue.splice(queue.length-1, 1);

                break;
                case "push":
                    queue.splice(0, 0, reg);
                    reg = 0;
                break;
                case "pull":
                    queue.splice(0, 0, queue[queue.length-1]);
                    queue.splice(queue.length-1, 1);
                break;
            }
            i++
        }
    }
}