Commit 0edb94a3 authored by Luke  Thorpe's avatar Luke Thorpe
Browse files

Improved answer to 'Gap 3'

parents 036f54ad ded52d24
No preview for this file type
......@@ -369,7 +369,9 @@ The type of the expression given to the return statement must match the return t
Once the return statement is executed, the function is immediately exited.
\paragraph{Exit Statements:}
\hl{... ??? ...}
\hl{An exit statement }\lit{exit}\hl{ is used terminate the execution of the running program with a particular exit code. The statement is given an expression that must evaluate to a value with type }\lit{int}\hl{ (with 0 and 1 typically denoting success and failure respectively).}
\hl{Upon encountering an exit statement, a program will terminate immediately after the given expression has been successfully evaluated; any code thereafter will be ignored. }
\fillgap{\hl{define exit statements}}{2 marks}
\paragraph{Print Statements:}
......@@ -394,18 +396,18 @@ The behaviour of the print statements for each type of expression is shown in Ta
\hline
\lit*{char} & Output a single-character string. & \lit*{\textquotesingle c\textquotesingle} & ``c'' \\
\hline
\lit*{string} or \lit*{char[]} & \hl{Output the character array converted into a single string.} & \lit*{\hl{['h','e','y','!']}} & \hl{``hey!''} \\
\lit*{string} or \lit*{char[]} & \hl{Output the character array converted into a single string.} & \lit*{\hl{[`h',`e',`y', `!']}} & \hl{``hey!''} \\
\hline
Other Array Types & \hl{Output the address of the array.} & \hl{[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]} & \hl{``\#addrs\#''} \\
Other Array Types & \hl{Output the address of the array.} & \lit*{\hl{[5, 6, 8]}} \hl{located at 0x22150} & \hl{``0x22150''} \\
\hline
\lit*{pair} & \hl{Output the address of the pair if it is not null. Else, (null) will be printed.} & \lit*{newpair(a, b)}\footnotemark[1] & \hl{``\#addrs\#''} \\
\lit*{pair} & \hl{Output the address of the pair if it is not null; otherwise, ``(null)'' will be printed.} & \lit*{\hl{newpair(a, b)}}\footnotemark[1] \hl{located at 0x23940} & \hl{``0x23940''} \\
\hline
\end{tabulary}
\caption{The behaviour of the print statements for each type of expression.}
\label{tab:print}
\end{table}
\footnotetext[1]{This is not exactly an expression because it can only appear on the right hand side of an assignment. However, it gives the best example here.}
\footnotetext[1]{This is not an expression, strictly speaking, since it can only appear on the right hand side of an assignment. As such, \lit{newpair} statements cannot be printed directly, although the principle is correct in this example. }
\paragraph{Conditional Branch Statements:}
A conditional branch statement \lit{if} evaluates an expression and determines which program path to follow.
......@@ -464,7 +466,7 @@ A character literal \synt{char-liter} is a single ASCII character between two \l
A \lit{\char`\\} can be used to escape the character that immediately follows the \lit{\char`\\}.
The meaning of each escaped character is shown in Table~\ref{tab:escapedcharacters}.
\fillgap{\hl{Fill in Table}~\ref{tab:escapedcharacters}}{2 marks}
%
%TODO:highlight \b \t \n \f
\begin{table}
\centering
\begin{tabular}{cclc}
......@@ -472,10 +474,10 @@ The meaning of each escaped character is shown in Table~\ref{tab:escapedcharacte
Representation & ASCII Value & Description & Symbol \\
\hline
\lit*{\char`\\ 0} & \lit*{0x00} & null terminator & NUL \\
\\lit*{hl{\char`\\ b}} & \lit*{0x08} & \hl{backspace} & \hl{BS} \\
\\lit*{hl{\char`\\ t}} & \lit*{0x09} & \hl{horizontal tab} & \hl{HT}\ \\
\\lit*{hl{\char`\\ n}} & \lit*{0x0a} & \hl{line feed} & \hl{LF} \\
\\lit*{hl{\char`\\ f}} & \lit*{0x0c} & \hl{form feed} & \hl{FF} \\
\lit*{\char`\\ b} & \lit*{0x08} & \hl{backspace} & \hl{BS} \\
\lit*{\char`\\ t} & \lit*{0x09} & \hl{horizontal tab} & \hl{HT}\ \\
\lit*{\char`\\ n} & \lit*{0x0a} & \hl{line feed} & \hl{LF} \\
\lit*{\char`\\ f} & \lit*{0x0c} & \hl{form feed} & \hl{FF} \\
\lit*{\char`\\ r} & \hl{0x0d} & carriage return & CR \\
\lit*{\char`\\ "} & \hl{0x22} & double quote & " \\
\lit*{\char`\\ '} & \hl{0x27} & single quote & ' \\
......@@ -532,8 +534,8 @@ All unary operators have the same precedence, they are evaluated from right to l
\lit{!} & \lit*{bool} & \lit*{bool} & Logical Not \\
\lit{-} & \lit*{int} & \lit*{int} & Negation \\
\lit{len} & T\lit*{[]} & \lit*{int} & Array Length \\
\lit{ord} & \hl{???} & \hl{???} & \hl{???} \\
\lit{chr} & \hl{???} & \hl{???} & \hl{???} \\
\lit{ord} & \hl{char} & \hl{int} & \hl{ASCII value} \\
\lit{chr} & \hl{int} & \hl{char} & \hl{Character with corresponding ASCII value} \\
\hline
\end{tabulary}
\caption{The unary operators of the WACC language with their types and meanings.}
......@@ -548,9 +550,11 @@ returning \lit{true} if the sub-expression evaluates to \lit{false} and vice-ver
\item The \lit{len} operator returns the length of the array referenced by the evaluation of its sub-expression.
\item The \lit{ord} operator \hl{... ??? ...} \fillgap{\hl{Define/describe the }\lit{\hl{ord}} \hl{operator}}{1 mark}
\item The \lit{ord} operator \hl{returns the ASCII value of the} \lit*{\hl{char}} \hl{referenced by the evaluation
of its sub-expression.} \fillgap{\hl{Define/describe the }\lit{\hl{ord}} \hl{operator}}{1 mark}
\item The \lit{chr} operator \hl{... ??? ...} \fillgap{\hl{Define/describe the }\lit{\hl{chr}} \hl{operator}}{1 mark}
\item The \lit{chr} operator {\hl{returns the} \lit*{\hl{char}} \hl{with ASCII value that of the integer referenced
by the evaluation of its sub-expression.}} \fillgap{\hl{Define/describe the }\lit{\hl{chr}} \hl{operator}}{1 mark}
\end{itemize}
......@@ -572,12 +576,18 @@ with 1 being the highest and 6 being the lowest.
\lit{\%} & 1 & \lit*{int} & \lit*{int} & \lit*{int} & Modulus \\
\lit{+} & 2 & \lit*{int} & \lit*{int} & \lit*{int} & Plus \\
\lit{-} & 2 & \lit*{int} & \lit*{int} & \lit*{int} & Minus \\
\lit{>} & 3 & \hl{???} & \hl{???} & \hl{???} & Greater Than \\
\lit{>=} & 3 & \hl{???} & \hl{???} & \hl{???} & Greater Than or Equal \\
\lit{<} & 3 & \hl{???} & \hl{???} & \hl{???} & Less Than \\
\lit{<=} & 3 & \hl{???} & \hl{???} & \hl{???} & Less Than or Equal \\
\lit{==} & 4 & \hl{???} & \hl{???} & \hl{???} & Equality \\
\lit{!=} & 4 & \hl{???} & \hl{???} & \hl{???} & Inequality \\
\lit{>} & 3 & \lit*{\hl{char}} / \lit*{\hl{int}} & \lit*{\hl{char}} / \lit*{\hl{int}}
& \lit*{\hl{bool}} & Greater Than \\
\lit{>=} & 3 & \lit*{\hl{char}} / \lit*{\hl{int}} & \lit*{\hl{char}} / \lit*{\hl{int}}
& \lit*{\hl{bool}} & Greater Than or Equal \\
\lit{<} & 3 & \lit*{\hl{char}} / \lit*{\hl{int}} & \lit*{\hl{char}} / \lit*{\hl{int}}
& \lit*{\hl{bool}} & Less Than \\
\lit{<=} & 3 & \lit*{\hl{char}} / \lit*{\hl{int}} & \lit*{\hl{char}} / \lit*{\hl{int}}
& \lit*{\hl{bool}} & Less Than or Equal \\
\lit{==} & 4 & \lit*{\hl{char}} / \lit*{\hl{int}} / \lit*{\hl{bool}} / \lit*{\hl{pair}}
& \lit*{\hl{char}} / \lit*{\hl{int}} / \lit*{\hl{bool}} / \lit*{\hl{pair}} & \lit*{\hl{bool}} & Equality \\
\lit{!=} & 4 & \lit*{\hl{char}} / \lit*{\hl{int}} / \lit*{\hl{bool}} / \lit*{\hl{pair}}
& \lit*{\hl{char}} / \lit*{\hl{int}} / \lit*{\hl{bool}} / \lit*{\hl{pair}} & \lit*{\hl{bool}} & Inequality \\
\lit{\&\&} & 5 & \lit*{bool} & \lit*{bool} & \lit*{bool} & Logical And \\
\lit{||} & 6 & \lit*{bool} & \lit*{bool} & \lit*{bool} & Logical Or \\
\hline
......@@ -627,7 +637,11 @@ Any other type of occurrence of whitespace is ignored by the compiler.
Note, in particular, that the code indentation in the example programs has no meaning, it simply aids readability.
Also note that whitespace inside a string or character literal is preserved by the compiler.
\hl{... ??? ...}
\hl{Comments are denote by the} \lit*{\lit{\hl{\#}}} \hl{character which must be on the same line.
A comment must be preceded by the hashtag. For example,} \lit*{\lit{\hl{\#this is a comment}}} \hl{is a valid comment, but}
\\*\lit*{\lit{\hl{this is treated as program statement \#comment}}} \hl{will throw a syntax error, with only `comment'
evaluated as a comment. Comments can be written above or below program statements, in-line, or after the} \lit*{\hl{;}}
\hl{character as well.}
\fillgap{\hl{Define/describe comments}}{3 marks}
......
# character array treated as a string
# Output:
# foo
# bar
# Program:
begin
int[] arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] ;
println arr
end
# character array treated as a string
# Output:
# foo
# bar
# Program:
begin
pair(int, int) tuple = newpair (0, 1) ;
println tuple
end
# exit instead of returning from a function
# Output:
# #empty#
# Exit:
# 42
# Program:
begin
char f() is
exit 42
end
char c = call f() ;
println c ;
exit 0
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment