Giter Club home page Giter Club logo

parsim's Introduction

Parsim

Build

Parsim is a monadic parser for a simple imperative language.

Implemented to study {functors, applicatives, monads}.

Grammar

<expr>   ::= <expr> <op> <expr> | <symbol> | <value>
<symbol> ::= [a-zA-Z]+
<value>  ::= [1-9][0-9]* | 0
<op>     ::= + | - | * | == | <
<prog>   ::= <symbol> = <expr>;
           | <prog> <prog>
           | if (<expr>) then {<prog>} else {<prog>}
           | while (<expr>) {<prog>}
           | return <expr>;

The corresponding ADT is defined in Types.hs.

Currently supporting only integer values (Int type).

Examples

Given the following program (defined in testdata/raw/test1-10.in):

y = 1 * 2;
x = 4 + y;
if (y == 2) then {
    z = 0;
    while (z < 3) {
        x = x + 1;
        y = x + 2;
        z = z + 1;
    }
    return y;
} else {
    return x + y;
}

you can use make run INPUT=testdata/raw/test1-10.in to run it, obtaining the following output:

P (Right 11)

which indicates a successful run.

You can also write an immediate short program, like this:

make run INPUT=<(echo "x = 23; y = x - 17; return x * y;")

Output: P (Right 138).

Errors

Syntax error

E.g. missing ;

a = 15;
b = 25;

while (0 < b) {
    if (b < a) then {
        a = a - b
    } else {
        b = b - a;
    }
}

c = a;
return c;

Output:

P (Left "Syntax error")

Uninitialized variable

Variable x is uninitialized:

z = 0;
while (z < 3) {
    x = x + z;
    z = z + 1;
}
return z;

Output:

P (Left "Uninitialized variable")

Missing return

No return path:

x = 10;
y = 20;
z = x + y;

Output:

P (Left "Missing return")

Resources

G. Hutton, E. Meijer - Monadic Parsing in Haskell

parsim's People

Contributors

alexandru-dinu avatar

Watchers

 avatar  avatar

parsim's Issues

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.