Syntax Overview
The core syntax of ScarScript — declarations, operators, control flow, loops, and strings.
Comments
// Single-line comment
/* Multi-line
comment */
Variables
Three declaration kinds. Variables are immutable by default — use mut when you need to reassign.
| Keyword | Mutable | Use Case |
|---|---|---|
val | No | Most variables — single assignment |
mut | Yes | Counters, accumulators, loop state |
const | No | Compile-time constants (requires type) |
// With explicit type annotation
val name: str = "Zawiyar"
mut count: int = 0
const MAX: int = 100
// With type inference (recommended)
val age = 25 // int
val price = 99.99 // dec
val active = true // bool
val label = "hi" // str
Inference rules:
42 → int, 3.14 → dec, "text" → str, true → bool. If inference fails, the compiler reports cannot infer type.Operators
Arithmetic
| Operator | Description | Example |
|---|---|---|
+ | Addition / concatenation | 2 + 3 → 5 |
- | Subtraction | 10 - 4 → 6 |
* | Multiplication | 6 * 7 → 42 |
/ | Division | 10 / 2 → 5 |
% | Modulo | 10 % 3 → 1 |
int + dec: Mixing an int with a dec automatically widens the int (
1 + 2.5 → 3.5). Mixing int with str is a type error.Comparison
| Operator | Description |
|---|---|
== | Equal |
!= | Not equal |
< <= > >= | Ordering |
Logical
| Operator | Description | Example |
|---|---|---|
and | Logical AND | age >= 18 and active |
or | Logical OR / fallback | sys.env("PORT") or "8080" |
not | Logical NOT | not flag |
JS-style
or fallback: When the left side is empty/zero/false, or returns the right side. This is how you provide defaults without if-statements.Control Flow
if / else if / else
val age = 20
if age < 13 {
print("child")
} else if age < 18 {
print("teen")
} else {
print("adult")
}
Conditions must be bool. There are no truthy/falsy conversions in if.
Loops
ScarScript has a single universal for keyword that covers every loop pattern.
Numeric range
for i in 0..5 {
print(i) // 0 1 2 3 4 (end exclusive)
}
Iterate a collection
val items = ["fast", "backend", "express"]
for item in items {
print(item)
}
Key-value iteration
for k, v in config {
print("{k} = {v}")
}
Condition loop (while-style)
mut n = 0
for n < 10 {
n = n + 1
}
There is no separate
while keyword — for condition { ... } covers that use case entirely.Strings
String interpolation
Embed expressions directly inside double-quoted strings with { }:
val name = "Zawiyar"
val age = 25
print("User {name} ki age {age} hai.")
// User Zawiyar ki age 25 hai.
Common operations
val s = "hello"
s.len() // 5
s.upper() // "HELLO"
s.lower() // "hello"
s.contains("ell") // true
Arrays
val nums = [1, 2, 3, 4, 5]
nums.len() // 5
nums[0] // 1
nums.push(6) // append (on mut arrays)
for n in nums {
print(n)
}
Reserved Names
These identifiers cannot be used as variable or function names:
| Category | Names |
|---|---|
| Functions | print, log |
| Modules | math, file, sys, time |
| Go keywords | func, return, if, else, for, range, var, const, type, import, etc. |
| Prefix | Any name starting with _ |
Using a reserved name produces: error: "print" is a builtin — cannot be redeclared.