Skip to content

documentation of parser function in Source 4 #365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions doc/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,12 @@ function equal(x, y) {
* @returns {number} length of <CODE>xs</CODE>
*/
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);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions doc/mce.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* returns the parse tree that results from parsing
* the string `str` as a Source program. The format
* the string <CODE>str</CODE> as a Source program. The format
* of the parse tree is described in chapter 4 of
* the textbook
* in <a href="https://sicp.comp.nus.edu.sg/">Structure and
Expand All @@ -11,14 +11,14 @@
function parse(str) {}

/**
* calls the function `f`
* with arguments given in list `xs`. For example: <PRE><CODE>function times(x, y) {
* calls the function <CODE>f</CODE>
* with arguments given in list <CODE>xs</CODE>. For example: <PRE><CODE>function times(x, y) {
* return x * y;
* }
* apply_in_underlying_javascript(times, list(2, 3)); // returns 6</CODE></PRE>
* @param {function} f - function to be applied
* @param {list} xs - arguments given in list
* @returns {boolean} whatever `f` returns
* @returns {boolean} whatever <CODE>f</CODE> returns
*/
function apply_in_underlying_javascript(f, xs) {}

3 changes: 3 additions & 0 deletions doc/source_header.tex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
\usepackage{amsmath,amssymb}

\usepackage[T1]{fontenc}

\usepackage[paper=portrait,pagesize]{typearea}

\usepackage{listings}

\lstdefinelanguage{JavaScript}{
Expand Down
137 changes: 135 additions & 2 deletions doc/source_interpreter.tex
Original file line number Diff line number Diff line change
@@ -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}
Expand All @@ -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 <statement>)} \\[1mm]
&& \textit{statement} &&\quad ::= &\quad && \textbf{\texttt{const}}\ \textit{name} \
\textbf{\texttt{=}}\ \textit{expression} \ \textbf{\texttt{;}}
&& \texttt{list("constant\_declaration", <name>, <expression>)} \\
&& && | &\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", <expression>, <expression>)} \\
&& && | &\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", <expression>)} \\
&& && | &\quad && \textit{if-statement} \quad
&& \textrm{see below}\\
&& && | &\quad && \textbf{\texttt{while}}\
\textbf{\texttt{(}}\ \textit{expression} \ \textbf{\texttt{)}} \
\textit{block}
&& \texttt{list("while\_loop", <expression>, <statement>)} \\
&& && | &\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", <statement>, <expression>, <statement>,}\\
&&&&&&&&&\ \ \ \ \ \ \texttt{ <statement>)} \\
&& && | &\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 <name>} \\
&& \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", <expression>, } \\
&&&&&&&&&\ \ \ \ \ \ \texttt{<statement>, <statement>)} \\
&& \textit{block} && ::= & && \textbf{\texttt{\{}}\ \textit{program} \ \textbf{\texttt{\}}} \quad
&& \texttt{list("block", <statement>)} \\
&& \textit{let} && ::= &\quad && \textbf{\texttt{let}}\ \textit{name} \
\textbf{\texttt{=}}\ \textit{expression}
&& \texttt{list("variable\_declaration", <name>, <expression>)} \\
&& \textit{assignment} && ::= &\quad && \textit{name} \
\textbf{\texttt{=}}\ \textit{expression}
&& \texttt{list("assignment", <name>, <expression>)} \\
\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", <name>, list of <expression>)} \\
&& && | &\quad && \textit{unary-operator} \
\textit{expression}
&& \texttt{list("application", <name>, list of <expression>)} \\
&& && | &\quad && \textit{expression} \
\textbf{\texttt{(}}\ \textit{expressions}\
\textbf{\texttt{)}}
&& \texttt{list("application", <expression>, list of <expression>)} \\
&& && | &\quad && (\ \textit{name}\ | \
\textbf{\texttt{(}}\ \textit{parameters}\ \textbf{\texttt{)}}\
)\
\texttt{\textbf{=>}}\ \textit{expression}
&& \texttt{list("function\_definition", <parameters>,} \\
&& && & && && \texttt{list("return\_statement", <expression>))} \\
&& && | &\quad && (\ \textit{name}\ | \
\textbf{\texttt{(}}\ \textit{parameters}\ \textbf{\texttt{)}}\
)\
\texttt{\textbf{=>}}\ \textit{block}
&& \texttt{list("function\_definition", <parameters>, <statement>)} \\
&& && | &\quad && \textit{expression} \ \textbf{\texttt{?}}\
\textit{expression}
\ \textbf{\texttt{:}}\
\textit{expression}\
&& \texttt{list("conditional\_expression", <expression>,} \\
&&&&&&&&&\ \ \ \ \ \ \texttt{<expression>, <expression>)} \\
&& && | &\quad && \textit{expression} \textbf{\texttt{[}}
\textit{expression} \textbf{\texttt{]}}
&& \texttt{list("array\_access", <expression>, <expression>)} \\
&& && | &\quad && \textbf{\texttt{[}}\
\textit{expressions}\
\textbf{\texttt{]}}
&& \texttt{list("array\_expression", list of <expression>)} \\
&& && | &\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 <expression>} \\
\end{alignat*}



\newpage
\KOMAoptions{paper=portrait,pagesize}
\recalctypearea