#pragma once
#include "IR.h"
#include "IRBuilder.h"
#include "SysYBaseVisitor.h"
#include "SysYParser.h"
#include <memory>
#include <unordered_map>
#include <forward_list>

namespace sysy {


// @brief 用于存储数组值的树结构
// 多位数组本质上是一维数组的嵌套可以用树来表示。
class ArrayValueTree {
private:
  Value *value = nullptr; /// 该节点存储的value
  std::vector<std::unique_ptr<ArrayValueTree>> children; /// 子节点列表

public:
  ArrayValueTree() = default;

public:
  auto getValue() const -> Value * { return value; }
  auto getChildren() const
      -> const std::vector<std::unique_ptr<ArrayValueTree>> & {
    return children;
  }

  void setValue(Value *newValue) { value = newValue; }
  void addChild(ArrayValueTree *newChild) { children.emplace_back(newChild); }
  void addChildren(const std::vector<ArrayValueTree *> &newChildren) {
    for (const auto &child : newChildren) {
      children.emplace_back(child);
    }
  }
};


class Utils {
public:
  // transform a tree of ArrayValueTree to a ValueCounter
  static void tree2Array(Type *type, ArrayValueTree *root,
                         const std::vector<Value *> &dims, unsigned numDims,
                         ValueCounter &result, IRBuilder *builder);                          
  static void
  createExternalFunction(const std::vector<Type *> &paramTypes,
                         const std::vector<std::string> &paramNames,
                         const std::vector<std::vector<Value *>> &paramDims,
                         Type *returnType, const std::string &funcName,
                         Module *pModule, IRBuilder *pBuilder);

  static void initExternalFunction(Module *pModule, IRBuilder *pBuilder);
};

class SysYIRGenerator : public SysYBaseVisitor {

private:
  std::unique_ptr<Module> module;
  IRBuilder builder;

public:
  SysYIRGenerator() = default;

  bool HasReturnInst;

public:
  Module *get() const { return module.get(); }
  IRBuilder *getBuilder(){ return &builder; }
public:
  std::any visitCompUnit(SysYParser::CompUnitContext *ctx) override;

  std::any visitGlobalConstDecl(SysYParser::GlobalConstDeclContext *ctx) override;
  std::any visitGlobalVarDecl(SysYParser::GlobalVarDeclContext *ctx) override;

  // std::any visitDecl(SysYParser::DeclContext *ctx) override;
  std::any visitConstDecl(SysYParser::ConstDeclContext *ctx) override;
  std::any visitVarDecl(SysYParser::VarDeclContext *ctx) override;
  
  std::any visitBType(SysYParser::BTypeContext *ctx) override;
  
  // std::any visitConstDef(SysYParser::ConstDefContext *ctx) override;
  // std::any visitVarDef(SysYParser::VarDefContext *ctx) override;

  std::any visitScalarInitValue(SysYParser::ScalarInitValueContext *ctx) override;
  std::any visitArrayInitValue(SysYParser::ArrayInitValueContext *ctx) override;

  std::any visitConstScalarInitValue(SysYParser::ConstScalarInitValueContext *ctx) override;
  std::any visitConstArrayInitValue(SysYParser::ConstArrayInitValueContext *ctx) override;

  // std::any visitConstInitVal(SysYParser::ConstInitValContext *ctx) override;
  std::any visitFuncType(SysYParser::FuncTypeContext* ctx) override;
  std::any visitFuncDef(SysYParser::FuncDefContext* ctx) override;
  // std::any visitInitVal(SysYParser::InitValContext *ctx) override;
  // std::any visitFuncFParam(SysYParser::FuncFParamContext *ctx) override;
  // std::any visitFuncFParams(SysYParser::FuncFParamsContext *ctx) override;

  std::any visitBlockStmt(SysYParser::BlockStmtContext* ctx) override;
  // std::any visitStmt(SysYParser::StmtContext *ctx) override;
  std::any visitAssignStmt(SysYParser::AssignStmtContext *ctx) override;
  // std::any visitExpStmt(SysYParser::ExpStmtContext *ctx) override;
  // std::any visitBlkStmt(SysYParser::BlkStmtContext *ctx) override;
  std::any visitIfStmt(SysYParser::IfStmtContext *ctx) override;
  std::any visitWhileStmt(SysYParser::WhileStmtContext *ctx) override;
  std::any visitBreakStmt(SysYParser::BreakStmtContext *ctx) override;
  std::any visitContinueStmt(SysYParser::ContinueStmtContext *ctx) override;
  std::any visitReturnStmt(SysYParser::ReturnStmtContext *ctx) override;
  
  // std::any visitExp(SysYParser::ExpContext *ctx) override;
  // std::any visitCond(SysYParser::CondContext *ctx) override;

  std::any visitLValue(SysYParser::LValueContext *ctx) override;

  std::any visitPrimaryExp(SysYParser::PrimaryExpContext *ctx) override;
  
  // std::any visitParenExp(SysYParser::ParenExpContext *ctx) override;
  std::any visitNumber(SysYParser::NumberContext *ctx) override;
  // std::any visitString(SysYParser::StringContext *ctx) override;
  
  std::any visitCall(SysYParser::CallContext *ctx) override;
  
  std::any visitUnaryExp(SysYParser::UnaryExpContext *ctx) override;
  // std::any visitUnaryOp(SysYParser::UnaryOpContext *ctx) override;
  
  // std::any visitUnExp(SysYParser::UnExpContext *ctx) override;

  std::any visitFuncRParams(SysYParser::FuncRParamsContext *ctx) override;
  std::any visitMulExp(SysYParser::MulExpContext *ctx) override;
  std::any visitAddExp(SysYParser::AddExpContext *ctx) override;
  std::any visitRelExp(SysYParser::RelExpContext *ctx) override;
  std::any visitEqExp(SysYParser::EqExpContext *ctx) override;
  std::any visitLAndExp(SysYParser::LAndExpContext *ctx) override;
  std::any visitLOrExp(SysYParser::LOrExpContext *ctx) override;
  
  // std::any visitConstExp(SysYParser::ConstExpContext *ctx) override;


}; // class SysYIRGenerator

} // namespace sysy