NouVeL/NVL/Environment.h

55 lines
1.6 KiB
C++

#pragma once
#include <unordered_map>
#include <functional>
#include <variant>
#include "Parser.h"
#include "Common.h"
namespace NVL::Environment {
enum class Type { Nil, StaticReference, Procedure, Number, String, Array };
struct Variable {
Type type;
std::variant<Number, String, Vector<Variable>, std::function<Variable(Vector<Variable>)>> value;
// Most things will have length 1 (including string); this is mostly for function arity and array length
u32 length;
bool operator==(const Variable& other) const;
Variable(); // Makes Nil, length 0
Variable(Type t); // Reserved for Nil
Variable(const Number& v, bool is_ref = false);
Variable(const String& v);
Variable(const Vector<Variable>& v);
Variable(const std::function<Variable(Vector<Variable>)>& v, u32 l);
};
class Environment {
private:
std::unordered_map<String, Variable> env{};
public:
void enter(const String& name, Variable p);
void enter(std::initializer_list<std::pair<String, Variable>> p);
void set(const String& name, Variable p);
const Variable& get(const String& name) const;
bool exists(const String& name);
};
extern Environment ENVIRONMENT;
Variable Eval(const Parse::Object& obj);
Variable Apply(const Parse::Command& c);
void EvalScene(const Parse::Scene & s);
struct MarkupSegment {
String str;
Vector<std::pair<String, Vector<String>>> efs;
};
struct MarkupString {
private:
mutable i32 mem_length = -1;
public:
Vector<MarkupSegment> segments{};
size_t length() const;
MarkupString substr(size_t idx) const;
};
MarkupString UnpackMarkupVariable(const Vector<Variable>& v);
}