#include "sem/Sema.h"

#include <stdexcept>
#include <string>

#include "sem/SymbolTable.h"

namespace {

void CheckExpr(SysYParser::ExpContext& exp, const SymbolTable& table);

void CheckPrimary(SysYParser::PrimaryContext& primary,
                  const SymbolTable& table) {
  if (primary.Number()) {
    return;
  }

  if (primary.Ident()) {
    const std::string name = primary.Ident()->getText();
    if (!table.Contains(name)) {
      throw std::runtime_error("[sema] 使用了未定义的变量: " + name);
    }
    return;
  }

  if (primary.exp()) {
    CheckExpr(*primary.exp(), table);
    return;
  }

  throw std::runtime_error("[sema] 暂不支持的 primary 形式");
}

void CheckExpr(SysYParser::ExpContext& exp, const SymbolTable& table) {
  if (!exp.addExp()) {
    throw std::runtime_error("[sema] 非法表达式");
  }
  const auto& terms = exp.addExp()->primary();
  for (auto* term : terms) {
    CheckPrimary(*term, table);
  }
}

}  // namespace

void RunSema(SysYParser::CompUnitContext& comp_unit) {
  auto* func = comp_unit.funcDef();
  if (!func || !func->block()) {
    throw std::runtime_error("[sema] 缺少 main 函数定义");
  }

  SymbolTable table;

  for (auto* stmt : func->block()->stmt()) {
    if (!stmt) {
      continue;
    }
    if (auto* decl = stmt->varDecl()) {
      const std::string name = decl->Ident()->getText();
      if (table.Contains(name)) {
        throw std::runtime_error("[sema] 重复定义变量: " + name);
      }
      if (decl->exp()) {
        CheckExpr(*decl->exp(), table);
      }
      table.Add(name);
      continue;
    }
    if (auto* ret = stmt->returnStmt()) {
      CheckExpr(*ret->exp(), table);
      break;
    }
    throw std::runtime_error("[sema] 暂不支持的语句类型");
  }
}
