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

This commit is contained in:
Riyyi
2023-03-26 22:38:53 +02:00
parent f132397e15
commit 424bbcc834
7 changed files with 45 additions and 13 deletions
+1
View File
@@ -14,6 +14,7 @@ AlignTrailingComments: true
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortCaseLabelsOnASingleLine: true
AllowShortLambdasOnASingleLine: All
AlwaysBreakTemplateDeclarations: Yes
+2 -2
View File
@@ -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)
{
}
+10 -4
View File
@@ -6,7 +6,7 @@
#pragma once
#include <cstdint> // int64_t
#include <cstdint> // int64_t, uint8_t
#include <functional> // std::function
#include <memory> // std::shared_ptr
#include <span>
@@ -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;
};
// -----------------------------------------
+1 -1
View File
@@ -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,
};
+10
View File
@@ -124,6 +124,16 @@ void Printer::printImpl(ASTNodePtr node)
printSpacing();
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)) {
printSpacing();
m_print += format("{}", std::static_pointer_cast<Symbol>(node)->symbol());
+20 -5
View File
@@ -318,6 +318,16 @@ ASTNodePtr Reader::readValue()
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);
}
@@ -373,7 +383,7 @@ void Reader::dumpImpl(ASTNodePtr node)
ASTNode* node_raw_ptr = node.get();
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(fg(ruc::format::TerminalColor::Blue), "ListContainer");
print(" <");
@@ -389,22 +399,27 @@ void Reader::dumpImpl(ASTNodePtr node)
else if (is<String>(node_raw_ptr)) {
print("{}", indentation);
print(fg(ruc::format::TerminalColor::Yellow), "StringNode");
print(" <{}>", static_cast<String*>(node_raw_ptr)->data());
print(" <{}>", node);
}
else if (is<Keyword>(node_raw_ptr)) {
print("{}", indentation);
print(fg(ruc::format::TerminalColor::Yellow), "KeywordNode");
print(" <{}>", static_cast<Keyword*>(node_raw_ptr)->keyword());
print(" <{}>", node);
}
else if (is<Number>(node_raw_ptr)) {
print("{}", indentation);
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)) {
print("{}", indentation);
print(fg(ruc::format::TerminalColor::Yellow), "SymbolNode");
print(" <{}>", static_cast<Symbol*>(node_raw_ptr)->symbol());
print(" <{}>", node);
}
print("\n");
}
+1 -1
View File
@@ -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);