Basics
This topic is meant to demonstrate the basics
Simple tutorial
Let's start with a simple demonstration of how Pile works. Each number or string you type is automatically pushed onto the stack. From there, you can perform operations.
# Adding two numbers
3 5 +
# The stack now contains: 8
# A more complex operation
4 2 * 6 +
# Multiplies 4 by 2, then adds 6. Stack result: 14
Pile operates with a single stack, so everything you do affects the same stack. Here's a demonstration of how stack manipulation works:
# Stack manipulation example
10 dup
# Duplicates the top value: 10 10
20 swap
# Swaps the top two values: 20 10
drop
# Removes the top value: 20
Use trace operation to write to the terminal the last item on top of the stack. Use Print to print to stdout the last string on top of the stack (if it is a number, converts it to a string)
# Use trace as a debugging operation
99 trace
# Use Print to write messages like Hello World
"Hello World" Print
Control flow
Pile uses simple keywords like if, else, and end to handle conditional logic. Here's a small example:
# Conditional example
5 5 = if
"Equal" trace
else # This is optional
"Not equal" trace
end
# Output: Equal
Procedures
In Pile, you can create reusable blocks of code called procedures. They do not take arguments directly but can manipulate the stack:
# Procedure example
proc say_hello
"Hello, Pile!" Print
end
# Calling the procedure
say_hello
# Output: Hello, Pile!
This code defines a procedure say_hello that prints "Hello, Pile!". When the procedure is called, the stack remains consistent.