Building

Students uses old version of Haskell and BNFC, therefore a newer version should be prepended to the PATH before building the project.

export PATH=~/.cabal/bin:/home/students/inf/PUBLIC/MRJP/ghc-8.2.2/bin:$PATH
  • Installing dependencies
make deps
  • Generating the parser
make grammar
  • Compiling the interpreter
make interpreter

Running make with no subcommand will execute all of the above.

Interpreter

Type definitions

data Var = ...   -- variable value
type Loc = ...   -- variable location

Monad Reader

Used to store the environment - a mapping from an identifier to a location.

import qualified Data.Map as DataMap

type Env = DataMap.Map Ident Loc

Monad State

Used as a store - a mapping from a location to the actual value. Both functions and variables are stored in the same store.

import qualified Data.Map as DataMap

type Store = DataMap.Map Loc Var

Monad Except

Used to handle runtime errors in the interpreter.

Static type checking

Monad Reader

Used to store the environment - a mapping from an identifier to a type.

import qualified Data.Map as DataMap

type Env = DataMap.Map Ident Type

Monad Except

Used to handle runtime errors in the interpreter.

Files structure

General

  • Main.hs mostly copied from a BNFC generated source code with small tweaks to start the interpreter on a given file.
  • Util.hs contains helper functions used in both interpreter and the type checker.
  • IInterpreter.hs defines types used in the interpreter.
  • TCheck.hs defines types used in the tupe checker.

Interpreter

  • IExec.hs implements exec function, which execs the given program.
  • IUtil.hs contains helper functions used in the interpreter.

Type checker

  • TExec.hs implements execType function, which runs the type check on the given program.
  • TUtil.hs contains helper functions used in the type checker.

Methods

  • MArray.hs implements methods, which can be called on an Array variable:
    • Append appends an element at the end of an array.
    • At returns an element at the given position.
    • Put inserts an element at the given position to an array.
    • Length returns the length of an array.
  • MError.hs implements methods, which can be called on an Error variable.
    • HaveOccurred returns whether an error is not empty.
  • MInt.hs implements methods, which can be called on an Int variable.
    • ToString converts an integer to its string representation.
  • MString.hs implements methods, which can be called on a String variable.
    • ToInt converts a string to an integer, if the given string does not represent a decimal number calling this method will result in an RuntimeError.
  • MTuple.hs implements methods, which can be called on a Tuple variable.
    • Extract extracts a single value from a tuple.
    • Exchange replaces a single element in a tuple.

All methods have a corresponding *Type function used within the type checker to validate the type.