Parser and interpreter for the while programming language
The while programming language is a verry simplified language, which is turing complete!
The set of WHILE programs is the language generated by the following grammar
where
(the set of non terminal Symbols)
(the set of terminal symbols)
(the start non terminal)
(the set of production rules)
as expected, i guess … lol
NOTE: the subtraction is only defined in
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)
lets say you have a function
lets calculate
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
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)] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
stack build
stack run
stack test
stack exec hlint src/*.hs app/*.hs test/*.hs
curl -SL https://get.haskellstack.org/ | sh
apt install ghc
apt install hlint
nix-shell
hlint src/*.hs app/*.hs test/*.hs
same as before
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.