diff --git a/doc/list.js b/doc/list.js index b60ebdc29..2dc58e58f 100644 --- a/doc/list.js +++ b/doc/list.js @@ -100,9 +100,12 @@ function equal(x, y) { * @returns {number} length of xs */ function length(xs) { - return is_null(xs) - ? 0 - : 1 + length(tail(xs)); + function iter(ys, acc) { + return is_null(ys) + ? acc + : iter(tail(ys), acc + 1); + } + return iter(xs, 0); } /** diff --git a/doc/mce.js b/doc/mce.js index f9d493f58..f384a4709 100644 --- a/doc/mce.js +++ b/doc/mce.js @@ -1,6 +1,6 @@ /** * returns the parse tree that results from parsing - * the string `str` as a Source program. The format + * the string str as a Source program. The format * of the parse tree is described in chapter 4 of * the textbook * in Structure and @@ -11,14 +11,14 @@ function parse(str) {} /** - * calls the function `f` - * with arguments given in list `xs`. For example:
function times(x, y) {
+ * calls the function f
+ * with arguments given in list xs. For example: 
function times(x, y) {
  * return x * y;
  * }
  * apply_in_underlying_javascript(times, list(2, 3)); // returns 6
* @param {function} f - function to be applied * @param {list} xs - arguments given in list - * @returns {boolean} whatever `f` returns + * @returns {boolean} whatever f returns */ function apply_in_underlying_javascript(f, xs) {} diff --git a/doc/source_header.tex b/doc/source_header.tex index b5cf83764..5cc18c1c8 100644 --- a/doc/source_header.tex +++ b/doc/source_header.tex @@ -7,6 +7,9 @@ \usepackage{amsmath,amssymb} \usepackage[T1]{fontenc} + +\usepackage[paper=portrait,pagesize]{typearea} + \usepackage{listings} \lstdefinelanguage{JavaScript}{ diff --git a/doc/source_interpreter.tex b/doc/source_interpreter.tex index a8c3760e5..9cbca1f03 100644 --- a/doc/source_interpreter.tex +++ b/doc/source_interpreter.tex @@ -1,8 +1,6 @@ \section*{Interpreter Support} \begin{itemize} -\item \lstinline{parse(x)}: \textit{primitive}, returns the parse tree that results from parsing -the string \lstinline{x} as a Source program. \item \lstinline{apply_in_underlying_javascript(f, xs)}: \textit{primitive}, calls the function \lstinline{f} with arguments \lstinline{xs}. For example: \begin{lstlisting} @@ -11,4 +9,139 @@ \section*{Interpreter Support} } apply_in_underlying_javascript(times, list(2, 3)); // returns 6 \end{lstlisting} +\item \lstinline{parse(x)}: \textit{primitive}, returns the parse tree that results from parsing +the string \lstinline{x} as a Source program. The following two pages describe the shape of the parse tree. \end{itemize} + +\newpage +\KOMAoptions{paper=landscape,pagesize} +\recalctypearea + +\begin{alignat*}{9} +&& \textit{program} &&\quad ::= &\quad && \textit{statement} \ \ldots + && \texttt{list("sequence", list of )} \\[1mm] +&& \textit{statement} &&\quad ::= &\quad && \textbf{\texttt{const}}\ \textit{name} \ + \textbf{\texttt{=}}\ \textit{expression} \ \textbf{\texttt{;}} + && \texttt{list("constant\_declaration", , )} \\ +&& && | &\quad && \textit{let} \ \textbf{\texttt{;}} + && \textrm{see below}\\ +&& && | &\quad && \textit{assignment} \ \textbf{\texttt{;}} + && \textrm{see below}\\ +&& && | &\quad && \textit{expression} \textbf{\texttt{[}} + \textit{expression} \textbf{\texttt{]}} \ + \textbf{\texttt{=}}\ \textit{expression} \ \textbf{\texttt{;}} + && \texttt{list("array\_assignment", , )} \\ +&& && | &\quad && \textbf{\texttt{function}}\ \textit{name} \ + \textbf{\texttt{(}}\ \textit{parameters} \ \textbf{\texttt{)}}\ \textit{block} \quad + && \textrm{treat as}:\ \textbf{\texttt{const}}\ \textit{name} \ + \textbf{\texttt{=}}\ \textit{parameters} \ \textbf{\texttt{=>}}\ \textit{block} \textbf{\texttt{;}} \\ +&& && | &\quad && \textbf{\texttt{return}}\ \textit{expression} \ \textbf{\texttt{;}} + && \texttt{list("return\_statement", )} \\ +&& && | &\quad && \textit{if-statement} \quad + && \textrm{see below}\\ +&& && | &\quad && \textbf{\texttt{while}}\ + \textbf{\texttt{(}}\ \textit{expression} \ \textbf{\texttt{)}} \ + \textit{block} + && \texttt{list("while\_loop", , )} \\ +&& && | &\quad && \textbf{\texttt{for}}\ \textbf{\texttt{(}} \ + (\ \textit{\hyperlink{for}{assignment}} \ | \ \textit{\hyperlink{for2}{let}}\ ) \textbf{\texttt{;}} \\ +&& && &\quad && \ \ \ \ \ \ \ \ \ \ \textit{expression} \ \textbf{\texttt{;}} \\ +&& && &\quad && \ \ \ \ \ \ \ \ \ \ \textit{assignment} \ \textbf{\texttt{)}} \ + \textit{block} + && \texttt{list("for\_loop", , , ,}\\ + &&&&&&&&&\ \ \ \ \ \ \texttt{ )} \\ +&& && | &\quad && \textbf{\texttt{break}}\ \textbf{\texttt{;}} + && \texttt{list("break\_statement")} \\ +&& && | &\quad && \textbf{\texttt{continue}}\ \textbf{\texttt{;}} + && \texttt{list("continue\_statement")} \\ +&& && | &\quad && \textit{block} + && \textrm{see below}\\ +&& && | &\quad && \textit{expression} \ \textbf{\texttt{;}} + && \textrm{see below}\\[1mm] +&& \textit{parameters} && ::= &\quad && \epsilon\ | \ \textit{name} \ + (\ \textbf{\texttt{,}} \ \textit{name}\ )\ \ldots + && \texttt{list of } \\ +&& \textit{if-statement} && ::= &\quad && \textbf{\texttt{if}}\ + \textbf{\texttt{(}}\ \textit{expression} \ \textbf{\texttt{)}}\ + \textit{block} \\ +&& && & && \textbf{\texttt{else}}\ + (\ \textit{block} + \ | \ + \textit{\href{https://sicp.comp.nus.edu.sg/chapters/21\#footnote-1}{if-statement}} \ ) + && \texttt{list("conditional\_statement", , } \\ + &&&&&&&&&\ \ \ \ \ \ \texttt{, )} \\ +&& \textit{block} && ::= & && \textbf{\texttt{\{}}\ \textit{program} \ \textbf{\texttt{\}}} \quad + && \texttt{list("block", )} \\ +&& \textit{let} && ::= &\quad && \textbf{\texttt{let}}\ \textit{name} \ + \textbf{\texttt{=}}\ \textit{expression} + && \texttt{list("variable\_declaration", , )} \\ +&& \textit{assignment} && ::= &\quad && \textit{name} \ + \textbf{\texttt{=}}\ \textit{expression} + && \texttt{list("assignment", , )} \\ +\end{alignat*} + +\begin{alignat*}{9} + && \textit{expression} && ::= &\quad && \textit{number} && \textrm{self-evaluating} \\ +&& && | &\quad && \textbf{\texttt{true}}\ |\ \textbf{\texttt{false}} + && \textrm{self-evaluating} \\ +&& && | &\quad && \textbf{\texttt{null}} + && \textrm{self-evaluating} \\ +&& && | &\quad && \textit{string} && \textrm{self-evaluating} \\ +&& && | &\quad && \textit{name} && \texttt{list("name", string)} \ \textrm{or}\ \texttt{list("name", string, location)} \\ +&& && | &\quad && \textit{expression} \ \textit{binary-operator} \ + \textit{expression} \qquad + && \texttt{list("application", , list of )} \\ +&& && | &\quad && \textit{unary-operator} \ + \textit{expression} + && \texttt{list("application", , list of )} \\ +&& && | &\quad && \textit{expression} \ + \textbf{\texttt{(}}\ \textit{expressions}\ + \textbf{\texttt{)}} + && \texttt{list("application", , list of )} \\ +&& && | &\quad && (\ \textit{name}\ | \ + \textbf{\texttt{(}}\ \textit{parameters}\ \textbf{\texttt{)}}\ + )\ + \texttt{\textbf{=>}}\ \textit{expression} + && \texttt{list("function\_definition", ,} \\ + && && & && && \texttt{list("return\_statement", ))} \\ +&& && | &\quad && (\ \textit{name}\ | \ + \textbf{\texttt{(}}\ \textit{parameters}\ \textbf{\texttt{)}}\ + )\ + \texttt{\textbf{=>}}\ \textit{block} + && \texttt{list("function\_definition", , )} \\ +&& && | &\quad && \textit{expression} \ \textbf{\texttt{?}}\ + \textit{expression} + \ \textbf{\texttt{:}}\ + \textit{expression}\ + && \texttt{list("conditional\_expression", ,} \\ + &&&&&&&&&\ \ \ \ \ \ \texttt{, )} \\ +&& && | &\quad && \textit{expression} \textbf{\texttt{[}} + \textit{expression} \textbf{\texttt{]}} + && \texttt{list("array\_access", , )} \\ +&& && | &\quad && \textbf{\texttt{[}}\ + \textit{expressions}\ + \textbf{\texttt{]}} + && \texttt{list("array\_expression", list of )} \\ +&& && | &\quad && \textbf{\texttt{(}}\ \textit{expression} \ + \textbf{\texttt{)}} && \textrm{treat as:}\ \textit{expression} \\[1mm] +&& \textit{binary-operator} \ + && ::= &\quad && \textbf{\texttt{+}}\ |\ \textbf{\texttt{-}}\ |\ \textbf{\texttt{*}}\ |\ \textbf{\texttt{/}}\ |\ \textbf{\texttt{\%}}\ |\ + \textbf{\texttt{===}}\ |\ \textbf{\texttt{!==}}\ \\ +&& && | &\quad && \texttt{\textbf{>}}\ |\ \texttt{\textbf{<}}\ |\ \texttt{\textbf{>=}}\ |\ \texttt{\textbf{<=}}\ + |\ \textbf{\texttt{\&\&}}\ |\ \texttt{\textbf{||}} + && \texttt{list("name", string)} \\[1mm] +&& \textit{unary-operator} + && ::= &\quad && \textbf{\texttt{!}}\ |\ \textbf{\texttt{-}} + && \texttt{list("name", string)} \\ +&& \textit{expressions} && ::= &\quad && \epsilon\ | \ \textit{expression}\ ( + \ \textbf{\texttt{,}} \ + \textit{expression} \ + )\ \ldots + && \texttt{list of } \\ +\end{alignat*} + + + +\newpage +\KOMAoptions{paper=portrait,pagesize} +\recalctypearea