# Slang ### Example code ```c # this is a comment #| and that is a multiline one. |# const u32 n = 123123 # n: const u32 (unsigned 32 bit) const i64 m = 10**18 # m: const i64 (signed 64 bit) const int z = 2**128 # z: const int (signed unsized) const auto q = 2**256 # q: const int (signed unsized) char f(str x) { # f(): char, x: str auto c = x[1] # c: char return c # char } auto g(str x) { # g(): char, x: str return x[0] # char } int h(int x) = x+1 # h(): int, x: int main { stdio.println(h(n), \ # comments allowed here too f('123asd') + g('32') + 1) #--> «123124 f» stdio.println(q/z/2**96) #--> «4294967296.0» } ``` ## Tokens * [Keywords](#Keywords) * [Identifiers](#Identifiers) * [Literals](#Literals) * [Operators](#Operators) * [Specials](#Specials) #### Token resolution order 1. Special 2. Operator 3. Literal 4. Keyword 5. Identifier ## Syntax structures _Note: `*` after syntax unit means any number of them._ ### Abstract * `{[<\n | ;>]* < <\n | ;>>* [<\n | ;>]*}` — `code` * `< | >` — `block` ### Primitive * `< | | | | | >` — `value` * `<() | | | >` — `expr` (`f(x+3)` is an instance of `expr`, also `f`, `x+3` and `x` are `expr`s too) ### Non-final * `[modifier]* ` — `typedef` (left-hand type definition) * `[typedef] [? | + | * | ** | =]` — `argdef` (argument definition) > `?` — if present then argument value, `none` else.
> `+` — tuple with at least one argument.
> `*` — tuple with any number of arguments.
> `**` — object with keyword arguments.
> `=` — default value if argument not specified. * `([[, ]*]) -> = ` — `lambda` (lambda function) * `<[, ]*[, *] | *>` — `callargs` * `< =|: [, =|: ]*[, **] | **>` — `callkwargs` * `\[\]` — `itemget` * `.` — `attrget` ### Final (ordered by resolution order) * ` ([[, ]*]) < | = >` — `funcdef` (function definition) * ` [expr]` — `keywordexpr` (keyword expression) * ` [= ]` — `vardef` (variable definition) * ` = ` — `assignment` * `[, ]* = ` — `unpackassignment` * `([ | | , ])` — `funccall` (function call) * `` — expr evaluation (only in REPL) * `if () ` — `conditional` * `for ( in ) ` — `forloop` * `while () ` — `whileloop` * `else ` — `elseclause` ## Keywords * `return [expr]` — return from function ### Modifiers * `const` — immutable/constant variable ### Reserved keywords * `def` * `try` * `catch` * `except` * `finally` * `raise` * `with` * `yield` * `include` * `using` * `default` ## Identifiers Non-empty sequence of alphanumeric characters plus underscore («_»), not starting with a digit character. Regex: `[^\W\d][\w]*` ### Data types * `i8`, `i16`, `i32`, `i64`, `i128` — fixed size integer * `u8`, `u16`, `u32`, `u64`, `u128` — fixed size unsigned integer * `f8`, `f16`, `f32`, `f64`, `f128` — fixed size IEEE-754 floating point number * `uf8`, `uf16`, `uf32`, `uf64`, `uf128` — fixed size unsigned floating point number * `c8`, `c16`, `c32`, `c64`, `c128` — fixed size complex number * `uc8`, `uc16`, `uc32`, `uc64`, `uc128` — fixed size unsigned complex number * `int` — unsized («big») integer * `uint` — unsized unsigned integer * `float` — unsized floating point number * `ufloat` — unsized unsigned floating point * `complex` — unsized complex number * `ucomplex` — unsized unsigned complex number * `bool` — logical (boolean) value * `byte` — single byte * `char` — UTF-8 character * `str` — UTF-8 string * `void` — nothing (or anything...) * `auto` — compile-time type deduction based on value ## Literals _Note: `*` after syntax unit here means any number of them, `+` means at least one._ * `<<0> | . | .>` — number * `<"" | ''>` — string ### Literal structures * `[ [, ]* ]` — list * `( [type] [, [type] ]* )` — tuple * `{ <: >* }` — map ## Operators * ` ` — unary operators usage * ` ` — binary operators usage * ` = ` — in-place operator usage ### Character operators A set of pre-defined character operators: * `!+-~` — unary charset * `%&*+-/<=>^|` — binary charset * `==`, `**`, `//`, `<<`, `>>` — double-char binary operators ### Keyword operators A set of pre-defined keyword operators: * `not` — unary keyword operator * `and`, `or`, `xor`, `is`, `is not`, `to` — binary keyword operators ## Specials * `#`, `#|`, `|#` — comment specials * `;` — expr separator special * `->`, `@.`, `@`, `.`, `:` — attrget optype specials * `[`, `]` — itemget specials * `\,?=(){}` — other specials charset # Footnotes All character class checks are performed in current locale.
--- _by Sdore, 2021-22_
_slang.sdore.me_