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;
|
2021-12-12 23:29:53 -05:00
|
|
|
std::variant<Number, std::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
|
2021-12-12 22:20:28 -05:00
|
|
|
int length;
|
|
|
|
bool operator==(const Variable& other) const;
|
|
|
|
Variable();
|
2021-12-12 23:29:53 -05:00
|
|
|
Variable(Type t);
|
2021-12-12 22:20:28 -05:00
|
|
|
Variable(const Number& v);
|
|
|
|
Variable(const std::string& v);
|
|
|
|
Variable(const std::vector<Variable>& v);
|
2021-12-17 01:05:38 -05:00
|
|
|
Variable(const std::function<Variable(std::vector<Variable>)>& v, int l);
|
2021-12-12 22:20:28 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class Environment {
|
|
|
|
private:
|
|
|
|
std::unordered_map<std::string, Variable> env{};
|
|
|
|
public:
|
|
|
|
void enter(const std::string& name, Variable p);
|
|
|
|
void set(const std::string& name, Variable p);
|
2021-12-17 01:05:38 -05:00
|
|
|
Variable& get(const std::string& name);
|
2021-12-12 22:20:28 -05:00
|
|
|
bool exists(const std::string& name);
|
|
|
|
};
|
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
|
|
|
}
|