Browse Source

Everywhere: Rename Value -> Constant, ASTNode -> Value

master
Riyyi 1 year ago
parent
commit
b9feb8e8b6
  1. 12
      src/ast.cpp
  2. 90
      src/ast.h
  3. 6
      src/environment.cpp
  4. 8
      src/environment.h
  5. 40
      src/eval.cpp
  6. 26
      src/eval.h
  7. 8
      src/forward.h
  8. 26
      src/functions.cpp
  9. 18
      src/printer.cpp
  10. 6
      src/printer.h
  11. 42
      src/reader.cpp
  12. 32
      src/reader.h
  13. 6
      src/step6_file.cpp

12
src/ast.cpp

@ -16,7 +16,7 @@
namespace blaze {
void Collection::add(ASTNodePtr node)
void Collection::add(ValuePtr node)
{
if (node == nullptr) {
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) {
return;
@ -66,7 +66,7 @@ Symbol::Symbol(const std::string& symbol)
// -----------------------------------------
Value::Value(State state)
Constant::Constant(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_body(body)
, 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)
{
}
@ -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;
return Formatter<std::string>::format(builder, printer.printNoErrorCheck(value));

90
src/ast.h

@ -23,9 +23,9 @@
namespace blaze {
class ASTNode {
class Value {
public:
virtual ~ASTNode() = default;
virtual ~Value() = default;
std::string className() const { return typeid(*this).name(); }
@ -39,7 +39,7 @@ public:
virtual bool isString() const { return false; }
virtual bool isKeyword() 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 isCallable() const { return false; }
virtual bool isFunction() const { return false; }
@ -47,18 +47,18 @@ public:
virtual bool isAtom() const { return false; }
protected:
ASTNode() {}
Value() {}
};
// -----------------------------------------
class Collection : public ASTNode {
class Collection : public Value {
public:
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(); }
bool empty() const { return m_nodes.size() == 0; }
@ -68,7 +68,7 @@ protected:
private:
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:
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(); }
bool empty() const { return m_elements.size() == 0; }
private:
virtual bool isHashMap() const override { return true; }
std::unordered_map<std::string, ASTNodePtr> m_elements;
std::unordered_map<std::string, ValuePtr> m_elements;
};
// -----------------------------------------
// "string"
class String final : public ASTNode {
class String final : public Value {
public:
String(const std::string& data);
virtual ~String() = default;
@ -134,7 +134,7 @@ private:
// -----------------------------------------
// :keyword
class Keyword final : public ASTNode {
class Keyword final : public Value {
public:
Keyword(const std::string& data);
virtual ~Keyword() = default;
@ -149,7 +149,7 @@ private:
// -----------------------------------------
// 123
class Number final : public ASTNode {
class Number final : public Value {
public:
Number(int64_t number);
virtual ~Number() = default;
@ -165,7 +165,7 @@ private:
// -----------------------------------------
// true, false, nil
class Value final : public ASTNode {
class Constant final : public Value {
public:
enum State : uint8_t {
Nil,
@ -173,13 +173,13 @@ public:
False,
};
Value(State state);
virtual ~Value() = default;
Constant(State state);
virtual ~Constant() = default;
State state() const { return m_state; }
private:
virtual bool isValue() const override { return true; }
virtual bool isConstant() const override { return true; }
const State m_state;
};
@ -187,7 +187,7 @@ private:
// -----------------------------------------
// Symbols
class Symbol final : public ASTNode {
class Symbol final : public Value {
public:
Symbol(const std::string& symbol);
virtual ~Symbol() = default;
@ -202,7 +202,7 @@ private:
// -----------------------------------------
class Callable : public ASTNode {
class Callable : public Value {
public:
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 {
public:
@ -236,36 +236,36 @@ private:
class Lambda final : public Callable {
public:
Lambda(std::vector<std::string> bindings, ASTNodePtr body, EnvironmentPtr env);
Lambda(std::vector<std::string> bindings, ValuePtr body, EnvironmentPtr env);
virtual ~Lambda() = default;
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; }
private:
virtual bool isLambda() const override { return true; }
const std::vector<std::string> m_bindings;
const ASTNodePtr m_body;
const ValuePtr m_body;
const EnvironmentPtr m_env;
};
// -----------------------------------------
class Atom final : public ASTNode {
class Atom final : public Value {
public:
Atom() = default;
Atom(ASTNodePtr pointer);
Atom(ValuePtr pointer);
virtual ~Atom() = default;
ASTNodePtr reset(ASTNodePtr value) { return m_value = value; }
ASTNodePtr deref() const { return m_value; }
ValuePtr reset(ValuePtr value) { return m_value = value; }
ValuePtr deref() const { return m_value; }
private:
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
template<>
inline bool ASTNode::fastIs<Collection>() const { return isCollection(); }
inline bool Value::fastIs<Collection>() const { return isCollection(); }
template<>
inline bool ASTNode::fastIs<List>() const { return isList(); }
inline bool Value::fastIs<List>() const { return isList(); }
template<>
inline bool ASTNode::fastIs<Vector>() const { return isVector(); }
inline bool Value::fastIs<Vector>() const { return isVector(); }
template<>
inline bool ASTNode::fastIs<HashMap>() const { return isHashMap(); }
inline bool Value::fastIs<HashMap>() const { return isHashMap(); }
template<>
inline bool ASTNode::fastIs<String>() const { return isString(); }
inline bool Value::fastIs<String>() const { return isString(); }
template<>
inline bool ASTNode::fastIs<Keyword>() const { return isKeyword(); }
inline bool Value::fastIs<Keyword>() const { return isKeyword(); }
template<>
inline bool ASTNode::fastIs<Number>() const { return isNumber(); }
inline bool Value::fastIs<Number>() const { return isNumber(); }
template<>
inline bool ASTNode::fastIs<Value>() const { return isValue(); }
inline bool Value::fastIs<Constant>() const { return isConstant(); }
template<>
inline bool ASTNode::fastIs<Symbol>() const { return isSymbol(); }
inline bool Value::fastIs<Symbol>() const { return isSymbol(); }
template<>
inline bool ASTNode::fastIs<Callable>() const { return isCallable(); }
inline bool Value::fastIs<Callable>() const { return isCallable(); }
template<>
inline bool ASTNode::fastIs<Function>() const { return isFunction(); }
inline bool Value::fastIs<Function>() const { return isFunction(); }
template<>
inline bool ASTNode::fastIs<Lambda>() const { return isLambda(); }
inline bool Value::fastIs<Lambda>() const { return isLambda(); }
template<>
inline bool ASTNode::fastIs<Atom>() const { return isAtom(); }
inline bool Value::fastIs<Atom>() const { return isAtom(); }
// clang-format on
} // namespace blaze
@ -324,6 +324,6 @@ inline bool ASTNode::fastIs<Atom>() const { return isAtom(); }
// -----------------------------------------
template<>
struct ruc::format::Formatter<blaze::ASTNodePtr> : public Formatter<std::string> {
void format(Builder& builder, blaze::ASTNodePtr value) const;
struct ruc::format::Formatter<blaze::ValuePtr> : public Formatter<std::string> {
void format(Builder& builder, blaze::ValuePtr value) const;
};

6
src/environment.cpp

@ -29,7 +29,7 @@ EnvironmentPtr Environment::create(EnvironmentPtr outer)
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 env = create(lambda_casted->env());
@ -75,7 +75,7 @@ bool Environment::exists(const std::string& symbol)
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)) {
m_values.erase(symbol);
@ -86,7 +86,7 @@ ASTNodePtr Environment::set(const std::string& symbol, ASTNodePtr value)
return value;
}
ASTNodePtr Environment::get(const std::string& symbol)
ValuePtr Environment::get(const std::string& symbol)
{
if (exists(symbol)) {
return m_values[symbol];

8
src/environment.h

@ -21,17 +21,17 @@ public:
// Factory functions instead of constructors because it can fail in the bindings/arguments case
static EnvironmentPtr create();
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);
ASTNodePtr set(const std::string& symbol, ASTNodePtr value);
ASTNodePtr get(const std::string& symbol);
ValuePtr set(const std::string& symbol, ValuePtr value);
ValuePtr get(const std::string& symbol);
protected:
Environment() {}
EnvironmentPtr m_outer { nullptr };
std::unordered_map<std::string, ASTNodePtr> m_values;
std::unordered_map<std::string, ValuePtr> m_values;
};
} // namespace blaze

40
src/eval.cpp

@ -19,7 +19,7 @@
namespace blaze {
Eval::Eval(ASTNodePtr ast, EnvironmentPtr env)
Eval::Eval(ValuePtr ast, EnvironmentPtr env)
: m_ast(ast)
, m_env(env)
{
@ -29,7 +29,7 @@ Eval::Eval(ASTNodePtr ast, EnvironmentPtr env)
void Eval::eval()
{
m_ast_stack = std::stack<ASTNodePtr>();
m_ast_stack = std::stack<ValuePtr>();
m_env_stack = std::stack<EnvironmentPtr>();
m_ast_stack.push(m_ast);
m_env_stack.push(m_env);
@ -37,9 +37,9 @@ void Eval::eval()
m_ast = evalImpl();
}
ASTNodePtr Eval::evalImpl()
ValuePtr Eval::evalImpl()
{
ASTNodePtr ast = nullptr;
ValuePtr ast = nullptr;
EnvironmentPtr env = nullptr;
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) {
return nullptr;
}
ASTNode* ast_raw_ptr = ast.get();
Value* ast_raw_ptr = ast.get();
if (is<Symbol>(ast_raw_ptr)) {
auto result = env->get(std::static_pointer_cast<Symbol>(ast)->symbol());
if (!result) {
@ -139,7 +139,7 @@ ASTNodePtr Eval::evalAst(ASTNodePtr ast, EnvironmentPtr env)
for (auto node : nodes) {
m_ast_stack.push(node);
m_env_stack.push(env);
ASTNodePtr eval_node = evalImpl();
ValuePtr eval_node = evalImpl();
if (eval_node == nullptr) {
return nullptr;
}
@ -153,7 +153,7 @@ ASTNodePtr Eval::evalAst(ASTNodePtr ast, EnvironmentPtr env)
for (auto& element : elements) {
m_ast_stack.push(element.second);
m_env_stack.push(env);
ASTNodePtr element_node = evalImpl();
ValuePtr element_node = evalImpl();
if (element_node == nullptr) {
return nullptr;
}
@ -165,7 +165,7 @@ ASTNodePtr Eval::evalAst(ASTNodePtr ast, EnvironmentPtr env)
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) {
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();
m_ast_stack.push(second_argument);
m_env_stack.push(env);
ASTNodePtr value = evalImpl();
ValuePtr value = evalImpl();
// Dont overwrite symbols after an error
if (Error::the().hasAnyError()) {
@ -195,7 +195,7 @@ ASTNodePtr Eval::evalDef(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
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) {
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
std::list<ASTNodePtr> binding_nodes;
std::list<ValuePtr> binding_nodes;
auto bindings = std::static_pointer_cast<Collection>(first_argument);
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();
m_ast_stack.push(*std::next(it));
m_env_stack.push(let_env);
ASTNodePtr value = evalImpl();
ValuePtr value = evalImpl();
let_env->set(key, value);
}
@ -247,7 +247,7 @@ void Eval::evalLet(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
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) {
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
}
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) {
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 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_env_stack.push(env);
auto first_evaluated = evalImpl();
if (!is<Value>(first_evaluated.get())
|| std::static_pointer_cast<Value>(first_evaluated)->state() == Value::True) {
if (!is<Constant>(first_evaluated.get())
|| std::static_pointer_cast<Constant>(first_evaluated)->state() == Constant::True) {
m_ast_stack.push(second_argument);
m_env_stack.push(env);
return; // TCO
@ -309,7 +309,7 @@ void Eval::evalIf(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
AST_CHECK(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());
@ -329,7 +329,7 @@ ASTNodePtr Eval::evalFn(const std::list<ASTNodePtr>& nodes, EnvironmentPtr 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) {
return nullptr;

26
src/eval.h

@ -18,27 +18,27 @@ class List;
class Eval {
public:
Eval(ASTNodePtr ast, EnvironmentPtr env);
Eval(ValuePtr ast, EnvironmentPtr env);
virtual ~Eval() = default;
void eval();
ASTNodePtr ast() const { return m_ast; }
ValuePtr ast() const { return m_ast; }
private:
ASTNodePtr evalImpl();
ASTNodePtr evalAst(ASTNodePtr ast, EnvironmentPtr env);
ASTNodePtr evalDef(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env);
void evalLet(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env);
void evalDo(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env);
void evalIf(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env);
ASTNodePtr evalFn(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env);
ASTNodePtr apply(std::shared_ptr<List> evaluated_list);
ASTNodePtr m_ast;
ValuePtr evalImpl();
ValuePtr evalAst(ValuePtr ast, EnvironmentPtr env);
ValuePtr evalDef(const std::list<ValuePtr>& nodes, EnvironmentPtr env);
void evalLet(const std::list<ValuePtr>& nodes, EnvironmentPtr env);
void evalDo(const std::list<ValuePtr>& nodes, EnvironmentPtr env);
void evalIf(const std::list<ValuePtr>& nodes, EnvironmentPtr env);
ValuePtr evalFn(const std::list<ValuePtr>& nodes, EnvironmentPtr env);
ValuePtr apply(std::shared_ptr<List> evaluated_list);
ValuePtr m_ast;
EnvironmentPtr m_env;
std::stack<ASTNodePtr> m_ast_stack;
std::stack<ValuePtr> m_ast_stack;
std::stack<EnvironmentPtr> m_env_stack;
};

8
src/forward.h

@ -13,8 +13,8 @@ namespace blaze {
// -----------------------------------------
// Types
class ASTNode;
typedef std::shared_ptr<ASTNode> ASTNodePtr;
class Value;
typedef std::shared_ptr<Value> ValuePtr;
class Environment;
typedef std::shared_ptr<Environment> EnvironmentPtr;
@ -22,8 +22,8 @@ typedef std::shared_ptr<Environment> EnvironmentPtr;
// -----------------------------------------
// Functions
extern ASTNodePtr read(std::string_view input);
ASTNodePtr eval(ASTNodePtr ast, EnvironmentPtr env);
extern ValuePtr read(std::string_view input);
ValuePtr eval(ValuePtr ast, EnvironmentPtr env);
extern void installFunctions(EnvironmentPtr env);

26
src/functions.cpp

@ -35,7 +35,7 @@
static struct FUNCTION_STRUCT_NAME(unique) \
FUNCTION_STRUCT_NAME(unique)( \
symbol, \
[](std::list<ASTNodePtr> nodes) -> ASTNodePtr lambda);
[](std::list<ValuePtr> nodes) -> ValuePtr lambda);
#define ADD_FUNCTION(symbol, lambda) ADD_FUNCTION_IMPL(__LINE__, symbol, lambda);
@ -159,7 +159,7 @@ ADD_FUNCTION(
number = current_number; \
} \
\
return makePtr<Value>((result) ? Value::True : Value::False); \
return makePtr<Constant>((result) ? Constant::True : Constant::False); \
}
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(
@ -217,7 +217,7 @@ ADD_FUNCTION(
}
}
return makePtr<Value>((result) ? Value::True : Value::False);
return makePtr<Constant>((result) ? Constant::True : Constant::False);
});
ADD_FUNCTION(
@ -231,7 +231,7 @@ ADD_FUNCTION(
auto first_argument = nodes.front();
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
}
else if (is<Collection>(first_argument.get())) {
@ -279,7 +279,7 @@ ADD_FUNCTION("pr-str", PRINTER_STRING(true, " "));
} \
print("\n"); \
\
return makePtr<Value>(Value::Nil); \
return makePtr<Constant>(Constant::Nil); \
}
ADD_FUNCTION("prn", PRINTER_PRINT(true));
@ -295,8 +295,8 @@ ADD_FUNCTION(
return nullptr;
}
std::function<bool(ASTNodePtr, ASTNodePtr)> equal =
[&equal](ASTNodePtr lhs, ASTNodePtr rhs) -> bool {
std::function<bool(ValuePtr, ValuePtr)> equal =
[&equal](ValuePtr lhs, ValuePtr rhs) -> bool {
if ((is<List>(lhs.get()) || is<Vector>(lhs.get()))
&& (is<List>(rhs.get()) || is<Vector>(rhs.get()))) {
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()) {
return true;
}
if (is<Value>(lhs.get()) && is<Value>(rhs.get())
&& std::static_pointer_cast<Value>(lhs)->state() == std::static_pointer_cast<Value>(rhs)->state()) {
if (is<Constant>(lhs.get()) && is<Constant>(rhs.get())
&& std::static_pointer_cast<Constant>(lhs)->state() == std::static_pointer_cast<Constant>(rhs)->state()) {
return true;
}
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(
@ -450,7 +450,7 @@ ADD_FUNCTION(
}
}
return makePtr<Value>((result) ? Value::True : Value::False);
return makePtr<Constant>((result) ? Constant::True : Constant::False);
});
// (deref myatom)
@ -521,7 +521,7 @@ ADD_FUNCTION(
nodes.pop_front();
nodes.push_front(atom->deref());
ASTNodePtr value = nullptr;
ValuePtr value = nullptr;
if (is<Function>(second_argument.get())) {
auto function = std::static_pointer_cast<Function>(second_argument)->function();
value = function(nodes);

18
src/printer.cpp

@ -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()) {
init();
@ -40,7 +40,7 @@ std::string Printer::print(ASTNodePtr node, bool 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();
@ -62,7 +62,7 @@ void Printer::init()
m_print = "";
}
void Printer::printImpl(ASTNodePtr node, bool print_readably)
void Printer::printImpl(ValuePtr node, bool print_readably)
{
auto printSpacing = [this]() -> void {
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)) {
printSpacing();
m_print += (is<List>(node_raw_ptr)) ? '(' : '[';
@ -119,13 +119,13 @@ void Printer::printImpl(ASTNodePtr node, bool print_readably)
printSpacing();
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();
std::string value;
switch (std::static_pointer_cast<Value>(node)->state()) {
case Value::Nil: value = "nil"; break;
case Value::True: value = "true"; break;
case Value::False: value = "false"; break;
switch (std::static_pointer_cast<Constant>(node)->state()) {
case Constant::Nil: value = "nil"; break;
case Constant::True: value = "true"; break;
case Constant::False: value = "false"; break;
}
m_print += format("{}", value);
}

6
src/printer.h

@ -17,12 +17,12 @@ public:
Printer();
virtual ~Printer();
std::string print(ASTNodePtr node, bool print_readably = true);
std::string printNoErrorCheck(ASTNodePtr node, bool print_readably = true);
std::string print(ValuePtr node, bool print_readably = true);
std::string printNoErrorCheck(ValuePtr node, bool print_readably = true);
private:
void init();
void printImpl(ASTNodePtr node, bool print_readably = true);
void printImpl(ValuePtr node, bool print_readably = true);
void printError();
bool m_first_node { true };

42
src/reader.cpp

@ -56,7 +56,7 @@ void Reader::read()
}
}
ASTNodePtr Reader::readImpl()
ValuePtr Reader::readImpl()
{
if (m_tokens.size() == 0) {
return nullptr;
@ -124,7 +124,7 @@ ASTNodePtr Reader::readImpl()
return nullptr;
}
ASTNodePtr Reader::readSpliceUnquote()
ValuePtr Reader::readSpliceUnquote()
{
ignore(); // ~@
@ -140,7 +140,7 @@ ASTNodePtr Reader::readSpliceUnquote()
return list;
}
ASTNodePtr Reader::readList()
ValuePtr Reader::readList()
{
ignore(); // (
@ -157,7 +157,7 @@ ASTNodePtr Reader::readList()
return list;
}
ASTNodePtr Reader::readVector()
ValuePtr Reader::readVector()
{
ignore(); // [
@ -173,7 +173,7 @@ ASTNodePtr Reader::readVector()
return vector;
}
ASTNodePtr Reader::readHashMap()
ValuePtr Reader::readHashMap()
{
ignore(); // {
@ -207,7 +207,7 @@ ASTNodePtr Reader::readHashMap()
return hash_map;
}
ASTNodePtr Reader::readQuote()
ValuePtr Reader::readQuote()
{
ignore(); // '
@ -223,7 +223,7 @@ ASTNodePtr Reader::readQuote()
return list;
}
ASTNodePtr Reader::readQuasiQuote()
ValuePtr Reader::readQuasiQuote()
{
ignore(); // `
@ -239,7 +239,7 @@ ASTNodePtr Reader::readQuasiQuote()
return list;
}
ASTNodePtr Reader::readUnquote()
ValuePtr Reader::readUnquote()
{
ignore(); // ~
@ -255,7 +255,7 @@ ASTNodePtr Reader::readUnquote()
return list;
}
ASTNodePtr Reader::readWithMeta()
ValuePtr Reader::readWithMeta()
{
ignore(); // ^
@ -268,15 +268,15 @@ ASTNodePtr Reader::readWithMeta()
auto list = makePtr<List>();
list->add(makePtr<Symbol>("with-meta"));
ASTNodePtr first = readImpl();
ASTNodePtr second = readImpl();
ValuePtr first = readImpl();
ValuePtr second = readImpl();
list->add(second);
list->add(first);
return list;
}
ASTNodePtr Reader::readDeref()
ValuePtr Reader::readDeref()
{
ignore(); // @
@ -292,19 +292,19 @@ ASTNodePtr Reader::readDeref()
return list;
}
ASTNodePtr Reader::readString()
ValuePtr Reader::readString()
{
std::string symbol = consume().symbol;
return makePtr<String>(symbol);
}
ASTNodePtr Reader::readKeyword()
ValuePtr Reader::readKeyword()
{
return makePtr<Keyword>(consume().symbol);
}
ASTNodePtr Reader::readValue()
ValuePtr Reader::readValue()
{
Token token = consume();
char* end_ptr = nullptr;
@ -314,13 +314,13 @@ ASTNodePtr Reader::readValue()
}
if (token.symbol == "nil") {
return makePtr<Value>(Value::Nil);
return makePtr<Constant>(Constant::Nil);
}
else if (token.symbol == "true") {
return makePtr<Value>(Value::True);
return makePtr<Constant>(Constant::True);
}
else if (token.symbol == "false") {
return makePtr<Value>(Value::False);
return makePtr<Constant>(Constant::False);
}
return makePtr<Symbol>(token.symbol);
@ -372,11 +372,11 @@ void Reader::dump()
dumpImpl(m_node);
}
void Reader::dumpImpl(ASTNodePtr node)
void Reader::dumpImpl(ValuePtr node)
{
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)) {
auto nodes = std::static_pointer_cast<List>(node)->nodes();
print("{}", indentation);
@ -406,7 +406,7 @@ void Reader::dumpImpl(ASTNodePtr node)
print(fg(ruc::format::TerminalColor::Yellow), "NumberNode");
print(" <{}>", node);
}
else if (is<Value>(node_raw_ptr)) {
else if (is<Constant>(node_raw_ptr)) {
print("{}", indentation);
print(fg(ruc::format::TerminalColor::Yellow), "ValueNode");
print(" <{}>", node);

32
src/reader.h

@ -25,7 +25,7 @@ public:
void dump();
ASTNodePtr node() { return m_node; }
ValuePtr node() { return m_node; }
private:
bool isEOF() const;
@ -35,21 +35,21 @@ private:
void ignore();
void retreat();
ASTNodePtr readImpl();
ASTNodePtr readSpliceUnquote(); // ~@
ASTNodePtr readList(); // ()
ASTNodePtr readVector(); // []
ASTNodePtr readHashMap(); // {}
ASTNodePtr readQuote(); // '
ASTNodePtr readQuasiQuote(); // `
ASTNodePtr readUnquote(); // ~
ASTNodePtr readWithMeta(); // ^
ASTNodePtr readDeref(); // @
ASTNodePtr readString(); // "foobar"
ASTNodePtr readKeyword(); // :keyword
ASTNodePtr readValue(); // number, "nil", "true", "false", symbol
ValuePtr readImpl();
ValuePtr readSpliceUnquote(); // ~@
ValuePtr readList(); // ()
ValuePtr readVector(); // []
ValuePtr readHashMap(); // {}
ValuePtr readQuote(); // '
ValuePtr readQuasiQuote(); // `
ValuePtr readUnquote(); // ~
ValuePtr readWithMeta(); // ^
ValuePtr readDeref(); // @
ValuePtr readString(); // "foobar"
ValuePtr readKeyword(); // :keyword
ValuePtr readValue(); // number, "nil", "true", "false", symbol
void dumpImpl(ASTNodePtr node);
void dumpImpl(ValuePtr node);
size_t m_index { 0 };
size_t m_indentation { 0 };
@ -59,7 +59,7 @@ private:
bool m_invalid_syntax { false };
bool m_is_unbalanced { false };
ASTNodePtr m_node { nullptr };
ValuePtr m_node { nullptr };
};
} // namespace blaze

6
src/step6_file.cpp

@ -34,7 +34,7 @@ static auto cleanup(int signal) -> void
std::exit(signal);
}
auto read(std::string_view input) -> ASTNodePtr
auto read(std::string_view input) -> ValuePtr
{
Lexer lexer(input);
lexer.tokenize();
@ -51,7 +51,7 @@ auto read(std::string_view input) -> ASTNodePtr
return reader.node();
}
auto eval(ASTNodePtr ast, EnvironmentPtr env) -> ASTNodePtr
auto eval(ValuePtr ast, EnvironmentPtr env) -> ValuePtr
{
if (env == nullptr) {
env = s_outer_env;
@ -63,7 +63,7 @@ auto eval(ASTNodePtr ast, EnvironmentPtr env) -> ASTNodePtr
return eval.ast();
}
static auto print(ASTNodePtr exp) -> std::string
static auto print(ValuePtr exp) -> std::string
{
Printer printer;

Loading…
Cancel
Save