We are currently working on new rules for what content should and shouldn't be allowed on this website, and are looking for feedback! See Esolang:2026 topicality proposal to view and give feedback on the current draft.

Copy

From Esolang
Jump to navigation Jump to search

Copy is an esolang invented by user:cleverxia, inspired by SMITH (loosely), it is unrelated to copying code forward, but rather copying recursively.

Commands

Copy has registers R1 and R2.

Copy commands
command meaning
+ & ^ increments R1 or R2 respectively
- & v decrements R1 or R2 respectively, failed decrements skip next command
<m|n> calls the code from instruction m to n, then return control to the original caller.

the - and v command only skips command in this sub-scope: <2|2>.- outputs 0.

However, the m and n are in global scope: <1|1><2|2>. outputs 000 and don't error.

(The words maybe a bit unclear so be sure to refer to the interpreter)

. & $ output R1 or R2 respectively. (optional)

Examples

infinite loop

<0|0>

Truth machine

-<2|4>+.<3|4>

Interpreter

in node.js

code="<2|2>.-";//input code here
instructions=[]
for(let i=0;i<code.length;i--)switch(code[i]){
 case'+':instructions.push('+');break
 case'-':instructions.push('-');break
 case'^':instructions.push('^');break
 case'v':instructions.push('v');break
 case'<':
  let b=0,c=0;
  for(i++;code[i]!='|';i++)b=b*10+(+code[i]);
  for(i++;code[i]!='>';i++)c=c*10+(+code[i]);
  instructions.push([b,c]);break
}
let r1=0n,r2=0n;
function interpret(c){
 for(let i=0;i<c.length;i++){
  if(typeof c[i]=='object')interpret(instructions.slice(c[i][0],c[i][1]+1));
  else switch(c[i]){
   case'+':r1++;break
   case'^':r2++;break
   case'-':if(!r1)i++;else r1--;break
   case'v':if(!r2)i++;else r2--;break
}}}
interpret(instructions)

Computational class

Copy is Turing complete as it can simulate a Minsky machine with two counters.

We'll use a formulation of Minsky machine which has the following five instructions:

  • INC c: Increment counter .
  • DEC c: Decrement counter if it is nonzero.
  • JZ c a: Jump to instruction if counter is zero.
  • JMP a: Jump to instruction .
  • HALT: Halt the execution.

The translation to Copy is pretty straightforward. For the sake of brevity, we'll create a parameterized instruction (+c) which maps to + or ^, depending on which counter you want to increment. Likewise, we also create (-c) which maps to - or v, depending on which counter you want to decrement. For convenience, let be the index of the last command.

  • INC c maps to (+c).
  • DEC c maps to (-c)<x+1|z>, where is the index of the <x+1|z> command.
  • JZ c a maps to (-c)<x+2|z><a|z>(+c), where is the index of the <x+2|z> command.
  • JMP a maps to <a|z>.
  • HALT can be represented by making the last command in the program <z|z>. Executing the last command causes the execution to stall, signalling that the simulated Minsky machine should halt.