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
4. Conditional Logic (if)
Simple If Statement
set "x" 15
if greater get "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
set "i" 1
while not greater get "i" 5 do
print get "i"
set "i" add get "i" 1
end
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)
set "i" 1
while not greater get "i" 20 do
if equal 0 modulus get "i" 15
print " FizzBuzz "
if equal 0 modulus get "i" 3
print " Fizz "
if equal 0 modulus get "i" 5
print " Buzz "
print get "i"
set "i" add get "i" 1
end
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 argument 1 argument 1
print square 5
print square 12
Output:
25
144
Syntax Breakdown:
-
define_word <name> <arity> <body> square- Function name1- Takes one argumentmultiply argument 1 argument 1- Function bodyargument N- Accesses the Nth parameter
8. Recursive Functions
Factorial
define_word "factorial" 1
if equal 0 argument 1
1
multiply argument 1 factorial add -1 argument 1
print factorial 0
print factorial 4
print factorial 5
Output:
1
24
120
How Recursion Works:
- Base case:
if equal 0 argument 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 argument 1 1
argument 1
add fib add -1 argument 1 fib add -2 argument 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
set "name" "Alice"
print get "name"
define_word "greet" 1
print argument 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 argument 1
1
multiply argument 1 factorial add -1 argument 1
Syntax:
! "<filename>" - Executes the specified file
The ! operator includes and runs another file's code.
11. do...end Blocks
Grouping Multiple Statements
set "x" 10
if greater get "x" 5 do
print " x is greater than 5 "
print " The value is: "
print get "x"
end do
print " x is not greater than 5 "
end
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 argument 1 argument 2
define_word is_prime 1 do
set n argument 1
if not greater get n 1
false
if equal get n 2
true
do
set i 2
set prime true
while not greater get i multiply get n get n do
if greater multiply get i get i get n
set i multiply get n get n
do
if is_divisible get n get i
set prime false
dont nothing
set i add get i 1
end
end
get prime
end
end
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.