https://github.com/rzbin/libra
♎️ Libra is a basic stack-based programming language simulated through Haskell.
https://github.com/rzbin/libra
Last synced: 7 months ago
JSON representation
♎️ Libra is a basic stack-based programming language simulated through Haskell.
- Host: GitHub
- URL: https://github.com/rzbin/libra
- Owner: rzbin
- Created: 2021-10-09T13:20:37.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2021-10-14T08:49:39.000Z (about 4 years ago)
- Last Synced: 2024-11-07T17:44:21.255Z (about 1 year ago)
- Language: Haskell
- Homepage:
- Size: 27.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
Awesome Lists containing this project
- awesome-programming-languages - Libra - Libra is a basic stack-based programming language simulated through Haskell. Heavily inspired by Porth and Forth (Uncategorized / Uncategorized)
README
# Libra ♎️
Libra is a basic stack-based programming language simulated through Haskell.
Heavily inspired by [Porth](https://github.com/tsoding/porth) and [Forth]()
Goals:
- [x] Basic integer operations
- [x] Data types "String" and "Boolean"
- [x] While loops
- [x] If statements
- [x] Function macros
- [x] Simple memory
- [x] Turing completeness (Runs [Rule110](https://en.wikipedia.org/wiki/Rule_110), see [`examples/rule110.♎️`](./examples/rule110.♎️))
## Example
Print numbers and words:
```pascal
123 456 + print
"Hello World!" print
```
yields
```
579
Hello World!
```
Print even numbers between 0 and 10
```pascal
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
```bash
$ ghci run.hs
```
Run programs by running
```bash
λ> run "file.♎️"
```
## Language
### Data types
Currently supported data types are integers, strings, and booleans. Pushed onto the stack as follows:
```pascal
123 "Hello World!" False
```
### Control flow
#### If statements
`if end`
If checks if the top stack element is True, if so it executes the body, else it skips to end.
`if else 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 run 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
```pascal
1 2 + print
```
yields `3`
- `-` : Subtract the two top stack elements
```pascal
10 2 - print
```
yields `8`
- `*` : Multiply the two top stack elements
```pascal
3 4 * print
```
yields `12`
- `/` : Integer divide the two top stack elements
```pascal
10 3 / print
```
yields `3`
- `%` : Mod divide the two top stack elements
```pascal
10 3 % print
```
yields `1`
#### Stack editing
- `dup` : Duplicate the top stack element
```pascal
"Hello" dup print print
```
yields `Hello Hello`
- `drop` : Drop the top stack element
```pascal
"Hello" "World" drop print
```
yields `Hello`
- `2dup` : Duplicate the two top stack element
```pascal
10 2 2dup + print / print
```
yields `12 5`
- `2drop` : Drop the two top stack element
```pascal
"Hello" 10 "World" 2drop print
```
yields `Hello`
- `swap` : Swap the two top stack element
```pascal
"Bottom" "Top" swap print print
```
yields `Bottom Top`
- `over` : Copies the element second on the stack
```pascal
"Hello" "World" over print print print
```
yields `Hello World Hello`
#### Output
- `print` : Print the top stack element followed by newline
```pascal
"Hello" print "World" print
```
yields
```
Hello
World
```
- `put` : Print the top stack element without a newline
```pascal
"Hello" put "World" put
```
yields
```
HelloWorld
```
#### Logic
- `!` : Negates the top stack element
```pascal
False ! print
```
yields `True`
- `|` : Logical OR's the top two stack elements
```pascal
True False | print
```
yields `True`
- `&` : Logical AND's the top two stack elements
```pascal
True False & print
```
yields `False`
#### Comparison
- `=` : Tests equality on the top two stack elements
```pascal
True False = print
```
yields `False`
- `<` : Tests if the second stack element is smaller than the top stack element
```pascal
2 5 < print
```
yields `True`
- `<` : Tests if the second stack element is greater than the top stack element
```pascal
2 5 > print
```
yields `False`
- `<=` : Tests if the second stack element is smaller than or equal to the top stack element
```pascal
2 5 <= print
```
yields `True`
- `<=` : Tests if the second stack element is greater than or equal to the top stack element
```pascal
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
```pascal
# 4 s
# 5 + "String" s
# r print
# 5 + r print
```
yields `4 "String"`
### Comments
- `~` : Ignores the rest of the line
```pascal
123 123 + ~ Add the numbers together
```
### Function macros
`$ []`
Expands name to body when used in code
```pascal
$ incr [1 +]
10 incr print
```
yields `11`