.\" .\" UCSD p-System cross compiler .\" Copyright (C) 2006, 2007, 2012 Peter Miller .\" .\" This program is free software; you can redistribute it and/or modify .\" it under the terms of the GNU General Public License as published by .\" the Free Software Foundation; either version 2 of the License, or (at .\" you option) any later version. .\" .\" This program is distributed in the hope that it will be useful, .\" but WITHOUT ANY WARRANTY; without even the implied warranty of .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU .\" General Public License for more details. .\" .\" You should have received a copy of the GNU General Public License along .\" with this program. If not, see .\" .ad l .hy 0 Compilers and Factories, 13
Back Next

Method Method

We solve this with a multi method, something C++ has no direct support for...
expression *
translator::expression_plus(expression *lhs, expression *rhs)
{
    switch (std::pair(lhs->e_id, rhs->e_id))
    {
    case std::pair(TYPE_INTEGER, TYPE_INTEGER):
        return new expression_add_integer(lhs, rhs);

    case std::pair(TYPE_REAL, TYPE_INTEGER):
        rhs = cast_int_real_factory(rhs);
        return new expression_add_real(lhs, rhs);

    case std::pair(TYPE_INTEGER, TYPE_REAL):
        lhs = cast_int_real_factory(lhs);
        return new expression_add_real(lhs, rhs);

    case std::pair(TYPE_REAL, TYPE_REAL):
        return new expression_add_real(lhs, rhs);
    }
    error("invalud additon expression");
    return new expression_error();
}
class translator
{
    blah blah
private:
    binary_dispatch op_add;
    blah blah
};

void
translator::translator()
{
    blah blah
    op_plus.push_back(type_integer::is_a, type_integer::is_a,
        &translator::expression_plus_integer_factory);
    op_plus.push_back(type_real::is_a, type_real::is_a,
        &translator::expression_plus_real_factory);
    op_plus.push_back(type_integer::is_a, type_real::is_a,
        &translator::expression_integer_plus_real);
    blah blah
}

expression *
translator::expression_plus(expression *lhs, expression *rhs)
{
    return op_plus.dispatch(lhs, rhs);
}

expression *
translator::expression_integer_plus_real(expression *lhs, expression *rhs)
{
    lhs = cast_int_real_factory(lhs);
    return expression_plus_real_factory(lhs, rhs);
}


Problem: we replaced those id things with vptrs.


.\" vim: set ts=8 sw=4 et :