Language Basics
Core syntax and fundamental concepts of Strada.
Program Structure
A Strada program consists of function definitions. Execution begins at the main function:
# Comments start with #
func helper() void {
say("I'm a helper function");
}
func main() int {
helper();
say("Hello from main!");
return 0;
}
Statements
Statements end with semicolons:
my int $x = 10;
say($x);
$x = $x + 1;
Comments
# This is a single-line comment
my int $x = 42; # Inline comment
Operators
Arithmetic Operators
| Operator | Description | Example |
|---|---|---|
+ | Addition | $a + $b |
- | Subtraction | $a - $b |
* | Multiplication | $a * $b |
/ | Division | $a / $b |
% | Modulo | $a % $b |
** | Exponentiation | $a ** $b |
Comparison Operators
| Numeric | String | Description |
|---|---|---|
== | eq | Equal |
!= | ne | Not equal |
< | lt | Less than |
> | gt | Greater than |
<= | le | Less than or equal |
>= | ge | Greater than or equal |
Important
Use
eq/ne for string comparison, not ==/!=.
Using numeric operators on strings compares them numerically.
Logical Operators
| Operator | Description | Example |
|---|---|---|
&& | Logical AND | $a && $b |
|| | Logical OR | $a || $b |
! | Logical NOT | !$a |
and | Low-precedence AND | $a and $b |
or | Low-precedence OR | $a or $b |
not | Low-precedence NOT | not $a |
String Operators
| Operator | Description | Example |
|---|---|---|
. | Concatenation | "Hello" . " World" |
x | Repetition | "ab" x 3 → "ababab" |
Bitwise Operators
| Operator | Description | Example |
|---|---|---|
& | Bitwise AND | $a & $b |
| | Bitwise OR | $a | $b |
^ | Bitwise XOR | $a ^ $b |
~ | Bitwise NOT | ~$a |
<< | Left shift | $a << 2 |
>> | Right shift | $a >> 2 |
Increment/Decrement
my int $i = 0;
$i++; # Postfix increment (returns old value)
$i--; # Postfix decrement
++$i; # Prefix increment (returns new value)
--$i; # Prefix decrement
Assignment Operators
$x = 10; # Assignment
$x += 5; # Add and assign ($x = $x + 5)
$x -= 3; # Subtract and assign
$x *= 2; # Multiply and assign
$x /= 4; # Divide and assign
$x .= "!"; # Concatenate and assign
Ternary and Other Operators
# Ternary conditional
my str $result = $x > 0 ? "positive" : "non-positive";
# Range operator (creates array)
my array @nums = (1..10);
# Reference operator
my int $x = 42;
my scalar $ref = \$x; # Reference to $x
# Function reference
my scalar $fn = &my_function;
Blocks
Blocks are delimited by curly braces and create a new scope:
{
my int $x = 10; # $x only visible in this block
say($x);
}
# $x is not accessible here
Truthiness
Values are considered "false" if they are:
- The number
0 - The empty string
"" undef
Everything else is "true".
if ($value) {
say("Value is true");
} else {
say("Value is false");
}
Magic Namespaces
Built-in functions are organized into namespaces:
Core Functions (No Namespace)
Common functions like say(), print(), push(), pop(), len(), etc.
math:: Namespace
math::sin(3.14)
math::cos(0)
math::sqrt(16)
math::pow(2, 10)
math::abs(-5)
math::floor(3.7)
math::ceil(3.2)
sys:: Namespace
sys::open("file.txt", "r")
sys::read($fd, 1024)
sys::write($fd, $data)
sys::close($fd)
sys::fork()
sys::exec("ls", ["-la"])
sys::getenv("PATH")
sys::time()
Regex Operations
Pattern Matching
my str $s = "Hello World";
# Match
if ($s =~ /World/) {
say("Found it!");
}
# Negated match
if ($s !~ /Goodbye/) {
say("Not found");
}
Substitution
my str $s = "Hello World";
# Replace first occurrence
$s =~ s/World/Strada/;
# Replace all occurrences (g flag)
$s =~ s/l/L/g;
# Case-insensitive (i flag)
$s =~ s/hello/Hi/i;
Input/Output
Console Output
say("With newline"); # Prints with newline
print("No newline"); # Prints without newline
File I/O
# Read entire file
my str $content = slurp("file.txt");
# Write to file
spew("output.txt", $content);
# Low-level file operations
my int $fd = sys::open("file.txt", "r");
my str $data = sys::read($fd, 1024);
sys::close($fd);