https://github.com/theflyingcodr/goforth
A golang implementation of a stack based reverse polish notation language, much like Forth or Bitcoin Script.
https://github.com/theflyingcodr/goforth
bitcoin-script forth golang golang-library stack
Last synced: about 1 year ago
JSON representation
A golang implementation of a stack based reverse polish notation language, much like Forth or Bitcoin Script.
- Host: GitHub
- URL: https://github.com/theflyingcodr/goforth
- Owner: theflyingcodr
- License: mit
- Created: 2021-02-10T00:09:22.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-02-10T10:42:31.000Z (about 5 years ago)
- Last Synced: 2023-03-04T05:08:56.128Z (about 3 years ago)
- Topics: bitcoin-script, forth, golang, golang-library, stack
- Language: Go
- Homepage:
- Size: 7.81 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GoForth
After reading about the [Forth](https://en.wikipedia.org/wiki/Forth_(programming_language)) programming language and its relationship to bitcoin script I decided to have a bash and writing an example in go, because, why not!
This is a stack based (LIFO) [reverse polish](https://en.wikipedia.org/wiki/Reverse_Polish_notation) language.
It has a limited set of simple operators as this is just a toy and learning experience at the moment, these are listed below:
| Operator | Description |
|---------- |-------------------------------------------------------------------------------------------- |
| ADD | Adds two numbers and pushes the result to the stack |
| SUB | Takes a number from another and pushes the result to the stack |
| EQ | Evaluates if the preceeding two values are equal, it will push 1 if TRUE or 0 if FALSE |
| NE | Evaluates if the preceeding two values are not equal, it will push 1 if TRUE or 0 if FALSE |
| HASH256 | Generates a SHA256 from the preceeding value and pushes to the stack |
More can be easily added by adding to the ops.go file - feel free to create an issue and PR if you want some more for some reason.
## Useage
It's a very simple public interface, just one function that takes your expression string and returns the last remaining item on the stack after evaluating every word in the expression.
To add two numbers for example, you would supply this string `2 3 ADD` which would evaluate to 5.
In terms of this library you would do:
```go
result := goforth.Run("1 10 ADD 11 EQ HASH256")
```
## Contributing
If for whatever reason you have a use for this and want to add more operators create an issue and PR and add them to the `ops.go` file.
Same goes if you spot an error, which is highly likely as I just threw this together very quickly.