Skip to content

Commit 4d8a92f

Browse files
authored
fixes #915 (#1046)
1 parent dbae760 commit 4d8a92f

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

xml/chapter4/section1/subsection2.xml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2140,6 +2140,56 @@ list("logical_composition",
21402140
as derived components.
21412141
</JAVASCRIPT>
21422142
</SPLIT>
2143+
<SOLUTION>
2144+
GitHub user EmilyOng provides a solution for the alternative:
2145+
how to implement
2146+
<JAVASCRIPTINLINE>&amp;&amp;</JAVASCRIPTINLINE> and
2147+
<JAVASCRIPTINLINE>||</JAVASCRIPTINLINE>
2148+
as derived components.
2149+
<SNIPPET>
2150+
<JAVASCRIPT>
2151+
function make_conditional_expr_decl(predicate, consequent_expression, alternative_expression) {
2152+
return list("conditional_expression", predicate, consequent_expression, alternative_expression);
2153+
}
2154+
function make_literal(value) {
2155+
return list("literal", value);
2156+
}
2157+
2158+
// Syntax selectors
2159+
function logical_operation(component) {
2160+
return head(tail(component));
2161+
}
2162+
function first_expression(component) {
2163+
return head(tail(tail(component)));
2164+
}
2165+
function second_expression(component) {
2166+
return head(tail(tail(tail(component))));
2167+
}
2168+
2169+
function logical_comp_decl_to_conditional_expr_decl(component) {
2170+
const operation = logical_operation(component);
2171+
2172+
return operation === "&amp;&amp;"
2173+
? make_conditional_expr_decl(
2174+
first_expression(component),
2175+
second_expression(component),
2176+
false
2177+
)
2178+
: operation === "||"
2179+
? make_conditional_expr_decl(
2180+
first_expression(component),
2181+
true,
2182+
second_expression(component)
2183+
)
2184+
: error(component, "unknown operation -- logical_comp_decl_to_conditional_expr_decl");
2185+
}
2186+
2187+
display(logical_comp_decl_to_conditional_expr_decl(parse("a &amp;&amp; b;")));
2188+
display(logical_comp_decl_to_conditional_expr_decl(parse("a || b;")));
2189+
display(logical_comp_decl_to_conditional_expr_decl(parse("(a &amp;&amp; !b) || (!a &amp;&amp; b);")));
2190+
</JAVASCRIPT>
2191+
</SNIPPET>
2192+
</SOLUTION>
21432193
</EXERCISE>
21442194

21452195
<EXERCISE>

0 commit comments

Comments
 (0)