Syntax Overview
Jaf uses simple expression-based syntax with C-like operators.
Note: Every expression in Jaf returns a value.
Data Types
Integers
x = 42
y = -10
z = 0
Booleans (Bits)
a = 1 > 0 # Returns 1 (true)
b = 0 == 1 # Returns 0 (false)
Arrays
arr = [1, 2, 3, 4, 5]
first = arr[0] # Access element (returns 1)
arr[2] = 99 # Modify element
Functions
Definition
func increment(x) {
x + 1
}
Function Call
result = increment(5) # Returns 6
Recursive Function
func factorial(n) {
if (n == 0) {
1
} else {
n * factorial(n - 1)
}
}
Control Flow
Conditional Expression
# Returns 1 if positive, 0 if not positive
result = if (x > 0) 1 else 0
While Loop
i = 0
while (i < 5) {
i = i + 1
}
# Loop returns last expression value (5)
Built-in Functions
| Function | Description | Example | Result |
|---|---|---|---|
length(array) | Get array length | length([1,2,3]) | 3 |
print(value) | Output value to console | print(42) | Outputs: 42 |
length(array)
arr = [1, 2, 3]
len = length(arr) # Returns 3
print(value)
# Basic output
print(42) # Outputs: 42
print("Hello") # Outputs: Hello (when strings are added)
# With variables
x = 10
print(x) # Outputs: 10
print(x * 2) # Outputs: 20
# Multiple prints for debugging
print("Starting calculation")
result = x + 5
print("Result is:")
print(result)
Note: print() returns void and is primarily used for debugging and output.
Operators
| Operator | Description | Example | Result |
|---|---|---|---|
| + - * / | Arithmetic | 5 + 3 | 8 |
| - (unary) | Unary minus | -5 | -5 |
| == != | Equality | 5 == 3 | 0 (false) |
| < > <= >= | Comparison | 5 < 10 | 1 (true) |
| && || | Logical | 1 && 0 | 0 (false) |
| = | Assignment | x = 5 | 5 |
| [] | Array access | arr[0] | First element |
| [] = | Array assignment | arr[0] = 10 | Modified array |
Example Programs
Debugging with print
func sumArray(arr) {
print("Starting sumArray")
total = 0
i = 0
while (i < length(arr)) {
print("Processing index: ")
print(i)
print("Value: ")
print(arr[i])
total = total + arr[i]
i = i + 1
}
print("Total sum: ")
print(total)
total
}
# Usage:
numbers = [1, 2, 3, 4, 5]
sum = sumArray(numbers) # Returns 15 with debug output
Find Maximum Value
func max(arr) {
maxVal = arr[0]
i = 1
while (i < length(arr)) {
if (arr[i] > maxVal) {
maxVal = arr[i]
}
i = i + 1
}
maxVal
}
Fibonacci Sequence
func fib(n) {
if (n == 0) {
0
} else {
if (n == 1) {
1
} else {
fib(n - 1) + fib(n - 2)
}
}
}
# Test with print:
print("Fibonacci numbers:")
n = 0
while (n <= 10) {
result = fib(n)
print("fib(")
print(n)
print(") = ")
print(result)
n = n + 1
}
Comments
# This is a single-line comment
x = 42 # Comments can go after code
# Comments are ignored by the interpreter
# Use them to explain your code
Output and Debugging
Use print() for output and debugging:
# Simple output
print("Hello, Jaf!")
# Debugging calculations
x = 5
y = 10
print("x = ")
print(x)
print("y = ")
print(y)
print("x + y = ")
print(x + y)
# Conditional debugging
debug = 1 # Set to 1 to enable debug output
if (debug) {
print("Debug mode enabled")
print("Intermediate values will be shown")
}