#include "irgen/IRGen.h"

#include <stdexcept>

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

void IRGenImpl::GenBlock(SysYParser::BlockContext& block) {
  for (auto* item : block.blockItem()) {
    if (item) {
      if (GenBlockItem(*item)) {
        break;
      }
    }
  }
}

bool IRGenImpl::GenBlockItem(SysYParser::BlockItemContext& item) {
  if (item.decl()) {
    GenDecl(*item.decl());
    return false;
  }
  if (item.stmt()) {
    return GenStmt(*item.stmt());
  }
  throw std::runtime_error("[irgen] 暂不支持的 blockItem 类型");
}

void IRGenImpl::GenDecl(SysYParser::DeclContext& decl) {
  if (decl.varDecl()) {
    GenVarDecl(*decl.varDecl());
    return;
  }
  throw std::runtime_error("[irgen] 暂不支持的声明类型");
}

void IRGenImpl::GenVarDecl(SysYParser::VarDeclContext& decl) {
  const std::string name = decl.Ident()->getText();
  if (locals_.find(name) != locals_.end()) {
    throw std::runtime_error("[irgen] 重复定义变量: " + name);
  }
  auto* slot = builder_.CreateAllocaI32(ir::DefaultContext().NextTemp());
  locals_[name] = slot;

  ir::Value* init = nullptr;
  if (decl.exp()) {
    init = GenExpr(*decl.exp());
  } else {
    init = ir::DefaultContext().GetConstInt(0);
  }
  builder_.CreateStore(init, slot);
}
