NouVeL/NVL/Environment.h

56 lines
1.6 KiB
C
Raw Normal View History

2021-12-12 22:20:28 -05:00
#pragma once
#include <unordered_map>
#include <functional>
#include <variant>
2021-12-17 01:05:38 -05:00
#include "Parser.h"
#include "Common.h"
2021-12-12 22:20:28 -05:00
2021-12-17 01:05:38 -05:00
namespace NVL::Environment {
enum class Type { Nil, StaticReference, Procedure, Number, String, Array };
2021-12-12 22:20:28 -05:00
struct Variable {
Type type;
2023-04-07 16:04:30 -04:00
std::variant<Number, String, Vector<Variable>, std::function<Variable(Vector<Variable>)>> value;
2021-12-17 01:05:38 -05:00
// Most things will have length 1 (including string); this is mostly for function arity and array length
2022-08-18 12:17:43 -04:00
u32 length;
2021-12-12 22:20:28 -05:00
bool operator==(const Variable& other) const;
2022-08-18 12:17:43 -04:00
Variable(); // Makes Nil, length 0
Variable(Type t); // Reserved for Nil
Variable(const Number& v, bool is_ref = false);
Variable(const String& v);
2023-04-07 16:04:30 -04:00
Variable(const Vector<Variable>& v);
Variable(const std::function<Variable(Vector<Variable>)>& v, u32 l);
2021-12-12 22:20:28 -05:00
};
class Environment {
private:
std::unordered_map<String, Variable> env{};
2021-12-12 22:20:28 -05:00
public:
void enter(const String& name, Variable p);
2022-08-27 04:40:06 -04:00
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);
2021-12-12 22:20:28 -05:00
};
2021-12-17 01:05:38 -05:00
2021-12-12 22:20:28 -05:00
extern Environment ENVIRONMENT;
2021-12-17 01:05:38 -05:00
Variable Eval(const Parse::Object& obj);
Variable Apply(const Parse::Command& c);
void EvalScene(const Parse::Scene & s);
2022-08-22 02:15:25 -04:00
struct MarkupSegment {
String str;
2023-04-07 16:04:30 -04:00
Vector<std::pair<String, Vector<String>>> efs;
2022-08-23 18:02:15 -04:00
};
struct MarkupString {
2022-08-28 20:54:30 -04:00
private:
mutable i32 mem_length = -1;
public:
2023-04-07 16:04:30 -04:00
Vector<MarkupSegment> segments{};
size_t length() const;
MarkupString substr(size_t idx) const;
};
2023-04-07 16:04:30 -04:00
MarkupString UnpackMarkupVariable(const Vector<Variable>& v);
2021-12-13 14:04:12 -05:00
}