|
|
@ -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; |
|
|
|
}; |
|
|
|
}; |
|
|
|