Libra is a basic stack-based programming language simulated through Haskell. Heavily inspired by Porth and Forth
Goals:
- Basic integer operations
- Data types "String" and "Boolean"
- While loops
- If statements
- Function macros
- Simple memory
- Turing completeness (Runs Rule110, see
examples/rule110.♎️
)
Print numbers and words:
123 456 + print
"Hello World!" print
yields
579
Hello World!
Print even numbers between 0 and 10
10 0 while 2dup < run
"Number: " put dup print
2 +
end
yields
Number: 0
Number: 2
Number: 4
Number: 6
Number: 8
Libra programs are simulated in Haskell, best done through the interactive shell started by running
$ ghci run.hs
Run programs by running
λ> run "file.♎️"
Currently supported data types are integers, strings, and booleans. Pushed onto the stack as follows:
123 "Hello World!" False
if <body> end
If checks if the top stack element is True, if so it executes the body, else it skips to end.
if <true_body> else <false_body> end
If checks if the top stack element is True, if so it executes the true body and after skips to the end, else it will execute the else body.
while <cond> run <body> end
While checks if a certain condition is true, if so it runs the body and returns to the while, if not, it skips to end.
+
: Sum the two top stack elements
1 2 + print
yields 3
-
: Subtract the two top stack elements
10 2 - print
yields 8
*
: Multiply the two top stack elements
3 4 * print
yields 12
/
: Integer divide the two top stack elements
10 3 / print
yields 3
%
: Mod divide the two top stack elements
10 3 % print
yields 1
dup
: Duplicate the top stack element
"Hello" dup print print
yields Hello Hello
drop
: Drop the top stack element
"Hello" "World" drop print
yields Hello
2dup
: Duplicate the two top stack element
10 2 2dup + print / print
yields 12 5
2drop
: Drop the two top stack element
"Hello" 10 "World" 2drop print
yields Hello
swap
: Swap the two top stack element
"Bottom" "Top" swap print print
yields Bottom Top
over
: Copies the element second on the stack
"Hello" "World" over print print print
yields Hello World Hello
print
: Print the top stack element followed by newline
"Hello" print "World" print
yields
Hello
World
put
: Print the top stack element without a newline
"Hello" put "World" put
yields
HelloWorld
!
: Negates the top stack element
False ! print
yields True
|
: Logical OR's the top two stack elements
True False | print
yields True
&
: Logical AND's the top two stack elements
True False & print
yields False
=
: Tests equality on the top two stack elements
True False = print
yields False
<
: Tests if the second stack element is smaller than the top stack element
2 5 < print
yields True
<
: Tests if the second stack element is greater than the top stack element
2 5 > print
yields False
<=
: Tests if the second stack element is smaller than or equal to the top stack element
2 5 <= print
yields True
<=
: Tests if the second stack element is greater than or equal to the top stack element
2 5 >= print
yields False
Memory works using a memory pointer which can be increased and decreased.
#
: Push the memory pointer to the stacks
: Store a token to a pointerr
: Read from a stack pointer
# 4 s
# 5 + "String" s
# r print
# 5 + r print
yields 4 "String"
~
: Ignores the rest of the line
123 123 + ~ Add the numbers together
$ <name> [<body>]
Expands name to body when used in code
$ incr [1 +]
10 incr print
yields 11