Up, Prev Next

Revision: Imperative vs OO

... imperative type-based dispatch ... facility of the C++ language
void expr_generate_code(struct expr *e);

void
expr_generate_code(expr *e)
{
    switch(e->e_op)
    {
class expression
{
public:
    virtual void generate_code() const = 0;
};
    case PLUS:
        expr_generate_code(e->e_lhs);
        expr_generate_code(e->e_rhs);
        emit(OP_ADI);
        break;
class expression_add:
    public expression
{
public:
    virtual void generate_code() const {
        lhs->generate_code();
        rhs->generate_code();
        gen_emit_byte(OP_ADI);
    }

private:
    expression *lhs;
    expression *rhs;
}
    case INT_CONST:
        gen_emit_byte(OP_SLDC);
        gen_emit_int(e->e_int);
        break;
class expression_int_const:
    public expression
{
public:
    virtual void generate_code() const {
        gen_emit_byte(OP_SLDC);
        gen_emit_int(e->value);
        break;
    }

private:
    short value;
}
    etc etc
    }
}
etc etc