KnightScript is a Lua-JS hybrid language that was designed to target Knight. KnightScript uses an optimizing compiler to compile directly to Knight. While KnightScript does require Lua for now, it will be self hosted in the future.
In order to run KnightScript, you must first install Lua. You can find standalone executables on the Lua download page.
Compile a file named example.kns:
lua main.lua example.kns -o example.knFor help:
lua main.lua -hSome parts of the compiler are not necessarily finished. However, most programs should compile and run as expected. Currently unsupported features consist of:
- Monkey-patching builtin functions
- Properly scoped
localdeclarations (currently all variables are global) - First class support for statements (
a = if (truthy) { value },a = b = 1) - Optimization passes reducing code size
- Immutable
constdeclarations - Resolving builtin functions as identifiers that can be passed
- Golfing argument variables in minify/golf mode (
__1=>a) - Passing functions as arguments using builtins; this works with non-builtin functions
- Integrating the knightc typechecker
Behavior that is working as intended:
- Function calls utilize the argument variables (e.g.
__1,__2...) - Resolving name collisions with internally generated variable names
- Placing
NULLin the fallback expression ofIFstatements when no else statement is provided - Using
| > a b ? a bto representa >= band vice versa (adding one to a side may be a more concise way, further inquiry required)
These features are working, but the implementation is not necessarily bug free. Help test these features and create an issue if a bug is found.
- Direct index assignment
array[n] = valueas an alternative toset(array, n, value) for (... in ...)statements
The language specification in its entirety is available in ebnf format. You can also check out the examples.
KnightScript contains a few variables and functions that cannot be overwritten or monkey patched. These variables and functions consist of:
true- Represents a truthy value.
false- Represents a falsey value.
null- Represents the absence of a value.
- Is the automatic return value of functions that have no return statement.
- IO
print- Called using
print(expression, ...). - Takes an infinite number of arguments and outputs each value in its string form with a trailing newline. If there is more than one parameter, each value is printed with a space inbetween.
- Internally represents the
OUTPUTfunction in Knight.
- Called using
write- Called using
write(expression). - Takes one argument and outputs its value in its string form without a trailing newline.
- Internally represents the
OUTPUTfunction in Knight when called with a string ending with\.
- Called using
read- Called using
read(). - Takes no arguments and returns a line from
stdin. - Internally represents the
PROMPTfunction in Knight.
- Called using
prompt- Called using
prompt(expression). - Takes one argument and outputs its string form without a trailing newline. It then returns the next line read from
stdin. - Internally represents the
PROMPTfunction in Knight when combined with anOUTPUTcall.
- Called using
- Arrays
join- Called using
join(array, expression). - Takes two arguments: one being the array to join into a string, and the other being the string value to use as a delimiter between each item.
- Internally represents the
^operation when combined with an array and string.
- Called using
pop- Called using
pop(array). - Takes one argument and removes and returns it's first item. Directly modifies the array following the pop.
- Internally represents assigning the array to the
]operation on the array, and returns the[operation on the array.
- Called using
push- Called using
push(array, value). - Takes two arguments: one being the array the value is being pushed to the beginning of, the other being the value being pushed into the array.
- Internally represents the
+operator with an array being combined into the boxed value.
- Called using
insert- Called using
insert(array, value). - Takes two arguments: one being the array the value is being pushed to the end of, the other being the value being pushed into the array.
- Internally represents the
+operator with an array.
- Called using
length- Called using
length(value). - Takes on argument and returns its length when converted to a string/list.
- Internally represents the
LENGTHfunction in Knight.
- Called using
- Other
random- Called using
random(min, max) - Extends
irandomand is incomplete.
- Called using
irandom- Called using
irandom(). - Takes no arguments and returns a random positive integer with a size depending on your Knight implementation.
- Internally represents the
RANDOMfunction in Knight.
- Called using
ascii- Called using
ascii(value). - Takes one argument and returns its ascii code if it is a string or its character if it is a number.
- Internally represents the
ASCIIfunction in Knight.
- Called using
quit