@@ -111,7 +111,7 @@ \subsubsection{Identifiers and Variables}\label{sec:grammar-id}
111111 <alphanum> ::= <upper> <alphanum>
112112 \alt <lower> <alphanum>
113113 \alt <natural> <alphanum>
114- \alt <upper> | <lower> | <natural>
114+ \alt <upper> | <lower> | <natural>
115115
116116 <lower> ::= "a" | "b" | ... | "z"
117117
@@ -131,9 +131,26 @@ \subsubsection{Arithmetical Expressions}\label{sec:grammar-aexp}
131131 <aterm> ::= <afactor> | <afactor> "*" <aterm>
132132
133133 <afactor> ::= "(" <aexp> ")"
134+ \alt `|' <array> `|'
135+ \alt <identifier> "[" <aexp> "]"
134136 \alt <integer> | <identifier>
135137\end {grammar }
136138
139+ \paragraph {Arrays }
140+
141+ An array is a sequence of data having the same type. It is defined by a type
142+ and a dimension, i.e. the number of elements contained in the array. The
143+ grammar to define and use and array is the following.
144+
145+ \begin {grammar }
146+ <array> ::= <basicarray> | <basicarray> "++" <array>
147+
148+ <basicarray> ::= "[" <asequence> "]"
149+ \alt <identifier>
150+
151+ <asequence> ::= <aexp> | <aexp> "," <asequence>
152+ \end {grammar }
153+
137154\subsubsection {Boolean Expressions }\label {sec:grammar-bexp }
138155
139156A grammar for the expressions in $ \mathrm {Bexp}$ is here presented. The
@@ -157,7 +174,7 @@ \subsubsection{Boolean Expressions}\label{sec:grammar-bexp}
157174 \alt "(" <bexp> ")"
158175 \alt <bcomparison>
159176
160- <bcomparison> ::= <aexp> "=" <aexp> | <aexp> "< =" <aexp>
177+ <bcomparison> ::= <aexp> "=" <aexp> | <aexp> "< =" <aexp> | <aexp> "< " <aexp>
161178\end {grammar}
162179
163180\subsubsection {Imperative Commands }\label {sec:grammar-com }
@@ -171,6 +188,8 @@ \subsubsection{Imperative Commands}\label{sec:grammar-com}
171188 <command> ::= <assignment> | <ifThenElse> | <while> | "skip"
172189
173190 <assignment> ::= <identifier> ":=" <aexp>
191+ \alt <identifier> ":=" <array>
192+ \alt <identifier> "[" <aexp> "]" ":=" <aexp>
174193
175194 <ifThenElse> ::= "if" <bexp> "then" <program> "end"
176195 \alt "if" <bexp> "then" <program> "else" <program> "end"
@@ -204,6 +223,13 @@ \section{Additions to the Syntax Given During Lectures}\label{sec:additions}
204223 languages like the POSIX Shell Command Language \cite {shell -syntax } or
205224 Python \cite {python -syntax }.The finite state automaton used to
206225 recognized the comments is shown in \autoref {fig:comment-automaton }.
226+ \item A new boolean comparison operator has been defined. This is the
227+ \texttt {< } operator (the same as the mathematical `` less than''
228+ operator). Given $ a$ and $ b$ two arithmetical expressions, the
229+ following holds:
230+ \[ a<b \iff \left (a\leq b\right ) \land \neg\left (a = b\right )\]
231+ This operator has been defined as a short hand to easily work with
232+ arrays.
207233\end {itemize }
208234
209235\begin {figure }[H]
@@ -220,6 +246,40 @@ \section{Additions to the Syntax Given During Lectures}\label{sec:additions}
220246 \label {fig:comment-automaton }
221247\end {figure }
222248
249+ \subsection {Arrays }
250+
251+ The IMP language has been extended with a new grammar to manage arrays. Arrays
252+ are defined as sequences of variables having the same type and a semantical
253+ connection between one another. In IMP, an array is indexed starting from zero
254+ (like in C++ \cite {Stroustrup2013 } and many other languages). An array is
255+ similar to the mathematical concept of `` vector'' , that will be used in the
256+ following paragraphs to define some operations on the arrays.
257+
258+ In IMP, the only supported primitive type is the integer, so an array in IMP of
259+ size $ n$ is an element of the set $ \mathbb {Z}^n$ , where $ \mathbb {Z}$ is the set
260+ of the positive and negative integers, including zero.
261+
262+ A few basic operations on arrays have been defined and are formalized in the
263+ following paragraphs.
264+
265+ \begin {itemize }
266+ \item An array $ A=\left [a_1 ,a_2 ,\ldots ,a_n\right ]\in \mathbb {Z}^n$ , as
267+ already stated, indexed starting from 0. In IMP (as in many other
268+ languages) the notation $ A[i]$ is used to get the ($ i+1 $ )-th element of
269+ the array, so $ \forall i=0 ,\ldots ,n-1 : A[i]=a_{i+1}$ .
270+ \item An array $ A\in \mathbb {Z}^n$ is said to be of `` size'' $ n$ . in IMP, to
271+ get the size of a vector, the operator
272+ $ |\cdot |:\mathbb {Z}^n\to\mathbb {N}$ is defined such that
273+ $ A\in \mathbb {Z}^n\mapsto n$ .
274+ \item A new operation is defined on the arrays: the concatenation
275+ `` \texttt {++ }'' . Given two arrays $ A=\left [ a_1 ,a_2 ,\ldots ,a_n \right ]
276+ \in \mathbb {Z}^n$ and $ B=\left [ b_1 ,b_2 ,\ldots ,b_m\right ]
277+ \in \mathbb {Z}^m$ the concatenation is the operation
278+ $ ++:\mathbb {Z}^n\to\mathbb {Z}^m\to\mathbb {Z}^{n+m}$ such that:
279+ \[ A++B=\left [ a_1, a_2, \ldots , a_n, b_1, b_2, \ldots , b_m \right ]\]
280+ \end {itemize }
281+
282+
223283\chapter {Design }
224284
225285The SIMPLI interpreter was defined by following a strategy that is similar to
@@ -377,7 +437,12 @@ \section{The Cli Module}
377437
378438\lstinputlisting [firstline=42, lastline=57]{Cli.hs}
379439
380- Finally, the actual exported function is defined. It gets the command line arguments using the standard function \lstinline |getArgs | and parses them using the standard function \lstinline |getOpt |. It eventually prints an error message and, if no errors occurred, it applies the default options. It then reads the source code from various sources (depending by the given arguments) in the following order:
440+ Finally, the actual exported function is defined. It gets the command line
441+ arguments using the standard function \lstinline |getArgs | and parses them using
442+ the standard function \lstinline |getOpt |. It eventually prints an error message
443+ and, if no errors occurred, it applies the default options. It then reads the
444+ source code from various sources (depending by the given arguments) in the
445+ following order:
381446\begin {enumerate }
382447 \item If a command is provided through `` \texttt {--command }'' , then the
383448 provided command is used as source code.
@@ -391,41 +456,50 @@ \section{The Cli Module}
391456\section {The Environment Module }
392457\lstinputlisting [firstline=1, lastline=1]{Environment.hs}
393458
394- The Environment module defines how the environment is managed. It exports all of its content, described in the following paragraphs.
459+ The Environment module defines how the environment is managed. It exports all
460+ of its content, described in the following paragraphs.
395461
396462The concept of `` variable'' is defined. A variable is seen as a record having a
397463name (the identifier of the variable), a type (in this version of the
398464interpreter, only integers are supported) and a value (the actual value
399465contained in the variable).
400466
401- \lstinputlisting [firstline=3 , lastline=6 ]{Environment.hs}
467+ \lstinputlisting [firstline=11 , lastline=15 ]{Environment.hs}
402468
403469Then, the \lstinline |Variable | type is defined as an instance of the class
404470\lstinline |Show |, to customize the way a variable is converted to String
405471(especially when printed as output).
406472
407- \lstinputlisting [firstline=8 , lastline=10 ]{Environment.hs}
473+ \lstinputlisting [firstline=17 , lastline=18 ]{Environment.hs}
408474
409475Then, the environment itself is defined as a sequence of variables.
410476
411- \lstinputlisting [firstline=12 , lastline=12 ]{Environment.hs}
477+ \lstinputlisting [firstline=20 , lastline=20 ]{Environment.hs}
412478
413- A function to modify an existing environment is then defined. Given an environment and a variable, it returns a new environment that is:
479+ A function to modify an existing environment is then defined. Given an
480+ environment and a variable, it returns a new environment that is:
414481\begin {itemize }
415482 \item The old environment plus the variable if the variable is not already
416483 in the environment.
417484 \item The old environment having the updated variable if the variable was
418485 already in the old environment (the previous value is then discarded
419486 after the update).
420487\end {itemize }
421- \lstinputlisting [firstline=14 , lastline=18 ]{Environment.hs}
488+ \lstinputlisting [firstline=22 , lastline=26 ]{Environment.hs}
422489
423- Finally , a function that searches a variable in an environment is provided.
490+ Furthermore , a function that searches a variable in an environment is provided.
424491Given an environment and a string, it searches in the environment for a
425492variable whose name is the given string and returns its value if the variable
426493is in the environment.
427494
428- \lstinputlisting [firstline=20, lastline=24]{Environment.hs}
495+ \lstinputlisting [firstline=28, lastline=32]{Environment.hs}
496+
497+ Finally, a specialized version of the two above functions to work with arrays
498+ is provided. The first one updates (or saves) an array, the second one searches
499+ all the values contained in an array in the environment.
500+
501+ \lstinputlisting [firstline=34, lastline=39]{Environment.hs}
502+ \lstinputlisting [firstline=41, lastline=48]{Environment.hs}
429503
430504\section {The Parser Module }
431505
@@ -464,7 +538,9 @@ \subsection{The Parser Main Module}
464538
465539\lstinputlisting [firstline=3, lastline=7]{Parser.hs}
466540
467- Then, a function to remove comments from the source code is provided. It removes, for each line, all characters after an hash symbol (see \autoref {sec:additions }).
541+ Then, a function to remove comments from the source code is provided. It
542+ removes, for each line, all characters after an hash symbol (see
543+ \autoref {sec:additions }).
468544
469545\lstinputlisting [firstline=8, lastline=15]{Parser.hs}
470546
@@ -490,7 +566,24 @@ \subsection{The Parser Core Module}
490566This module defines all the Core variable types and functions used by all other
491567modules. It starts by defining a Parser type, defining it as an instance of the
492568classes \lstinline |Functor |, \lstinline |Applicative |, \lstinline |Monad | and
493- \lstinline |Alternative |.
569+ \lstinline |Alternative |. A brief explaination on the reasons that led to this
570+ choices is given in the following paragraphs.
571+
572+ \begin {itemize }
573+ \item A Parser is a Functor such that the function \lstinline |fmap | can be
574+ applied to it. In other words, a Parser is a Functor because a function
575+ can be applied to the generic type that the Parser holds without
576+ loosing the Parser structure itself.
577+ \item A Parser is an Applicative because it is a Functor that can be
578+ applied sequentially and has no dependencies on the results of
579+ previously applied parsers (i.e. a parser can be applied sequentially
580+ discarding the previous parsers' output).
581+ \item A Parser is a Monad because it is an Applicative that can use the
582+ result of previously applied parsers (i.e. a parser can be applied
583+ sequentially using the output of the previous parsers).
584+ \item A Parser is an Alternative because it is an Applicative that can fail
585+ and may have an alternative parser to run when that happens.
586+ \end {itemize }
494587
495588\lstinputlisting [firstline=5, lastline=5]{Parser/Core.hs}
496589\lstinputlisting [firstline=10, lastline=14]{Parser/Core.hs}
@@ -540,25 +633,38 @@ \subsection{The Parser Environment Module}
540633`` \emph {do-blocks }'' . To do that, it uses the Environment module and the core
541634parsers' definitions.
542635
543- \lstinputlisting [firstline=2 , lastline=3 ]{Parser/Environment.hs}
636+ \lstinputlisting [firstline=9 , lastline=11 ]{Parser/Environment.hs}
544637
545638It then provides a simple function to update an environment given a variable.
546639This basically is the Parser-monad representation of the function
547640\lstinline |modifyEnv | provided in the Environment module.
548641
549- \lstinputlisting [firstline=5, lastline=6]{Parser/Environment.hs}
642+ \lstinputlisting [firstline=13, lastline=14]{Parser/Environment.hs}
643+
644+ It then provides a simple function to read a variable value from an
645+ environment. This basically is the Parser-monad representation of the function
646+ \lstinline |searchVariable | provided in the Environment module.
647+
648+ \lstinputlisting [firstline=16, lastline=19]{Parser/Environment.hs}
550649
551- Finally it provides a simple function to read a variable value from an
552- environment . This basically is the Parser-monad representation of the
553- function \lstinline |searchVariable | provided in the Environment module.
650+ Furthermore, it provides a simple function to update an environment given a
651+ variable . This basically is the Parser-monad representation of the function
652+ \lstinline |modifyArray | provided in the Environment module.
554653
555- \lstinputlisting [firstline=8, lastline=11]{Parser/Environment.hs}
654+ \lstinputlisting [firstline=21, lastline=22]{Parser/Environment.hs}
655+
656+ Finally, it provides a simple function to read an array value from an
657+ environment. This basically is the Parser-monad representation of the function
658+ \lstinline |searchArray | provided in the Environment module.
659+
660+ \lstinputlisting [firstline=24, lastline=27]{Parser/Environment.hs}
556661
557662\subsection {The Parser Aexp Module }
558663\lstinputlisting [firstline=1, lastline=1]{Parser/Aexp.hs}
559664
560665This modules provide a parser \lstinline |aexp | for the $ \mathrm {Aexp}$
561- expressions. It uses the other parsers modules, as follows.
666+ expressions and a parser \lstinline |array | to work with arrays. It uses the
667+ other parsers modules, as follows.
562668
563669\lstinputlisting [firstline=3, lastline=7]{Parser/Aexp.hs}
564670
@@ -595,7 +701,8 @@ \subsection{The Parser Com Module}
595701
596702It should be noted that, to deal with the `` while-do'' construct, the parser
597703simply repeats the while by prepending it to the input string until the
598- condition is false (function \lstinline |repeatWhile |).
704+ condition is false (function \lstinline |repeatWhile |). This is basically a
705+ simple implementation of the \emph {fixed point } operator.
599706
600707\subsection {The Parser Readers Module }
601708\lstinputlisting [firstline=1, lastline=1]{Parser/Readers.hs}
0 commit comments