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.

KeywordMutableUse Case
valNoMost variables — single assignment
mutYesCounters, accumulators, loop state
constNoCompile-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

OperatorDescriptionExample
+Addition / concatenation2 + 3 → 5
-Subtraction10 - 4 → 6
*Multiplication6 * 7 → 42
/Division10 / 2 → 5
%Modulo10 % 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

OperatorDescription
==Equal
!=Not equal
< <= > >=Ordering

Logical

OperatorDescriptionExample
andLogical ANDage >= 18 and active
orLogical OR / fallbacksys.env("PORT") or "8080"
notLogical NOTnot 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:

CategoryNames
Functionsprint, log
Modulesmath, file, sys, time
Go keywordsfunc, return, if, else, for, range, var, const, type, import, etc.
PrefixAny name starting with _

Using a reserved name produces: error: "print" is a builtin — cannot be redeclared.