Skip to content

rzbin/libra

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Libra ♎️

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.♎️)

Example

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

Usage

Libra programs are simulated in Haskell, best done through the interactive shell started by running

$ ghci run.hs

Run programs by running

λ> run "file.♎️"

Language

Data types

Currently supported data types are integers, strings, and booleans. Pushed onto the stack as follows:

123 "Hello World!" False

Control flow

If statements

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 loops

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.

Manipulation words

Integer arithmetic

  • + : 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

Stack editing

  • 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

Output

  • 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

Logic

  • ! : 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

Comparison

  • = : 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

Memory works using a memory pointer which can be increased and decreased.

  • # : Push the memory pointer to the stack
  • s : Store a token to a pointer
  • r : Read from a stack pointer
# 4 s
# 5 + "String" s
# r print
# 5 + r print

yields 4 "String"

Comments

  • ~ : Ignores the rest of the line
123 123 + ~ Add the numbers together

Function macros

$ <name> [<body>]

Expands name to body when used in code

$ incr [1 +]
10 incr print

yields 11

About

♎️ Libra is a basic stack-based programming language simulated through Haskell.

Resources

Stars

Watchers

Forks