Syntax Overview
Quartz uses a familiar C-like syntax with curly braces, semicolons, and dot notation. If you've used Java, C++, JavaScript, or similar languages, you'll feel right at home.
Basic Structure
A Quartz program consists of imports, declarations, and statements:
// Import modules
import system.io as io;
import system.math as math;
// Declare variables
let message = "Hello, World!";
var counter = 0;
// Execute statements
io.out.println(message);
counter = counter + 1;
Comments
Quartz supports single-line comments:
// This is a single-line comment
let x = 42; // Inline comment
Variables
Immutable Variables (let)
let name = "Alice"; // String
let age = 30; // Integer
let pi = 3.14159; // Double
let active = true; // Boolean
Mutable Variables (var)
var count = 0;
count = count + 1; // OK - var is mutable
let fixed = 10;
fixed = 20; // Error - let is immutable
Type Annotations (Optional)
let x: int = 42;
let name: string = "Bob";
let rate: double = 0.15;
let flag: bool = false;
Data Types
| Type | Description | Example |
|---|---|---|
| `int` | 32-bit integer | `42`, `-17`, `0` |
| `double` | 64-bit floating point | `3.14`, `-0.5`, `1e10` |
| `string` | Text string | `"hello"`, `'world'` |
| `bool` | Boolean | `true`, `false` |
| Array | Ordered collection | `[1, 2, 3]` |
| Dictionary | Key-value mapping | `{"key": "value"}` |
Operators
Arithmetic
let a = 10 + 5; // Addition: 15
let b = 10 - 5; // Subtraction: 5
let c = 10 * 5; // Multiplication: 50
let d = 10 / 5; // Division: 2
let e = 10 % 3; // Modulo: 1
Comparison
let eq = 5 == 5; // Equal: true
let ne = 5 != 3; // Not equal: true
let lt = 3 < 5; // Less than: true
let gt = 5 > 3; // Greater than: true
let le = 3 <= 3; // Less or equal: true
let ge = 5 >= 5; // Greater or equal: true
Logical
let and_result = true && false; // false
let or_result = true || false; // true
let not_result = !true; // false
Compound Assignment
var x = 10;
x += 5; // x = 15
x -= 3; // x = 12
x *= 2; // x = 24
x /= 4; // x = 6
Control Flow
If Statements
if (condition) {
// code
} else if (other_condition) {
// code
} else {
// code
}
While Loops
var i = 0;
while (i < 10) {
io.out.println(i);
i += 1;
}
For Loops
// For-in loop over array indices
let items = [1, 2, 3, 4, 5];
for (i in items) {
io.out.println(items[i]);
}
// Traditional for loop
for (var i = 0; i < 10; i += 1) {
io.out.println(i);
}
Loop Control
while (true) {
if (done) {
break; // Exit loop
}
if (skip) {
continue; // Skip to next iteration
}
}
Functions
// Function definition
fn greet(name: string) {
io.out.println("Hello,", name);
}
// Function with return type
fn add(a: int, b: int) -> int {
return a + b;
}
// Function call
greet("World");
let sum = add(5, 3);
Lambdas
// Lambda expression
let double = (x) => x * 2;
// Using lambdas with collections
import system.collection as c;
let numbers = [1, 2, 3, 4, 5];
let doubled = c.map(numbers, (x) => x * 2);
let evens = c.filter(numbers, (x) => x % 2 == 0);
Classes
class Person {
string name;
int age;
fn greet() {
io.out.println("Hi, I'm", name);
}
fn haveBirthday() {
age += 1;
}
}
let person = new Person();
person.name = "Alice";
person.age = 30;
person.greet();
Imports
// Import with alias (recommended)
import system.io as io;
import system.math as math;
// Using imported modules
io.out.println("Hello!");
let sqrt = math.sqrt(16);
📝 Note
Modules with names that are keywords (like string) must be imported with an alias.
Arrays & Dictionaries
Arrays
let numbers = [1, 2, 3, 4, 5];
// Indexing
let first = numbers[0]; // 1
let last = numbers[4]; // 5
// Modification
numbers[0] = 10;
Dictionaries
let config = {
"name": "MyApp",
"version": "1.0",
"debug": true
};
// Access
let name = config["name"]; // "MyApp"
// Modification
config["version"] = "2.0";
Exception Handling
try {
// Code that might throw
throw new ValueError("Invalid input");
} catch (ValueError e) {
io.out.println("Caught error:", e.message);
} finally {
// Always executed
io.out.println("Cleanup");
}
Complete Example
import system.io as io;
import system.math as math;
import system.string as str;
class Calculator {
string name;
fn add(a: int, b: int) -> int {
return a + b;
}
fn sqrt(x: double) -> double {
return math.sqrt(x);
}
fn describe() {
io.out.println("Calculator:", str.uppercase(name));
}
}
// Main program
let calc = new Calculator();
calc.name = "sciCalc";
calc.describe();
io.out.println("5 + 3 =", calc.add(5, 3));
io.out.println("√16 =", calc.sqrt(16.0));
// Working with collections
let numbers = [1, 4, 9, 16, 25];
for (i in numbers) {
let n = numbers[i];
io.out.println("√" + n + " =", math.sqrt(n));
}