Language Basics
Variables
Section titled “Variables”Variables in Quant must be declared with an explicit type annotation using the colon (:) syntax.
Declaration
Section titled “Declaration”name: type;Declaration with Initialization
Section titled “Declaration with Initialization”name: type = value;Examples
Section titled “Examples”fn main() -> int { // Integer variable x: int = 42;
// Boolean variable is_active: bool = True;
// String variable message: str = "Hello";
// Uninitialized variable count: int; count = 10;
return 0;}Constants
Section titled “Constants”Use the const qualifier to make variables immutable:
fn main() -> int { pi: const float = 3.14159; max_size: const int = 100;
// pi = 3.14; // Error: cannot modify const variable
return 0;}The const qualifier can also be used in type declarations:
matrix: [[const int]]; // Array of arrays containing const integersPrimitive Types
Section titled “Primitive Types”Quant supports several primitive types:
Integer Types
Section titled “Integer Types”int: Default 32-bit signed integerint<N>: Custom bit-size signed integer (e.g.,int<64>)int<N, s>: Custom bit-size with signedness (e.g.,int<32, u>for unsigned)
a: int = 42; // 32-bit signedb: int<64> = 1000000; // 64-bit signedc: int<32, u> = 100; // 32-bit unsignedFloat Types
Section titled “Float Types”float: Default 64-bit floating-pointfloat<32>: 32-bit floating-pointfloat<64>: 64-bit floating-point
x: float = 3.14159;y: float<32> = 2.718;Boolean Type
Section titled “Boolean Type”bool: True or False
flag: bool = True;is_valid: bool = False;String Type
Section titled “String Type”str: String type for text
message: str = "Hello, Quant!";Void Type
Section titled “Void Type”void: Used for functions that don’t return a value
fn print_message() -> void { print("No return value\n");}Type Casting
Section titled “Type Casting”Quant supports explicit type casting using the type name as a function:
import math
fn main() -> int { x: float = 3.14; y: int = int(x); // Cast float to int: y = 3
z: float = float(42); // Cast int to float: z = 42.0
// Casting in expressions result: int = int(math.sqrt(16));
return 0;}Expressions
Section titled “Expressions”Arithmetic Expressions
Section titled “Arithmetic Expressions”a: int = 10 + 5; // Additionb: int = 20 - 8; // Subtractionc: int = 6 * 7; // Multiplicationd: int = 20 / 4; // Divisione: int = 17 % 5; // ModuloComparison Expressions
Section titled “Comparison Expressions”x: bool = (5 > 3); // Greater thany: bool = (5 < 3); // Less thanz: bool = (5 == 5); // Equal tow: bool = (5 != 3); // Not equal toa: bool = (5 >= 5); // Greater than or equalb: bool = (5 <= 3); // Less than or equalLogical Expressions
Section titled “Logical Expressions”x: bool = True && False; // Logical ANDy: bool = True || False; // Logical ORz: bool = !True; // Logical NOTBitwise Expressions
Section titled “Bitwise Expressions”a: int = 5 & 3; // Bitwise ANDb: int = 5 | 3; // Bitwise ORc: int = 5 ^ 3; // Bitwise XORd: int = 5 << 1; // Left shifte: int = 5 >> 1; // Right shiftf: int = ~5; // Bitwise NOTAssignment Operators
Section titled “Assignment Operators”Any bitwise, arithmetic, or logical expression can be assigned to a variable using the assignment operator (=). Additionally, compound assignment operators are supported:
x: int = 10;
x = 20; // Assignmentx += 5; // Add and assign: x = x + 5x -= 3; // Subtract and assign: x = x - 3x *= 2; // Multiply and assign: x = x * 2x /= 4; // Divide and assign: x = x / 4x %= 3; // Modulo and assign: x = x % 3Function Calls
Section titled “Function Calls”Call functions using parentheses:
from std import print
fn add(a: int, b: int) -> int { return a + b;}
fn main() -> int { result: int = add(5, 3); print("Result: %d\n", result); return 0;}Variables are scoped to the block in which they are declared:
fn main() -> int { x: int = 10; // Outer scope
{ y: int = 20; // Inner scope print("%d\n", x); // Can access outer scope }
// print("%d\n", y); // Error: y not in scope
return 0;}