#include <exception>
#include <iostream>

#include "frontend/AntlrDriver.h"
#include "frontend/AstBuilder.h"
#include "ir/IR.h"
#include "irgen/IRGen.h"
#include "sem/Sema.h"
#include "utils/CLI.h"
#include "utils/Log.h"
#include "ast/AstNodes.h"

int main(int argc, char** argv) {
  try {
    auto opts = ParseCLI(argc, argv);
    auto antlr = ParseFileWithAntlr(opts.input);
    auto ast = BuildAst(antlr.tree);
    ast::PrintAST(*ast);  // 调试 AST
    ast = RunSema(std::move(ast));
    auto module = GenerateIR(*ast);

    ir::IRPrinter printer;
    printer.Print(*module);
  } catch (const std::exception& ex) {
    LOG_ERROR(ex.what());
    return 1;
  }
  return 0;
}
