Quick Reference
Fast reference guide for Kronos syntax
Quick Reference
Syntax cheat sheet for Kronos. For detailed explanations, see the Language Reference.
Variables
set name to "Alice" # Immutable
let counter to 0 # Mutable
let age to 25 as number # With type annotationData Types
| Type | Example |
|---|---|
| Number | 42, 3.14, -10 |
| String | "Hello", f"Hi {name}" |
| Boolean | true, false |
| Null | null |
| List | list 1, 2, 3 |
| Map | map key: "value" |
Operators
| Operation | Syntax |
|---|---|
| Addition | a plus b |
| Subtraction | a minus b |
| Multiplication | a times b |
| Division | a divided by b |
| Modulo | a mod b |
| Equal | a is equal b |
| Not equal | a is not equal b |
| Greater | a is greater than b |
| Less | a is less than b |
| And/Or/Not | a and b, a or b, not a |
Control Flow
# Conditional
if condition:
statement
# Pattern matching
match value:
case 1:
print "one"
default:
print "other"
# For loop
for i in range 1 to 10:
print i
# For-each loop
for item in items:
print item
# While loop
while condition:
statementFunctions
function greet with name:
print f"Hello, {name}!"
call greet with "Alice"
# Default parameters
function greet with name, greeting = "Hello":
return f"{greeting}, {name}!"
call greet with "Alice" # Uses default
call greet with "Bob", "Hi" # Override default
# Variadic parameters
function sum with ...numbers:
let total to 0
for n in numbers:
let total to total plus n
return total
call sum with 1, 2, 3, 4, 5 # Accepts any number of args
# Named arguments
call create_point with z: 3, x: 1, y: 2
# Multiple return values
function divide with a, b:
return a divided by b, a mod b
set q, r to call divide with 10, 3
# Variable swapping
let x, y to y, xStrings
set char to text at 0 # Indexing
set slice to text from 0 to 5 # Slicing
set msg to f"Hello, {name}!" # F-strings
set fmt to f"{price:.2f}" # Format specifiersLists
set nums to list 1, 2, 3
set nums2 to [1, 2, 3]
set squares to [x times x for x in range 1 to 6]
print nums at 0 # Access: 1
let nums at 0 to 10 # ModifyMaps
set user to map name: "Alice", age: 30
print user at "name" # Access
delete user at "age" # Delete keyImports
import math
import utils from "utils.kr"Exception Handling
try:
raise "Error"
catch error:
print error
finally:
print "Done"File I/O
set content to call read_file with "file.txt"
call write_file with "out.txt", "Hello"Common Functions
call len with items # Length
call uppercase with text # "HELLO"
call split with text, "," # Split string
call sort with numbers # Sort list
call to_number with "42" # Parse numberRunning Programs
./kronos script.kr # Run file
./kronos # Start REPL
./kronos -e "code" # Execute inline