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!" printyields
579
Hello World!
Print even numbers between 0 and 10
10 0 while 2dup < run
"Number: " put dup print
2 +
endyields
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.hsRun programs by running
λ> run "file.♎️"Currently supported data types are integers, strings, and booleans. Pushed onto the stack as follows:
123 "Hello World!" Falseif <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 + printyields 3
-: Subtract the two top stack elements
10 2 - printyields 8
*: Multiply the two top stack elements
3 4 * printyields 12
/: Integer divide the two top stack elements
10 3 / printyields 3
%: Mod divide the two top stack elements
10 3 % printyields 1
dup: Duplicate the top stack element
"Hello" dup print printyields Hello Hello
drop: Drop the top stack element
"Hello" "World" drop printyields Hello
2dup: Duplicate the two top stack element
10 2 2dup + print / printyields 12 5
2drop: Drop the two top stack element
"Hello" 10 "World" 2drop printyields Hello
swap: Swap the two top stack element
"Bottom" "Top" swap print printyields Bottom Top
over: Copies the element second on the stack
"Hello" "World" over print print printyields Hello World Hello
print: Print the top stack element followed by newline
"Hello" print "World" printyields
Hello
World
put: Print the top stack element without a newline
"Hello" put "World" putyields
HelloWorld
!: Negates the top stack element
False ! printyields True
|: Logical OR's the top two stack elements
True False | printyields True
&: Logical AND's the top two stack elements
True False & printyields False
=: Tests equality on the top two stack elements
True False = printyields False
<: Tests if the second stack element is smaller than the top stack element
2 5 < printyields True
<: Tests if the second stack element is greater than the top stack element
2 5 > printyields False
<=: Tests if the second stack element is smaller than or equal to the top stack element
2 5 <= printyields True
<=: Tests if the second stack element is greater than or equal to the top stack element
2 5 >= printyields 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 printyields 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 printyields 11