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 println 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 println to print messages like Hello World
"Hello World" println

Pile works with the Last-in, First-out (LIFO) mechanism of executing operations. That means that the order of the operands of some operations that matter the order of the operands is reversed:

# Subtraction, for example, is reversed in order
1 10 - # This is 10 - 1 and not 1 - 10

# Division can be weird
2 10 / # This is 10/2 and not 2/10

1 10 + # In addition, the order doesn't matter
10 1 + # In addition, the order doesn't matter

5 2 * # Same for multiplication
2 5 * # Same for multiplication

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!" println
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.

Learn more about Pile in the next topics.