-
Notifications
You must be signed in to change notification settings - Fork 0
Syntax & Types Reference
Krait is designed to have a Pythonic frontend with an extremely efficient C/Rust-like static type backend. This guide serves as a precise reference for both human developers and LLM context windows to understand Krait's language grammar, type system, and structural rules.
Krait enforces strict, visual clean-code principles:
-
No Curly Braces (
{}): Block scopes are defined entirely by indentation. -
No Semicolons (
;): Line endings are delimited by newlines (\n). - Indentation Method: You must use spaces (typically 4 spaces) consistently. Mixing tabs and spaces, or using inconsistent indentation depths, will trigger syntax errors during parsing.
-
Comments: Code comments start with a
#character and extend to the end of the line.
# This is a valid comment in Krait
make add_one(x)
return x + 1Krait is a statically typed language, meaning that every variable's type is determined at compile time. However, to keep code clean and free of cognitive noise, Krait features implicit type inference. You do not write explicit type annotations (like int or float); instead, the compiler infers them from assignments and expressions.
| Primitive Type | Description | Example Lit |
|---|---|---|
Int |
64-bit signed integer |
42, 0 - 5
|
Float |
64-bit floating-point number |
3.14, 1.0
|
Str |
UTF-8 encoded string literal |
"hello", "Krait"
|
Bool |
Boolean truth value |
true, false
|
Void |
Indicates the absence of a value | Implicitly returned by side-effect functions |
Variables in Krait are declared or updated using the set keyword. Because of strict static typing, once a variable has been declared with a specific type, its type cannot be changed.
set age = 21 # Infers Int
set price = 19.99 # Infers Float
set name = "Alice" # Infers Str
set is_active = true # Infers Boolset age = 22 # Valid: age is still an Int
# set age = "twenty" # Compile Error! Cannot reassign Int to Str.Functions are defined using the make keyword followed by the function name, parameters in parentheses, and an indented block.
make function_name(param1, param2)
# body of function
return value- Parameter types are inferred based on their usage in the function body.
- Return values are optional; if no
returnis provided, orreturnis called without an argument,Voidis returned. - Recursive function definitions are fully supported and compiled efficiently.
make calculate_area(width, height)
return width * height
set area = calculate_area(10, 5) # area is Int (50)Structs in Krait represent custom data structures. They are defined using make followed by the Struct name (no parentheses) and an indented list of fields with their default values.
make Point
x = 0
y = 0Instantiate a struct using the new operator:
set p = new PointNote
Instantiating a struct returns a pointer to a heap-allocated memory structure. Heap allocations are managed via Krait's compile-time Ownership Model (see Memory Model & Ownership).
Read or write field values using the dot (.) operator:
set p.x = 10
set p.y = 20
show p.x # Prints 10Krait keeps control flow minimal to eliminate runtime overhead and optimize branching.
Conditional execution uses the when keyword. There is no else keyword; instead, structure code with early returns or sequential when checks.
make sign_of(n)
when n < 0
return 0 - 1
when n > 0
return 1
return 0Important
The condition expression in a when statement must evaluate to a Bool type. Any other type will trigger a compile-time Type Mismatch error.
Loops are represented by the repeat block, which executes its body a specific number of times.
make power(base, exp)
set result = 1
repeat exp times
set result = result * base
return resultTo support ultra-fast system integration and avoid runtime performance bottlenecks, Krait features a built-in Foreign Function Interface (FFI) to import and invoke native C functions directly.
extern make external_function_name(param1, param2)You can import the standard C putchar function directly to write custom console output:
extern make putchar(char_code)
make print_hi()
putchar(72) # 'H'
putchar(105) # 'i'
putchar(10) # '\n'Krait includes a modern module compiler that resolves code files recursively.
- The
import module_namecommand looks up the filelib/module_name.krrelative to the build path. - The compiler parses and merges the imported module's abstract syntax tree (AST) into your application namespace before executing semantic checks or generating LLVM-IR.
# Import math standard library
import math
# Use functions defined inside lib/math.kr
set p = power(2, 8)
show p # Prints 256