Language Features
Core syntax, types, control flow, functions, arrays, and built-ins.
Types and literals
Turf has built-in types:
intdoubleboolstringvoid(for functions that don’t return a value)
Examples:
int a = 42
double b = 3.14
bool ok = true
string msg = "hello"
Comments
Use // for single-line comments:
// This is a comment
printline("ok")
Variables and assignment
Variables are declared with an explicit type:
int x = 5
double pi = 3.14
bool flag = true
string name = "Turf"
Reassignment uses =:
x = x + 1
Turf also supports compound assignment and increments:
x += 1
x -= 1
x *= 2
x /= 2
x++
x--
Operators
Arithmetic:
+-*/%(modulo)^(exponentiation)
Comparison:
<><=>===!=
Boolean logic:
&&||
Unary negation:
int a = -5
double b = -3.14
Control flow
If / elseif / else
Statement-like form:
int x = 9
if x > 10 {
printline("big")
} elseif x > 5 {
printline("medium")
} else {
printline("small")
}
if is also an expression (it evaluates to a value), so you can assign it:
int a = 10
int b = 20
int m = if a > b then a else b
printline(m)
You can also use block branches for expression form:
int m2 = if a > b { a } else { b }
While
int i = 0
while i < 5 {
i = i + 1
}
Inside loops:
breakexits the loopcontinuejumps to the next iteration
For ranges
For loops use this shape:
for i in 0..n step <step> {
// body
}
Key points:
- The range end is exclusive: the loop condition is
i < end(ori > endwhen stepping downward). stepis typically one of:i += 1i++i--
Example:
int sum = 0
for i in 1..10 step i += 1 {
sum = sum + i
}
printline(sum)
Functions
Define a function with fn, a return type, a name, and typed parameters:
fn int add(int a, int b) {
return a + b
}
int result = add(3, 7)
printline(result)
Void functions:
fn void do_something(int x) {
if x > 0 {
printline(x)
} else {
printline(0)
}
}
Arrays
Arrays are fixed-size and have an element type:
int[5] nums
nums[0] = 10
nums[1] = 20
printline(nums[0])
Arrays can also be initialized with a list:
int[3] primes = [2, 3, 5]
printline(primes[2])
Read/write elements with arr[index]:
nums[i] = nums[i] * 2
Get the size with .length:
printline(nums.length)
Built-in functions
Turf includes a few built-ins:
print(x)prints without a newlineprintline(x)prints with a newlinetypeof(expr)returns the type name as astringlengthof("...")returns the number of characters as anint
Example:
int x = 10
print("typeof(x): ")
printline(typeof(x))
printline(lengthof("hello"))
Type casting
Explicit casts use int(...), double(...), and string(...):
int a = int("100")
double b = double(42)
string s = string(true)
printline(a)
printline(b)
printline(s)