@@ -2140,6 +2140,56 @@ list("logical_composition",
2140
2140
as derived components.
2141
2141
</JAVASCRIPT >
2142
2142
</SPLIT >
2143
+ <SOLUTION >
2144
+ GitHub user EmilyOng provides a solution for the alternative:
2145
+ how to implement
2146
+ <JAVASCRIPTINLINE >&& </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 === "&& "
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 && b;")));
2188
+ display(logical_comp_decl_to_conditional_expr_decl(parse("a || b;")));
2189
+ display(logical_comp_decl_to_conditional_expr_decl(parse("(a && !b) || (!a && b);")));
2190
+ </JAVASCRIPT >
2191
+ </SNIPPET >
2192
+ </SOLUTION >
2143
2193
</EXERCISE >
2144
2194
2145
2195
<EXERCISE >
0 commit comments