Skip to content

Commit 9e505b4

Browse files
authored
documentation of parser function in Source 4 (#365)
* parser documentation * Thomas's comments plus: making implementation of length iterative, consistent with specs * minor jsdocs syntax issue
1 parent 0561be1 commit 9e505b4

File tree

4 files changed

+148
-9
lines changed

4 files changed

+148
-9
lines changed

doc/list.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,12 @@ function equal(x, y) {
100100
* @returns {number} length of <CODE>xs</CODE>
101101
*/
102102
function length(xs) {
103-
return is_null(xs)
104-
? 0
105-
: 1 + length(tail(xs));
103+
function iter(ys, acc) {
104+
return is_null(ys)
105+
? acc
106+
: iter(tail(ys), acc + 1);
107+
}
108+
return iter(xs, 0);
106109
}
107110

108111
/**

doc/mce.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* returns the parse tree that results from parsing
3-
* the string `str` as a Source program. The format
3+
* the string <CODE>str</CODE> as a Source program. The format
44
* of the parse tree is described in chapter 4 of
55
* the textbook
66
* in <a href="https://sicp.comp.nus.edu.sg/">Structure and
@@ -11,14 +11,14 @@
1111
function parse(str) {}
1212

1313
/**
14-
* calls the function `f`
15-
* with arguments given in list `xs`. For example: <PRE><CODE>function times(x, y) {
14+
* calls the function <CODE>f</CODE>
15+
* with arguments given in list <CODE>xs</CODE>. For example: <PRE><CODE>function times(x, y) {
1616
* return x * y;
1717
* }
1818
* apply_in_underlying_javascript(times, list(2, 3)); // returns 6</CODE></PRE>
1919
* @param {function} f - function to be applied
2020
* @param {list} xs - arguments given in list
21-
* @returns {boolean} whatever `f` returns
21+
* @returns {boolean} whatever <CODE>f</CODE> returns
2222
*/
2323
function apply_in_underlying_javascript(f, xs) {}
2424

doc/source_header.tex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
\usepackage{amsmath,amssymb}
88

99
\usepackage[T1]{fontenc}
10+
11+
\usepackage[paper=portrait,pagesize]{typearea}
12+
1013
\usepackage{listings}
1114

1215
\lstdefinelanguage{JavaScript}{

doc/source_interpreter.tex

Lines changed: 135 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
\section*{Interpreter Support}
22

33
\begin{itemize}
4-
\item \lstinline{parse(x)}: \textit{primitive}, returns the parse tree that results from parsing
5-
the string \lstinline{x} as a Source program.
64
\item \lstinline{apply_in_underlying_javascript(f, xs)}: \textit{primitive}, calls the function \lstinline{f}
75
with arguments \lstinline{xs}. For example:
86
\begin{lstlisting}
@@ -11,4 +9,139 @@ \section*{Interpreter Support}
119
}
1210
apply_in_underlying_javascript(times, list(2, 3)); // returns 6
1311
\end{lstlisting}
12+
\item \lstinline{parse(x)}: \textit{primitive}, returns the parse tree that results from parsing
13+
the string \lstinline{x} as a Source program. The following two pages describe the shape of the parse tree.
1414
\end{itemize}
15+
16+
\newpage
17+
\KOMAoptions{paper=landscape,pagesize}
18+
\recalctypearea
19+
20+
\begin{alignat*}{9}
21+
&& \textit{program} &&\quad ::= &\quad && \textit{statement} \ \ldots
22+
&& \texttt{list("sequence", list of <statement>)} \\[1mm]
23+
&& \textit{statement} &&\quad ::= &\quad && \textbf{\texttt{const}}\ \textit{name} \
24+
\textbf{\texttt{=}}\ \textit{expression} \ \textbf{\texttt{;}}
25+
&& \texttt{list("constant\_declaration", <name>, <expression>)} \\
26+
&& && | &\quad && \textit{let} \ \textbf{\texttt{;}}
27+
&& \textrm{see below}\\
28+
&& && | &\quad && \textit{assignment} \ \textbf{\texttt{;}}
29+
&& \textrm{see below}\\
30+
&& && | &\quad && \textit{expression} \textbf{\texttt{[}}
31+
\textit{expression} \textbf{\texttt{]}} \
32+
\textbf{\texttt{=}}\ \textit{expression} \ \textbf{\texttt{;}}
33+
&& \texttt{list("array\_assignment", <expression>, <expression>)} \\
34+
&& && | &\quad && \textbf{\texttt{function}}\ \textit{name} \
35+
\textbf{\texttt{(}}\ \textit{parameters} \ \textbf{\texttt{)}}\ \textit{block} \quad
36+
&& \textrm{treat as}:\ \textbf{\texttt{const}}\ \textit{name} \
37+
\textbf{\texttt{=}}\ \textit{parameters} \ \textbf{\texttt{=>}}\ \textit{block} \textbf{\texttt{;}} \\
38+
&& && | &\quad && \textbf{\texttt{return}}\ \textit{expression} \ \textbf{\texttt{;}}
39+
&& \texttt{list("return\_statement", <expression>)} \\
40+
&& && | &\quad && \textit{if-statement} \quad
41+
&& \textrm{see below}\\
42+
&& && | &\quad && \textbf{\texttt{while}}\
43+
\textbf{\texttt{(}}\ \textit{expression} \ \textbf{\texttt{)}} \
44+
\textit{block}
45+
&& \texttt{list("while\_loop", <expression>, <statement>)} \\
46+
&& && | &\quad && \textbf{\texttt{for}}\ \textbf{\texttt{(}} \
47+
(\ \textit{\hyperlink{for}{assignment}} \ | \ \textit{\hyperlink{for2}{let}}\ ) \textbf{\texttt{;}} \\
48+
&& && &\quad && \ \ \ \ \ \ \ \ \ \ \textit{expression} \ \textbf{\texttt{;}} \\
49+
&& && &\quad && \ \ \ \ \ \ \ \ \ \ \textit{assignment} \ \textbf{\texttt{)}} \
50+
\textit{block}
51+
&& \texttt{list("for\_loop", <statement>, <expression>, <statement>,}\\
52+
&&&&&&&&&\ \ \ \ \ \ \texttt{ <statement>)} \\
53+
&& && | &\quad && \textbf{\texttt{break}}\ \textbf{\texttt{;}}
54+
&& \texttt{list("break\_statement")} \\
55+
&& && | &\quad && \textbf{\texttt{continue}}\ \textbf{\texttt{;}}
56+
&& \texttt{list("continue\_statement")} \\
57+
&& && | &\quad && \textit{block}
58+
&& \textrm{see below}\\
59+
&& && | &\quad && \textit{expression} \ \textbf{\texttt{;}}
60+
&& \textrm{see below}\\[1mm]
61+
&& \textit{parameters} && ::= &\quad && \epsilon\ | \ \textit{name} \
62+
(\ \textbf{\texttt{,}} \ \textit{name}\ )\ \ldots
63+
&& \texttt{list of <name>} \\
64+
&& \textit{if-statement} && ::= &\quad && \textbf{\texttt{if}}\
65+
\textbf{\texttt{(}}\ \textit{expression} \ \textbf{\texttt{)}}\
66+
\textit{block} \\
67+
&& && & && \textbf{\texttt{else}}\
68+
(\ \textit{block}
69+
\ | \
70+
\textit{\href{https://sicp.comp.nus.edu.sg/chapters/21\#footnote-1}{if-statement}} \ )
71+
&& \texttt{list("conditional\_statement", <expression>, } \\
72+
&&&&&&&&&\ \ \ \ \ \ \texttt{<statement>, <statement>)} \\
73+
&& \textit{block} && ::= & && \textbf{\texttt{\{}}\ \textit{program} \ \textbf{\texttt{\}}} \quad
74+
&& \texttt{list("block", <statement>)} \\
75+
&& \textit{let} && ::= &\quad && \textbf{\texttt{let}}\ \textit{name} \
76+
\textbf{\texttt{=}}\ \textit{expression}
77+
&& \texttt{list("variable\_declaration", <name>, <expression>)} \\
78+
&& \textit{assignment} && ::= &\quad && \textit{name} \
79+
\textbf{\texttt{=}}\ \textit{expression}
80+
&& \texttt{list("assignment", <name>, <expression>)} \\
81+
\end{alignat*}
82+
83+
\begin{alignat*}{9}
84+
&& \textit{expression} && ::= &\quad && \textit{number} && \textrm{self-evaluating} \\
85+
&& && | &\quad && \textbf{\texttt{true}}\ |\ \textbf{\texttt{false}}
86+
&& \textrm{self-evaluating} \\
87+
&& && | &\quad && \textbf{\texttt{null}}
88+
&& \textrm{self-evaluating} \\
89+
&& && | &\quad && \textit{string} && \textrm{self-evaluating} \\
90+
&& && | &\quad && \textit{name} && \texttt{list("name", string)} \ \textrm{or}\ \texttt{list("name", string, location)} \\
91+
&& && | &\quad && \textit{expression} \ \textit{binary-operator} \
92+
\textit{expression} \qquad
93+
&& \texttt{list("application", <name>, list of <expression>)} \\
94+
&& && | &\quad && \textit{unary-operator} \
95+
\textit{expression}
96+
&& \texttt{list("application", <name>, list of <expression>)} \\
97+
&& && | &\quad && \textit{expression} \
98+
\textbf{\texttt{(}}\ \textit{expressions}\
99+
\textbf{\texttt{)}}
100+
&& \texttt{list("application", <expression>, list of <expression>)} \\
101+
&& && | &\quad && (\ \textit{name}\ | \
102+
\textbf{\texttt{(}}\ \textit{parameters}\ \textbf{\texttt{)}}\
103+
)\
104+
\texttt{\textbf{=>}}\ \textit{expression}
105+
&& \texttt{list("function\_definition", <parameters>,} \\
106+
&& && & && && \texttt{list("return\_statement", <expression>))} \\
107+
&& && | &\quad && (\ \textit{name}\ | \
108+
\textbf{\texttt{(}}\ \textit{parameters}\ \textbf{\texttt{)}}\
109+
)\
110+
\texttt{\textbf{=>}}\ \textit{block}
111+
&& \texttt{list("function\_definition", <parameters>, <statement>)} \\
112+
&& && | &\quad && \textit{expression} \ \textbf{\texttt{?}}\
113+
\textit{expression}
114+
\ \textbf{\texttt{:}}\
115+
\textit{expression}\
116+
&& \texttt{list("conditional\_expression", <expression>,} \\
117+
&&&&&&&&&\ \ \ \ \ \ \texttt{<expression>, <expression>)} \\
118+
&& && | &\quad && \textit{expression} \textbf{\texttt{[}}
119+
\textit{expression} \textbf{\texttt{]}}
120+
&& \texttt{list("array\_access", <expression>, <expression>)} \\
121+
&& && | &\quad && \textbf{\texttt{[}}\
122+
\textit{expressions}\
123+
\textbf{\texttt{]}}
124+
&& \texttt{list("array\_expression", list of <expression>)} \\
125+
&& && | &\quad && \textbf{\texttt{(}}\ \textit{expression} \
126+
\textbf{\texttt{)}} && \textrm{treat as:}\ \textit{expression} \\[1mm]
127+
&& \textit{binary-operator} \
128+
&& ::= &\quad && \textbf{\texttt{+}}\ |\ \textbf{\texttt{-}}\ |\ \textbf{\texttt{*}}\ |\ \textbf{\texttt{/}}\ |\ \textbf{\texttt{\%}}\ |\
129+
\textbf{\texttt{===}}\ |\ \textbf{\texttt{!==}}\ \\
130+
&& && | &\quad && \texttt{\textbf{>}}\ |\ \texttt{\textbf{<}}\ |\ \texttt{\textbf{>=}}\ |\ \texttt{\textbf{<=}}\
131+
|\ \textbf{\texttt{\&\&}}\ |\ \texttt{\textbf{||}}
132+
&& \texttt{list("name", string)} \\[1mm]
133+
&& \textit{unary-operator}
134+
&& ::= &\quad && \textbf{\texttt{!}}\ |\ \textbf{\texttt{-}}
135+
&& \texttt{list("name", string)} \\
136+
&& \textit{expressions} && ::= &\quad && \epsilon\ | \ \textit{expression}\ (
137+
\ \textbf{\texttt{,}} \
138+
\textit{expression} \
139+
)\ \ldots
140+
&& \texttt{list of <expression>} \\
141+
\end{alignat*}
142+
143+
144+
145+
\newpage
146+
\KOMAoptions{paper=portrait,pagesize}
147+
\recalctypearea

0 commit comments

Comments
 (0)