NouVeL/NVL/Environment.h

41 lines
1.2 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 {
2021-12-12 23:29:53 -05:00
enum class Type { Procedure, Number, String, Array, Nil };
2021-12-12 22:20:28 -05:00
struct Variable {
Type type;
2022-08-18 12:17:43 -04:00
std::variant<Number, String, std::vector<Variable>, std::function<Variable(std::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
2021-12-12 22:20:28 -05:00
Variable(const Number& v);
Variable(const String& v);
2021-12-12 22:20:28 -05:00
Variable(const std::vector<Variable>& v);
2022-08-18 12:17:43 -04:00
Variable(const std::function<Variable(std::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);
void set(const String& name, Variable p);
Variable& get(const String& name);
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);
2021-12-13 14:04:12 -05:00
}