Attributes

An attribute is metadata applied to the file or to a statement. Attributes are modeled on attributes in Rust

Inner attributes, written with a bang (!) after the hash (#), apply to the item that the attribute is declared within. Outer attributes, written without the bang after the hash, apply to the statement that follows the attribute.

syntax

inner = "#![" attributes "]"

outer = "#[" attributes "]"

attributes = (
varname [= value]
|
varname "(" attribute1, attribute2, ..., attributeN ")"
)

value = ( number | " string ")

binary

Unlike JavaScript, by default, a kaoscript file is a module.
A module can not be executed by the command line kaoscript, it will need to be imported from another file.

For a kaoscript file to be executed as binary, it would need the attribute #![bin].

#![bin]
extern console
console.log('Hello World!')

conditional compilation

Condition compilation allows to transpile the code depending of the targeted engine.

// if the engine is IE (*trident*) or Safari 8 or under (*lte(jsc-v8)*)
#[if(any(trident, lte(jsc-v8)))]
impl String {
endsWith(value: String): Boolean => this.length >= value.length && this.slice(this.length - value.length) == value
}
#[else]
disclose String {
endsWith(search: String): Boolean
}

By default, the only available targets are ecma-v5 and ecma-v6.
@kaoscript/target-commons provides those additional targets.

operators

operatordescription
any(item1, ..., itemN)true if at least one item is true
all(item1, ..., itemN)true if all the items are true
none(item1, ..., itemN)true if none of the item are true
one(item1, ..., itemN)true if only and only one item is true
lt(item)less than
lte(item)less than or equals
gt(item)greater than
gte(item)greater than or equals

rules

kaoscript allows to apply some rules about the code.

ruledesciption
no-undefinedall variables must have been defined
ignore-misfitaccessing an unmatched member doesn't throw an error
non-exhaustivethe declaration is not exhaustive;
access to unmatched members will not throw an error

no-undefined

By default, undefined variables assigned will be created.

name = 'John'
// like: let name = 'John'

You can change this behaviour with the rule no-undefined.

#![rules(no-undefined)]
func foobar() {
x = 42
// throw the ReferenceException error: "x" is not defined
console.log(x)
}

ignore-misfit

By default, if a member (field/method for a class, variable/function for a namespace or field for an enum) or a function is accessed and can't be matched with the given arguments, it will throw an error.

The rule ignore-misfit disables this behaviour and ignores mismatched members or functions.

non-exhaustive

The rule non-exhaustive can only be applied to external declarations or to the statement disclose.

By default, external declarations without any definitions are considered as non-exhaustive, whereas those with a definition are considired as exhaustive.
The statement disclose forces the associated declaration to be considired as exhaustive.

extern namespace console
// console: non-exhaustive
console.log('foobar')
extern namespace console {
func log(...)
}
// console: exhaustive
console.log('foobar')
// print: foobar
console.debug('foobar')
// throw an error
extern class Foobar
// Foobar: non-exhaustive
Foobar.log('foobar')
extern class Foobar
// Foobar: non-exhaustive
disclose Foobar {
log(...)
}
// Foobar: exhaustive
console.log('foobar')
// print: foobar
console.debug('foobar')
// throw an error

runtime

kaoscript needs a runtime to add dynamics functions on classes (Helper) and to do type checking (Type).

The runtime is imported only when it's needed.

The default runtime is @kaoscript/runtime and provides only the bare minimum.

But you can provide your own runtime with the following attributes

#![runtime(package="@kaoscript/runtime")]
#![runtime(type(alias="Type", member="Type"))]
#![runtime(helper(alias="Helper", member="Helper"))]