Talk:Unassignable

From Esolang
Jump to navigation Jump to search

For people who want a better example, here is an :≠ program that I found lying around on my hard drive. --ais523 22:11, 13 February 2008 (UTC)

# Public domain by ais523 2006
# This example program counts from 1 to 9999, counting in binary but
# displaying the output as decimal. Most of the code is used for
# binary-to-decimal conversion.
# This program demonstrates many programming techniques that can be used
# in Unassignable, and tests most Unassignable features (apart from the
# ABCD).

declarations

integer input(16383)=0;
integer countmax(16383)=9999;
integer curdivisor(1023)=1000;
integer curdivover10(1023)=0;
integer digits2check(7)=4;
integer nextiterlimit(7)=3; #digits2check - 1
integer inputrestore(15)=10;
integer checkloop(1023)=1;
integer checklooptemp(1023)=0;
integer tenloop1(7)=0;
integer tenloop2(15)=0;
function main=activated;
function bin2decout=activated;
function out0=deactivated;
function out1=deactivated;
function out2=deactivated;
function out3=deactivated;
function out4=deactivated;
function out5=deactivated;
function out6=deactivated;
function out7=deactivated;
function out8=deactivated;
function out9=deactivated;
function checkdigit=deactivated;
function restoreonce=deactivated;
function goingup=deactivated;
function goingdown=deactivated;
function dividingby10=deactivated;
function dividingby10_2=deactivated;
function dividingby10_3=deactivated;
function calcchecklooptemp=deactivated;
function checklooptemprestore=deactivated;
function checklooptempdec=deactivated;
function nextiter=activated;

definitions
main
{
  run
  {
    countmax->loop;
  }
}

countmax
{
  iterate
  {
    input->increment(1);
    bin2decout->call;
  }
}

bin2decout
{
  run
  {
    # Output the variable
    digits2check->loop;
    # Restore temporaries, by adding/subtracting the known offset they
    # have from their original values. The values are effectively being
    # specified in binary, except -1 is a legal value for a bit. The 512s
    # are separated to prevent over/underflow.
    # (512 - 32 + 512 - 1 + 8 = 999)
    # +/-curdivisor's original value-1
    curdivisor->increment(512);
    curdivisor->decrement(32);
    curdivisor->increment(512);
    curdivisor->decrement(1);
    curdivisor->increment(8);
    checkloop->decrement(512);
    checkloop->increment(32);
    checkloop->decrement(512);
    checkloop->increment(1);
    checkloop->decrement(8);
    # +digits2check's original value:
    nextiterlimit->increment(4);
    nextiter->activate;
    # Output a newline
    io->output(N);
  }
}

digits2check
{
  iterate
  {
    # Output the current digit
    goingdown->activate;
    checkdigit->activate;
    checkloop->loop;
    checkdigit->deactivate;
    goingdown->deactivate;
    # Restore the value of the input, restoring tenloop
    goingup->activate;
    restoreonce->activate;
    checkloop->loop;
    restoreonce->deactivate;
    goingup->deactivate;
    nextiterlimit->decrement(1);
    nextiter->call;
  }
}

nextiterlimit
{
  underflow
  {
    nextiter->deactivate;
  }
}

nextiter
{
  run
  {
    # Figure out curdivisor/10
    dividingby10_3->activate;
    dividingby10_2->activate;
    curdivisor->loop;
    dividingby10_3->deactivate;
    dividingby10_2->deactivate;
    # Set curdivisor to curdivisor/10
    # (reversibly, by subtracting 9*curdivisor/10 from it)
    curdivover10->loop;
    # and clear curdivisor/10 (by subtracting curdivisor from it)
    dividingby10->activate;
    curdivisor->loop;
    dividingby10->deactivate;
    # Multiply checkloop by 10
    calcchecklooptemp->activate;
    checkloop->loop;
    calcchecklooptemp->deactivate;
    checklooptemp->loop;
    # and restore checklooptemp
    checklooptemprestore->activate;
    checklooptempdec->activate;
    checkloop->loop;
    checklooptemprestore->deactivate;
    checklooptempdec->deactivate;
  }
}

