This is a fully functioning Knight 2.0.1 implementation in Rust. More details about Knight, its license, and specifications can be found in the knight-lang repo.
The Rust implementation is a "paragon" implementation: It implements nearly all the suggested extensions—and some compiler-specific quality of life ones—, is able to be embedded within other Rust programs (eg the discord-bot
directory), and even catches all undefined behaviour.
Simply run cargo run -- (-e 'expr' | -f filename)
, and it'll run your program. Alternatively, you can instead compile the binary with cargo build
, and then execute it via ./target/debug/knight (-e 'expr' | -f filename)
.
By default, the "normal" extensions are enabled, and only simple forms of undefined behaviour are caught. However, you can use cargo run --no-default-features --features=strict-compliance -- ...
to disable all extensions, and catch every single form of undefined behaviour. This can be somewhat slow, however.
The following is the list of flags, and their descriptions. To enable flags, pass --features=<flag1>,<flag2>,...
as an argument after the build
/run
. Note that most of these flags are enabled by default (see the Cargo.toml
's features.default
value for specifics); to disable them, you must run --no-default-features
, which will only use features you explicitly state
Extensions that the spec suggests.
value-function
: Enables theVALUE
functioneval-function
: Enables theEVAL
functionassign-to-strings
: Convert strings to variables if theyre the first arg to=
.handle-function
: Enables the use ofHANDLE
, which is try-catch.yeet-function
: Enables the use ofYEET
, which is throw.use-function
: Enables theUSE
function, which is for importing files.system-function
: Enables the$
function, which is for doing system calls.spec-extensions
: Enables everything in this section.
Custom extensions by the Rust implementation that add additional functionality.
list-extensions
: Provides useful extensions for lists, such as* list BLOCK ...
will map all the elements of the list to the return value of the block.string-extensions
: Provides useful extensions for strings, such as/ string sep
will split a string by thesep
.xsrand-function
: Enables the use ofXSRAND
, used for seedingRANDOM
.xrange-function
: Enables the use ofXRANGE
, which is used to construct listsxreverse-function
: Enables the use ofXREVERSE
, which can be used to reverse lists.compiler-extensions
: Enables everything in this section.
Extensions that change how Knight itself works, possibly making valid programs invalid.
negative-index-length
: Allows the use of negative start positions when indexing into strings/lists; this acts like it does in other languages, such as python.assign-to-prompt
: If you assign to thePROMPT
function (i.e.= PROMPT ...
), then the next timePROMPT
is called, that value will be returned. Multiple assignments start a queue. If a block is assigned, each timePROMPT
is called, that block is executed, and its return value is the line.assign-to-system
: The same asassign-to-prompt
, except for the$
function. If a block is passed, stdin will be assigned to the_
variable.assign-to-lists
: Allows you to assign to lists of strings, which can be used for argument destructoring.assignment-overloads
: Enablesassign-to-strings
,assign-to-prompt
,assign-to-system
, andassign-to-lists
.qol-extensions
: Enables everything in this section.
These extensions can be enabled to make sure knight programs are fully spec compliant.
forbid-trailing-tokens
: Raises an error if more than one expression is present in the source code.container-length-limit
: Ensures that containers do not grow larger thani32::MAX
.strict-charset
: Disables Unicode support, only supporting the strict subset of ASCII Knight requires.checked-overflow
: Checks for under/overflow in integer arithmetic operations.strict-integers
: Usei32
(instead of the defaulti64
) for integers; Also enables some niche integer spec requirements.strict-call-argument
: Ensures thatCALL
is only ever executed withBLOCK
's return value.verify-variable-names
: Ensures that all variable names are at most 127 characters long.strict-compliance
: Enables everything in this section; With this enabled, and all other features disabled, only 100% valid Knight programs will execute properly.
Miscellaneous extensions that don't fit into other categories
multithreaded
: Enable this to turn on multithreading support. Since Knight is normally single threaded, the Rusut implementation by default uses single-threaded data structures such asRc
andRefCell
.extensions
: Enablesspec-extensions
andcompiler-extensions
, andqol-extensions
.