lilypond-1.3.145
[lilypond.git] / lily / translator-def.cc
blobaa33ce33fa5ed53e18d043bd5b4138ad904c2374
1 /*
2 translator-def.cc -- implement Translator_def
4 source file of the GNU LilyPond music typesetter
6 (c) 2000--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
10 #include "lily-proto.hh"
11 #include "translator-def.hh"
12 #include "translator-group.hh"
13 #include "warn.hh"
14 #include "music-output-def.hh"
16 #include "ly-smobs.icc"
18 int
19 Translator_def::print_smob (SCM smob, SCM port, scm_print_state*)
21 Translator_def* me = (Translator_def*) SCM_CELL_WORD_1 (smob);
23 scm_puts ("#<Translator_def ", port);
24 scm_display (me->type_name_, port);
25 scm_puts (">", port);
26 return 1;
30 SCM
31 Translator_def::mark_smob (SCM smob)
33 Translator_def* me = (Translator_def*) SCM_CELL_WORD_1 (smob);
35 scm_gc_mark (me->type_aliases_);
36 scm_gc_mark (me->consists_name_list_);
37 scm_gc_mark (me->accepts_name_list_);
38 scm_gc_mark (me->end_consists_name_list_);
39 scm_gc_mark (me->property_ops_);
40 scm_gc_mark (me->translator_group_type_);
41 return me->type_name_;
44 SCM push_sym;
45 SCM assign_sym;
47 static void
48 foo_init ()
50 push_sym = scm_permanent_object (ly_symbol2scm ("push"));
51 assign_sym = scm_permanent_object (ly_symbol2scm ("assign"));
54 ADD_SCM_INIT_FUNC (transdef, foo_init);
56 Translator_def::Translator_def ()
58 type_aliases_ = SCM_EOL;
59 translator_group_type_ = SCM_EOL;
60 accepts_name_list_ = SCM_EOL;
61 consists_name_list_ = SCM_EOL;
62 end_consists_name_list_ = SCM_EOL;
63 property_ops_ = SCM_EOL;
64 type_name_ = SCM_EOL;
66 Translator_def::~Translator_def ()
70 Translator_def::Translator_def (Translator_def const & s)
71 : Input (s)
73 consists_name_list_ = scm_list_copy (s.consists_name_list_);
74 end_consists_name_list_ = scm_list_copy (s.end_consists_name_list_);
75 accepts_name_list_ = scm_list_copy (s.accepts_name_list_);
76 property_ops_ = scm_list_copy (s.property_ops_);
77 type_aliases_ = s.type_aliases_;
78 translator_group_type_ = s.translator_group_type_;
79 type_name_ = s.type_name_;
84 void
85 Translator_def::set_acceptor (SCM name, bool add)
87 if (add)
88 this->accepts_name_list_ = gh_cons (name, this->accepts_name_list_);
89 else
90 this->accepts_name_list_ = scm_delete_x (name, this->accepts_name_list_);
94 SCM
95 Translator_def::modify_definition (SCM list, SCM str, bool add)
97 String s = ly_scm2string (str);
98 if (!get_translator_l (s))
99 error (_ ("Program has no such type"));
101 if (add)
103 if (scm_memq (str, list) != SCM_BOOL_F)
105 warning (_f ("Already contains: `%s'", s));
106 warning (_f ("Not adding translator: `%s'", s));
108 else
109 list= gh_cons (str, list);
111 else
113 list = scm_delete_x (str, list);
115 return list;
120 void
121 Translator_def::remove_element (SCM s)
123 this->end_consists_name_list_ = modify_definition (this->end_consists_name_list_, s, false);
124 this->consists_name_list_ = modify_definition (this->consists_name_list_, s, false);
127 void
128 Translator_def::add_element (SCM s)
130 this->consists_name_list_ = modify_definition (this->consists_name_list_, s, true);
133 void
134 Translator_def::add_last_element (SCM s)
136 this->end_consists_name_list_ = modify_definition (this->end_consists_name_list_, s, true);
138 void
139 Translator_def::add_push_property (SCM props, SCM syms, SCM vals)
141 this->property_ops_ = gh_cons (gh_list (push_sym, props, syms, vals, SCM_UNDEFINED),
142 this->property_ops_);
145 void
146 Translator_def::add_pop_property (SCM props, SCM syms)
148 this->property_ops_ = gh_cons (gh_list (push_sym, props, syms, SCM_UNDEFINED),
149 this->property_ops_);
153 Do it. SYMS maybe a symbol or a list of symbols. VAL is
154 SCM_UNDEFINED in case of a pop
156 void
157 Translator_def::apply_pushpop_property (Translator_group* me,SCM syms, SCM eprop, SCM val)
159 if (gh_symbol_p (syms))
160 dynamic_cast<Translator_group*> (me)->execute_single_pushpop_property (syms, eprop, val);
161 else for (SCM s = syms; gh_pair_p (s); s = gh_cdr (s))
162 dynamic_cast<Translator_group*> (me)->execute_single_pushpop_property (gh_car (s), eprop, val);
167 Link_array<Translator_def>
168 Translator_def::path_to_acceptable_translator (SCM type_str, Music_output_def* odef) const
170 assert (gh_string_p (type_str));
172 Link_array<Translator_def> accepted_arr;
173 for (SCM s = accepts_name_list_; gh_pair_p (s); s = gh_cdr (s))
175 Translator_def *t = unsmob_translator_def (odef->find_translator_l (gh_car (s)));
176 if (!t)
177 continue;
178 accepted_arr.push (t);
181 Link_array<Translator_def> best_result;
182 for (int i=0; i < accepted_arr.size (); i++)
186 don't check aliases, because \context Staff should not create RhythmicStaff.
188 if (gh_equal_p (accepted_arr[i]->type_name_, type_str))
190 best_result.push (accepted_arr[i]);
191 return best_result;
195 int best_depth= INT_MAX;
196 for (int i=0; i < accepted_arr.size (); i++)
198 Translator_def * g = accepted_arr[i];
200 Link_array<Translator_def> result
201 = g->path_to_acceptable_translator (type_str, odef);
202 if (result.size () && result.size () < best_depth)
204 result.insert (g,0);
205 best_result = result;
209 return best_result;
211 IMPLEMENT_UNSMOB (Translator_def,translator_def);
212 IMPLEMENT_SMOBS (Translator_def);
213 IMPLEMENT_DEFAULT_EQUAL_P (Translator_def);
216 static SCM
217 trans_list (SCM namelist, Translator_group*tg)
219 SCM l = SCM_EOL;
220 for (SCM s = namelist; gh_pair_p (s) ; s = gh_cdr (s))
222 Translator * t = get_translator_l (ly_scm2string (gh_car (s)));
223 if (!t)
224 warning (_f ("can't find: `%s'", s));
225 else
227 Translator * tr = t->clone ();
228 SCM str = tr->self_scm ();
229 l = gh_cons (str, l);
231 tr->daddy_trans_l_ = tg;
232 tr->output_def_l_ = tg->output_def_l_;
234 scm_unprotect_object (str);
237 return l;
241 Translator_group *
242 Translator_def::instantiate (Music_output_def* md)
244 Translator * g = get_translator_l (ly_scm2string (translator_group_type_));
245 g = g->clone ();
247 Translator_group *tg = dynamic_cast<Translator_group*> (g);
248 tg->output_def_l_ = md;
249 tg->definition_ = self_scm ();
250 tg->type_str_ = ly_scm2string (type_name_);
251 SCM l1 = trans_list (consists_name_list_, tg);
252 SCM l2 =trans_list (end_consists_name_list_,tg);
253 l1 = scm_reverse_x (l1, l2);
255 tg->simple_trans_list_ = l1;
257 return tg;
261 void
262 Translator_def::apply_property_operations (Translator_group*tg)
264 SCM correct_order = scm_reverse (property_ops_); // pity of the mem.
265 for (SCM s = correct_order; gh_pair_p (s); s = gh_cdr (s))
267 SCM entry = gh_car (s);
268 SCM type = gh_car (entry);
269 entry = gh_cdr (entry);
271 if (type == push_sym)
273 SCM val = gh_cddr (entry);
274 val = gh_pair_p (val) ? gh_car (val) : SCM_UNDEFINED;
276 apply_pushpop_property (tg, gh_car (entry), gh_cadr (entry), val);
278 else if (type == assign_sym)
280 tg->set_property (gh_car (entry), gh_cadr (entry));
286 Translator_def::clone_scm () const
288 Translator_def * t = new Translator_def (*this);
289 return t->unprotected_smobify_self ();
293 Translator_def::make_scm ()
295 Translator_def* t = new Translator_def;
296 return t->unprotected_smobify_self ();
299 void
300 Translator_def::add_property_assign (SCM nm, SCM val)
302 this->property_ops_ = gh_cons (gh_list (assign_sym, scm_string_to_symbol (nm), val, SCM_UNDEFINED),
303 this->property_ops_);
307 Default child context as a SCM string, or something else if there is
308 none.
311 Translator_def::default_child_context_name ()
313 SCM d = accepts_name_list_;
314 return gh_pair_p (d) ? gh_car (scm_last_pair (d)) : SCM_EOL;
318 Translator_def::to_alist ()const
320 SCM l = SCM_EOL;
322 l = gh_cons (gh_cons (ly_symbol2scm ("consists"), consists_name_list_), l);
323 l = gh_cons (gh_cons (ly_symbol2scm ("end-consists"), end_consists_name_list_), l);
324 l = gh_cons (gh_cons (ly_symbol2scm ("accepts"), accepts_name_list_), l);
325 l = gh_cons (gh_cons (ly_symbol2scm ("property-ops"), property_ops_), l);
326 l = gh_cons (gh_cons (ly_symbol2scm ("type-name"), type_name_), l); // junkme.
327 l = gh_cons (gh_cons (ly_symbol2scm ("group-type"), translator_group_type_), l);
329 return l;