diff --git a/src/ast.cpp b/src/ast.cpp index f1930fc..f4ff91b 100644 --- a/src/ast.cpp +++ b/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 bindings, ASTNodePtr body, EnvironmentPtr env) +Lambda::Lambda(std::vector bindings, ValuePtr body, EnvironmentPtr env) : m_bindings(bindings) , m_body(body) , m_env(env) @@ -90,7 +90,7 @@ Lambda::Lambda(std::vector 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::format(Builder& builder, blaze::ASTNodePtr value) const +void Formatter::format(Builder& builder, blaze::ValuePtr value) const { blaze::Printer printer; return Formatter::format(builder, printer.printNoErrorCheck(value)); diff --git a/src/ast.h b/src/ast.h index 5fa6198..9b3fa0c 100644 --- a/src/ast.h +++ b/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& nodes() const { return m_nodes; } + const std::list& 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 m_nodes; + std::list 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& elements() const { return m_elements; } + const std::unordered_map& 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 m_elements; + std::unordered_map 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)>; +using FunctionType = std::function)>; class Function final : public Callable { public: @@ -236,36 +236,36 @@ private: class Lambda final : public Callable { public: - Lambda(std::vector bindings, ASTNodePtr body, EnvironmentPtr env); + Lambda(std::vector bindings, ValuePtr body, EnvironmentPtr env); virtual ~Lambda() = default; std::vector 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 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 makePtr(Args&&... args) // clang-format off template<> -inline bool ASTNode::fastIs() const { return isCollection(); } +inline bool Value::fastIs() const { return isCollection(); } template<> -inline bool ASTNode::fastIs() const { return isList(); } +inline bool Value::fastIs() const { return isList(); } template<> -inline bool ASTNode::fastIs() const { return isVector(); } +inline bool Value::fastIs() const { return isVector(); } template<> -inline bool ASTNode::fastIs() const { return isHashMap(); } +inline bool Value::fastIs() const { return isHashMap(); } template<> -inline bool ASTNode::fastIs() const { return isString(); } +inline bool Value::fastIs() const { return isString(); } template<> -inline bool ASTNode::fastIs() const { return isKeyword(); } +inline bool Value::fastIs() const { return isKeyword(); } template<> -inline bool ASTNode::fastIs() const { return isNumber(); } +inline bool Value::fastIs() const { return isNumber(); } template<> -inline bool ASTNode::fastIs() const { return isValue(); } +inline bool Value::fastIs() const { return isConstant(); } template<> -inline bool ASTNode::fastIs() const { return isSymbol(); } +inline bool Value::fastIs() const { return isSymbol(); } template<> -inline bool ASTNode::fastIs() const { return isCallable(); } +inline bool Value::fastIs() const { return isCallable(); } template<> -inline bool ASTNode::fastIs() const { return isFunction(); } +inline bool Value::fastIs() const { return isFunction(); } template<> -inline bool ASTNode::fastIs() const { return isLambda(); } +inline bool Value::fastIs() const { return isLambda(); } template<> -inline bool ASTNode::fastIs() const { return isAtom(); } +inline bool Value::fastIs() const { return isAtom(); } // clang-format on } // namespace blaze @@ -324,6 +324,6 @@ inline bool ASTNode::fastIs() const { return isAtom(); } // ----------------------------------------- template<> -struct ruc::format::Formatter : public Formatter { - void format(Builder& builder, blaze::ASTNodePtr value) const; +struct ruc::format::Formatter : public Formatter { + void format(Builder& builder, blaze::ValuePtr value) const; }; diff --git a/src/environment.cpp b/src/environment.cpp index 056d9b9..025774d 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -29,7 +29,7 @@ EnvironmentPtr Environment::create(EnvironmentPtr outer) return env; } -EnvironmentPtr Environment::create(const ASTNodePtr lambda, std::list arguments) +EnvironmentPtr Environment::create(const ValuePtr lambda, std::list arguments) { auto lambda_casted = std::static_pointer_cast(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]; diff --git a/src/environment.h b/src/environment.h index e16ba88..45ae5de 100644 --- a/src/environment.h +++ b/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 arguments); + static EnvironmentPtr create(const ValuePtr lambda, std::list 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 m_values; + std::unordered_map m_values; }; } // namespace blaze diff --git a/src/eval.cpp b/src/eval.cpp index b810cf6..69f88a1 100644 --- a/src/eval.cpp +++ b/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(); + m_ast_stack = std::stack(); m_env_stack = std::stack(); 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(ast_raw_ptr)) { auto result = env->get(std::static_pointer_cast(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& nodes, EnvironmentPtr env) +ValuePtr Eval::evalDef(const std::list& 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& nodes, EnvironmentPtr env) std::string symbol = std::static_pointer_cast(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& nodes, EnvironmentPtr env) return env->set(symbol, value); } -void Eval::evalLet(const std::list& nodes, EnvironmentPtr env) +void Eval::evalLet(const std::list& 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& nodes, EnvironmentPtr env) } // Get the nodes out of the List or Vector - std::list binding_nodes; + std::list binding_nodes; auto bindings = std::static_pointer_cast(first_argument); binding_nodes = bindings->nodes(); @@ -236,7 +236,7 @@ void Eval::evalLet(const std::list& nodes, EnvironmentPtr env) std::string key = std::static_pointer_cast(*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& nodes, EnvironmentPtr env) return; // TCO } -void Eval::evalDo(const std::list& nodes, EnvironmentPtr env) +void Eval::evalDo(const std::list& 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& nodes, EnvironmentPtr env) return; // TCO } -void Eval::evalIf(const std::list& nodes, EnvironmentPtr env) +void Eval::evalIf(const std::list& 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& 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::Nil); + auto third_argument = (nodes.size() == 3) ? *std::next(std::next(nodes.begin())) : makePtr(Constant::Nil); m_ast_stack.push(first_argument); m_env_stack.push(env); auto first_evaluated = evalImpl(); - if (!is(first_evaluated.get()) - || std::static_pointer_cast(first_evaluated)->state() == Value::True) { + if (!is(first_evaluated.get()) + || std::static_pointer_cast(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& nodes, EnvironmentPtr env) AST_CHECK(type, value) \ auto variable = std::static_pointer_cast(value); -ASTNodePtr Eval::evalFn(const std::list& nodes, EnvironmentPtr env) +ValuePtr Eval::evalFn(const std::list& nodes, EnvironmentPtr env) { ARG_COUNT_CHECK("fn*", nodes.size() != 2, nodes.size()); @@ -329,7 +329,7 @@ ASTNodePtr Eval::evalFn(const std::list& nodes, EnvironmentPtr env) return makePtr(bindings, second_argument, env); } -ASTNodePtr Eval::apply(std::shared_ptr evaluated_list) +ValuePtr Eval::apply(std::shared_ptr evaluated_list) { if (evaluated_list == nullptr) { return nullptr; diff --git a/src/eval.h b/src/eval.h index 2fd3838..0ba2850 100644 --- a/src/eval.h +++ b/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& nodes, EnvironmentPtr env); - void evalLet(const std::list& nodes, EnvironmentPtr env); - void evalDo(const std::list& nodes, EnvironmentPtr env); - void evalIf(const std::list& nodes, EnvironmentPtr env); - ASTNodePtr evalFn(const std::list& nodes, EnvironmentPtr env); - ASTNodePtr apply(std::shared_ptr evaluated_list); - - ASTNodePtr m_ast; + ValuePtr evalImpl(); + ValuePtr evalAst(ValuePtr ast, EnvironmentPtr env); + ValuePtr evalDef(const std::list& nodes, EnvironmentPtr env); + void evalLet(const std::list& nodes, EnvironmentPtr env); + void evalDo(const std::list& nodes, EnvironmentPtr env); + void evalIf(const std::list& nodes, EnvironmentPtr env); + ValuePtr evalFn(const std::list& nodes, EnvironmentPtr env); + ValuePtr apply(std::shared_ptr evaluated_list); + + ValuePtr m_ast; EnvironmentPtr m_env; - std::stack m_ast_stack; + std::stack m_ast_stack; std::stack m_env_stack; }; diff --git a/src/forward.h b/src/forward.h index e240ee7..5acb910 100644 --- a/src/forward.h +++ b/src/forward.h @@ -13,8 +13,8 @@ namespace blaze { // ----------------------------------------- // Types -class ASTNode; -typedef std::shared_ptr ASTNodePtr; +class Value; +typedef std::shared_ptr ValuePtr; class Environment; typedef std::shared_ptr EnvironmentPtr; @@ -22,8 +22,8 @@ typedef std::shared_ptr 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); diff --git a/src/functions.cpp b/src/functions.cpp index 37a52b8..37c514f 100644 --- a/src/functions.cpp +++ b/src/functions.cpp @@ -35,7 +35,7 @@ static struct FUNCTION_STRUCT_NAME(unique) \ FUNCTION_STRUCT_NAME(unique)( \ symbol, \ - [](std::list nodes) -> ASTNodePtr lambda); + [](std::list 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((result) ? Value::True : Value::False); \ + return makePtr((result) ? Constant::True : Constant::False); \ } ADD_FUNCTION("<", NUMBER_COMPARE(<)); @@ -197,7 +197,7 @@ ADD_FUNCTION( } } - return makePtr((result) ? Value::True : Value::False); + return makePtr((result) ? Constant::True : Constant::False); }); ADD_FUNCTION( @@ -217,7 +217,7 @@ ADD_FUNCTION( } } - return makePtr((result) ? Value::True : Value::False); + return makePtr((result) ? Constant::True : Constant::False); }); ADD_FUNCTION( @@ -231,7 +231,7 @@ ADD_FUNCTION( auto first_argument = nodes.front(); size_t result = 0; - if (is(first_argument.get()) && std::static_pointer_cast(nodes.front())->state() == Value::Nil) { + if (is(first_argument.get()) && std::static_pointer_cast(nodes.front())->state() == Constant::Nil) { // result = 0 } else if (is(first_argument.get())) { @@ -279,7 +279,7 @@ ADD_FUNCTION("pr-str", PRINTER_STRING(true, " ")); } \ print("\n"); \ \ - return makePtr(Value::Nil); \ + return makePtr(Constant::Nil); \ } ADD_FUNCTION("prn", PRINTER_PRINT(true)); @@ -295,8 +295,8 @@ ADD_FUNCTION( return nullptr; } - std::function equal = - [&equal](ASTNodePtr lhs, ASTNodePtr rhs) -> bool { + std::function equal = + [&equal](ValuePtr lhs, ValuePtr rhs) -> bool { if ((is(lhs.get()) || is(lhs.get())) && (is(rhs.get()) || is(rhs.get()))) { auto lhs_nodes = std::static_pointer_cast(lhs)->nodes(); @@ -347,8 +347,8 @@ ADD_FUNCTION( && std::static_pointer_cast(lhs)->number() == std::static_pointer_cast(rhs)->number()) { return true; } - if (is(lhs.get()) && is(rhs.get()) - && std::static_pointer_cast(lhs)->state() == std::static_pointer_cast(rhs)->state()) { + if (is(lhs.get()) && is(rhs.get()) + && std::static_pointer_cast(lhs)->state() == std::static_pointer_cast(rhs)->state()) { return true; } if (is(lhs.get()) && is(rhs.get()) @@ -369,7 +369,7 @@ ADD_FUNCTION( } } - return makePtr((result) ? Value::True : Value::False); + return makePtr((result) ? Constant::True : Constant::False); }); ADD_FUNCTION( @@ -450,7 +450,7 @@ ADD_FUNCTION( } } - return makePtr((result) ? Value::True : Value::False); + return makePtr((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(second_argument.get())) { auto function = std::static_pointer_cast(second_argument)->function(); value = function(nodes); diff --git a/src/printer.cpp b/src/printer.cpp index a4343bd..d13721b 100644 --- a/src/printer.cpp +++ b/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(node_raw_ptr)) { printSpacing(); m_print += (is(node_raw_ptr)) ? '(' : '['; @@ -119,13 +119,13 @@ void Printer::printImpl(ASTNodePtr node, bool print_readably) printSpacing(); m_print += format("{}", std::static_pointer_cast(node)->number()); } - else if (is(node_raw_ptr)) { + else if (is(node_raw_ptr)) { printSpacing(); std::string value; - switch (std::static_pointer_cast(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(node)->state()) { + case Constant::Nil: value = "nil"; break; + case Constant::True: value = "true"; break; + case Constant::False: value = "false"; break; } m_print += format("{}", value); } diff --git a/src/printer.h b/src/printer.h index 2e982ab..88befcf 100644 --- a/src/printer.h +++ b/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 }; diff --git a/src/reader.cpp b/src/reader.cpp index 3ffcee1..e82c803 100644 --- a/src/reader.cpp +++ b/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->add(makePtr("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(symbol); } -ASTNodePtr Reader::readKeyword() +ValuePtr Reader::readKeyword() { return makePtr(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::Nil); + return makePtr(Constant::Nil); } else if (token.symbol == "true") { - return makePtr(Value::True); + return makePtr(Constant::True); } else if (token.symbol == "false") { - return makePtr(Value::False); + return makePtr(Constant::False); } return makePtr(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(node_raw_ptr)) { auto nodes = std::static_pointer_cast(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(node_raw_ptr)) { + else if (is(node_raw_ptr)) { print("{}", indentation); print(fg(ruc::format::TerminalColor::Yellow), "ValueNode"); print(" <{}>", node); diff --git a/src/reader.h b/src/reader.h index 10bb2cf..d67ba25 100644 --- a/src/reader.h +++ b/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 diff --git a/src/step6_file.cpp b/src/step6_file.cpp index b60ff02..fb488b1 100644 --- a/src/step6_file.cpp +++ b/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;