Code Examples
Learn Pangea through practical examples, from simple arithmetic to recursive functions.
1. Hello World
English Version
print " Hello, World! "
Italian Version
stampa " Ciao, Mondo! "
Output:
Hello, World!
2. Simple Arithmetic
Addition and Multiplication
print add 5 6
print multiply 7 8
print add multiply 2 3 4
Output:
11
56
10
Explanation:
add 5 6→ 5 + 6 = 11multiply 7 8→ 7 × 8 = 56add multiply 2 3 4→ (2 × 3) + 4 = 10
3. Variables
Setting and Getting Variables
set "x" 10
set "y" 20
print get "x"
print add get "x" get "y"
Output:
10
30
How it works:
set "x" 10- Stores 10 in variable xget "x"- Retrieves the value of x- Variables are scoped to the current function/context
Variable Syntax Sugar ($name and name=)
x= 10
print $x
y= add $x 5
print $y
Output:
10
15
How it works:
x= 10- Shorthand forset "x" 10$x- Shorthand forget "x"- Works identically in Italian (
metti/prendi)
4. Conditional Logic (if)
Simple If Statement
x= 15
if greater $x 10
print " x is greater than 10 "
print " x is not greater than 10 "
Output:
x is greater than 10
Syntax:
if <condition> <then-branch> <else-branch>
The if word takes exactly 3 arguments. Both branches are
expressions that get evaluated only if selected.
5. Loops (while)
Count to 5
i= 1
while not greater $i 5 {
print $i
i= add $i 1
}
Output:
1
2
3
4
5
Explanation:
-
while <condition> <body>- Executes body while condition is true do ... end- Groups multiple statements- Loop continues while i ≤ 5
6. FizzBuzz
Classic FizzBuzz (English)
i= 1
while not greater $i 20 {
if equal 0 modulus $i 15
print " FizzBuzz "
if equal 0 modulus $i 3
print " Fizz "
if equal 0 modulus $i 5
print " Buzz "
print $i
i= add $i 1
}
Sample Output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
FizzBuzz in Italian
metti "i" 1
mentre non maggiore prendi "i" 20 fai
se uguale 0 modulo prendi "i" 15
stampa " FizzBuzz ... multiplo di 15 "
se uguale 0 modulo prendi "i" 3
stampa " Fizz ... multiplo di 3 "
se uguale 0 modulo prendi "i" 5
stampa " Buzz ... multiplo di 5 "
stampa prendi "i"
metti "i" somma prendi "i" 1
fine
7. User-Defined Functions
Square Function
define_word "square" 1
multiply @1 @1
print square 5
print square 12
Output:
25
144
Syntax Breakdown:
-
define_word <name> <arity> <body> square- Function name1- Takes one argumentmultiply @1 @1- Function bodyargument N- Accesses the Nth parameter
8. Recursive Functions
Factorial
define_word "factorial" 1
if equal 0 @1
1
multiply @1 factorial add -1 @1
print factorial 0
print factorial 4
print factorial 5
Output:
1
24
120
How Recursion Works:
- Base case:
if equal 0 @1→ return 1 - Recursive case:
n × factorial(n - 1) - Example: factorial(4) = 4 × 3 × 2 × 1 = 24
Fibonacci
define_word "fib" 1
if not greater @1 1
@1
add fib add -1 @1 fib add -2 @1
print fib 0
print fib 1
print fib 5
print fib 10
Output:
0
1
5
55
9. String Literals
Quoted Literals with Escapes
name= "Alice"
print $name
define_word "greet" 1
print @1
greet " Hello! "
print "line1\nline2"
print "tab:\tvalue"
print "quote: \"ok\""
Output:
Alice
Hello!
line1
line2
tab: value
quote: "ok"
String Rules:
Strings are quoted with "...". Supported escapes are
\", \\, \n, and
\t.
- Use quoted names for variable and function-name data
- Unknown escapes are rejected with an explicit parser error
- Unterminated strings are rejected with an explicit parser error
10. File Includes
main.words
print " Loading factorial... "
! "factorial.words"
print " Computing factorial(5) "
print factorial 5
factorial.words
define_word "factorial" 1
if equal 0 @1
1
multiply @1 factorial add -1 @1
Syntax:
! "<filename>" - Executes the specified file
The ! operator includes and runs another file's code.
11. do...end Blocks
Grouping Multiple Statements
x= 10
if greater $x 5 {
print " x is greater than 5 "
print " The value is: "
print $x
} {
print " x is not greater than 5 "
}
Output (when x = 10):
x is greater than 5
The value is:
10
Usage:
-
do ... endcreates a block that evaluates all statements inside - Returns the value of the last expression
- Useful for if/while branches that need multiple statements
12. Complex Example: Prime Number Check
is_prime Function
define_word "is_divisible" 2
equal 0 modulus @1 @2
define_word "is_prime" 1 {
n= @1
if not greater $n 1 {
return false
} {
pass
}
i= 2
while not greater multiply $i $i $n {
if is_divisible $n $i {
return false
} {
pass
}
i= add $i 1
}
return true
}
print is_prime 2
print is_prime 17
print is_prime 20
Output:
true
true
false
13. Bilingual Programs
The same logic in English and Italian:
| English | Italian |
|---|---|
print |
stampa |
add |
somma |
multiply |
moltiplica |
set / get |
metti / prendi |
if |
se |
while |
mentre |
do / end |
fai / fine |
define_word |
definisci_parola |
argument |
argomento |
⚠ Running Modes
To run Italian programs:
lua src/pangea1/main.lua italian program.parole
For English (default):
lua src/pangea1/main.lua program.words
Tips and Best Practices
💡 Formatting
- Use indentation to show structure (though not required)
- One statement per line for readability
- Group related logic with
do...endblocks
💡 Debugging
- Use
printliberally to trace execution - Type
?in the REPL to see all available words - Test functions in isolation before combining
💡 Performance
- Recursion depth is limited by Lua's stack
- No tail-call optimization
- Consider iterative solutions for deep recursion
🎓 Next Steps
Explore the complete Language Reference to see all built-in words and their usage.