Skip to content

SnakPe/Whi-L

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Whi-L

What is this?

Whi-L is an interpreter for the while language, a language primarily used in theoretical computer science. It also has a REPL for quick experimentation.

How to Use?

  1. Compile Whi-l.hs using ghc:
ghc Whi-l.hs
  1. For the REPL, just use the resulting executable:
Whi-l
  1. To execute a file, add the file path at the end, e.g.
Whi-l "examples/multiplyXandY.wh"
  1. Using the --help option gives you a brief oveview as well
Whi-l --help

Formal Grammar

The while language has the following grammar
$\left<\mathrm{INDENT}\right>$ A means that every new statement in A needs to have an added tab character
\n stands for a new line

Program $\to$ Statement (; Statement)*
IndentedProgram $\to$ $\left<\mathrm{INDENT}\right>$ Program

Statement $\to$ While | If | Ass | Skip
While $\to$ while Boolean do \n IndentedProgram
If $\to$ if Boolean then \n IndentedProgram \n else \n IndentedProgram
Ass $\to$ Variable := Expression
Skip $\to$ skip

Expression $\to$ Boolean | Arithmetic | Variable

Boolean $\to$ true | false | Variable | Not | And | Or | Bigger | Smaller | Equals | ( Boolean )
Not $\to$ ! Boolean
And $\to$ Boolean & Boolean
Or $\to$ Boolean | Boolean
Bigger $\to$ Arithmetic > Arithmetic
Smaller $\to$ Arithmetic < Arithmetic
Equals $\to$ Arithmetic = Arithmetic

Arithmetic $\to$ Number | Variable | Plus | Minus | (Arithmetic)
Plus $\to$ Arithmetic + Arithmetic
Minus $\to$ Arithmetic - Arithmetic
Number $\to$ [0-9]+

Variables $\to$ ([a-z] | [A-Z])([a-z] | [A-Z] | [0-9])*

Inline comments can be made by using "--", for example

x := 69 -- HAHA 69 so funny lol

Bugs/Weird Behaviour

  • If a line only contains a comment with some space before the --, that space is not allowed to contain tabs, because that would indicate the start of a subprogram.
  • A semicolon is only placed if there is following statement in the same subprogramm. Noticably, the last statement cannot end in a semicolon, as well as the last statement in the first block B1 of an if ... then B1 else B2.
  • The minus operation is right associative, so e.g. 1-1-1 = 1-(1-1) = 1-0 = 1, and not -1 as one might expect. This can be seen as a feature or as a bug ig, but adds to the cursedness of this language implementation.