#include "irgen/IRGen.h"

#include <stdexcept>

#include "SysYParser.h"
#include "ir/IR.h"

bool IRGenImpl::GenStmt(SysYParser::StmtContext& stmt) {
  if (stmt.returnStmt()) {
    GenReturnStmt(*stmt.returnStmt());
    return true;
  }
  throw std::runtime_error("[irgen] 暂不支持的语句类型");
}

void IRGenImpl::GenReturnStmt(SysYParser::ReturnStmtContext& ret) {
  if (!ret.exp()) {
    throw std::runtime_error("[irgen] return 缺少表达式");
  }
  ir::Value* v = GenExpr(*ret.exp());
  builder_.CreateRet(v);
}