curdivisor
{
  iterate
  {
    dividingby10->call;
    goingdown->call;
    goingup->call;
    dividingby10_3->call;
  }
}

dividingby10_3
{
  run
  {
    tenloop1->increment(1);
    tenloop2->increment(1);
  }
}

curdivover10
{
  iterate
  {
    # The limitation of increment/decrement arguments to powers of 2
    # is not a problem in practice
    curdivisor->decrement(1);
    curdivisor->decrement(8);
  }
}

goingdown
{
  run
  {
    input->decrement(1);
    tenloop1->increment(1);
    tenloop2->increment(1);
  }
}

goingup
{
  run
  {
    input->increment(1);
    tenloop1->decrement(1);
    tenloop2->decrement(1);
  }
}

dividingby10
{
  run
  {
    curdivover10->decrement(1);
  }
}

# The following two objects lead to an effective integer tenloop(10)
# if incremented and decremented together, by 1 at a time.
# Although they refer to each other, if they are incremented and decremented
# together there is never recursion.

tenloop1
{
  overflow
  {
    tenloop2->increment(4);
    tenloop2->increment(2);
  }
  underflow
  {
    tenloop2->decrement(4);
    tenloop2->decrement(2);
  }
}

tenloop2
{
  overflow
  {
    tenloop1->decrement(2);
    dividingby10_2->call;
    checklooptempdec->call;
  }
  underflow
  {
    tenloop1->increment(2);
  }
}

dividingby10_2
{
  run
  {
    curdivover10->increment(1);
  }
}

checkloop
{
  iterate
  {
    # This shows how to do 4 different loops based on the same integer:
    # only one of these functions is activated at a time.
    checkdigit->call;
    restoreonce->call;
    checklooptemprestore->call;
    calcchecklooptemp->call;
  }
}

calcchecklooptemp
{
  run
  {
    checklooptemp->increment(1);
  }
}

restoreonce
{
  run
  {
    inputrestore->loop;
  }
}

inputrestore
{
  iterate
  {
    curdivisor->loop;
  }
}

input
{
  underflow
  {
    # The only activated function is the digit we need to output
    out0->call;
    out1->call;
    out2->call;
    out3->call;
    out4->call;
    out5->call;
    out6->call;
    out7->call;
    out8->call;
    out9->call;
  }
}

checklooptemp
{
  iterate
  {
    checkloop->increment(8);
    checkloop->increment(1);
  }
}

checklooptempdec
{
  run
  {
    checklooptemp->decrement(1);
  }
}

checklooptemprestore
{
  run
  {
    tenloop1->increment(1);
    tenloop2->increment(1);
    # This runs a multiple of 10 times, so restoring tenloop is unneccesary.
  }
}

checkdigit
{
  run
  {
    out0->activate;
    curdivisor->loop;
    out0->deactivate;
    out1->activate;
    curdivisor->loop;
    out1->deactivate;
    out2->activate;
    curdivisor->loop;
    out2->deactivate;
    out3->activate;
    curdivisor->loop;
    out3->deactivate;
    out4->activate;
    curdivisor->loop;
    out4->deactivate;
    out5->activate;
    curdivisor->loop;
    out5->deactivate;
    out6->activate;
    curdivisor->loop;
    out6->deactivate;
    out7->activate;
    curdivisor->loop;
    out7->deactivate;
    out8->activate;
    curdivisor->loop;
    out8->deactivate;
    out9->activate;
    curdivisor->loop;
    out9->deactivate;
  }
}

out0
{
  run
  {
    io->output(0);
  }
}

out1
{
  run
  {
    io->output(1);
  }
}

out2
{
  run
  {
    io->output(2);
  }
}

out3
{
  run
  {
    io->output(3);
  }
}

out4
{
  run
  {
    io->output(4);
  }
}

out5
{
  run
  {
    io->output(5);
  }
}

out6
{
  run
  {
    io->output(6);
  }
}

out7
{
  run
  {
    io->output(7);
  }
}

out8
{
  run
  {
    io->output(8);
  }
}

out9
{
  run
  {
    io->output(9);
  }
}