Loader

From Esolang
Jump to navigation Jump to search

Loader is an esolang by SuperJedi224, in which recursive module loading is the principal mode of recursion and iteration.

A program consists of the main module (the one that will be directly called from the interpreter) and zero or more secondary modules. The file name for a module is the name of the module (which may not contain any spaces) with the extension .ldr.

Modules may be loaded on either a copy of the current memory space (load), or on the current memory space itself (loadip).

The memory space consists of zero or more variables (whose names consist exclusively of uppercase letters), each of which may store a 32-bit sigined integer, and a bidirectionally unbounded tape of 32-bit signed integers.

Language Details

Unary Operators

- Unary Negation
~ Boolean NOT operator (yields 1 if the argument is 0, 0 otherwise)
* Splat operator (for pointers; when used in an expression, yields the value on the tape pointed at by the argument)

Instructions

incr <variable> -> Increment that variable; when used in an expression, yield the variable's new value. All variables may be assumed to be preinitialized to 0.
incr *<value> -> Increment the value on the tape pointed at by this value; when used in an expression, yield the target's new value
decr <variable> -> Decrement that variable; when used in an expression, yield the variable's new value
decr *<value> -> Decrement the value on the tape pointed at by this value; when used in an expression, yield the target's new value
erase <index> -> Zero all cells on the tape from the specified index through the next cell which is already zero. May not be used in expressions.
exit -> Exit the program
load <module> -> Load and execute the specified module on a copy of the current memory. When used in an expression, yields the value that module call returned.
loadip <module> -> Load and execute the specified module on the current memory itself. When used in an expression, yields the value that module call returned.
print <values...> -> Printing the specified values to the output, in the specidfied order and on separate lines. May not be used in expressions.
printbool <value> -> Print TRUE if the specified value is nonzero, and FALSE otherwise; followed by a newline. May not be used in expressions.
printchar <values...> -> Prints the specified values as ascii characters, in the specified order. Then, prints a newline. May not be used in expressions.
printf "<string>" <subs...> -> Prints the specified string with the specified values substituted for instances of %n (which are replaced with numeric values), %c (which are replaced with ascii characters), and/or %b (which are replaced with TRUE if the corresponding value is nonzero, and FALSE otherwise). 
printst <index> -> Prints as characters the values on the tape starting at that index and continuing up to (but not including) the next 0. Then, prints a newline. May not be used in expressions.
write "<string>" <index> -> Write to the tape the ascii codepoints of the characters in that string, starting at the specified index on the tape and continuing rightwards. May not be used in expressions.
readline <index> -> Reads the next line of input onto the tape (as the corresponding ascii codepoints), starting at the specified index on the tape. When used in expressions, yields the number of characters read. On EOF, does nothing and returns 0.
set <variables...> =<value> -> Set each variable in the (comma-separated) list to the specified value, *<expression> may be substituted for any variable. When used in an expression, yield the specified value.
throw "<string>" <subs...> -> Throws an error. Substitutions work as with printf.
warn "<string>" <subs...> -> Throws a warning. Substitutions work as with printf.
yield <value> -> Exit the current module and return the specified value. "yield 0" is implicitly appended to every module. May not be used in expressions.

Special Expressions

@IN -> Yields a number taken from the input

Conditionals

Single-line conditionals may be formed by preceding that line of code with an expression, followed by a colon. That line will only be run if the expression evaluates to a nonzero value.

Comments

As in GridScript, single-line comments begin with double exclamation points. However, unlinke in GridScript, a comment can be appended to the end of a line and is not required to be put on its own line.

Interpreter

A java interpreter can be found here.

This interpreter takes the name of the main module from a command line argument or STDIN, and takes all program input from STDIN.

Example Programs

Count down from 100

(run from main.ldr)

~B:set A,B =100
print A
decr A
A:load main

Alternate Version in 2 modules

(main module, run from any .ldr file)

set A 100
load decrement

(decrement.ldr)

print A
decr A
A:load decrement

Truth Machine

(run from main.ldr)

~B:set B =@IN
~B:print 0
~B:exit
print 1
load main

Alternate Version in 2 modules

(main module, run from any .ldr file)

set B =@IN
B:load one
print 0

(one.ldr)

print 1
load one

Hello, World!

printf "Hello, World!"

Cat Program

(Run from main.ldr)

erase 0
readline 0
*0:printst 0
*0:load main

99 Bottles of Beer

(Run from main.ldr)

~I:set I,BOTTLES =99

~BOTTLES:printf "No more bottles of beer on the wall!"
~BOTTLES:printf "No more bottles of beer!"
~BOTTLES:printf "You go to the store and you buy some more,"
~BOTTLES:printf "99 bottles of beer on the wall!"
~BOTTLES:exit

set PLURAL =BOTTLES
decr PLURAL

PLURAL:printf "%n bottles of beer on the wall!" BOTTLES
PLURAL:printf "%n bottles of beer!" BOTTLES

~PLURAL:printf "1 bottle of beer on the wall!"
~PLURAL:printf "1 bottle of beer!"

printf "You take one down and you pass it around,"

decr BOTTLES
decr PLURAL

BOTTLES:PLURAL:printf "%n bottles of beer on the wall!" BOTTLES
BOTTLES:~PLURAL:printf "1 bottle of beer on the wall!"
~BOTTLES:printf "No more bottles of beer on the wall!"

printf ""
load main