<commit_before>#pragma once

#include <vector>
#include "common.hh"
#include "value.hh"

namespace tadpole {

enum class Code : u8_t {
  CONSTANT,

  NIL,
  FALSE,
  TRUE,
  POP,

  DEF_GLOBAL,
  GET_GLOBAL,
  SET_GLOBAL,
  GET_LOCAL,
  SET_LOCAL,
  GET_UPVALUE,
  SET_UPVALUE,

  ADD,
  SUB,
  MUL,
  DIV,

  CALL_0,
  CALL_1,
  CALL_2,
  CALL_3,
  CALL_4,
  CALL_5,
  CALL_6,
  CALL_7,
  CALL_8,

  CLOSURE,

  CLOSE_UPVALUE,

  RETURN,
};

inline int operator-(Code a, Code b) noexcept {
  return as_type<int>(a) - as_type<int>(b);
}

class Chunk final : private UnCopyable {
  std::vector<u8_t> codes_;
  std::vector<int> lines_;
  std::vector<Value> constants_;
public:
  template <typename T> inline u8_t write(T c, int lineno) noexcept {
    codes_.push_back(as_type<u8_t>(c));
    lines_.push_back(lineno);
    return as_type<u8_t>(codes_.size() - 1);
  }

  inline u8_t add_constant(Value value) noexcept {
    constants_.push_back(value);
    return as_type<u8_t>(constants_.size() - 1);
  }

  inline void write_constant(Value value, int lineno) noexcept {
    write(Code::CONSTANT, lineno);
    write(add_constant(value), lineno);
  }

  inline sz_t codes_count() const noexcept { return codes_.size(); }
  inline const u8_t* codes() const noexcept { return codes_.data(); }<commit_msg>:construction: chore(code): add operation support for Code<commit_after>#pragma once

#include <vector>
#include "common.hh"
#include "value.hh"

namespace tadpole {

enum class Code : u8_t {
  CONSTANT,

  NIL,
  FALSE,
  TRUE,
  POP,

  DEF_GLOBAL,
  GET_GLOBAL,
  SET_GLOBAL,
  GET_LOCAL,
  SET_LOCAL,
  GET_UPVALUE,
  SET_UPVALUE,

  ADD,
  SUB,
  MUL,
  DIV,

  CALL_0,
  CALL_1,
  CALL_2,
  CALL_3,
  CALL_4,
  CALL_5,
  CALL_6,
  CALL_7,
  CALL_8,

  CLOSURE,

  CLOSE_UPVALUE,

  RETURN,
};

inline int operator-(Code a, Code b) noexcept {
  return as_type<int>(a) - as_type<int>(b);
}

template <typename T> inline Code operator+(Code a, T b) noexcept {
  return as_type<Code>(as_type<int>(a) + as_type<int>(b));
}

class Chunk final : private UnCopyable {
  std::vector<u8_t> codes_;
  std::vector<int> lines_;
  std::vector<Value> constants_;
public:
  template <typename T> inline u8_t write(T c, int lineno) noexcept {
    codes_.push_back(as_type<u8_t>(c));
    lines_.push_back(lineno);
    return as_type<u8_t>(codes_.size() - 1);
  }

  inline u8_t add_constant(Value value) noexcept {
    constants_.push_back(value);
    return as_type<u8_t>(constants_.size() - 1);
  }

  inline void write_constant(Value value, int lineno) noexcept {
    write(Code::CONSTANT, lineno);
    write(add_constant(value), lineno);
  }

  inline sz_t codes_count() const noexcept { return codes_.size(); }
  inline const u8_t* codes() const noexcept { return codes_.data(); }
{'fraction_non_alphanumeric': 0.1259473114399134, 'fraction_numerical': 0.013713460844460484, 'mean_word_length': 3.8975265017667846, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '7526683', 'n_tokens_mistral': 1136, 'n_tokens_neox': 1092, 'n_words': 247}