Exemplo n.º 1
0
void compile (struct Exp_t *exp)
{
  switch (exp->kind){
  case EXP_INT:{
    struct Exp_Int *p = (struct Exp_Int *)exp;
    emit (Stack_Push_new (p->i));
    break;
  }
  case EXP_SUM:{
    struct Exp_Sum *sp = (struct Exp_Sum *)exp;
    compile (sp->left);
    compile (sp->right);
    emit (Stack_Add_new ());
    break;
  }
  default:
    break;
  }
}
void compile (struct Exp_t *exp)
{
  switch (exp->kind){
  case EXP_INT:{
    struct Exp_Int *p = (struct Exp_Int *)exp;
    emit (Stack_Push_new (p->i));
    break;
  }
  case EXP_SUM:{
    //TODO()
    struct Exp_Sum *p=(struct Exp_Sum *)exp;
    compile(p->left);
    compile(p->right);
    //int a=pop();
    //int b=pop();
    //emit(Stack_Push_new(a+b));
    emit(Stack_Add_new());  //if you dont want to use Constant folding
    break;
  }
  default:
    break;
  }
}