{"id":18421089,"url":"https://github.com/felixbd/while","last_synced_at":"2025-04-13T11:22:01.973Z","repository":{"id":198461294,"uuid":"698016728","full_name":"felixbd/while","owner":"felixbd","description":"Parser and interpreter for the While programming language (in haskell)","archived":false,"fork":false,"pushed_at":"2023-12-04T23:22:57.000Z","size":93,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-16T07:17:38.695Z","etag":null,"topics":["haskell","interpreter","parser","while"],"latest_commit_sha":null,"homepage":"","language":"Haskell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/felixbd.png","metadata":{"files":{"readme":"README.org","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-09-29T00:47:33.000Z","updated_at":"2023-11-29T18:49:04.000Z","dependencies_parsed_at":null,"dependency_job_id":"2bb226ec-733b-4307-b851-05b1a31bed0c","html_url":"https://github.com/felixbd/while","commit_stats":null,"previous_names":["felixbd/while"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felixbd%2Fwhile","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felixbd%2Fwhile/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felixbd%2Fwhile/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felixbd%2Fwhile/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/felixbd","download_url":"https://codeload.github.com/felixbd/while/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248703744,"owners_count":21148223,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["haskell","interpreter","parser","while"],"created_at":"2024-11-06T04:24:15.307Z","updated_at":"2025-04-13T11:22:01.939Z","avatar_url":"https://github.com/felixbd.png","language":"Haskell","funding_links":[],"categories":[],"sub_categories":[],"readme":"#+title: WHILE-Programme\n#+author: Felix Drees\n#+STARTUP: latexpreview\n#+OPTIONS: toc:nil tex:t  # tex:verbatim num:nil\n#+keywords: while programs, turing complete, turing machines\n\n* while\n\nParser and interpreter for the *while* programming language\n\nThe *while* programming language is a verry simplified language, which is turing complete!\n\n[[https://github.com/felixbd/while/actions/workflows/ci-test.yml/badge.svg?branch=main]]\n\n\n** syntax specification via context-free type-2 grammar\n\nThe set of *WHILE* programs is the language generated by the following grammar\n\n$$ G = (N, \\sum, P, S) $$\n\nwhere\n\n$N = \\{ Prog, Var, Const, Expr \\}$\n\n(the set of non terminal Symbols)\n\n$\\sum = \\{ loop, while, do, end, +, -, ;, :=, != \\} \\cup \\mathbb{N}_0 \\cup VariableNames$\n\n(the set of terminal symbols)\n\n$S = Prog$\n\n(the start non terminal)\n\n$P = \\{$\n\n$Prog \\to // comment \\dots$\n\n$Prog \\to Prog; Prog$\n\n$Prog \\to Var := Expr$\n\n$Expr \\to Var \\mid Const \\mid Expr + Expr \\mid Expr - Expr$\n\n$Prog \\to \\textrm{loop Expr do Prog end}$\n\n$Prog \\to \\textrm{while Expr != Expr do Prog end}$\n\n$Var \\to VariableName$  (that has to start with ascii letter and can contain digits or underscore)\n\n$Const \\to \\mathbb{N}_0$\n\n$\\}$\n\n(the set of production rules)\n\n\n** semantic\n\nas expected, i guess ... lol\n\nNOTE: the subtraction is only defined in $\\mathbb{N}_0$\ntherefore $5 - 8$ will result in $0$\n\nalso i added a max recursion depth like python3 to prevent users to write non terminating code ...\n(the max rec depth for a *while* loop is *1500* iterations)\n\n\n** usage\n\n*** example\n\nlets say you have a function $\\psi$ for calculating a simple multiplication $a \\cdot b$\n\n$$\\psi \\colon \\mathbb{N}^2_0 \\longrightarrow \\mathbb{N}_0$$\n\n$$(x_1, x_2) \\mapsto x_1 \\cdot x_2$$\n\nlets calculate $420 \\cdot 69$ via $\\psi(v)$ where $v := (420, 69)$, the result should be $28980$.\n\n#+begin_src shell :exports both :results output\ncat ./examples/multiplication-func.while\n#+end_src\n\n#+RESULTS:\n#+begin_example\n// init var state\nrv := 0;\na := 420;\nb := 69;\n\n// loop will be exec b-times\nwhile b != 0 do\n\n    // calculate rv += a (b-times)\n    //  -\u003e a * b = a + a + ... b-times\n\n    temp := rv;\n    loop a do temp := temp + 1 end;\n    rv := temp;\n\n    b := b - 1\nend\n#+end_example\n\nsince this is a bit tidies i allowed users to use expressions in the context\n\n- conditions\n- additions\n- and subtractions\n\n(refer to the grammar defined above)\n\nshortened (not commpletely while-language correct ...) version\n\n#+begin_src\nx0 := 0;\nx1 := 420;\nx2 := 69;\n\nloop x2 do x0 := x0 + x1 end\n#+end_src\n\n\n*** how to call `stack run`\n\n#+begin_src shell :exports both :results output\nstack run\n#+end_src\n\n#+RESULTS:\n:\n: = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n: \u001b[91m[Error]\u001b[0m: Usage: stack run \u003cfilename\u003e\n: Use 'stack run \u003cfilename\u003e' to interpret a while source file.\n: = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n:\n\n#+begin_src shell :exports both :results output\nstack run ./examples/multiplication-func.while\n#+end_src\n\n#+RESULTS:\n#+begin_example\n\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n\u001b[92m[Interpreting file]\u001b[0m    ./examples/multiplication-func.while\n\u001b[92m[READING .WHILE FILE]\u001b[0m \"./examples/multiplication-func.while\"\n\u001b[92m[DONE TOKENIZE]\u001b[0m\n\u001b[92m[DONE PARSING AST]\u001b[0m\n\u001b[92m[RESULT OF EVALUATION]\u001b[0m\n[(\"temp\",28980),(\"b\",0),(\"a\",420),(\"rv\",28980)]\n\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n#+end_example\n\n\n*** compile\n\n#+begin_src shell\nstack build\n#+end_src\n\n\n*** run\n\n#+begin_src shell\nstack run\n#+end_src\n\n\n*** test\n\n#+begin_src shell\nstack test\n#+end_src\n\n\n*** linting\n\n#+begin_src shell\nstack exec hlint src/*.hs app/*.hs test/*.hs\n#+end_src\n\n\n** requirements\n\n*** using `apt`\n\n**** [[https://docs.haskellstack.org/en/stable/][Haskell Tool Stack]]\n\n#+begin_src shell\ncurl -SL https://get.haskellstack.org/ | sh\n#+end_src\n\n\n**** [[https://www.haskell.org/ghc/][Glasgow Haskell Compiler]]\n\n#+begin_src shell\napt install ghc\n#+end_src\n\n**** [[https://github.com/ndmitchell/hlint#readme][HLint]]\n\n#+begin_src shell\napt install hlint\n#+end_src\n\n\n*** using `nix`\n\n#+begin_src shell\nnix-shell\n#+end_src\n\n**** hlint\n\n#+begin_src shell\nhlint src/*.hs app/*.hs test/*.hs\n#+end_src\n\n\n**** build and run\n\nsame as before\n\n\n** LICENSE (BSD-3-Clause)\n\n#+begin_src shell :exports both :results output\ncat ./LICENSE\n#+end_src\n\n#+RESULTS:\n#+begin_example\nCopyright Felix Drees (c) 2023\n\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met:\n\n    ,* Redistributions of source code must retain the above copyright\n      notice, this list of conditions and the following disclaimer.\n\n    ,* Redistributions in binary form must reproduce the above\n      copyright notice, this list of conditions and the following\n      disclaimer in the documentation and/or other materials provided\n      with the distribution.\n\n    ,* Neither the name of Felix Drees nor the names of other\n      contributors may be used to endorse or promote products derived\n      from this software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\nLIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\nA PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\nOWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\nSPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\nLIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\nDATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\nTHEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\nOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n#+end_example\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffelixbd%2Fwhile","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffelixbd%2Fwhile","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffelixbd%2Fwhile/lists"}