LOOP parser #
This file defines a simple parser for the LOOP language, written using the standard Parsec
library.
The accepted syntax for a loop program is defined as follows:
$$ \begin{array}{lclcl} \texttt{Ident} & \ni & x, y & & \\ \texttt{Com} & \ni & P, Q & ::= & \texttt{SKIP} \mid x \; \texttt{=} \; \texttt{0} \mid x \; \texttt{=} \; y \mid x \; \texttt{=} \; x \; \texttt{+} \; \texttt{1} \\ & & & & \mid P \texttt{;} Q \mid \texttt{LOOP} \; x \; \texttt{DO} \; P \; \texttt{END} \end{array} $$
Some example programs can be found in the Examples folder.
References #
This work is strongly based on the following resources:
Parses a variable.
Equations
- var = do let fc ← var.firstChar let rest ← var.nonFirstChar.many pure { data := fc :: rest.toList }
Instances For
Equations
Instances For
Equations
- var.nonFirstChar = Lean.Parsec.satisfy fun (c : Char) => c.isAlpha || c.isDigit || c == '_'
Instances For
Parses a syntactic construct followed by a sequence of blank or tab characters.
Equations
Instances For
Parses a newline character.
Equations
- newline = do HOrElse.hOrElse (Lean.Parsec.skipChar '\n') fun (x : Unit) => Lean.Parsec.skipString "\x0d\n" Lean.Parsec.ws
Instances For
Parses a program file.
Equations
- parse = do Lean.Parsec.ws let prog ← pCom Lean.Parsec.ws Lean.Parsec.eof pure prog
Instances For
Parses a SKIP command with 'SKIP' syntax.
Equations
- pSKIP = do lexeme (Lean.Parsec.skipString "SKIP") pure LOOP.Com.SKIP
Instances For
Parses a zeroing command with 'x = 0' syntax.
Equations
- pZER = do let i ← lexeme var lexeme (Lean.Parsec.skipChar '=') lexeme (Lean.Parsec.skipChar '0') pure (LOOP.Com.ZER i)
Instances For
Parses an assignment command with 'x = y' syntax.
Equations
- pASN = do let i1 ← lexeme var lexeme (Lean.Parsec.skipChar '=') let i2 ← lexeme var pure (LOOP.Com.ASN i1 i2)
Instances For
Parses an increment command with 'x += 1' syntax.
Equations
- pINC = do let i ← lexeme var lexeme (Lean.Parsec.skipString "+=") lexeme (Lean.Parsec.skipChar '1') pure (LOOP.Com.INC i)