Skip to content
/ while Public

Parser and interpreter for the While programming language (in haskell)

License

Notifications You must be signed in to change notification settings

felixbd/while

Repository files navigation

WHILE-Programme

while

Parser and interpreter for the while programming language

The while programming language is a verry simplified language, which is turing complete!

https://github.com/felixbd/while/actions/workflows/ci-test.yml/badge.svg?branch=main

syntax specification via context-free type-2 grammar

The set of WHILE programs is the language generated by the following grammar

$$ G = (N, ∑, P, S) $$

where

$N = \{ Prog, Var, Const, Expr \}$

(the set of non terminal Symbols)

$∑ = \{ loop, while, do, end, +, -, ;, :=, != \} ∪ \mathbb{N}_0 ∪ VariableNames$

(the set of terminal symbols)

$S = Prog$

(the start non terminal)

$P = \{$

$Prog → // comment …$

$Prog → Prog; Prog$

$Prog → Var := Expr$

$Expr → Var \mid Const \mid Expr + Expr \mid Expr - Expr$

$Prog → \textrm{loop Expr do Prog end}$

$Prog → \textrm{while Expr != Expr do Prog end}$

$Var → VariableName$ (that has to start with ascii letter and can contain digits or underscore)

$Const → \mathbb{N}_0$

$\}$

(the set of production rules)

semantic

as expected, i guess … lol

NOTE: the subtraction is only defined in $\mathbb{N}_0$ therefore $5 - 8$ will result in $0$

also i added a max recursion depth like python3 to prevent users to write non terminating code … (the max rec depth for a while loop is 1500 iterations)

usage

example

lets say you have a function $ψ$ for calculating a simple multiplication $a ⋅ b$

$$ψ \colon \mathbb{N}^2_0 \longrightarrow \mathbb{N}_0$$

$$(x_1, x_2) \mapsto x_1 ⋅ x_2$$

lets calculate $420 ⋅ 69$ via $ψ(v)$ where $v := (420, 69)$, the result should be $28980$.

cat ./examples/multiplication-func.while
// init var state
rv := 0;
a := 420;
b := 69;

// loop will be exec b-times
while b != 0 do

    // calculate rv += a (b-times)
    //  -> a * b = a + a + ... b-times

    temp := rv;
    loop a do temp := temp + 1 end;
    rv := temp;

    b := b - 1
end

since this is a bit tidies i allowed users to use expressions in the context

  • conditions
  • additions
  • and subtractions

(refer to the grammar defined above)

shortened (not commpletely while-language correct …) version

x0 := 0;
x1 := 420;
x2 := 69;

loop x2 do x0 := x0 + x1 end

how to call `stack run`

stack run

:

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
�[91m[Error]�[0m: Usage: stack run <filename>
Use 'stack run <filename>' to interpret a while source file.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

:

stack run ./examples/multiplication-func.while
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

�[92m[Interpreting file]�[0m    ./examples/multiplication-func.while
�[92m[READING .WHILE FILE]�[0m "./examples/multiplication-func.while"
�[92m[DONE TOKENIZE]�[0m
�[92m[DONE PARSING AST]�[0m
�[92m[RESULT OF EVALUATION]�[0m
[("temp",28980),("b",0),("a",420),("rv",28980)]

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

compile

stack build

run

stack run

test

stack test

linting

stack exec hlint src/*.hs app/*.hs test/*.hs

requirements

using `apt`

curl -SL https://get.haskellstack.org/ | sh
apt install ghc
apt install hlint

using `nix`

nix-shell

hlint

hlint src/*.hs app/*.hs test/*.hs

build and run

same as before

LICENSE (BSD-3-Clause)

cat ./LICENSE
Copyright Felix Drees (c) 2023

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.

    * Neither the name of Felix Drees nor the names of other
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.