// 你需要提交这份代码
#pragma once

#include "visitor.h"

struct calculator : visitor
{
  /// TODO: 完成所有需求
  ~calculator() override = default;
  std::any visit_num(num_node *ctx) override
  {
    // 处理 num_node 节点的逻辑
    long long *p_long_long = std::any_cast<long long>(&ctx->number);
    double *p_double = std::any_cast<double>(&ctx->number);
    std::any res;
    if (p_long_long != nullptr)
      res = std::any((long long)*p_long_long);
    else if (p_double != nullptr)
      res = std::any((double)*p_double);
    else
      res = std::any((long long)0);
    return res;
  }

  std::any visit_add(add_node *ctx) override
  {
    // 处理 add_node 节点的逻辑
    std::any l_res = this->visit(ctx->lnode);
    std::any r_res = this->visit(ctx->rnode);
    long long *p_l_long_long = std::any_cast<long long>(&l_res);
    long long *p_r_long_long = std::any_cast<long long>(&r_res);
    double *p_l_double = std::any_cast<double>(&l_res);
    double *p_r_double = std::any_cast<double>(&r_res);
    if((p_l_long_long!=nullptr)&&(p_r_long_long!=nullptr))
      return std::any((long long)(*p_l_long_long + *p_r_long_long));
    else if((p_l_double!=nullptr)&&(p_r_double!=nullptr))
      return std::any((double)(*p_l_double + *p_r_double));
    else if((p_l_long_long!=nullptr)&&(p_r_double!=nullptr))
      return std::any((double)(*p_l_long_long + *p_r_double));
    else if((p_l_double!=nullptr)&&(p_r_long_long!=nullptr))
      return std::any((double)(*p_l_double + *p_r_long_long));
    else
      return std::any((long long)0);
  }

  std::any visit_sub(sub_node *ctx) override
  {
    // 处理 sub_node 节点的逻辑
    std::any l_res = this->visit(ctx->lnode);
    std::any r_res = this->visit(ctx->rnode);
    long long *p_l_long_long = std::any_cast<long long>(&l_res);
    long long *p_r_long_long = std::any_cast<long long>(&r_res);
    double *p_l_double = std::any_cast<double>(&l_res);
    double *p_r_double = std::any_cast<double>(&r_res);
    if((p_l_long_long!=nullptr)&&(p_r_long_long!=nullptr))
      return std::any((long long)(*p_l_long_long - *p_r_long_long));
    else if((p_l_double!=nullptr)&&(p_r_double!=nullptr))
      return std::any((double)(*p_l_double - *p_r_double));
    else if((p_l_long_long!=nullptr)&&(p_r_double!=nullptr))
      return std::any((double)(*p_l_long_long - *p_r_double));
    else if((p_l_double!=nullptr)&&(p_r_long_long!=nullptr))
      return std::any((double)(*p_l_double - *p_r_long_long));
    else
      return std::any((long long)0);
  }

  std::any visit_mul(mul_node *ctx) override
  {
    // 处理 mul_node 节点的逻辑
    std::any l_res = this->visit(ctx->lnode);
    std::any r_res = this->visit(ctx->rnode);
    long long *p_l_long_long = std::any_cast<long long>(&l_res);
    long long *p_r_long_long = std::any_cast<long long>(&r_res);
    double *p_l_double = std::any_cast<double>(&l_res);
    double *p_r_double = std::any_cast<double>(&r_res);
    if((p_l_long_long!=nullptr)&&(p_r_long_long!=nullptr))
      return std::any((long long)(*p_l_long_long * *p_r_long_long));
    else if((p_l_double!=nullptr)&&(p_r_double!=nullptr))
      return std::any((double)(*p_l_double * *p_r_double));
    else if((p_l_long_long!=nullptr)&&(p_r_double!=nullptr))
      return std::any((double)(*p_l_long_long * *p_r_double));
    else if((p_l_double!=nullptr)&&(p_r_long_long!=nullptr))
      return std::any((double)(*p_l_double * *p_r_long_long));
    else
      return std::any((long long)0);
  }

  std::any visit_div(div_node *ctx) override
  {
    // 处理 div_node 节点的逻辑
    std::any l_res = this->visit(ctx->lnode);
    std::any r_res = this->visit(ctx->rnode);
    long long *p_l_long_long = std::any_cast<long long>(&l_res);
    long long *p_r_long_long = std::any_cast<long long>(&r_res);
    double *p_l_double = std::any_cast<double>(&l_res);
    double *p_r_double = std::any_cast<double>(&r_res);
    if((p_l_long_long!=nullptr)&&(p_r_long_long!=nullptr))
      return std::any((long long)(*p_l_long_long / *p_r_long_long));
    else if((p_l_double!=nullptr)&&(p_r_double!=nullptr))
      return std::any((double)(*p_l_double / *p_r_double));
    else if((p_l_long_long!=nullptr)&&(p_r_double!=nullptr))
      return std::any((double)(*p_l_long_long / *p_r_double));
    else if((p_l_double!=nullptr)&&(p_r_long_long!=nullptr))
      return std::any((double)(*p_l_double / *p_r_long_long));
    else
      return std::any((long long)0);
  }
};