Knight

From Esolang
Jump to navigation Jump to search

Knight is an imperative programming language created by Sampersand in April 2021. It was designed to be easy to write interpreters for, while still being usable. There are over 20 official interpreters.

Syntax

Knight uses Polish notation: Instead of x OP y, it is OP x y. Because of this, the language has fixed arity.

Knight contains a number of builtins. They are either represented by uppercase letters or symbols. The former may be referred to by the first letter of the name, optionally followed by any sequence of uppercase letters and underscores (so, for example, WHILE can also be W, WHILST, WAT, or WHY_WOULD_ANYONE_DO_THIS). Variables are a lowercase letter or underscore followed by any number of digits, lowercase letters, or underscores.

All tokens are separated by their character class. So, WxOx is interpreted as W x O x, while WXox is interpreted as WX ox.

The majority of Knight's builtins will coerce the second argument to the type of the first. So, for example, + 2 "2" is 4, while + "2" 2 is "22".

Builtins

For a full and up-to-date list of builtins (and plenty more), see the official spec.

Builtin Function
# Line comment
:[](){} (tab)(CR)(LF) Whitespace tokens. Brackets must be balanced.
; Returns its second argument. Bread and butter pattern to chain multiple statements together. It is commonly mixed with : as a style choice (as seen in the FizzBuzz example).
TRUE true Boolean constant
FALSE false Boolean constant
NULL null
= Assigns a value to a variable.
'" String literal. There are no escapes in Knight; the entire string is read from quote to matching quote.
PROMPT Reads a line from standard input. Does not include the newline.
OUTPUT Outputs the first argument to standard output, followed by a newline (unless the string ends in \).
RANDOM Returns a random number from (a minimum) of 0-32767
+ If the first argument is a number, the second argument is added, if the first is a string, the second argument is appended.
* If the first argument is a number, the second argument is multiplied, if the first is a string, the string is repeated the second argument times.
-/%^ Basic arithmetic for subtract, divide, modulo, and power.
? Returns true if the two arguments are equal in type and value, false otherwise.
! Returns the logical negation of the argument
&| Logical and/or, returns the value of the argument that was evaluated
<> Arithmetic or lexicographical less-than or greater-than
WHILE While loop
IF If the first argument is truthy, evaluates the first statement, otherwise the second statement.
GET Obtains a substring
SUBSTITUTE Substitutes a range in a string with another string.
BLOCK Instead of evaluating its arguments, returns a block of code to be used with CALL
CALL Invokes a function created by BLOCK
EVAL Evaluates a string as a Knight statement.
` Runs a shell command, and returns the standard output
QUIT Quits the program with the given return value
LENGTH Returns the length of the string in bytes
ASCII Converts the first character of a String to a Number with its codepoint, or vice versa
DUMP Dumps a representation of its argument for debugging.

Examples

"Hello, World!"

OUTPUT("Hello, World!")

"Hello, World!" (shortest)

O"Hello, World!"

FizzBuzz to 100

; = x 1
: WHILE (> 101 x) {
    ; OUTPUT (
        IF (!% x 15) { "FizzBuzz" } 
        IF (!% x 3) { "Fizz" }
        IF (!% x 5) { "Buzz" }
        { x }
    : )
    : = x + x 1
: }

Simple cat program

WHILE (= line PROMPT()) {
    OUTPUT (line)
}

Links