Basic Programs
Get started with XXML through these fundamental examples covering basic syntax, variables, control flow, and functions.
Hello World
The simplest XXML program:
#import Language::Core;
[ Entrypoint
{
Run Console::printLine(String::Constructor("Hello, World!"));
Exit(0);
}
]Note
Entrypoint block as its entry point. The Exit(0) call terminates the program with a success status code.Variables
Working with different types:
1 #import Language::Core; 2 3 [ Entrypoint 4 { 5 // Integer 6 Instantiate Integer^ As <age> = Integer::Constructor(25); 7 8 // String 9 Instantiate String^ As <name> = String::Constructor("Alice"); 10 11 // Boolean 12 Instantiate Bool^ As <active> = Bool::Constructor(true); 13 14 // Print values 15 Run Console::printLine(String::Constructor("Name: ").concat(name)); 16 Run Console::printLine(String::Constructor("Age: ").concat(age.toString())); 17 18 If (active) -> { 19 Run Console::printLine(String::Constructor("Status: Active")); 20 } 21 22 Exit(0); 23 } 24 ]
User Input
Reading input from the console:
#import Language::Core;
[ Entrypoint
{
Run Console::printLine(String::Constructor("What is your name?"));
Instantiate String^ As <name> = Console::readLine();
Run Console::printLine(String::Constructor("Hello, ").concat(name).concat(String::Constructor("!")));
Exit(0);
}
]Arithmetic Operations
XXML uses method calls for arithmetic operations:
| Operation | Method | Example |
|---|---|---|
| Addition | a.add(b) | 15 + 4 = 19 |
| Subtraction | a.subtract(b) | 15 - 4 = 11 |
| Multiplication | a.multiply(b) | 15 * 4 = 60 |
| Division | a.divide(b) | 15 / 4 = 3 |
| Modulo | a.modulo(b) | 15 % 4 = 3 |
1 #import Language::Core; 2 3 [ Entrypoint 4 { 5 Instantiate Integer^ As <a> = Integer::Constructor(15); 6 Instantiate Integer^ As <b> = Integer::Constructor(4); 7 8 Instantiate Integer^ As <sum> = a.add(b); 9 Instantiate Integer^ As <diff> = a.subtract(b); 10 Instantiate Integer^ As <product> = a.multiply(b); 11 Instantiate Integer^ As <quotient> = a.divide(b); 12 Instantiate Integer^ As <remainder> = a.modulo(b); 13 14 Run Console::printLine(String::Constructor("a = ").concat(a.toString())); 15 Run Console::printLine(String::Constructor("b = ").concat(b.toString())); 16 Run Console::printLine(String::Constructor("a + b = ").concat(sum.toString())); 17 Run Console::printLine(String::Constructor("a - b = ").concat(diff.toString())); 18 Run Console::printLine(String::Constructor("a * b = ").concat(product.toString())); 19 Run Console::printLine(String::Constructor("a / b = ").concat(quotient.toString())); 20 Run Console::printLine(String::Constructor("a % b = ").concat(remainder.toString())); 21 22 Exit(0); 23 } 24 ]
Conditionals
Use If / Else for conditional branching:
1 #import Language::Core; 2 3 [ Entrypoint 4 { 5 Instantiate Integer^ As <score> = Integer::Constructor(85); 6 7 If (score.greaterThanOrEqual(Integer::Constructor(90))) -> { 8 Run Console::printLine(String::Constructor("Grade: A")); 9 } Else -> { 10 If (score.greaterThanOrEqual(Integer::Constructor(80))) -> { 11 Run Console::printLine(String::Constructor("Grade: B")); 12 } Else -> { 13 If (score.greaterThanOrEqual(Integer::Constructor(70))) -> { 14 Run Console::printLine(String::Constructor("Grade: C")); 15 } Else -> { 16 Run Console::printLine(String::Constructor("Grade: F")); 17 } 18 } 19 } 20 21 Exit(0); 22 } 23 ]
Tip
equals(), greaterThan(),lessThan(), greaterThanOrEqual(), and lessThanOrEqual().For Loops
The For loop iterates over a range:
1 #import Language::Core; 2 3 [ Entrypoint 4 { 5 // Count from 1 to 10 6 Run Console::printLine(String::Constructor("Counting:")); 7 For (Integer^ <i> = 1 .. 11) -> 8 { 9 Run Console::printLine(i.toString()); 10 } 11 12 // Calculate sum 13 Instantiate Integer^ As <sum> = Integer::Constructor(0); 14 For (Integer^ <i> = 1 .. 101) -> 15 { 16 Set sum = sum.add(i); 17 } 18 Run Console::printLine(String::Constructor("Sum 1-100: ").concat(sum.toString())); 19 20 Exit(0); 21 } 22 ]
Warning
1 .. 11 is exclusive of the end value, so it iterates from 1 to 10. To include 10, use 1 .. 11.While Loops
The While loop continues while a condition is true:
1 #import Language::Core; 2 3 [ Entrypoint 4 { 5 // Countdown 6 Instantiate Integer^ As <count> = Integer::Constructor(10); 7 8 While (count.greaterThan(Integer::Constructor(0))) -> 9 { 10 Run Console::printLine(count.toString()); 11 Set count = count.subtract(Integer::Constructor(1)); 12 } 13 14 Run Console::printLine(String::Constructor("Liftoff!")); 15 16 Exit(0); 17 } 18 ]
FizzBuzz
The classic programming challenge:
1 #import Language::Core; 2 3 [ Entrypoint 4 { 5 For (Integer^ <i> = 1 .. 101) -> 6 { 7 Instantiate Bool^ As <divBy3> = i.modulo(Integer::Constructor(3)).equals(Integer::Constructor(0)); 8 Instantiate Bool^ As <divBy5> = i.modulo(Integer::Constructor(5)).equals(Integer::Constructor(0)); 9 10 If (divBy3.and(divBy5)) -> { 11 Run Console::printLine(String::Constructor("FizzBuzz")); 12 } Else -> { 13 If (divBy3) -> { 14 Run Console::printLine(String::Constructor("Fizz")); 15 } Else -> { 16 If (divBy5) -> { 17 Run Console::printLine(String::Constructor("Buzz")); 18 } Else -> { 19 Run Console::printLine(i.toString()); 20 } 21 } 22 } 23 } 24 25 Exit(0); 26 } 27 ]
Factorial
Demonstrates defining a class with a method:
1 #import Language::Core; 2 3 [ Class <MathUtils> Final Extends None 4 [ Public <> 5 Constructor = default; 6 7 Method <factorial> Returns Integer^ Parameters (Parameter <n> Types Integer%) Do 8 { 9 Instantiate Integer^ As <result> = Integer::Constructor(1); 10 11 For (Integer^ <i> = 2 .. n.add(Integer::Constructor(1))) -> 12 { 13 Set result = result.multiply(i); 14 } 15 16 Return result; 17 } 18 ] 19 ] 20 21 [ Entrypoint 22 { 23 Instantiate MathUtils^ As <math> = MathUtils::Constructor(); 24 25 For (Integer^ <n> = 0 .. 11) -> 26 { 27 Instantiate Integer^ As <fact> = math.factorial(n); 28 Run Console::printLine( 29 n.toString().concat(String::Constructor("! = ")).concat(fact.toString()) 30 ); 31 } 32 33 Exit(0); 34 } 35 ]
Note
n uses Integer% (copy) because we only need the value, not ownership of the original.Working with Lists
Using the Collections module to work with lists:
1 #import Language::Core; 2 #import Language::Collections; 3 4 [ Entrypoint 5 { 6 // Create a list of numbers 7 Instantiate Collections::List<Integer>^ As <numbers> = 8 Collections::List@Integer::Constructor(); 9 10 // Add numbers 11 Run numbers.add(Integer::Constructor(5)); 12 Run numbers.add(Integer::Constructor(2)); 13 Run numbers.add(Integer::Constructor(8)); 14 Run numbers.add(Integer::Constructor(1)); 15 Run numbers.add(Integer::Constructor(9)); 16 17 // Find sum and max 18 Instantiate Integer^ As <sum> = Integer::Constructor(0); 19 Instantiate Integer^ As <max> = numbers.get(Integer::Constructor(0)); 20 21 For (Integer^ <i> = 0 .. numbers.size()) -> 22 { 23 Instantiate Integer& As <num> = numbers.get(i); 24 Set sum = sum.add(num); 25 26 If (num.greaterThan(max)) -> { 27 Set max = num; 28 } 29 } 30 31 Run Console::printLine(String::Constructor("Count: ").concat(numbers.size().toString())); 32 Run Console::printLine(String::Constructor("Sum: ").concat(sum.toString())); 33 Run Console::printLine(String::Constructor("Max: ").concat(max.toString())); 34 35 Exit(0); 36 } 37 ]
Quick Reference
InstantiateCreate a new variable
SetAssign a value to a variable
RunExecute a method call
If / ElseConditional branching
ForRange-based iteration
WhileCondition-based loop
ReturnReturn a value from method
Exit()Terminate the program
ClassDefine a new class
Next Steps
Now that you understand the basics, explore ownership patterns to learn how to effectively use XXML's ownership system, or dive into the language reference for complete syntax documentation.