Subtract

From Esolang
Jump to navigation Jump to search

Substract is a stack-based language.

Commands

command description
! push 1.
- pop a, pop b, push b-a.
: duplicates the top of the stack.
[ jump past the matching ] if the top of the stack is 0.
] jump back to the matching [ if the top of the stack is nonzero.
@ pop a, pop the a-th item of the stack and push it.
. output the character at the top of the stack.
, input a character and push it at the top of the stack. (EOF=-1)

all characters other than ! - : [ ] @ . , will be ignored.

Examples

This is still a work in progress. It may be changed in the future.

Interpreter

written in node.js

code=`
!!:-!--!:-!--!:-!--!:-!--!:-!--!:-!--!:-!--!:-!--!:-!--.
`;//input code here
input=`1234`//input input here

stk=[];tmp=0;ip=0;next=_=>ip<input.length?input.codePointAt(ip++):0;pop=_=>stk.length?stk.pop():0;eval([...code].map(_=>({
'!':'stk.push(1)',
':':'tmp=pop();stk.push(tmp);stk.push(tmp)',
'-':'tmp=pop();stk.push(pop()-tmp)',
'[':'while(pop()){',
']':'}',
'@':'tmp=pop();stk=stk.slice(0,stk.length-tmp).concat(stk.slice(stk.length-tmp+1)).concat([stk[stk.length-tmp]])',
',':'stk.push(next())',
'.':'process.stdout.write(String.fromCharCode(pop()))'
}[_]||'')).join`
`)

Computational class

if there is no restriction on number sizes, it is Turing-complete by trivial reduction from 3-cell brainfuck, with x+1=x-(-1)=x-((1-1)-1).