Alight

From Esolang
Jump to navigation Jump to search

Alight is a two-dimensional esoteric programming language by Abraham Karplus, distinguished from countless others by not using single character commands. In fact, the commands for Alight look somewhat like those for a non-esoteric imperative programming language.

Control Flow

Commands are separated by the semicolon character, ";".The program begins at the command begin, which may (like any command) be written forwards, backwards, up, or down. It continues in that direction through other commands, some of which may make it change direction. Any direction changes are done as if the semicolon at the ends of the command were the command. The program continue until it reaches the command end in the direction it is going (encountering, say, dne will not).

Commands

Whitespace is allowed in a command where it does not split an identifier.

<nothing>

A semicolon followed immediately by another is a nop command, useful for spacing.

var x

Create a new variable called x, initialized to nil. Variable names must be alphanumeric (any length), and not reserved words.

set x <expr>

Set x to the result of <expr>. Enhanced assignment is not supported; use x=x+1 instead.

skip <expr>

If <expr> is true, skips the next command.

turn <expr>

If <expr> is true, turns a quarter turn left, otherwise turns a quarter turn right.

inp x

Read a character from input into x.

out x

Output x as a character.

wait <expr>

Waits for <expr> seconds, which need not be an integer.

Expressions

Types

There are three data types in Alight: numbers, lists, and special values. Characters are represented as numbers and strings as lists of characters. Lists may contain either numbers or other lists (but not both). Lists are created with the syntax

[<expr>, <expr>, <expr>]

and may contain zero or more items. The syntax

'c

represents the number 99, the ASCII value of that character. The syntax

"str"

creates a list of characters ['s, 't, 'r]. It is legal to use the semicolon character within a string. Lists are indexed beginning at 0.5 (Any index that is not precisely 0.5+k for nonnegative integer k is an error).

Constants

There are currently four special values in Alight

nil
eof
left
right

They represent an absence of data, the end of file, boolean true, and boolean false, respectively. New variables initially contain nil. Lists of numbers or lists may contain these values.

Operations

All operators are postfix, so grouping symbols are not required. All operations except the unary logical not are binary.

Arithmetic

The standard arithmetic operations,

+ - * /

are used. Additionally, the trunc builtin function returns the truncation of a number, and sign returns the sign of a number. The plus and times operators may be used for list concatenation and repetition, respectively.

Comparison

The operators for comparison are

= < >

representing equality, less-than, and greater-than, respectively. Other comparisons may be created by chaining these with the logic operators. These can compare any values (not just numbers), though less-than and greater-than will always return false on other types, and objects of different types return false on comparison.

Logic

The logic operators are

! & | ^

representing not, and, or, and exclusive or, respectively. Bitwise operators are not supported.

List Functions

The function at takes a list, a index number, and optionally a value. If called with two arguments, it returns the value of the list at that point (or nilif the list is too short). If called with three, it return a copy of the list with that point set to the value (raising an error if the value is the wrong type of the list is too short). The function len takes a list and optionally a positive integer. If called with one argument, it returns the length of the list. If called with two, it appends the specified number of nil values to the end of the list, returning the new list.

Calling Functions

Any function may be called with the syntax

<function>{<argument>, <argument>, ...}

Defining Functions

A function is defined in much the same manner as a program, except that the keyword begin is replaced with func, and func and end have a slightly different syntax:

func <name>{<argument>, <argument>, ...}

Defines the function with a given name and arguments (variable names).

end <value>

Returns from the function as the given value. If unspecified, nil is returned.

Namespaces

All functions names are in a separate global namespace. Each function gets its own variable namespace when called and cannot access any other.

Evil Hack

There is nothing preventing the code for a function from merging with the main code or that of another function. When this happens, the function continues as always in its own namespace until it reaches some end command, at which point control returns to wherever it was in the code.

Examples

Cat

Using turn

                              d
                              n
                              e
begin;var c;inp c;turn c = eof;
           t                  t
           h                  u
           g                  r
           i                  n
           r                   
                              r
           n                  i
           r                  g
           u                  h
           t                  t
           ;thgir nrut;;;c tuo;

Using skip

begin;var c;inp c;skip c = eof;turn right;end
           t                             t
           h                             u
           g                             r
           i                             n
           r                              
                                         r
           n                             i
           r                             g
           u                             h
           t                             t
           ;thgir nrut;;;;;;;;;;;;;;c tuo;

Reversed Cat

begin;var c;var l;set l [];inp c;;;;;;;;;;;;;;;;;;;skip c = eof;turn right;var x;set x len{l}-0.5;;;;;;;;;;;skip sign{x} > 0;end;turn right;
                          t                                               t                      t                                         t
                          h                                               u                      h                                         u
                          g                                               r                      g                                         r
                          i                                               n                      i                                         n
                          r                                                                      r                                          
                                                                          r                                                                r
                          n                                               i                      n                                         i
                          r                                               g                      r                                         g
                          u                                               h                      u                                         h
                          t                                               t                      t                                         t
                          ;thgir nrut;}c ,5.0-}l{nel ,l{ta;}1 ,l{nel l tes;                      ;thgir nrut;1-x x tes;c tuo;}x ,l{ta c tes;