Browse Source

Everywhere: Add support for nil, true, false data types

master
Riyyi 2 years ago
parent
commit
424bbcc834
  1. 1
      .clang-format
  2. 4
      src/ast.cpp
  3. 14
      src/ast.h
  4. 2
      src/lexer.h
  5. 10
      src/printer.cpp
  6. 25
      src/reader.cpp
  7. 2
      src/reader.h

1
.clang-format

@ -14,6 +14,7 @@ AlignTrailingComments: true
AllowAllArgumentsOnNextLine: false AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: true AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: false AllowAllParametersOfDeclarationOnNextLine: false
AllowShortCaseLabelsOnASingleLine: true
AllowShortLambdasOnASingleLine: All AllowShortLambdasOnASingleLine: All
AlwaysBreakTemplateDeclarations: Yes AlwaysBreakTemplateDeclarations: Yes

4
src/ast.cpp

@ -56,8 +56,8 @@ Symbol::Symbol(const std::string& symbol)
// ----------------------------------------- // -----------------------------------------
Value::Value(const std::string& value) Value::Value(State state)
: m_value(value) : m_state(state)
{ {
} }

14
src/ast.h

@ -6,7 +6,7 @@
#pragma once #pragma once
#include <cstdint> // int64_t #include <cstdint> // int64_t, uint8_t
#include <functional> // std::function #include <functional> // std::function
#include <memory> // std::shared_ptr #include <memory> // std::shared_ptr
#include <span> #include <span>
@ -180,15 +180,21 @@ private:
// true, false, nil // true, false, nil
class Value final : public ASTNode { class Value final : public ASTNode {
public: public:
explicit Value(const std::string& value); enum State : uint8_t {
Nil,
True,
False,
};
explicit Value(State state);
virtual ~Value() = default; virtual ~Value() = default;
virtual bool isValue() const override { return true; } virtual bool isValue() const override { return true; }
const std::string& value() const { return m_value; } State state() const { return m_state; }
private: private:
std::string m_value; State m_state;
}; };
// ----------------------------------------- // -----------------------------------------

2
src/lexer.h

@ -33,7 +33,7 @@ struct Token {
At, // @ At, // @
String, // "foobar" String, // "foobar"
Keyword, // :keyword Keyword, // :keyword
Value, // number, "true", "false", "nil", symbol Value, // number, "nil", "true", "false", symbol
Comment, // ; Comment, // ;
Error, Error,
}; };

10
src/printer.cpp

@ -124,6 +124,16 @@ void Printer::printImpl(ASTNodePtr node)
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)) {
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;
}
m_print += format("{}", value);
}
else if (is<Symbol>(node_raw_ptr)) { else if (is<Symbol>(node_raw_ptr)) {
printSpacing(); printSpacing();
m_print += format("{}", std::static_pointer_cast<Symbol>(node)->symbol()); m_print += format("{}", std::static_pointer_cast<Symbol>(node)->symbol());

25
src/reader.cpp

@ -318,6 +318,16 @@ ASTNodePtr Reader::readValue()
return makePtr<Number>(result); return makePtr<Number>(result);
} }
if (token.symbol == "nil") {
return makePtr<Value>(Value::Nil);
}
else if (token.symbol == "true") {
return makePtr<Value>(Value::True);
}
else if (token.symbol == "false") {
return makePtr<Value>(Value::False);
}
return makePtr<Symbol>(token.symbol); return makePtr<Symbol>(token.symbol);
} }
@ -373,7 +383,7 @@ void Reader::dumpImpl(ASTNodePtr node)
ASTNode* node_raw_ptr = node.get(); ASTNode* node_raw_ptr = node.get();
if (is<List>(node_raw_ptr)) { if (is<List>(node_raw_ptr)) {
List* list = static_cast<List*>(node_raw_ptr); auto list = std::static_pointer_cast<List>(node);
print("{}", indentation); print("{}", indentation);
print(fg(ruc::format::TerminalColor::Blue), "ListContainer"); print(fg(ruc::format::TerminalColor::Blue), "ListContainer");
print(" <"); print(" <");
@ -389,22 +399,27 @@ void Reader::dumpImpl(ASTNodePtr node)
else if (is<String>(node_raw_ptr)) { else if (is<String>(node_raw_ptr)) {
print("{}", indentation); print("{}", indentation);
print(fg(ruc::format::TerminalColor::Yellow), "StringNode"); print(fg(ruc::format::TerminalColor::Yellow), "StringNode");
print(" <{}>", static_cast<String*>(node_raw_ptr)->data()); print(" <{}>", node);
} }
else if (is<Keyword>(node_raw_ptr)) { else if (is<Keyword>(node_raw_ptr)) {
print("{}", indentation); print("{}", indentation);
print(fg(ruc::format::TerminalColor::Yellow), "KeywordNode"); print(fg(ruc::format::TerminalColor::Yellow), "KeywordNode");
print(" <{}>", static_cast<Keyword*>(node_raw_ptr)->keyword()); print(" <{}>", node);
} }
else if (is<Number>(node_raw_ptr)) { else if (is<Number>(node_raw_ptr)) {
print("{}", indentation); print("{}", indentation);
print(fg(ruc::format::TerminalColor::Yellow), "NumberNode"); print(fg(ruc::format::TerminalColor::Yellow), "NumberNode");
print(" <{}>", static_cast<Number*>(node_raw_ptr)->number()); print(" <{}>", node);
}
else if (is<Value>(node_raw_ptr)) {
print("{}", indentation);
print(fg(ruc::format::TerminalColor::Yellow), "ValueNode");
print(" <{}>", node);
} }
else if (is<Symbol>(node_raw_ptr)) { else if (is<Symbol>(node_raw_ptr)) {
print("{}", indentation); print("{}", indentation);
print(fg(ruc::format::TerminalColor::Yellow), "SymbolNode"); print(fg(ruc::format::TerminalColor::Yellow), "SymbolNode");
print(" <{}>", static_cast<Symbol*>(node_raw_ptr)->symbol()); print(" <{}>", node);
} }
print("\n"); print("\n");
} }

2
src/reader.h

@ -47,7 +47,7 @@ private:
ASTNodePtr readDeref(); // @ ASTNodePtr readDeref(); // @
ASTNodePtr readString(); // "foobar" ASTNodePtr readString(); // "foobar"
ASTNodePtr readKeyword(); // :keyword ASTNodePtr readKeyword(); // :keyword
ASTNodePtr readValue(); // number, "true", "false", "nil", symbol ASTNodePtr readValue(); // number, "nil", "true", "false", symbol
void dumpImpl(ASTNodePtr node); void dumpImpl(ASTNodePtr node);

Loading…
Cancel
Save