An open API service indexing awesome lists of open source software.

https://github.com/felixbd/while

Parser and interpreter for the While programming language (in haskell)
https://github.com/felixbd/while

haskell interpreter parser while

Last synced: about 2 months ago
JSON representation

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

Awesome Lists containing this project

README

        

#+title: WHILE-Programme
#+author: Felix Drees
#+STARTUP: latexpreview
#+OPTIONS: toc:nil tex:t # tex:verbatim num:nil
#+keywords: while programs, turing complete, turing machines

* 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, \sum, P, S) $$

where

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

(the set of non terminal Symbols)

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

(the set of terminal symbols)

$S = Prog$

(the start non terminal)

$P = \{$

$Prog \to // comment \dots$

$Prog \to Prog; Prog$

$Prog \to Var := Expr$

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

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

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

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

$Const \to \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 $\psi$ for calculating a simple multiplication $a \cdot b$

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

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

lets calculate $420 \cdot 69$ via $\psi(v)$ where $v := (420, 69)$, the result should be $28980$.

#+begin_src shell :exports both :results output
cat ./examples/multiplication-func.while
#+end_src

#+RESULTS:
#+begin_example
// 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
#+end_example

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

#+begin_src
x0 := 0;
x1 := 420;
x2 := 69;

loop x2 do x0 := x0 + x1 end
#+end_src

*** how to call `stack run`

#+begin_src shell :exports both :results output
stack run
#+end_src

#+RESULTS:
:
: = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
: [91m[Error][0m: Usage: stack run
: Use 'stack run ' to interpret a while source file.
: = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
:

#+begin_src shell :exports both :results output
stack run ./examples/multiplication-func.while
#+end_src

#+RESULTS:
#+begin_example

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

[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)]

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

#+end_example

*** compile

#+begin_src shell
stack build
#+end_src

*** run

#+begin_src shell
stack run
#+end_src

*** test

#+begin_src shell
stack test
#+end_src

*** linting

#+begin_src shell
stack exec hlint src/*.hs app/*.hs test/*.hs
#+end_src

** requirements

*** using `apt`

**** [[https://docs.haskellstack.org/en/stable/][Haskell Tool Stack]]

#+begin_src shell
curl -SL https://get.haskellstack.org/ | sh
#+end_src

**** [[https://www.haskell.org/ghc/][Glasgow Haskell Compiler]]

#+begin_src shell
apt install ghc
#+end_src

**** [[https://github.com/ndmitchell/hlint#readme][HLint]]

#+begin_src shell
apt install hlint
#+end_src

*** using `nix`

#+begin_src shell
nix-shell
#+end_src

**** hlint

#+begin_src shell
hlint src/*.hs app/*.hs test/*.hs
#+end_src

**** build and run

same as before

** LICENSE (BSD-3-Clause)

#+begin_src shell :exports both :results output
cat ./LICENSE
#+end_src

#+RESULTS:
#+begin_example
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.
#+end_example