Indefinite

From Esolang
Jump to navigation Jump to search

Indefinite is an esoteric programming language created by User: LolloDev5123. At the moment of writing, Indefinite doesn't have any implementation, and therefore it's just an idea.

Syntax & Overview

Indefinite is an imperative, declarative and functional language that supports a vast set of operators, conditions, loops, and variables.

Variable Definition

The following snippet of code declares a variable that can be accessed by any other statement in the code (global):

@x;

or:

@x = "Hello, world!";

The other following snippet of code declares a variable that can be accessed only by statements in the same function or scope of the variable (local):

$x;

or:

$x = "Hello, world!";

Function definition

The following snippet of code declares a global function:

fn @x(a):
   -- Comment!!
   => a + 10 -- => is equivalent to return
;

or in it's short arrow function form:

fn @x(a) => a + 10;

To declare a local function (eg. for functions inside functions), x just needs to be declared with $ instead of @.

Function Calls

The following snippet of code calls a global (or local) function:

::x(1);     --> 1 + 10 = 11

If statements

The following snippet of code determines which group of statements to run, depending on different conditions and expectations:

.if $x == 1:       -- if x == 1 then call y
 ::y();
,if $x % 2 == 0:        -- elseif x % 2 == 0 then call y
 ::y();
,      -- else call z
 ::z();
;      -- end

Instead of ending with a semicolon, if statements can also end with an unless statement:

.if $x % 2 == 0:
 ::y();
!: $x == 4;       --> unless x == 4, 
                       considers all the expressions above if condition is falsey <--

If expressions

Indefinite also supports the use of if expressions instead of statements: fn @x(a, b):

 .if a == 1: -- No need to use @ or $ for arguments
  => b
 ,
  => a
 ;
;

Becomes: fn @x(a, b):

 => .if a == 1: b , a;

For Loops

-- Start (i), increment => limit/end
$for 10 as i, 1 => 20: 
   .if i % 3 == 0: skip;
   => ::doSomething();
;

While Loops

$while stop false:
   ::doSomething();
;

Arrays and tables

An array can store variables and index them numerically, a table can do the same but it also supports string indexes. The following snippet of code declares a global array:

@[]x = 1, 3, 5, ..;
--> With the values 1, 3, and 5.
    The ".." after "5," is used to repeat the array a second time,
    producing 1, 3, 5, 1, 3, 5 <--

The following snippet of code declares a local table:

${}x = "x":"hello", "y":"world";
-- With the strings "hello" and "world" indexed as x and y.

::foo( $x{"y"} );
-- Calls the function foo with the y index of table x ("y":"world").
 

Operators

Arithmetic Operators

Instruction Description
x + y Addition
x - y Subtraction
x * y Multiplication
x / y Division
x % y Modulus
-x Unary Negation
√x Unary Square Root
x√y Root with custom base
x! Unary factorial
|x| Unary absolute value

Relation Operators (returns a bool)

Instruction Description
x == y Equal to
x != y Not equal to
x > y Greater than
x < y Smaller than
x >= y Greater than or equal to
x <= y Smaller than or equal to
x && y AND operator
x || y OR operator
!x Unary boolean inverse (!true = false)

Bitwise Operators

Instruction Description
x & y Bitwise AND
x | y Bitwise OR
x ^^ y Bitwise XOR
~x Unary Bitwise NOT
x << y Bitwise Left Shift
x >> y Bitwise Right Shift
x# Unary Popcount (Hamming Weight)

Misc Operators

Instruction Description
s .. s String Concatenation
#t Length of string or table/array
s * r String Repetition

99 Bottles of Beer

Note the "::" function call prefix, it is necessary only when the function is being called from the start of a line syntactically. When it's used, it also allows for functions to be called without any arguments.

$bottles = 99;
fn $plural(bottles): 
   => .if bottles != 1: "s", "";
;
$while bottles > 0:
   ::print(bottles.." bottle"..plural(bottles).." of beer on the wall")
   ::print(bottles.." bottle"..plural(bottles).." of beer")
   ::print("Take one down, pass it around")
   bottles--;
   ::print(bottles.." bottle"..plural(bottles).." of beer on the wall")
   ::print
;