Useless and barely functional toy programming language interpreter written in Haskell from scratch (without any research how to actually lay things out)
The missing link between C and Python
C style syntax with Python like runtime interpretation and built in functions.
Main function void main() {}
is the entry point.
void main() {
print("Hello, World!");
}
Alternately, statements are executed from top to bottom in case no main is found.
print("Hello, World!");
- void
- bool
- int
- float
- str
str(1)
->"1"
int("1")
->1
float("1.3")
->1.3
There is no operator precedence
Operation | void | bool | int | float | str |
---|---|---|---|---|---|
+ | x | x | ✓ | ✓ | ✓ |
- | x | x | ✓ | ✓ | x |
* | x | x | ✓ | ✓ | x |
\ | x | x | ✓ | x | x |
% | x | x | ✓ | x | x |
== | x | ✓ | ✓ | ✓ | ✓ |
!= | x | ✓ | ✓ | ✓ | ✓ |
< | x | ✓ | ✓ | ✓ | x |
> | x | ✓ | ✓ | ✓ | x |
<= | x | ✓ | ✓ | ✓ | x |
>= | x | ✓ | ✓ | ✓ | x |
&& | x | ✓ | ✓ | x | x |
|| | x | ✓ | ✓ | x | x |
If:
if 1 < 2 {
i = 1;
} else {
i = -1;
}
while:
int j = 0;
while j < 10 {
println(str(j));
j = j + 1;
}
int f() {
return 42;
}
void main() {
int x = f();
}
struct T {
int x;
int y;
}
T t;
t.x = 2;
- print -> prints string without buffering
- println -> print + appends newline at the end
- input -> reads stdin and outputs str
- left side of operation has to be atomic (because of left recursive grammar)
- no arrays (yet)
- many bugs
- bad error messages from parser and interpreter
- types are optional and currently not always strictly enforced
- Arrays
- Classes (or something similar)
- String manipulation
- Multiple files support
- Enums
- Pattern matching
- switch
- for
- define grammar
Dependencies:
- stack
- ghc-9.4.7:
stack --compiler ghc-9.4.7 setup
docker build --target exe -t peter .
docker run peter ./examples/print.mmm
> stack run -- --help
Usage: peter-exe [-i|--inline STRING] [PATH]
Available options:
-i,--inline STRING Inline string to parse and interpret
-h,--help Show this help text
stack run -- ./examples/main_hello_world.mmm
stack run -- -i "void main() { print(\"Hello, World\"); }"
stack run -- ./examples/short_hello_world.mmm
stack run -- -i "print(\"Hello, World\");"
stack test
cd ./test/E2E/Interpreter/examples/ && ./check_examples.sh
ormolu --mode inplace $(find . -name '*.hs')
docker build .
+ GitHub Actions