Code Monkey home page Code Monkey logo

imp's Introduction

IMP - a simple imperative language

IMP is a small language of while programs. IMP is called an "imperative" language because program execution involeves carrying out a series of explicit commands to change state.

The formal semantics of programming languages - Glynn Winskel

This OCaml project implements a simple interpreter of the IMP programming language described by Winskell. The language is extended with some tweaks which are not present in the book, such as the print and read functions. The interpreter makes use of ocamllex, menhir and ppx as dependencies. To build the executable run dune run and to execute type dune exec imp.

Be aware that this implementation is a toy, it surely has some not checked errors ๐Ÿ™‚

Grammar

The grammar is expressed using the BNF.

Arithmetic Expressions
a ::= n | X | a0 + a1 | a0 - a1 | a0 * a1 | read | ( a )

Boolean Expressions
b ::= true | false | a0 = a1 | a0 <= a1 | a0 < a1 | a0 >= a1 | a0 > a1 | !b | b0 || b1 | b0 && b1 | ( b )

Commands
c ::= skip | print a | c0; c1 | if b then c0 else c1 end | while b do c end

A simple program

The program shown below implement the factorial function.

n := read;
f := 1;
while n > 0 do
    f := f * n;
    n := n - 1;
end
print f;

All the examples can be found inside the playground directory.

Added Features

The features added to the language are a few:

  • the end keyword has been introduced to mark the end of while/if block;
  • the read expression is useful to read a number from the standard input (โš ๏ธ No check is performed onto the typed input);
  • the print command is the most complex one: it prints a number onto the screen.

imp's People

Contributors

gabryon99 avatar

Stargazers

 avatar

Watchers

 avatar  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.