I did say the going would be slow, I believe.
Cat's Eye Technologies
11 years ago
120 | 120 | |
121 | 121 | compile_branch(P,B,Mod,FmlNms, prodbranch(Fs, Ls, E)) = |
122 | 122 | |
123 | # all_pattern_variables = set() | |
124 | ||
125 | 123 | emitln('{') & |
126 | 124 | indent & |
127 | 125 | emit_formal_match_patterns(FmlNms, Fs, nil) → PatNms & |
129 | 127 | emit_pattern_match_expression(PatNms, FmlNms) & |
130 | 128 | emitln(' 1) {') & |
131 | 129 | indent & |
132 | ||
133 | # # declare and get variables which are found in patterns for this branch | |
134 | # for fml_num in xrange(0, len(branch.formals)): | |
135 | # variables = [] | |
136 | # formal = branch.formals[fml_num] | |
137 | # formal.to_term().collect_variables(variables) | |
138 | # for variable in variables: | |
139 | # self.emit('struct term *%s = ' | |
140 | # 'term_find_variable(pattern%s, "%s");' % | |
141 | # (variable.name, fml_num, variable.name) | |
142 | # ) | |
143 | # all_pattern_variables.add(variable.name) | |
144 | ||
130 | emit_matched_variables(Fs, PatNms) & | |
145 | 131 | emit_locals(Ls) & |
146 | 132 | compile_r(P, prodbranch(Fs, Ls, E), Mod, E) & |
147 | 133 | |
166 | 152 | emitln(') &&') & |
167 | 153 | emit_pattern_match_expression(PatNms, FmlNms). |
168 | 154 | |
155 | # declare and get variables which are found in patterns for this branch | |
156 | emit_matched_variables(nil, nil) = 'ok'. | |
157 | emit_matched_variables(list(F, Fs), list(PatNm, PatNms)) = | |
158 | collect_variables(F, nil) → Vars & | |
159 | emit_assign_matched_variables(PatNm, Vars) & | |
160 | emit_matched_variables(Fs, PatNms). | |
161 | ||
162 | emit_assign_matched_variables(PatNm, nil) = 'ok'. | |
163 | emit_assign_matched_variables(PatNm, list(variable(VN), T)) = | |
164 | emit('struct term *') & | |
165 | emit(VN) & | |
166 | emit(' = term_find_variable(') & | |
167 | emit(PatNm) & | |
168 | emit(', "') & | |
169 | emit(VN) & | |
170 | emitln('");') & | |
171 | emit_assign_matched_variables(PatNm, T). | |
172 | ||
173 | # TODO should be like compile_r(term) but... different | |
174 | collect_variables(F, L) = L. | |
169 | 175 | |
170 | 176 | # .... general compiler continues .... |
171 | 177 | |
420 | 426 | |
421 | 427 | ############### emitting pattern terms ############### |
422 | 428 | |
423 | # should be like compile_r(term), except... with differences | |
429 | # should be like compile_r(term), except variables are different | |
424 | 430 | |
425 | 431 | emit_pattern(atom(T)) = |
426 | 432 | emit('struct term *') & |
432 | 438 | emitln('");') & |
433 | 439 | Nm. |
434 | 440 | |
435 | # TODO: THIS IS WHERE IT SHOULD BE DIFFERENT | |
436 | 441 | emit_pattern(variable(VN)) = |
437 | 442 | emit('struct term *') & |
438 | 443 | $:gensym('pattern') → Nm & |
439 | 444 | emit(Nm) & |
440 | emit(' = ') & | |
441 | emit(VN) & | |
442 | emitln(';') & | |
445 | emit(' = term_new_variable("') & | |
446 | emit(VN) & | |
447 | emit('", term_new_from_cstring("nil_') & | |
448 | emit(VN) & | |
449 | emitln('"));') & | |
443 | 450 | Nm. |
444 | 451 | |
445 | 452 | emit_pattern(constructor(T,Ts)) = |