diff --git a/.clang-format b/.clang-format index 3afd345..c171fd6 100644 --- a/.clang-format +++ b/.clang-format @@ -14,6 +14,7 @@ AlignTrailingComments: true AllowAllArgumentsOnNextLine: false AllowAllConstructorInitializersOnNextLine: true AllowAllParametersOfDeclarationOnNextLine: false +AllowShortCaseLabelsOnASingleLine: true AllowShortLambdasOnASingleLine: All AlwaysBreakTemplateDeclarations: Yes diff --git a/src/ast.cpp b/src/ast.cpp index a756420..c8f05d3 100644 --- a/src/ast.cpp +++ b/src/ast.cpp @@ -56,8 +56,8 @@ Symbol::Symbol(const std::string& symbol) // ----------------------------------------- -Value::Value(const std::string& value) - : m_value(value) +Value::Value(State state) + : m_state(state) { } diff --git a/src/ast.h b/src/ast.h index 363f643..75f32f8 100644 --- a/src/ast.h +++ b/src/ast.h @@ -6,7 +6,7 @@ #pragma once -#include // int64_t +#include // int64_t, uint8_t #include // std::function #include // std::shared_ptr #include @@ -180,15 +180,21 @@ private: // true, false, nil class Value final : public ASTNode { public: - explicit Value(const std::string& value); + enum State : uint8_t { + Nil, + True, + False, + }; + + explicit Value(State state); virtual ~Value() = default; virtual bool isValue() const override { return true; } - const std::string& value() const { return m_value; } + State state() const { return m_state; } private: - std::string m_value; + State m_state; }; // ----------------------------------------- diff --git a/src/lexer.h b/src/lexer.h index c8fbcb2..052fef8 100644 --- a/src/lexer.h +++ b/src/lexer.h @@ -33,7 +33,7 @@ struct Token { At, // @ String, // "foobar" Keyword, // :keyword - Value, // number, "true", "false", "nil", symbol + Value, // number, "nil", "true", "false", symbol Comment, // ; Error, }; diff --git a/src/printer.cpp b/src/printer.cpp index e2cbef8..e7399bb 100644 --- a/src/printer.cpp +++ b/src/printer.cpp @@ -124,6 +124,16 @@ void Printer::printImpl(ASTNodePtr node) printSpacing(); m_print += format("{}", std::static_pointer_cast(node)->number()); } + 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; + } + m_print += format("{}", value); + } else if (is(node_raw_ptr)) { printSpacing(); m_print += format("{}", std::static_pointer_cast(node)->symbol()); diff --git a/src/reader.cpp b/src/reader.cpp index 1b968e7..1e2a77d 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -318,6 +318,16 @@ ASTNodePtr Reader::readValue() return makePtr(result); } + if (token.symbol == "nil") { + return makePtr(Value::Nil); + } + else if (token.symbol == "true") { + return makePtr(Value::True); + } + else if (token.symbol == "false") { + return makePtr(Value::False); + } + return makePtr(token.symbol); } @@ -373,7 +383,7 @@ void Reader::dumpImpl(ASTNodePtr node) ASTNode* node_raw_ptr = node.get(); if (is(node_raw_ptr)) { - List* list = static_cast(node_raw_ptr); + auto list = std::static_pointer_cast(node); print("{}", indentation); print(fg(ruc::format::TerminalColor::Blue), "ListContainer"); print(" <"); @@ -389,22 +399,27 @@ void Reader::dumpImpl(ASTNodePtr node) else if (is(node_raw_ptr)) { print("{}", indentation); print(fg(ruc::format::TerminalColor::Yellow), "StringNode"); - print(" <{}>", static_cast(node_raw_ptr)->data()); + print(" <{}>", node); } else if (is(node_raw_ptr)) { print("{}", indentation); print(fg(ruc::format::TerminalColor::Yellow), "KeywordNode"); - print(" <{}>", static_cast(node_raw_ptr)->keyword()); + print(" <{}>", node); } else if (is(node_raw_ptr)) { print("{}", indentation); print(fg(ruc::format::TerminalColor::Yellow), "NumberNode"); - print(" <{}>", static_cast(node_raw_ptr)->number()); + print(" <{}>", node); + } + else if (is(node_raw_ptr)) { + print("{}", indentation); + print(fg(ruc::format::TerminalColor::Yellow), "ValueNode"); + print(" <{}>", node); } else if (is(node_raw_ptr)) { print("{}", indentation); print(fg(ruc::format::TerminalColor::Yellow), "SymbolNode"); - print(" <{}>", static_cast(node_raw_ptr)->symbol()); + print(" <{}>", node); } print("\n"); } diff --git a/src/reader.h b/src/reader.h index 981fcd5..10bb2cf 100644 --- a/src/reader.h +++ b/src/reader.h @@ -47,7 +47,7 @@ private: ASTNodePtr readDeref(); // @ ASTNodePtr readString(); // "foobar" ASTNodePtr readKeyword(); // :keyword - ASTNodePtr readValue(); // number, "true", "false", "nil", symbol + ASTNodePtr readValue(); // number, "nil", "true", "false", symbol void dumpImpl(ASTNodePtr node);