X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Ftranslator-group.cc;h=4dcdca35b38d177ad9d0ce2b978b2fff10100c3d;hb=c659cb200486c2f908703696a1b2873e78c8160a;hp=ff63c26e16a2d89338745a23fe9aa1d39a4487ec;hpb=31a43e122332d7a8e703ff5f01286eacc4ea7a27;p=lilypond.git diff --git a/lily/translator-group.cc b/lily/translator-group.cc index ff63c26e16..4dcdca35b3 100644 --- a/lily/translator-group.cc +++ b/lily/translator-group.cc @@ -3,474 +3,145 @@ source file of the GNU LilyPond music typesetter - (c) 1997--2001 Han-Wen Nienhuys + (c) 1997--2004 Han-Wen Nienhuys */ -#include "music-output-def.hh" #include "translator-group.hh" -#include "translator.hh" -#include "debug.hh" -#include "moment.hh" -#include "scm-hash.hh" -#include "killing-cons.tcc" -#include "translator-def.hh" -Translator_group::Translator_group (Translator_group const&s) - : Translator (s) -{ - iterator_count_ =0; - - Scheme_hash_table * tab = new Scheme_hash_table (*s.properties_dict ()); - properties_scm_ = tab->self_scm (); - scm_unprotect_object (tab->self_scm ()); -} - -Scheme_hash_table* -Translator_group::properties_dict () const -{ - return Scheme_hash_table::unsmob (properties_scm_); -} +#include "output-def.hh" +#include "warn.hh" +#include "scm-hash.hh" +#include "context-def.hh" +#include "context.hh" +#include "main.hh" +#include "music.hh" -Translator_group::~Translator_group () +Translator_group* +Translator_group::get_daddy_translator () const { - - //assert (removable_b ()); + return context ()->get_parent_context ()->implementation (); } -Translator_group::Translator_group () -{ - iterator_count_ = 0; - Scheme_hash_table *tab = new Scheme_hash_table ; - properties_scm_ = tab->self_scm (); - - scm_unprotect_object (tab->self_scm ()); -} - void -Translator_group::check_removal () +translator_each (SCM list, Translator_method method) { - SCM next = SCM_EOL; - for (SCM p = trans_group_list_; gh_pair_p (p); p = next) - { - next = gh_cdr (p); - - Translator_group *trg = dynamic_cast (unsmob_translator (gh_car (p))); - - trg->check_removal (); - if (trg->removable_b ()) - terminate_translator (trg); - } + for (SCM p = list; scm_is_pair (p); p = scm_cdr (p)) + (unsmob_translator (scm_car (p))->*method) (); } -SCM -Translator_group::add_translator (SCM list, Translator *t) -{ - list = gh_append2 (list, gh_cons (t->self_scm (), SCM_EOL)); - t->daddy_trans_l_ = this; - t->output_def_l_ = output_def_l_; - if (Translator_group*tg = dynamic_cast (t)) - { - unsmob_translator_def (tg->definition_)->apply_property_operations (tg); - } - - t->initialize (); - return list; -} void -Translator_group::add_group_translator (Translator *t) +Translator_group::initialize () { - trans_group_list_ = add_translator (trans_group_list_,t); + SCM tab = scm_make_vector (scm_int2num (19), SCM_BOOL_F); + context ()->set_property ("acceptHashTable", tab); } - bool -Translator_group::removable_b () const +translator_accepts_any_of (Translator*tr, SCM ifaces) { - return trans_group_list_ == SCM_EOL && ! iterator_count_; + SCM ack_ifs = scm_assoc (ly_symbol2scm ("events-accepted"), + tr->translator_description ()); + ack_ifs = scm_cdr (ack_ifs); + for (SCM s = ifaces; scm_is_pair (s); s = scm_cdr (s)) + if (scm_c_memq (scm_car (s), ack_ifs) != SCM_BOOL_F) + return true; + return false; } -Translator_group * -Translator_group::find_existing_translator_l (String n, String id) +SCM +find_accept_translators (SCM gravlist, SCM ifaces) { - if (is_alias_b (n) && (id_str_ == id || id.empty_b ())) - return this; - - Translator_group* r = 0; - for (SCM p = trans_group_list_; !r && gh_pair_p (p); p = gh_cdr (p)) + SCM l = SCM_EOL; + for (SCM s = gravlist; scm_is_pair (s); s = scm_cdr (s)) { - Translator * t = unsmob_translator (gh_car (p)); - - r = dynamic_cast (t)->find_existing_translator_l (n, id); + Translator* tr = unsmob_translator (scm_car (s)); + if (translator_accepts_any_of (tr, ifaces)) + l = scm_cons (tr->self_scm (), l); } + l = scm_reverse_x (l, SCM_EOL); - return r; + return l; } - - - -Translator_group* -Translator_group::find_create_translator_l (String n, String id) +bool +Translator_group::try_music (Music* m) { - Translator_group * existing = find_existing_translator_l (n,id); - if (existing) - return existing; + SCM tab = get_property ("acceptHashTable"); + SCM name = scm_sloppy_assq (ly_symbol2scm ("name"), + m->get_property_alist (false)); - Link_array path - = unsmob_translator_def (definition_)->path_to_acceptable_translator (ly_str02scm ((char*)n.ch_C ()), output_def_l ()); + if (!scm_is_pair (name)) + return false; - if (path.size ()) + name = scm_cdr (name); + SCM accept_list = scm_hashq_ref (tab, name, SCM_UNDEFINED); + if (accept_list == SCM_BOOL_F) { - Translator_group * current = this; - - // start at 1. The first one (index 0) will be us. - for (int i=0; i < path.size (); i++) - { - Translator_group * new_group = path[i]->instantiate (output_def_l_); - - current->add_group_translator (new_group); - current = new_group; - } - current->id_str_ = id; - return current; + accept_list = find_accept_translators (get_simple_trans_list (), + m->get_property ("types")); + scm_hashq_set_x (tab, name, accept_list); } - Translator_group *ret = 0; - if (daddy_trans_l_) - ret = daddy_trans_l_->find_create_translator_l (n,id); - else + for (SCM p = accept_list; scm_is_pair (p); p = scm_cdr (p)) { - warning (_f ("can't find or create `%s' called `%s'", n, id)); - ret =0; + Translator * t = unsmob_translator (scm_car (p)); + if (t && t->try_music (m)) + return true; } - return ret; + return false; } -bool -Translator_group::try_music_on_nongroup_children (Music *m) -{ - bool hebbes_b =false; - - for (SCM p = simple_trans_list_; !hebbes_b && gh_pair_p (p); p = gh_cdr (p)) - { - hebbes_b = unsmob_translator (gh_car (p))->try_music (m); - } - return hebbes_b; -} -bool -Translator_group::try_music (Music* m) +SCM +Translator_group::get_simple_trans_list () { - bool hebbes_b = try_music_on_nongroup_children (m); - - if (!hebbes_b && daddy_trans_l_) - hebbes_b = daddy_trans_l_->try_music (m); - return hebbes_b ; + return simple_trans_list_; } -int -Translator_group::depth_i () const -{ - return (daddy_trans_l_) ? daddy_trans_l_->depth_i () + 1 : 0; -} -Translator_group* -Translator_group::ancestor_l (int level) +void +recurse_over_translators (Context * c, Translator_method ptr, Direction dir) { - if (!level || !daddy_trans_l_) - return this; + Translator_group * tg + = dynamic_cast (c->implementation ()); - return daddy_trans_l_->ancestor_l (level-1); -} -void -Translator_group::terminate_translator (Translator*r_l) -{ - r_l->removal_processing (); /* - Return value ignored. GC does the rest. + Top down: */ - remove_translator_p (r_l); -} - - -/** - Remove a translator from the hierarchy. - */ -Translator * -Translator_group::remove_translator_p (Translator*trans_l) -{ - assert (trans_l); - - trans_group_list_ = scm_delq_x (trans_l->self_scm (), trans_group_list_); - trans_l->daddy_trans_l_ = 0; - return trans_l; -} - -bool -Translator_group::is_bottom_translator_b () const -{ - return !gh_string_p (unsmob_translator_def (definition_)->default_child_context_name ()); - -} - -Translator_group* -Translator_group::get_default_interpreter () -{ - if (!is_bottom_translator_b ()) + if (dir == DOWN) { - SCM nm = unsmob_translator_def (definition_)->default_child_context_name (); - SCM st = output_def_l ()->find_translator_l (nm); + translator_each (tg->get_simple_trans_list (), + ptr); - Translator_def *t = unsmob_translator_def (st); - if (!t) - { - warning (_f ("can't find or create: `%s'", ly_scm2string (nm).ch_C ())); - t = unsmob_translator_def (this->definition_); - } - Translator_group *tg = t->instantiate (output_def_l_); - add_group_translator (tg); - - if (!tg->is_bottom_translator_b ()) - return tg->get_default_interpreter (); - else - return tg; + (tg->*ptr) (); } - return this; -} -static void -static_each (SCM list, Method_pointer method) -{ - for (SCM p = list; gh_pair_p (p); p = gh_cdr (p)) - (unsmob_translator (gh_car (p))->*method) (); - -} - -void -Translator_group::each (Method_pointer method) -{ - static_each (simple_trans_list_, method); - static_each (trans_group_list_, method); -} - - -/* - PROPERTIES - */ -Translator_group* -Translator_group::where_defined (SCM sym) const -{ - if (properties_dict ()->elem_b (sym)) + for (SCM s = c->children_contexts () ; scm_is_pair (s); + s =scm_cdr (s)) { - return (Translator_group*)this; + recurse_over_translators (unsmob_context (scm_car (s)), ptr, dir); } - return (daddy_trans_l_) ? daddy_trans_l_->where_defined (sym) : 0; -} - -/* - return SCM_EOL when not found. -*/ -SCM -Translator_group::get_property (SCM sym) const -{ - SCM val =SCM_EOL; - if (properties_dict ()->try_retrieve (sym, &val)) - return val; - - if (daddy_trans_l_) - return daddy_trans_l_->get_property (sym); - - return val; -} - -void -Translator_group::set_property (String id, SCM val) -{ - set_property (ly_symbol2scm (id.ch_C ()), val); -} - - -void -Translator_group::set_property (SCM sym, SCM val) -{ - properties_dict ()->set (sym, val); -} - -/* - TODO: look up to check whether we have inherited var? - */ -void -Translator_group::unset_property (SCM sym) -{ - properties_dict ()->remove (sym); -} - - -/* - Push or pop (depending on value of VAL) a single entry (ELTPROP . VAL) - entry from a translator property list by name of PROP -*/ -void -Translator_group::execute_single_pushpop_property (SCM prop, SCM eltprop, SCM val) -{ - if (gh_symbol_p (prop)) + if (dir == UP) { - if (val != SCM_UNDEFINED) - { - SCM prev = get_property (prop); + translator_each (tg->get_simple_trans_list (), + ptr); - if (gh_pair_p (prev) || prev == SCM_EOL) - { - bool ok = type_check_assignment (val, eltprop, ly_symbol2scm ("backend-type?")); - - - - if (ok) - { - prev = gh_cons (gh_cons (eltprop, val), prev); - set_property (prop, prev); - } - } - else - { - // warning here. - } - - } - else - { - SCM prev = get_property (prop); - - SCM newprops= SCM_EOL ; - while (gh_pair_p (prev) && gh_caar (prev) != eltprop) - { - newprops = gh_cons (gh_car (prev), newprops); - prev = gh_cdr (prev); - } - - if (gh_pair_p (prev)) - { - newprops = scm_reverse_x (newprops, gh_cdr (prev)); - set_property (prop, newprops); - } - } + (tg->*ptr) (); } } - - - - -/* - STUBS -*/ -void -Translator_group::stop_translation_timestep () +Translator_group::Translator_group() { - each (&Translator::pre_move_processing); + simple_trans_list_ = SCM_EOL; } void -Translator_group::start_translation_timestep () +Translator_group::derived_mark () const { - each (&Translator::post_move_processing); + scm_gc_mark (simple_trans_list_); } - -void -Translator_group::do_announces () -{ - each (&Translator::announces); -} - -void -Translator_group::initialize () -{ - each (&Translator::initialize); -} - -void -Translator_group::finalize () -{ - each (&Translator::removal_processing); -} - - -bool -type_check_assignment (SCM val, SCM sym, SCM type_symbol) -{ - bool ok = true; - SCM type_p = SCM_EOL; - - if (gh_symbol_p (sym)) - type_p = scm_object_property (sym, type_symbol); - - if (type_p != SCM_EOL && !gh_procedure_p (type_p)) - { - warning (_f ("Can't find property type-check for `%s'. Perhaps you made a typing error?", - ly_symbol2string (sym).ch_C ())); - } - else - { - if (val != SCM_EOL - && gh_procedure_p (type_p) - && gh_call1 (type_p, val) == SCM_BOOL_F) - { - SCM errport = scm_current_error_port (); - ok = false; - SCM typefunc = scm_eval2 (ly_symbol2scm ("type-name"), SCM_EOL); - SCM type_name = gh_call1 (typefunc, type_p); - - scm_puts (_f ("Type check for `%s' failed; value `%s' must be of type `%s'", - ly_symbol2string (sym).ch_C (), - ly_scm2string (ly_write2scm (val)).ch_C (), - ly_scm2string (type_name).ch_C ()).ch_C (), - errport); - scm_puts ("\n", errport); - } - } - return ok; -} - -SCM -ly_get_trans_property (SCM context, SCM name) -{ - Translator *t = unsmob_translator (context); - Translator_group* tr= dynamic_cast (t); - if (!t || !tr) - { - /* programming_error? */ - warning (_ ("ly-get-trans-property: expecting a Translator_group argument")); - return SCM_EOL; - } - return tr->get_property (name); - -} -SCM -ly_set_trans_property (SCM context, SCM name, SCM val) -{ - - Translator *t = unsmob_translator (context); - Translator_group* tr= dynamic_cast (t); - if (tr) - { - tr->set_property (name, val); - } - return SCM_UNSPECIFIED; -} - - - - -void -add_trans_scm_funcs () -{ - scm_make_gsubr ("ly-get-trans-property", 2, 0, 0, (Scheme_function_unknown)ly_get_trans_property); - scm_make_gsubr ("ly-set-trans-property", 3, 0, 0, (Scheme_function_unknown)ly_set_trans_property); -} - -ADD_SCM_INIT_FUNC (trans_scm, add_trans_scm_funcs);