Refactor runtime.h, moved definitions to .cpp

This commit is contained in:
2026-01-23 00:58:38 +03:00
parent 6f7b5c2fe6
commit 9f16142bec
3 changed files with 125 additions and 91 deletions

111
src/backend/runtime.cpp Normal file
View File

@@ -0,0 +1,111 @@
#include "../../includes/expr-eval/backend/runtime.h"
#include <stdexcept>
RuntimeVar::RuntimeVar()
: type(RuntimeVarType::NIL),
value("nil") {
}
RuntimeVar::RuntimeVar(std::string v)
: type(RuntimeVarType::STRING),
value(std::move(v)) {
}
RuntimeVar::RuntimeVar(const bool t)
: type(RuntimeVarType::BOOL),
value(t ? "true" : "false"),
b_value(t) {
}
RuntimeVar::RuntimeVar(const double d)
: type(RuntimeVarType::NUMBER),
value(std::format("{}", d)),
d_value(d) {
}
std::string RuntimeVar::typeStr() const {
switch (type) {
case RuntimeVarType::STRING:
return "string";
case RuntimeVarType::NUMBER:
return "number";
case RuntimeVarType::NIL:
return "nil";
case RuntimeVarType::BOOL:
return "bool";
default: return "undefined";
}
}
double RuntimeVar::toDouble() const {
if (type == RuntimeVarType::NUMBER)
return stod(value);
throw std::runtime_error("Expected number type");
}
std::string RuntimeVar::toString() const {
return value;
}
bool RuntimeVar::toBool() const {
if (value == "false" || value == "nil") return false;
if (type == RuntimeVarType::NUMBER) return toDouble() != 0.0f;
return true;
}
RuntimeVar RuntimeVar::operator+(const RuntimeVar &other) const {
if (type != other.type)
throw std::runtime_error(std::format("Expected same types to op '+' but found {} and {}.",
typeStr(), other.typeStr()));
if (type == RuntimeVarType::STRING) {
return RuntimeVar{value + other.value};
}
if (type == RuntimeVarType::NUMBER) {
const auto res = toDouble() + other.toDouble();
return RuntimeVar{res};
}
throw std::runtime_error(std::format("Op '+' not supported for type {}", typeStr()));
}
RuntimeVar RuntimeVar::operator-(const RuntimeVar &other) const {
if (type != other.type)
throw std::runtime_error(std::format("Expected same types to op '+' but found {} and {}.",
typeStr(), other.typeStr()));
if (type == RuntimeVarType::NUMBER) {
const auto res = toDouble() - other.toDouble();
return RuntimeVar{res};
}
throw std::runtime_error(std::format("Op '+' not supported for type {}", typeStr()));
}
RuntimeVar RuntimeVar::operator*(const RuntimeVar &other) const {
if (type != other.type)
throw std::runtime_error(std::format("Expected same types to op '+' but found {} and {}.",
typeStr(), other.typeStr()));
if (type == RuntimeVarType::NUMBER) {
const auto res = toDouble() * other.toDouble();
return RuntimeVar{res};
}
throw std::runtime_error(std::format("Op '+' not supported for type {}", typeStr()));
}
RuntimeVar RuntimeVar::operator/(const RuntimeVar &other) const {
if (type != other.type)
throw std::runtime_error(std::format("Expected same types to op '+' but found {} and {}.",
typeStr(), other.typeStr()));
if (type == RuntimeVarType::NUMBER) {
const auto res = toDouble() / other.toDouble();
return RuntimeVar{res};
}
throw std::runtime_error(std::format("Op '+' not supported for type {}", typeStr()));
}