GithubHelp home page GithubHelp logo

antonkesy / peter-lang Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 138 KB

Useless and barely functional toy programming language interpreter written in Haskell

License: Apache License 2.0

Dockerfile 1.67% Haskell 96.67% Shell 1.66%

peter-lang's Introduction

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

peter-lang's People

Contributors

antonkesy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.