<commit_before>#include "eval.h"

#include <glob.h>

#include "ast.h"
#include "file.h"
#include "file_cache.h"
#include "rule.h"
#include "strutil.h"
#include "value.h"
#include "var.h"

EvalResult::~EvalResult() {
  for (Rule* r : rules)
    delete r;
  for (auto p : rule_vars)
    delete p.second;
  delete vars;
}

Evaluator::Evaluator(const Vars* vars)
    : in_vars_(vars),
      vars_(new Vars()),
      last_rule_(NULL) {
}

Evaluator::~Evaluator() {
  for (Rule* r : rules_) {
    delete r;
  }
  delete vars_;
  // for (auto p : rule_vars) {
  //   delete p.second;
  // }
}

void Evaluator::EvalAssign(const AssignAST* ast) {
  loc_ = ast->loc();
  last_rule_ = NULL;

  const char* origin = "file";

  StringPiece lhs = Intern(*ast->lhs->Eval(this));
  Var* rhs = NULL;
  bool needs_assign = true;
  switch (ast->op) {
    case AssignOp::COLON_EQ:
      rhs = new SimpleVar(ast->rhs->Eval(this), origin);
      break;
    case AssignOp::EQ:
      rhs = new RecursiveVar(ast->rhs, origin);
      break;
    case AssignOp::PLUS_EQ: {
      Var* prev = LookupVarInCurrentScope(lhs);
      if (!prev->IsDefined()) {
        rhs = new RecursiveVar(ast->rhs, origin);
      } else {
        prev->AppendVar(this, ast->rhs);
        rhs = prev;
        needs_assign = false;
      }
      break;
    }
    case AssignOp::QUESTION_EQ: {
      Var* prev = LookupVarInCurrentScope(lhs);
      if (!prev->IsDefined()) {
        rhs = new RecursiveVar(ast->rhs, origin);
      } else {
        // TODO
        abort();
      }
      break;
    }
  }

  LOG("Assign: %.*s=%s", SPF(lhs), rhs->DebugString().c_str());
  if (needs_assign)
    vars_->Assign(lhs, rhs);
}

void Evaluator::EvalRule(const RuleAST* ast) {
  loc_ = ast->loc();<commit_msg>[C++] Fix assign_types.mk<commit_after>#include "eval.h"

#include <glob.h>

#include "ast.h"
#include "file.h"
#include "file_cache.h"
#include "rule.h"
#include "strutil.h"
#include "value.h"
#include "var.h"

EvalResult::~EvalResult() {
  for (Rule* r : rules)
    delete r;
  for (auto p : rule_vars)
    delete p.second;
  delete vars;
}

Evaluator::Evaluator(const Vars* vars)
    : in_vars_(vars),
      vars_(new Vars()),
      last_rule_(NULL) {
}

Evaluator::~Evaluator() {
  for (Rule* r : rules_) {
    delete r;
  }
  delete vars_;
  // for (auto p : rule_vars) {
  //   delete p.second;
  // }
}

void Evaluator::EvalAssign(const AssignAST* ast) {
  loc_ = ast->loc();
  last_rule_ = NULL;

  const char* origin = "file";

  StringPiece lhs = Intern(*ast->lhs->Eval(this));
  Var* rhs = NULL;
  bool needs_assign = true;
  switch (ast->op) {
    case AssignOp::COLON_EQ:
      rhs = new SimpleVar(ast->rhs->Eval(this), origin);
      break;
    case AssignOp::EQ:
      rhs = new RecursiveVar(ast->rhs, origin);
      break;
    case AssignOp::PLUS_EQ: {
      Var* prev = LookupVarInCurrentScope(lhs);
      if (!prev->IsDefined()) {
        rhs = new RecursiveVar(ast->rhs, origin);
      } else {
        prev->AppendVar(this, ast->rhs);
        rhs = prev;
        needs_assign = false;
      }
      break;
    }
    case AssignOp::QUESTION_EQ: {
      Var* prev = LookupVarInCurrentScope(lhs);
      if (!prev->IsDefined()) {
        rhs = new RecursiveVar(ast->rhs, origin);
      } else {
        rhs = prev;
        needs_assign = false;
      }
      break;
    }
  }

  LOG("Assign: %.*s=%s", SPF(lhs), rhs->DebugString().c_str());
  if (needs_assign)
    vars_->Assign(lhs, rhs);
}

void Evaluator::EvalRule(const RuleAST* ast) {
  loc_ = ast->loc();
{'fraction_non_alphanumeric': 0.16463241555492478, 'fraction_numerical': 0.0, 'mean_word_length': 2.6900523560209426, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3235775', 'n_tokens_mistral': 1321, 'n_tokens_neox': 1335, 'n_words': 283}