111 lines
3.7 KiB
C++
111 lines
3.7 KiB
C++
#include "Environment.h"
|
|
#include "Parser.h"
|
|
|
|
#include <stdexcept>
|
|
|
|
namespace NVL::Environment {
|
|
Environment ENVIRONMENT;
|
|
Variable::Variable() : type(Type::Nil), length(0) { }
|
|
Variable::Variable(Type t) : type(t), length(0) {
|
|
if (t != Type::Nil)
|
|
throw std::runtime_error("Cannot create non-nil object with type alone");
|
|
}
|
|
Variable::Variable(const Number& v) : type(Type::Number), value(v), length(1) {}
|
|
Variable::Variable(const String& v) : type(Type::String), value(v), length(1) {}
|
|
Variable::Variable(const std::vector<Variable>& v) : type(Type::Array), value(v), length(v.size()) {}
|
|
Variable::Variable(const std::function < Variable(std::vector<Variable>)>& v, u32 l) : type(Type::Procedure), value(v), length(l) {}
|
|
|
|
bool Variable::operator==(const Variable& other) const {
|
|
if (other.type != type)
|
|
return false;
|
|
switch (type) {
|
|
case Type::Number:
|
|
return std::get<Number>(other.value) == std::get<Number>(value);
|
|
case Type::String:
|
|
return std::get<String>(other.value) == std::get<String>(value);
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
void Environment::enter(const String& name, Variable p) {
|
|
if (!env.emplace(name, p).second)
|
|
throw std::runtime_error("Redefinition of symbol in environment");
|
|
}
|
|
|
|
void Environment::enter(std::initializer_list<std::pair<String, Variable>> p) {
|
|
for (auto& i : p)
|
|
enter(i.first, i.second);
|
|
}
|
|
|
|
void Environment::set(const String& name, Variable p) {
|
|
if (env.find(name) == env.end())
|
|
throw std::runtime_error("Attempting to set undefined variable");
|
|
env[name] = p;
|
|
}
|
|
|
|
Variable& Environment::get(const String& name) {
|
|
return env.at(name);
|
|
}
|
|
|
|
bool Environment::exists(const String& name) {
|
|
return env.find(name) != env.end();
|
|
}
|
|
|
|
Variable Eval(const Parse::Object& obj) {
|
|
switch (obj.type) {
|
|
case Parse::Type::Symbol:
|
|
return std::get<String>(obj.value);
|
|
case Parse::Type::Number:
|
|
return Variable(std::get<Number>(obj.value));
|
|
case Parse::Type::String:
|
|
return Variable(std::get<String>(obj.value));
|
|
case Parse::Type::Array:
|
|
{
|
|
std::vector<Variable> v{};
|
|
for (const auto& x : std::get<std::vector<Parse::Object>>(obj.value))
|
|
v.push_back(Eval(x));
|
|
return Variable(v);
|
|
}
|
|
case Parse::Type::Subexpression:
|
|
{
|
|
return Apply(std::get<std::vector<Parse::Object>>(obj.value));
|
|
}
|
|
}
|
|
}
|
|
|
|
Variable Apply(const Parse::Command& c) {
|
|
std::vector<Variable> args{};
|
|
for (u32 i = 1; i < c.size(); i++)
|
|
args.push_back(Eval(c[i]));
|
|
return std::get<std::function<Variable(std::vector<Variable>)>>(ENVIRONMENT.get(std::get<String>(c[0].value)).value)(args);
|
|
}
|
|
|
|
void EvalScene(const Parse::Scene& s) {
|
|
for (const auto& c : s.get())
|
|
Apply(c);
|
|
}
|
|
|
|
// Check ParseMarkup for more information
|
|
Markup UnpackMarkupVariable(const Variable& m) {
|
|
const auto& range = std::get<std::vector<Variable>>(std::get<std::vector<Variable>>(m.value)[0].value);
|
|
const u32 begin = std::get<Number>(range[0].value), end = std::get<Number>(range[1].value);
|
|
|
|
std::vector<std::pair<String, std::vector<String>>> efs;
|
|
for (const Variable& combination : std::get<std::vector<Variable>>(std::get<std::vector<Variable>>(m.value)[1].value)) {
|
|
if (combination.type == Type::String) {
|
|
const String ef = std::get<String>(combination.value);
|
|
efs.push_back({ ef, {} });
|
|
}
|
|
else if (combination.type == Type::Array) {
|
|
const std::vector<Variable>& ef_t = std::get<std::vector<Variable>>(combination.value);
|
|
const String& ef = std::get<String>(ef_t[0].value);
|
|
const std::vector<Variable>& args = std::get<std::vector<Variable>>(ef_t[1].value);
|
|
std::vector<String> ss{};
|
|
for (const auto& s : args) { ss.push_back(std::get<String>(s.value)); }
|
|
efs.push_back({ ef, ss });
|
|
}
|
|
}
|
|
return { begin, end, efs };
|
|
}
|
|
}
|