.\" .\" 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, 12
Back Next

Double Trouble

There is another case to be covered when you are generating code, not just pretty printing. It's the manual polymorphism, again, but the 2D case this time...
Not uncommon C implemntation... Desired C++ implementation...
void
expression_plus_compile::generate_code()
{
    lhs->generate_code();
    rhs->generate_code();
    switch (PAIR(lhs->get_type(), rhs->get_type()))
    {
    case PAIR(TYPE_INTEGER, TYPE_INTEGER):
        emit(OP_ADI);
        break;

    case PAIR(TYPE_REAL, TYPE_INTEGER):
        emit(OP_FLT);
        emit(OP_ADR);
        break;

    case PAIR(TYPE_INTEGER, TYPE_REAL):
        emit(OP_FLO);
        emit(OP_ADR);
        break;

    case PAIR(TYPE_REAL, TYPE_REAL):
        emit(OP_ADR);
        break;
    }
}
void
expression_plus_integer_compile::generate_code()
{
    lhs->generate_code();
    rhs->generate_code();
    emit(OP_ADI);
}





// and take care of the implict casts
// somewhere else, too.





void
expression_plus_real_compile::generate_code()
{
    lhs->generate_code();
    rhs->generate_code();
    emit(OP_ADR);
}

But how do we get there?

// vim: set ts=8 sw=4 et :