9

From Esolang
Jump to navigation Jump to search

9 is an esolang designed by User:Fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff with the goal of compiling to 90. As such, it cannot perform infinite loops(90 could), but has bounded loops built-in.

Declarations

There are 4 types of declarations. They may only appear outside of code blocks

1. Variable bits: a variable bit is one bit of information that can be turned on, but not off.

declare name;

2. Constants: can be either a bit, number, or string.

const name = value;

3. Subroutines: can be called within the program and act as blocks. Subroutines may not return anything

name { code }

4. Lambdas: equivalent to expressions, must be given names

name(arguments) => expression;

Simple statements

1. Increment a variable bit. This statement has three equivalent syntaxes.

name++;
++name;
punch name;

2. In-place Boolean addition. Equivalent to the C code variable |= value

name += value;

3. Running a subroutine. This statement has two equivalent syntaxes.

run subroutine;
subroutine();

4. Print statements. They take one argument (number, string, or boolean) and they print it.

print value;

A statement may contain just an expression, however this is ignored, unless it is undefined, in which case undefined behavior happens.

Compound statements

1. If statements. They may not have an else block, nor an else if block.

if condition { code }

2. Repeat statements. They repeat the code a specified number of times. These statements may have numbers, while if-statements may not.

repeat amount { code }

3. While statements. They repeat the code while a condition is true, with a specified limit on the amount of times.

repeat limit while condition { code }

4. Repeat with index statements. Same as repeat statements, but they keep track of the index in a variable. The variable may not be modified or declared anywhere in the program.

repeat amount with variable { code }

5. Conditional operator statements. They are composed of a simple statement followed by a question mark, and then a condition. If the condition is true, the statement is run.

statement ? condition;

6. With statements. They may only appear in loops and take an argument as a constant. On the nth iteration, the with clause activates.

with constant { code }

Operators

There are 4 boolean operators: !, ~, |, &. Note that ! and ~ have the same effect.
There is also 1 math operator: +. It works as you'd expect, but it is only useful for loops. Finally, there is the built-in prescan function, which takes an argument as a string and gets input from the user as that type. Note that the input is taken in before the code is executed, to make it possible to compile to 90.

Example Program

const amount = prescan();
// This program is a looping counter.
// Input the amount of iterations you want and the program will output a "staircase" of that height. 

main {
   repeat amount with index {
      repeat index {
         print "1";
      }
      print "\n";
   }
}

Truth-machine (loops for 10,000 iterations)

declare a;
const inf = 10000;

main {
   a += prescan();
   print a;
   if a {
      repeat inf {
         print 1;
      }
   }
}

Converting 9 programs to 90

Converting 9 programs to 90 is quite hard, and it is broken up into multiple steps.

Converting the program to just print, ++ and ?

Firstly, we take input from the prescan function replace the function with that. Next, we replace all lambdas and subroutines with their occurrences. So:

nand(x, y) => !(x&y)
hello { print "Hello, World!"; }
main { print nand(1, 0); }

would become:

main { print !(1& 0); run hello; }

Expressions

Next, we need to create and expression simplifier.
There will be 2 types, simple and complex. Simple takes the form:

AND: args
OR: args

Where AND [and if the last one is 0 or if it's 1.] OR is the result.
Complex takes the form:

ADD: numbers
   AND:    args
   OR:     args
   WEIGHT: Number
  (Multiple AND OR WEIGHT triplets)

To convert an expression to a simple expression, do this:
1. Take all the values before & and put them in AND
2. Take

All other statements

repeat and repeat with blocks can be made like this.

  1. Replace all repeat blocks with the ADD component of the complex expression.
  2. Add the WEIGHT component with an if statement.

while loops are just repeat loops but each iteration has an if block.
It is trivial to transform an if block into a ? statement.

+= statements can be ++ ?

Transforming this into 90

var++  ->   var $
?      ->   tobytes( state 90 ment )
  • print is ommitted.
  • tobytes converts it to a series of octets

Simple Expressions can be made by nested ifs.