Everywhere: Rename Value -> Constant, ASTNode -> Value
This commit is contained in:
+6
-6
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
void Collection::add(ASTNodePtr node)
|
void Collection::add(ValuePtr node)
|
||||||
{
|
{
|
||||||
if (node == nullptr) {
|
if (node == nullptr) {
|
||||||
return;
|
return;
|
||||||
@@ -27,7 +27,7 @@ void Collection::add(ASTNodePtr node)
|
|||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|
||||||
void HashMap::add(const std::string& key, ASTNodePtr value)
|
void HashMap::add(const std::string& key, ValuePtr value)
|
||||||
{
|
{
|
||||||
if (value == nullptr) {
|
if (value == nullptr) {
|
||||||
return;
|
return;
|
||||||
@@ -66,7 +66,7 @@ Symbol::Symbol(const std::string& symbol)
|
|||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|
||||||
Value::Value(State state)
|
Constant::Constant(State state)
|
||||||
: m_state(state)
|
: m_state(state)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -81,7 +81,7 @@ Function::Function(const std::string& name, FunctionType function)
|
|||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|
||||||
Lambda::Lambda(std::vector<std::string> bindings, ASTNodePtr body, EnvironmentPtr env)
|
Lambda::Lambda(std::vector<std::string> bindings, ValuePtr body, EnvironmentPtr env)
|
||||||
: m_bindings(bindings)
|
: m_bindings(bindings)
|
||||||
, m_body(body)
|
, m_body(body)
|
||||||
, m_env(env)
|
, m_env(env)
|
||||||
@@ -90,7 +90,7 @@ Lambda::Lambda(std::vector<std::string> bindings, ASTNodePtr body, EnvironmentPt
|
|||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|
||||||
Atom::Atom(ASTNodePtr pointer)
|
Atom::Atom(ValuePtr pointer)
|
||||||
: m_value(pointer)
|
: m_value(pointer)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -99,7 +99,7 @@ Atom::Atom(ASTNodePtr pointer)
|
|||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|
||||||
void Formatter<blaze::ASTNodePtr>::format(Builder& builder, blaze::ASTNodePtr value) const
|
void Formatter<blaze::ValuePtr>::format(Builder& builder, blaze::ValuePtr value) const
|
||||||
{
|
{
|
||||||
blaze::Printer printer;
|
blaze::Printer printer;
|
||||||
return Formatter<std::string>::format(builder, printer.printNoErrorCheck(value));
|
return Formatter<std::string>::format(builder, printer.printNoErrorCheck(value));
|
||||||
|
|||||||
@@ -23,9 +23,9 @@
|
|||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
class ASTNode {
|
class Value {
|
||||||
public:
|
public:
|
||||||
virtual ~ASTNode() = default;
|
virtual ~Value() = default;
|
||||||
|
|
||||||
std::string className() const { return typeid(*this).name(); }
|
std::string className() const { return typeid(*this).name(); }
|
||||||
|
|
||||||
@@ -39,7 +39,7 @@ public:
|
|||||||
virtual bool isString() const { return false; }
|
virtual bool isString() const { return false; }
|
||||||
virtual bool isKeyword() const { return false; }
|
virtual bool isKeyword() const { return false; }
|
||||||
virtual bool isNumber() const { return false; }
|
virtual bool isNumber() const { return false; }
|
||||||
virtual bool isValue() const { return false; }
|
virtual bool isConstant() const { return false; }
|
||||||
virtual bool isSymbol() const { return false; }
|
virtual bool isSymbol() const { return false; }
|
||||||
virtual bool isCallable() const { return false; }
|
virtual bool isCallable() const { return false; }
|
||||||
virtual bool isFunction() const { return false; }
|
virtual bool isFunction() const { return false; }
|
||||||
@@ -47,18 +47,18 @@ public:
|
|||||||
virtual bool isAtom() const { return false; }
|
virtual bool isAtom() const { return false; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
ASTNode() {}
|
Value() {}
|
||||||
};
|
};
|
||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|
||||||
class Collection : public ASTNode {
|
class Collection : public Value {
|
||||||
public:
|
public:
|
||||||
virtual ~Collection() = default;
|
virtual ~Collection() = default;
|
||||||
|
|
||||||
void add(ASTNodePtr node);
|
void add(ValuePtr node);
|
||||||
|
|
||||||
const std::list<ASTNodePtr>& nodes() const { return m_nodes; }
|
const std::list<ValuePtr>& nodes() const { return m_nodes; }
|
||||||
size_t size() const { return m_nodes.size(); }
|
size_t size() const { return m_nodes.size(); }
|
||||||
bool empty() const { return m_nodes.size() == 0; }
|
bool empty() const { return m_nodes.size() == 0; }
|
||||||
|
|
||||||
@@ -68,7 +68,7 @@ protected:
|
|||||||
private:
|
private:
|
||||||
virtual bool isCollection() const override { return true; }
|
virtual bool isCollection() const override { return true; }
|
||||||
|
|
||||||
std::list<ASTNodePtr> m_nodes;
|
std::list<ValuePtr> m_nodes;
|
||||||
};
|
};
|
||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
@@ -98,27 +98,27 @@ private:
|
|||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|
||||||
// {}
|
// {}
|
||||||
class HashMap final : public ASTNode {
|
class HashMap final : public Value {
|
||||||
public:
|
public:
|
||||||
HashMap() = default;
|
HashMap() = default;
|
||||||
virtual ~HashMap() = default;
|
virtual ~HashMap() = default;
|
||||||
|
|
||||||
void add(const std::string& key, ASTNodePtr value);
|
void add(const std::string& key, ValuePtr value);
|
||||||
|
|
||||||
const std::unordered_map<std::string, ASTNodePtr>& elements() const { return m_elements; }
|
const std::unordered_map<std::string, ValuePtr>& elements() const { return m_elements; }
|
||||||
size_t size() const { return m_elements.size(); }
|
size_t size() const { return m_elements.size(); }
|
||||||
bool empty() const { return m_elements.size() == 0; }
|
bool empty() const { return m_elements.size() == 0; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual bool isHashMap() const override { return true; }
|
virtual bool isHashMap() const override { return true; }
|
||||||
|
|
||||||
std::unordered_map<std::string, ASTNodePtr> m_elements;
|
std::unordered_map<std::string, ValuePtr> m_elements;
|
||||||
};
|
};
|
||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|
||||||
// "string"
|
// "string"
|
||||||
class String final : public ASTNode {
|
class String final : public Value {
|
||||||
public:
|
public:
|
||||||
String(const std::string& data);
|
String(const std::string& data);
|
||||||
virtual ~String() = default;
|
virtual ~String() = default;
|
||||||
@@ -134,7 +134,7 @@ private:
|
|||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|
||||||
// :keyword
|
// :keyword
|
||||||
class Keyword final : public ASTNode {
|
class Keyword final : public Value {
|
||||||
public:
|
public:
|
||||||
Keyword(const std::string& data);
|
Keyword(const std::string& data);
|
||||||
virtual ~Keyword() = default;
|
virtual ~Keyword() = default;
|
||||||
@@ -149,7 +149,7 @@ private:
|
|||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
// 123
|
// 123
|
||||||
class Number final : public ASTNode {
|
class Number final : public Value {
|
||||||
public:
|
public:
|
||||||
Number(int64_t number);
|
Number(int64_t number);
|
||||||
virtual ~Number() = default;
|
virtual ~Number() = default;
|
||||||
@@ -165,7 +165,7 @@ private:
|
|||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|
||||||
// true, false, nil
|
// true, false, nil
|
||||||
class Value final : public ASTNode {
|
class Constant final : public Value {
|
||||||
public:
|
public:
|
||||||
enum State : uint8_t {
|
enum State : uint8_t {
|
||||||
Nil,
|
Nil,
|
||||||
@@ -173,13 +173,13 @@ public:
|
|||||||
False,
|
False,
|
||||||
};
|
};
|
||||||
|
|
||||||
Value(State state);
|
Constant(State state);
|
||||||
virtual ~Value() = default;
|
virtual ~Constant() = default;
|
||||||
|
|
||||||
State state() const { return m_state; }
|
State state() const { return m_state; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual bool isValue() const override { return true; }
|
virtual bool isConstant() const override { return true; }
|
||||||
|
|
||||||
const State m_state;
|
const State m_state;
|
||||||
};
|
};
|
||||||
@@ -187,7 +187,7 @@ private:
|
|||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|
||||||
// Symbols
|
// Symbols
|
||||||
class Symbol final : public ASTNode {
|
class Symbol final : public Value {
|
||||||
public:
|
public:
|
||||||
Symbol(const std::string& symbol);
|
Symbol(const std::string& symbol);
|
||||||
virtual ~Symbol() = default;
|
virtual ~Symbol() = default;
|
||||||
@@ -202,7 +202,7 @@ private:
|
|||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|
||||||
class Callable : public ASTNode {
|
class Callable : public Value {
|
||||||
public:
|
public:
|
||||||
virtual ~Callable() = default;
|
virtual ~Callable() = default;
|
||||||
|
|
||||||
@@ -215,7 +215,7 @@ private:
|
|||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|
||||||
using FunctionType = std::function<ASTNodePtr(std::list<ASTNodePtr>)>;
|
using FunctionType = std::function<ValuePtr(std::list<ValuePtr>)>;
|
||||||
|
|
||||||
class Function final : public Callable {
|
class Function final : public Callable {
|
||||||
public:
|
public:
|
||||||
@@ -236,36 +236,36 @@ private:
|
|||||||
|
|
||||||
class Lambda final : public Callable {
|
class Lambda final : public Callable {
|
||||||
public:
|
public:
|
||||||
Lambda(std::vector<std::string> bindings, ASTNodePtr body, EnvironmentPtr env);
|
Lambda(std::vector<std::string> bindings, ValuePtr body, EnvironmentPtr env);
|
||||||
virtual ~Lambda() = default;
|
virtual ~Lambda() = default;
|
||||||
|
|
||||||
std::vector<std::string> bindings() const { return m_bindings; }
|
std::vector<std::string> bindings() const { return m_bindings; }
|
||||||
ASTNodePtr body() const { return m_body; }
|
ValuePtr body() const { return m_body; }
|
||||||
EnvironmentPtr env() const { return m_env; }
|
EnvironmentPtr env() const { return m_env; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual bool isLambda() const override { return true; }
|
virtual bool isLambda() const override { return true; }
|
||||||
|
|
||||||
const std::vector<std::string> m_bindings;
|
const std::vector<std::string> m_bindings;
|
||||||
const ASTNodePtr m_body;
|
const ValuePtr m_body;
|
||||||
const EnvironmentPtr m_env;
|
const EnvironmentPtr m_env;
|
||||||
};
|
};
|
||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|
||||||
class Atom final : public ASTNode {
|
class Atom final : public Value {
|
||||||
public:
|
public:
|
||||||
Atom() = default;
|
Atom() = default;
|
||||||
Atom(ASTNodePtr pointer);
|
Atom(ValuePtr pointer);
|
||||||
virtual ~Atom() = default;
|
virtual ~Atom() = default;
|
||||||
|
|
||||||
ASTNodePtr reset(ASTNodePtr value) { return m_value = value; }
|
ValuePtr reset(ValuePtr value) { return m_value = value; }
|
||||||
ASTNodePtr deref() const { return m_value; }
|
ValuePtr deref() const { return m_value; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual bool isAtom() const override { return true; }
|
virtual bool isAtom() const override { return true; }
|
||||||
|
|
||||||
ASTNodePtr m_value;
|
ValuePtr m_value;
|
||||||
};
|
};
|
||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
@@ -280,43 +280,43 @@ std::shared_ptr<T> makePtr(Args&&... args)
|
|||||||
|
|
||||||
// clang-format off
|
// clang-format off
|
||||||
template<>
|
template<>
|
||||||
inline bool ASTNode::fastIs<Collection>() const { return isCollection(); }
|
inline bool Value::fastIs<Collection>() const { return isCollection(); }
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool ASTNode::fastIs<List>() const { return isList(); }
|
inline bool Value::fastIs<List>() const { return isList(); }
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool ASTNode::fastIs<Vector>() const { return isVector(); }
|
inline bool Value::fastIs<Vector>() const { return isVector(); }
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool ASTNode::fastIs<HashMap>() const { return isHashMap(); }
|
inline bool Value::fastIs<HashMap>() const { return isHashMap(); }
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool ASTNode::fastIs<String>() const { return isString(); }
|
inline bool Value::fastIs<String>() const { return isString(); }
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool ASTNode::fastIs<Keyword>() const { return isKeyword(); }
|
inline bool Value::fastIs<Keyword>() const { return isKeyword(); }
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool ASTNode::fastIs<Number>() const { return isNumber(); }
|
inline bool Value::fastIs<Number>() const { return isNumber(); }
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool ASTNode::fastIs<Value>() const { return isValue(); }
|
inline bool Value::fastIs<Constant>() const { return isConstant(); }
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool ASTNode::fastIs<Symbol>() const { return isSymbol(); }
|
inline bool Value::fastIs<Symbol>() const { return isSymbol(); }
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool ASTNode::fastIs<Callable>() const { return isCallable(); }
|
inline bool Value::fastIs<Callable>() const { return isCallable(); }
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool ASTNode::fastIs<Function>() const { return isFunction(); }
|
inline bool Value::fastIs<Function>() const { return isFunction(); }
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool ASTNode::fastIs<Lambda>() const { return isLambda(); }
|
inline bool Value::fastIs<Lambda>() const { return isLambda(); }
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool ASTNode::fastIs<Atom>() const { return isAtom(); }
|
inline bool Value::fastIs<Atom>() const { return isAtom(); }
|
||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
} // namespace blaze
|
} // namespace blaze
|
||||||
@@ -324,6 +324,6 @@ inline bool ASTNode::fastIs<Atom>() const { return isAtom(); }
|
|||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
struct ruc::format::Formatter<blaze::ASTNodePtr> : public Formatter<std::string> {
|
struct ruc::format::Formatter<blaze::ValuePtr> : public Formatter<std::string> {
|
||||||
void format(Builder& builder, blaze::ASTNodePtr value) const;
|
void format(Builder& builder, blaze::ValuePtr value) const;
|
||||||
};
|
};
|
||||||
|
|||||||
+3
-3
@@ -29,7 +29,7 @@ EnvironmentPtr Environment::create(EnvironmentPtr outer)
|
|||||||
return env;
|
return env;
|
||||||
}
|
}
|
||||||
|
|
||||||
EnvironmentPtr Environment::create(const ASTNodePtr lambda, std::list<ASTNodePtr> arguments)
|
EnvironmentPtr Environment::create(const ValuePtr lambda, std::list<ValuePtr> arguments)
|
||||||
{
|
{
|
||||||
auto lambda_casted = std::static_pointer_cast<Lambda>(lambda);
|
auto lambda_casted = std::static_pointer_cast<Lambda>(lambda);
|
||||||
auto env = create(lambda_casted->env());
|
auto env = create(lambda_casted->env());
|
||||||
@@ -75,7 +75,7 @@ bool Environment::exists(const std::string& symbol)
|
|||||||
return m_values.find(symbol) != m_values.end();
|
return m_values.find(symbol) != m_values.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNodePtr Environment::set(const std::string& symbol, ASTNodePtr value)
|
ValuePtr Environment::set(const std::string& symbol, ValuePtr value)
|
||||||
{
|
{
|
||||||
if (exists(symbol)) {
|
if (exists(symbol)) {
|
||||||
m_values.erase(symbol);
|
m_values.erase(symbol);
|
||||||
@@ -86,7 +86,7 @@ ASTNodePtr Environment::set(const std::string& symbol, ASTNodePtr value)
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNodePtr Environment::get(const std::string& symbol)
|
ValuePtr Environment::get(const std::string& symbol)
|
||||||
{
|
{
|
||||||
if (exists(symbol)) {
|
if (exists(symbol)) {
|
||||||
return m_values[symbol];
|
return m_values[symbol];
|
||||||
|
|||||||
+4
-4
@@ -21,17 +21,17 @@ public:
|
|||||||
// Factory functions instead of constructors because it can fail in the bindings/arguments case
|
// Factory functions instead of constructors because it can fail in the bindings/arguments case
|
||||||
static EnvironmentPtr create();
|
static EnvironmentPtr create();
|
||||||
static EnvironmentPtr create(EnvironmentPtr outer);
|
static EnvironmentPtr create(EnvironmentPtr outer);
|
||||||
static EnvironmentPtr create(const ASTNodePtr lambda, std::list<ASTNodePtr> arguments);
|
static EnvironmentPtr create(const ValuePtr lambda, std::list<ValuePtr> arguments);
|
||||||
|
|
||||||
bool exists(const std::string& symbol);
|
bool exists(const std::string& symbol);
|
||||||
ASTNodePtr set(const std::string& symbol, ASTNodePtr value);
|
ValuePtr set(const std::string& symbol, ValuePtr value);
|
||||||
ASTNodePtr get(const std::string& symbol);
|
ValuePtr get(const std::string& symbol);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Environment() {}
|
Environment() {}
|
||||||
|
|
||||||
EnvironmentPtr m_outer { nullptr };
|
EnvironmentPtr m_outer { nullptr };
|
||||||
std::unordered_map<std::string, ASTNodePtr> m_values;
|
std::unordered_map<std::string, ValuePtr> m_values;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace blaze
|
} // namespace blaze
|
||||||
|
|||||||
+20
-20
@@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
namespace blaze {
|
namespace blaze {
|
||||||
|
|
||||||
Eval::Eval(ASTNodePtr ast, EnvironmentPtr env)
|
Eval::Eval(ValuePtr ast, EnvironmentPtr env)
|
||||||
: m_ast(ast)
|
: m_ast(ast)
|
||||||
, m_env(env)
|
, m_env(env)
|
||||||
{
|
{
|
||||||
@@ -29,7 +29,7 @@ Eval::Eval(ASTNodePtr ast, EnvironmentPtr env)
|
|||||||
|
|
||||||
void Eval::eval()
|
void Eval::eval()
|
||||||
{
|
{
|
||||||
m_ast_stack = std::stack<ASTNodePtr>();
|
m_ast_stack = std::stack<ValuePtr>();
|
||||||
m_env_stack = std::stack<EnvironmentPtr>();
|
m_env_stack = std::stack<EnvironmentPtr>();
|
||||||
m_ast_stack.push(m_ast);
|
m_ast_stack.push(m_ast);
|
||||||
m_env_stack.push(m_env);
|
m_env_stack.push(m_env);
|
||||||
@@ -37,9 +37,9 @@ void Eval::eval()
|
|||||||
m_ast = evalImpl();
|
m_ast = evalImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNodePtr Eval::evalImpl()
|
ValuePtr Eval::evalImpl()
|
||||||
{
|
{
|
||||||
ASTNodePtr ast = nullptr;
|
ValuePtr ast = nullptr;
|
||||||
EnvironmentPtr env = nullptr;
|
EnvironmentPtr env = nullptr;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -117,13 +117,13 @@ ASTNodePtr Eval::evalImpl()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNodePtr Eval::evalAst(ASTNodePtr ast, EnvironmentPtr env)
|
ValuePtr Eval::evalAst(ValuePtr ast, EnvironmentPtr env)
|
||||||
{
|
{
|
||||||
if (ast == nullptr || env == nullptr) {
|
if (ast == nullptr || env == nullptr) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNode* ast_raw_ptr = ast.get();
|
Value* ast_raw_ptr = ast.get();
|
||||||
if (is<Symbol>(ast_raw_ptr)) {
|
if (is<Symbol>(ast_raw_ptr)) {
|
||||||
auto result = env->get(std::static_pointer_cast<Symbol>(ast)->symbol());
|
auto result = env->get(std::static_pointer_cast<Symbol>(ast)->symbol());
|
||||||
if (!result) {
|
if (!result) {
|
||||||
@@ -139,7 +139,7 @@ ASTNodePtr Eval::evalAst(ASTNodePtr ast, EnvironmentPtr env)
|
|||||||
for (auto node : nodes) {
|
for (auto node : nodes) {
|
||||||
m_ast_stack.push(node);
|
m_ast_stack.push(node);
|
||||||
m_env_stack.push(env);
|
m_env_stack.push(env);
|
||||||
ASTNodePtr eval_node = evalImpl();
|
ValuePtr eval_node = evalImpl();
|
||||||
if (eval_node == nullptr) {
|
if (eval_node == nullptr) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
@@ -153,7 +153,7 @@ ASTNodePtr Eval::evalAst(ASTNodePtr ast, EnvironmentPtr env)
|
|||||||
for (auto& element : elements) {
|
for (auto& element : elements) {
|
||||||
m_ast_stack.push(element.second);
|
m_ast_stack.push(element.second);
|
||||||
m_env_stack.push(env);
|
m_env_stack.push(env);
|
||||||
ASTNodePtr element_node = evalImpl();
|
ValuePtr element_node = evalImpl();
|
||||||
if (element_node == nullptr) {
|
if (element_node == nullptr) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
@@ -165,7 +165,7 @@ ASTNodePtr Eval::evalAst(ASTNodePtr ast, EnvironmentPtr env)
|
|||||||
return ast;
|
return ast;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNodePtr Eval::evalDef(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
|
ValuePtr Eval::evalDef(const std::list<ValuePtr>& nodes, EnvironmentPtr env)
|
||||||
{
|
{
|
||||||
if (nodes.size() != 2) {
|
if (nodes.size() != 2) {
|
||||||
Error::the().add(format("wrong number of arguments: def!, {}", nodes.size()));
|
Error::the().add(format("wrong number of arguments: def!, {}", nodes.size()));
|
||||||
@@ -184,7 +184,7 @@ ASTNodePtr Eval::evalDef(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
|
|||||||
std::string symbol = std::static_pointer_cast<Symbol>(first_argument)->symbol();
|
std::string symbol = std::static_pointer_cast<Symbol>(first_argument)->symbol();
|
||||||
m_ast_stack.push(second_argument);
|
m_ast_stack.push(second_argument);
|
||||||
m_env_stack.push(env);
|
m_env_stack.push(env);
|
||||||
ASTNodePtr value = evalImpl();
|
ValuePtr value = evalImpl();
|
||||||
|
|
||||||
// Dont overwrite symbols after an error
|
// Dont overwrite symbols after an error
|
||||||
if (Error::the().hasAnyError()) {
|
if (Error::the().hasAnyError()) {
|
||||||
@@ -195,7 +195,7 @@ ASTNodePtr Eval::evalDef(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
|
|||||||
return env->set(symbol, value);
|
return env->set(symbol, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Eval::evalLet(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
|
void Eval::evalLet(const std::list<ValuePtr>& nodes, EnvironmentPtr env)
|
||||||
{
|
{
|
||||||
if (nodes.size() != 2) {
|
if (nodes.size() != 2) {
|
||||||
Error::the().add(format("wrong number of arguments: let*, {}", nodes.size()));
|
Error::the().add(format("wrong number of arguments: let*, {}", nodes.size()));
|
||||||
@@ -212,7 +212,7 @@ void Eval::evalLet(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get the nodes out of the List or Vector
|
// Get the nodes out of the List or Vector
|
||||||
std::list<ASTNodePtr> binding_nodes;
|
std::list<ValuePtr> binding_nodes;
|
||||||
auto bindings = std::static_pointer_cast<Collection>(first_argument);
|
auto bindings = std::static_pointer_cast<Collection>(first_argument);
|
||||||
binding_nodes = bindings->nodes();
|
binding_nodes = bindings->nodes();
|
||||||
|
|
||||||
@@ -236,7 +236,7 @@ void Eval::evalLet(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
|
|||||||
std::string key = std::static_pointer_cast<Symbol>(*it)->symbol();
|
std::string key = std::static_pointer_cast<Symbol>(*it)->symbol();
|
||||||
m_ast_stack.push(*std::next(it));
|
m_ast_stack.push(*std::next(it));
|
||||||
m_env_stack.push(let_env);
|
m_env_stack.push(let_env);
|
||||||
ASTNodePtr value = evalImpl();
|
ValuePtr value = evalImpl();
|
||||||
let_env->set(key, value);
|
let_env->set(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,7 +247,7 @@ void Eval::evalLet(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
|
|||||||
return; // TCO
|
return; // TCO
|
||||||
}
|
}
|
||||||
|
|
||||||
void Eval::evalDo(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
|
void Eval::evalDo(const std::list<ValuePtr>& nodes, EnvironmentPtr env)
|
||||||
{
|
{
|
||||||
if (nodes.size() == 0) {
|
if (nodes.size() == 0) {
|
||||||
Error::the().add(format("wrong number of arguments: do, {}", nodes.size()));
|
Error::the().add(format("wrong number of arguments: do, {}", nodes.size()));
|
||||||
@@ -267,7 +267,7 @@ void Eval::evalDo(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
|
|||||||
return; // TCO
|
return; // TCO
|
||||||
}
|
}
|
||||||
|
|
||||||
void Eval::evalIf(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
|
void Eval::evalIf(const std::list<ValuePtr>& nodes, EnvironmentPtr env)
|
||||||
{
|
{
|
||||||
if (nodes.size() != 2 && nodes.size() != 3) {
|
if (nodes.size() != 2 && nodes.size() != 3) {
|
||||||
Error::the().add(format("wrong number of arguments: if, {}", nodes.size()));
|
Error::the().add(format("wrong number of arguments: if, {}", nodes.size()));
|
||||||
@@ -276,13 +276,13 @@ void Eval::evalIf(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
|
|||||||
|
|
||||||
auto first_argument = *nodes.begin();
|
auto first_argument = *nodes.begin();
|
||||||
auto second_argument = *std::next(nodes.begin());
|
auto second_argument = *std::next(nodes.begin());
|
||||||
auto third_argument = (nodes.size() == 3) ? *std::next(std::next(nodes.begin())) : makePtr<Value>(Value::Nil);
|
auto third_argument = (nodes.size() == 3) ? *std::next(std::next(nodes.begin())) : makePtr<Constant>(Constant::Nil);
|
||||||
|
|
||||||
m_ast_stack.push(first_argument);
|
m_ast_stack.push(first_argument);
|
||||||
m_env_stack.push(env);
|
m_env_stack.push(env);
|
||||||
auto first_evaluated = evalImpl();
|
auto first_evaluated = evalImpl();
|
||||||
if (!is<Value>(first_evaluated.get())
|
if (!is<Constant>(first_evaluated.get())
|
||||||
|| std::static_pointer_cast<Value>(first_evaluated)->state() == Value::True) {
|
|| std::static_pointer_cast<Constant>(first_evaluated)->state() == Constant::True) {
|
||||||
m_ast_stack.push(second_argument);
|
m_ast_stack.push(second_argument);
|
||||||
m_env_stack.push(env);
|
m_env_stack.push(env);
|
||||||
return; // TCO
|
return; // TCO
|
||||||
@@ -309,7 +309,7 @@ void Eval::evalIf(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
|
|||||||
AST_CHECK(type, value) \
|
AST_CHECK(type, value) \
|
||||||
auto variable = std::static_pointer_cast<type>(value);
|
auto variable = std::static_pointer_cast<type>(value);
|
||||||
|
|
||||||
ASTNodePtr Eval::evalFn(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
|
ValuePtr Eval::evalFn(const std::list<ValuePtr>& nodes, EnvironmentPtr env)
|
||||||
{
|
{
|
||||||
ARG_COUNT_CHECK("fn*", nodes.size() != 2, nodes.size());
|
ARG_COUNT_CHECK("fn*", nodes.size() != 2, nodes.size());
|
||||||
|
|
||||||
@@ -329,7 +329,7 @@ ASTNodePtr Eval::evalFn(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
|
|||||||
return makePtr<Lambda>(bindings, second_argument, env);
|
return makePtr<Lambda>(bindings, second_argument, env);
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNodePtr Eval::apply(std::shared_ptr<List> evaluated_list)
|
ValuePtr Eval::apply(std::shared_ptr<List> evaluated_list)
|
||||||
{
|
{
|
||||||
if (evaluated_list == nullptr) {
|
if (evaluated_list == nullptr) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|||||||
+12
-12
@@ -18,27 +18,27 @@ class List;
|
|||||||
|
|
||||||
class Eval {
|
class Eval {
|
||||||
public:
|
public:
|
||||||
Eval(ASTNodePtr ast, EnvironmentPtr env);
|
Eval(ValuePtr ast, EnvironmentPtr env);
|
||||||
virtual ~Eval() = default;
|
virtual ~Eval() = default;
|
||||||
|
|
||||||
void eval();
|
void eval();
|
||||||
|
|
||||||
ASTNodePtr ast() const { return m_ast; }
|
ValuePtr ast() const { return m_ast; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ASTNodePtr evalImpl();
|
ValuePtr evalImpl();
|
||||||
ASTNodePtr evalAst(ASTNodePtr ast, EnvironmentPtr env);
|
ValuePtr evalAst(ValuePtr ast, EnvironmentPtr env);
|
||||||
ASTNodePtr evalDef(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env);
|
ValuePtr evalDef(const std::list<ValuePtr>& nodes, EnvironmentPtr env);
|
||||||
void evalLet(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env);
|
void evalLet(const std::list<ValuePtr>& nodes, EnvironmentPtr env);
|
||||||
void evalDo(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env);
|
void evalDo(const std::list<ValuePtr>& nodes, EnvironmentPtr env);
|
||||||
void evalIf(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env);
|
void evalIf(const std::list<ValuePtr>& nodes, EnvironmentPtr env);
|
||||||
ASTNodePtr evalFn(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env);
|
ValuePtr evalFn(const std::list<ValuePtr>& nodes, EnvironmentPtr env);
|
||||||
ASTNodePtr apply(std::shared_ptr<List> evaluated_list);
|
ValuePtr apply(std::shared_ptr<List> evaluated_list);
|
||||||
|
|
||||||
ASTNodePtr m_ast;
|
ValuePtr m_ast;
|
||||||
EnvironmentPtr m_env;
|
EnvironmentPtr m_env;
|
||||||
|
|
||||||
std::stack<ASTNodePtr> m_ast_stack;
|
std::stack<ValuePtr> m_ast_stack;
|
||||||
std::stack<EnvironmentPtr> m_env_stack;
|
std::stack<EnvironmentPtr> m_env_stack;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+4
-4
@@ -13,8 +13,8 @@ namespace blaze {
|
|||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
// Types
|
// Types
|
||||||
|
|
||||||
class ASTNode;
|
class Value;
|
||||||
typedef std::shared_ptr<ASTNode> ASTNodePtr;
|
typedef std::shared_ptr<Value> ValuePtr;
|
||||||
|
|
||||||
class Environment;
|
class Environment;
|
||||||
typedef std::shared_ptr<Environment> EnvironmentPtr;
|
typedef std::shared_ptr<Environment> EnvironmentPtr;
|
||||||
@@ -22,8 +22,8 @@ typedef std::shared_ptr<Environment> EnvironmentPtr;
|
|||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
// Functions
|
// Functions
|
||||||
|
|
||||||
extern ASTNodePtr read(std::string_view input);
|
extern ValuePtr read(std::string_view input);
|
||||||
ASTNodePtr eval(ASTNodePtr ast, EnvironmentPtr env);
|
ValuePtr eval(ValuePtr ast, EnvironmentPtr env);
|
||||||
|
|
||||||
extern void installFunctions(EnvironmentPtr env);
|
extern void installFunctions(EnvironmentPtr env);
|
||||||
|
|
||||||
|
|||||||
+13
-13
@@ -35,7 +35,7 @@
|
|||||||
static struct FUNCTION_STRUCT_NAME(unique) \
|
static struct FUNCTION_STRUCT_NAME(unique) \
|
||||||
FUNCTION_STRUCT_NAME(unique)( \
|
FUNCTION_STRUCT_NAME(unique)( \
|
||||||
symbol, \
|
symbol, \
|
||||||
[](std::list<ASTNodePtr> nodes) -> ASTNodePtr lambda);
|
[](std::list<ValuePtr> nodes) -> ValuePtr lambda);
|
||||||
|
|
||||||
#define ADD_FUNCTION(symbol, lambda) ADD_FUNCTION_IMPL(__LINE__, symbol, lambda);
|
#define ADD_FUNCTION(symbol, lambda) ADD_FUNCTION_IMPL(__LINE__, symbol, lambda);
|
||||||
|
|
||||||
@@ -159,7 +159,7 @@ ADD_FUNCTION(
|
|||||||
number = current_number; \
|
number = current_number; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
return makePtr<Value>((result) ? Value::True : Value::False); \
|
return makePtr<Constant>((result) ? Constant::True : Constant::False); \
|
||||||
}
|
}
|
||||||
|
|
||||||
ADD_FUNCTION("<", NUMBER_COMPARE(<));
|
ADD_FUNCTION("<", NUMBER_COMPARE(<));
|
||||||
@@ -197,7 +197,7 @@ ADD_FUNCTION(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return makePtr<Value>((result) ? Value::True : Value::False);
|
return makePtr<Constant>((result) ? Constant::True : Constant::False);
|
||||||
});
|
});
|
||||||
|
|
||||||
ADD_FUNCTION(
|
ADD_FUNCTION(
|
||||||
@@ -217,7 +217,7 @@ ADD_FUNCTION(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return makePtr<Value>((result) ? Value::True : Value::False);
|
return makePtr<Constant>((result) ? Constant::True : Constant::False);
|
||||||
});
|
});
|
||||||
|
|
||||||
ADD_FUNCTION(
|
ADD_FUNCTION(
|
||||||
@@ -231,7 +231,7 @@ ADD_FUNCTION(
|
|||||||
auto first_argument = nodes.front();
|
auto first_argument = nodes.front();
|
||||||
|
|
||||||
size_t result = 0;
|
size_t result = 0;
|
||||||
if (is<Value>(first_argument.get()) && std::static_pointer_cast<Value>(nodes.front())->state() == Value::Nil) {
|
if (is<Constant>(first_argument.get()) && std::static_pointer_cast<Constant>(nodes.front())->state() == Constant::Nil) {
|
||||||
// result = 0
|
// result = 0
|
||||||
}
|
}
|
||||||
else if (is<Collection>(first_argument.get())) {
|
else if (is<Collection>(first_argument.get())) {
|
||||||
@@ -279,7 +279,7 @@ ADD_FUNCTION("pr-str", PRINTER_STRING(true, " "));
|
|||||||
} \
|
} \
|
||||||
print("\n"); \
|
print("\n"); \
|
||||||
\
|
\
|
||||||
return makePtr<Value>(Value::Nil); \
|
return makePtr<Constant>(Constant::Nil); \
|
||||||
}
|
}
|
||||||
|
|
||||||
ADD_FUNCTION("prn", PRINTER_PRINT(true));
|
ADD_FUNCTION("prn", PRINTER_PRINT(true));
|
||||||
@@ -295,8 +295,8 @@ ADD_FUNCTION(
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::function<bool(ASTNodePtr, ASTNodePtr)> equal =
|
std::function<bool(ValuePtr, ValuePtr)> equal =
|
||||||
[&equal](ASTNodePtr lhs, ASTNodePtr rhs) -> bool {
|
[&equal](ValuePtr lhs, ValuePtr rhs) -> bool {
|
||||||
if ((is<List>(lhs.get()) || is<Vector>(lhs.get()))
|
if ((is<List>(lhs.get()) || is<Vector>(lhs.get()))
|
||||||
&& (is<List>(rhs.get()) || is<Vector>(rhs.get()))) {
|
&& (is<List>(rhs.get()) || is<Vector>(rhs.get()))) {
|
||||||
auto lhs_nodes = std::static_pointer_cast<Collection>(lhs)->nodes();
|
auto lhs_nodes = std::static_pointer_cast<Collection>(lhs)->nodes();
|
||||||
@@ -347,8 +347,8 @@ ADD_FUNCTION(
|
|||||||
&& std::static_pointer_cast<Number>(lhs)->number() == std::static_pointer_cast<Number>(rhs)->number()) {
|
&& std::static_pointer_cast<Number>(lhs)->number() == std::static_pointer_cast<Number>(rhs)->number()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (is<Value>(lhs.get()) && is<Value>(rhs.get())
|
if (is<Constant>(lhs.get()) && is<Constant>(rhs.get())
|
||||||
&& std::static_pointer_cast<Value>(lhs)->state() == std::static_pointer_cast<Value>(rhs)->state()) {
|
&& std::static_pointer_cast<Constant>(lhs)->state() == std::static_pointer_cast<Constant>(rhs)->state()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (is<Symbol>(lhs.get()) && is<Symbol>(rhs.get())
|
if (is<Symbol>(lhs.get()) && is<Symbol>(rhs.get())
|
||||||
@@ -369,7 +369,7 @@ ADD_FUNCTION(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return makePtr<Value>((result) ? Value::True : Value::False);
|
return makePtr<Constant>((result) ? Constant::True : Constant::False);
|
||||||
});
|
});
|
||||||
|
|
||||||
ADD_FUNCTION(
|
ADD_FUNCTION(
|
||||||
@@ -450,7 +450,7 @@ ADD_FUNCTION(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return makePtr<Value>((result) ? Value::True : Value::False);
|
return makePtr<Constant>((result) ? Constant::True : Constant::False);
|
||||||
});
|
});
|
||||||
|
|
||||||
// (deref myatom)
|
// (deref myatom)
|
||||||
@@ -521,7 +521,7 @@ ADD_FUNCTION(
|
|||||||
nodes.pop_front();
|
nodes.pop_front();
|
||||||
nodes.push_front(atom->deref());
|
nodes.push_front(atom->deref());
|
||||||
|
|
||||||
ASTNodePtr value = nullptr;
|
ValuePtr value = nullptr;
|
||||||
if (is<Function>(second_argument.get())) {
|
if (is<Function>(second_argument.get())) {
|
||||||
auto function = std::static_pointer_cast<Function>(second_argument)->function();
|
auto function = std::static_pointer_cast<Function>(second_argument)->function();
|
||||||
value = function(nodes);
|
value = function(nodes);
|
||||||
|
|||||||
+9
-9
@@ -29,7 +29,7 @@ Printer::~Printer()
|
|||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|
||||||
std::string Printer::print(ASTNodePtr node, bool print_readably)
|
std::string Printer::print(ValuePtr node, bool print_readably)
|
||||||
{
|
{
|
||||||
if (Error::the().hasAnyError()) {
|
if (Error::the().hasAnyError()) {
|
||||||
init();
|
init();
|
||||||
@@ -40,7 +40,7 @@ std::string Printer::print(ASTNodePtr node, bool print_readably)
|
|||||||
return printNoErrorCheck(node, print_readably);
|
return printNoErrorCheck(node, print_readably);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Printer::printNoErrorCheck(ASTNodePtr node, bool print_readably)
|
std::string Printer::printNoErrorCheck(ValuePtr node, bool print_readably)
|
||||||
{
|
{
|
||||||
init();
|
init();
|
||||||
|
|
||||||
@@ -62,7 +62,7 @@ void Printer::init()
|
|||||||
m_print = "";
|
m_print = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
void Printer::printImpl(ASTNodePtr node, bool print_readably)
|
void Printer::printImpl(ValuePtr node, bool print_readably)
|
||||||
{
|
{
|
||||||
auto printSpacing = [this]() -> void {
|
auto printSpacing = [this]() -> void {
|
||||||
if (!m_first_node && !m_previous_node_is_list) {
|
if (!m_first_node && !m_previous_node_is_list) {
|
||||||
@@ -70,7 +70,7 @@ void Printer::printImpl(ASTNodePtr node, bool print_readably)
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
ASTNode* node_raw_ptr = node.get();
|
Value* node_raw_ptr = node.get();
|
||||||
if (is<Collection>(node_raw_ptr)) {
|
if (is<Collection>(node_raw_ptr)) {
|
||||||
printSpacing();
|
printSpacing();
|
||||||
m_print += (is<List>(node_raw_ptr)) ? '(' : '[';
|
m_print += (is<List>(node_raw_ptr)) ? '(' : '[';
|
||||||
@@ -119,13 +119,13 @@ void Printer::printImpl(ASTNodePtr node, bool print_readably)
|
|||||||
printSpacing();
|
printSpacing();
|
||||||
m_print += format("{}", std::static_pointer_cast<Number>(node)->number());
|
m_print += format("{}", std::static_pointer_cast<Number>(node)->number());
|
||||||
}
|
}
|
||||||
else if (is<Value>(node_raw_ptr)) {
|
else if (is<Constant>(node_raw_ptr)) {
|
||||||
printSpacing();
|
printSpacing();
|
||||||
std::string value;
|
std::string value;
|
||||||
switch (std::static_pointer_cast<Value>(node)->state()) {
|
switch (std::static_pointer_cast<Constant>(node)->state()) {
|
||||||
case Value::Nil: value = "nil"; break;
|
case Constant::Nil: value = "nil"; break;
|
||||||
case Value::True: value = "true"; break;
|
case Constant::True: value = "true"; break;
|
||||||
case Value::False: value = "false"; break;
|
case Constant::False: value = "false"; break;
|
||||||
}
|
}
|
||||||
m_print += format("{}", value);
|
m_print += format("{}", value);
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -17,12 +17,12 @@ public:
|
|||||||
Printer();
|
Printer();
|
||||||
virtual ~Printer();
|
virtual ~Printer();
|
||||||
|
|
||||||
std::string print(ASTNodePtr node, bool print_readably = true);
|
std::string print(ValuePtr node, bool print_readably = true);
|
||||||
std::string printNoErrorCheck(ASTNodePtr node, bool print_readably = true);
|
std::string printNoErrorCheck(ValuePtr node, bool print_readably = true);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void init();
|
void init();
|
||||||
void printImpl(ASTNodePtr node, bool print_readably = true);
|
void printImpl(ValuePtr node, bool print_readably = true);
|
||||||
void printError();
|
void printError();
|
||||||
|
|
||||||
bool m_first_node { true };
|
bool m_first_node { true };
|
||||||
|
|||||||
+21
-21
@@ -56,7 +56,7 @@ void Reader::read()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNodePtr Reader::readImpl()
|
ValuePtr Reader::readImpl()
|
||||||
{
|
{
|
||||||
if (m_tokens.size() == 0) {
|
if (m_tokens.size() == 0) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@@ -124,7 +124,7 @@ ASTNodePtr Reader::readImpl()
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNodePtr Reader::readSpliceUnquote()
|
ValuePtr Reader::readSpliceUnquote()
|
||||||
{
|
{
|
||||||
ignore(); // ~@
|
ignore(); // ~@
|
||||||
|
|
||||||
@@ -140,7 +140,7 @@ ASTNodePtr Reader::readSpliceUnquote()
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNodePtr Reader::readList()
|
ValuePtr Reader::readList()
|
||||||
{
|
{
|
||||||
ignore(); // (
|
ignore(); // (
|
||||||
|
|
||||||
@@ -157,7 +157,7 @@ ASTNodePtr Reader::readList()
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNodePtr Reader::readVector()
|
ValuePtr Reader::readVector()
|
||||||
{
|
{
|
||||||
ignore(); // [
|
ignore(); // [
|
||||||
|
|
||||||
@@ -173,7 +173,7 @@ ASTNodePtr Reader::readVector()
|
|||||||
return vector;
|
return vector;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNodePtr Reader::readHashMap()
|
ValuePtr Reader::readHashMap()
|
||||||
{
|
{
|
||||||
ignore(); // {
|
ignore(); // {
|
||||||
|
|
||||||
@@ -207,7 +207,7 @@ ASTNodePtr Reader::readHashMap()
|
|||||||
return hash_map;
|
return hash_map;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNodePtr Reader::readQuote()
|
ValuePtr Reader::readQuote()
|
||||||
{
|
{
|
||||||
ignore(); // '
|
ignore(); // '
|
||||||
|
|
||||||
@@ -223,7 +223,7 @@ ASTNodePtr Reader::readQuote()
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNodePtr Reader::readQuasiQuote()
|
ValuePtr Reader::readQuasiQuote()
|
||||||
{
|
{
|
||||||
ignore(); // `
|
ignore(); // `
|
||||||
|
|
||||||
@@ -239,7 +239,7 @@ ASTNodePtr Reader::readQuasiQuote()
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNodePtr Reader::readUnquote()
|
ValuePtr Reader::readUnquote()
|
||||||
{
|
{
|
||||||
ignore(); // ~
|
ignore(); // ~
|
||||||
|
|
||||||
@@ -255,7 +255,7 @@ ASTNodePtr Reader::readUnquote()
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNodePtr Reader::readWithMeta()
|
ValuePtr Reader::readWithMeta()
|
||||||
{
|
{
|
||||||
ignore(); // ^
|
ignore(); // ^
|
||||||
|
|
||||||
@@ -268,15 +268,15 @@ ASTNodePtr Reader::readWithMeta()
|
|||||||
|
|
||||||
auto list = makePtr<List>();
|
auto list = makePtr<List>();
|
||||||
list->add(makePtr<Symbol>("with-meta"));
|
list->add(makePtr<Symbol>("with-meta"));
|
||||||
ASTNodePtr first = readImpl();
|
ValuePtr first = readImpl();
|
||||||
ASTNodePtr second = readImpl();
|
ValuePtr second = readImpl();
|
||||||
list->add(second);
|
list->add(second);
|
||||||
list->add(first);
|
list->add(first);
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNodePtr Reader::readDeref()
|
ValuePtr Reader::readDeref()
|
||||||
{
|
{
|
||||||
ignore(); // @
|
ignore(); // @
|
||||||
|
|
||||||
@@ -292,19 +292,19 @@ ASTNodePtr Reader::readDeref()
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNodePtr Reader::readString()
|
ValuePtr Reader::readString()
|
||||||
{
|
{
|
||||||
std::string symbol = consume().symbol;
|
std::string symbol = consume().symbol;
|
||||||
|
|
||||||
return makePtr<String>(symbol);
|
return makePtr<String>(symbol);
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNodePtr Reader::readKeyword()
|
ValuePtr Reader::readKeyword()
|
||||||
{
|
{
|
||||||
return makePtr<Keyword>(consume().symbol);
|
return makePtr<Keyword>(consume().symbol);
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNodePtr Reader::readValue()
|
ValuePtr Reader::readValue()
|
||||||
{
|
{
|
||||||
Token token = consume();
|
Token token = consume();
|
||||||
char* end_ptr = nullptr;
|
char* end_ptr = nullptr;
|
||||||
@@ -314,13 +314,13 @@ ASTNodePtr Reader::readValue()
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (token.symbol == "nil") {
|
if (token.symbol == "nil") {
|
||||||
return makePtr<Value>(Value::Nil);
|
return makePtr<Constant>(Constant::Nil);
|
||||||
}
|
}
|
||||||
else if (token.symbol == "true") {
|
else if (token.symbol == "true") {
|
||||||
return makePtr<Value>(Value::True);
|
return makePtr<Constant>(Constant::True);
|
||||||
}
|
}
|
||||||
else if (token.symbol == "false") {
|
else if (token.symbol == "false") {
|
||||||
return makePtr<Value>(Value::False);
|
return makePtr<Constant>(Constant::False);
|
||||||
}
|
}
|
||||||
|
|
||||||
return makePtr<Symbol>(token.symbol);
|
return makePtr<Symbol>(token.symbol);
|
||||||
@@ -372,11 +372,11 @@ void Reader::dump()
|
|||||||
dumpImpl(m_node);
|
dumpImpl(m_node);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Reader::dumpImpl(ASTNodePtr node)
|
void Reader::dumpImpl(ValuePtr node)
|
||||||
{
|
{
|
||||||
std::string indentation = std::string(m_indentation * 2, ' ');
|
std::string indentation = std::string(m_indentation * 2, ' ');
|
||||||
|
|
||||||
ASTNode* node_raw_ptr = node.get();
|
Value* node_raw_ptr = node.get();
|
||||||
if (is<Collection>(node_raw_ptr)) {
|
if (is<Collection>(node_raw_ptr)) {
|
||||||
auto nodes = std::static_pointer_cast<List>(node)->nodes();
|
auto nodes = std::static_pointer_cast<List>(node)->nodes();
|
||||||
print("{}", indentation);
|
print("{}", indentation);
|
||||||
@@ -406,7 +406,7 @@ void Reader::dumpImpl(ASTNodePtr node)
|
|||||||
print(fg(ruc::format::TerminalColor::Yellow), "NumberNode");
|
print(fg(ruc::format::TerminalColor::Yellow), "NumberNode");
|
||||||
print(" <{}>", node);
|
print(" <{}>", node);
|
||||||
}
|
}
|
||||||
else if (is<Value>(node_raw_ptr)) {
|
else if (is<Constant>(node_raw_ptr)) {
|
||||||
print("{}", indentation);
|
print("{}", indentation);
|
||||||
print(fg(ruc::format::TerminalColor::Yellow), "ValueNode");
|
print(fg(ruc::format::TerminalColor::Yellow), "ValueNode");
|
||||||
print(" <{}>", node);
|
print(" <{}>", node);
|
||||||
|
|||||||
+16
-16
@@ -25,7 +25,7 @@ public:
|
|||||||
|
|
||||||
void dump();
|
void dump();
|
||||||
|
|
||||||
ASTNodePtr node() { return m_node; }
|
ValuePtr node() { return m_node; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool isEOF() const;
|
bool isEOF() const;
|
||||||
@@ -35,21 +35,21 @@ private:
|
|||||||
void ignore();
|
void ignore();
|
||||||
void retreat();
|
void retreat();
|
||||||
|
|
||||||
ASTNodePtr readImpl();
|
ValuePtr readImpl();
|
||||||
ASTNodePtr readSpliceUnquote(); // ~@
|
ValuePtr readSpliceUnquote(); // ~@
|
||||||
ASTNodePtr readList(); // ()
|
ValuePtr readList(); // ()
|
||||||
ASTNodePtr readVector(); // []
|
ValuePtr readVector(); // []
|
||||||
ASTNodePtr readHashMap(); // {}
|
ValuePtr readHashMap(); // {}
|
||||||
ASTNodePtr readQuote(); // '
|
ValuePtr readQuote(); // '
|
||||||
ASTNodePtr readQuasiQuote(); // `
|
ValuePtr readQuasiQuote(); // `
|
||||||
ASTNodePtr readUnquote(); // ~
|
ValuePtr readUnquote(); // ~
|
||||||
ASTNodePtr readWithMeta(); // ^
|
ValuePtr readWithMeta(); // ^
|
||||||
ASTNodePtr readDeref(); // @
|
ValuePtr readDeref(); // @
|
||||||
ASTNodePtr readString(); // "foobar"
|
ValuePtr readString(); // "foobar"
|
||||||
ASTNodePtr readKeyword(); // :keyword
|
ValuePtr readKeyword(); // :keyword
|
||||||
ASTNodePtr readValue(); // number, "nil", "true", "false", symbol
|
ValuePtr readValue(); // number, "nil", "true", "false", symbol
|
||||||
|
|
||||||
void dumpImpl(ASTNodePtr node);
|
void dumpImpl(ValuePtr node);
|
||||||
|
|
||||||
size_t m_index { 0 };
|
size_t m_index { 0 };
|
||||||
size_t m_indentation { 0 };
|
size_t m_indentation { 0 };
|
||||||
@@ -59,7 +59,7 @@ private:
|
|||||||
bool m_invalid_syntax { false };
|
bool m_invalid_syntax { false };
|
||||||
bool m_is_unbalanced { false };
|
bool m_is_unbalanced { false };
|
||||||
|
|
||||||
ASTNodePtr m_node { nullptr };
|
ValuePtr m_node { nullptr };
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace blaze
|
} // namespace blaze
|
||||||
|
|||||||
+3
-3
@@ -34,7 +34,7 @@ static auto cleanup(int signal) -> void
|
|||||||
std::exit(signal);
|
std::exit(signal);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto read(std::string_view input) -> ASTNodePtr
|
auto read(std::string_view input) -> ValuePtr
|
||||||
{
|
{
|
||||||
Lexer lexer(input);
|
Lexer lexer(input);
|
||||||
lexer.tokenize();
|
lexer.tokenize();
|
||||||
@@ -51,7 +51,7 @@ auto read(std::string_view input) -> ASTNodePtr
|
|||||||
return reader.node();
|
return reader.node();
|
||||||
}
|
}
|
||||||
|
|
||||||
auto eval(ASTNodePtr ast, EnvironmentPtr env) -> ASTNodePtr
|
auto eval(ValuePtr ast, EnvironmentPtr env) -> ValuePtr
|
||||||
{
|
{
|
||||||
if (env == nullptr) {
|
if (env == nullptr) {
|
||||||
env = s_outer_env;
|
env = s_outer_env;
|
||||||
@@ -63,7 +63,7 @@ auto eval(ASTNodePtr ast, EnvironmentPtr env) -> ASTNodePtr
|
|||||||
return eval.ast();
|
return eval.ast();
|
||||||
}
|
}
|
||||||
|
|
||||||
static auto print(ASTNodePtr exp) -> std::string
|
static auto print(ValuePtr exp) -> std::string
|
||||||
{
|
{
|
||||||
Printer printer;
|
Printer printer;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user