Cutw

From Esolang
Jump to navigation Jump to search

Cutw is an esoteric programming language that is made out of cute characters.

Commands

j - Move pointer right.
i - Move pointer left.
w - Puts input at cell at pointer.
t - Outputs cell at pointer as ASCII.
u - Branflake's [.
c - Branflake's ].
n - Increments cell at pointer.
m - Decrements cell at pointer.

Examples

Cat

wt

hi

nnnnnnnnujnnnnnnnnnnnnnimcjtnt

JavaScript interpreter

function cutw(program,input){
    var stack=[];
    var matches={};
    var ip=0;
    var tape=[];
    var p=0;
    var output='';
    for(let i=0;i<1000000;i++){
        tape.push(0);
    }
    for(var i=0;i<program.length;i++){
        if(program[i]=='u'){
            stack.push(i);
        }
        if(program[i]=='c'){
            if(stack.length==0){
                throw new Error('c does not match u');
            }
            var mt=stack.pop();
            matches[mt]=i;
            matches[i]=mt;
        }
    }
    if(stack.length!=0){
        throw new Error('u does not match c');
    }
    while(ip<program.length){
        if(program[ip]=='n'){
            tape[p]=tape[p]+1;
            if(tape[p]==256) tape[p]=0;
            ip=ip+1;
        }
        if(program[ip]=='m'){
            tape[p]=tape[p]-1;
            if(tape[p]==-1) tape[p]=255;
            ip=ip+1;
        }
        if(program[ip]=='j'){
            p=p+1;
            if(p>=1000000){
                throw new Error('Pointer overflow');
            }
            ip=ip+1;
        }
        if(program[ip]=='i'){
            p=p-1;
            if(p<0){
                throw new Error('Pointer underflow');
            }
            ip=ip+1;
        }
        if(program[ip]=='w'){
            if(input==''){
                tape[p]=0;
            }else{
                tape[p]=input.charCodeAt(0)%256;
                input=input.slice(1);
            }
            ip=ip+1;
        }
        if(program[ip]=='t'){
            output+=String.fromCharCode(tape[p]);
            ip=ip+1;
        }
        if(program[ip]=='u'){
            if(tape[p]==0){
                ip=matches[ip];
            }else{
                ip=ip+1;
            }
        }
        if(program[ip]=='c'){
            if(tape[p]!=0){
                ip=matches[ip];
            }else{
                ip=ip+1;
            }
        }
        if(!('nmwtucij'.includes(program[ip]))){
            ip=ip+1;
        }
    }
    return output;
}

See also