Skip to content

Useless and barely functional toy programming language interpreter written in Haskell

License

Notifications You must be signed in to change notification settings

antonkesy/peter-lang

Repository files navigation

peter-lang

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

Syntax

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

Limitations & Issues

  • 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

Missing/Planned Features

  • Arrays
  • Classes (or something similar)
  • String manipulation
  • Multiple files support
  • Enums
  • Pattern matching
  • switch
  • for
  • define grammar

Installation

Native

Dependencies:

  • stack
  • ghc-9.4.7: stack --compiler ghc-9.4.7 setup

Docker

docker build --target exe -t peter .
docker run peter ./examples/print.mmm

Usage

> 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\");"

Tests

  • stack test
  • cd ./test/E2E/Interpreter/examples/ && ./check_examples.sh

Formatting

ormolu

ormolu --mode inplace $(find . -name '*.hs')

CI

docker build . + GitHub Actions

About

Useless and barely functional toy programming language interpreter written in Haskell

Resources

License

Stars

Watchers

Forks