Compare commits
2
Commits
f132397e15
...
6573ac0b22
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6573ac0b22 | ||
|
|
424bbcc834 |
@@ -14,6 +14,7 @@ AlignTrailingComments: true
|
||||
AllowAllArgumentsOnNextLine: false
|
||||
AllowAllConstructorInitializersOnNextLine: true
|
||||
AllowAllParametersOfDeclarationOnNextLine: false
|
||||
AllowShortCaseLabelsOnASingleLine: true
|
||||
AllowShortLambdasOnASingleLine: All
|
||||
|
||||
AlwaysBreakTemplateDeclarations: Yes
|
||||
|
||||
+2
-2
@@ -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)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
// -----------------------------------------
|
||||
|
||||
@@ -57,6 +57,16 @@ GlobalEnvironment::GlobalEnvironment()
|
||||
sub();
|
||||
mul();
|
||||
div();
|
||||
|
||||
lt();
|
||||
lte();
|
||||
gt();
|
||||
gte();
|
||||
|
||||
list();
|
||||
is_list();
|
||||
is_empty();
|
||||
count();
|
||||
}
|
||||
|
||||
} // namespace blaze
|
||||
|
||||
+16
-5
@@ -38,11 +38,22 @@ public:
|
||||
virtual ~GlobalEnvironment() = default;
|
||||
|
||||
private:
|
||||
// TODO: Add more native functions
|
||||
void add();
|
||||
void sub();
|
||||
void mul();
|
||||
void div();
|
||||
void add(); // +
|
||||
void sub(); // -
|
||||
void mul(); // *
|
||||
void div(); // /
|
||||
|
||||
void lt(); // <
|
||||
void lte(); // <=
|
||||
void gt(); // >
|
||||
void gte(); // >=
|
||||
|
||||
void list(); // list
|
||||
void is_list(); // list?
|
||||
void is_empty(); // empty?
|
||||
void count(); // count
|
||||
|
||||
// void equal(); // =
|
||||
};
|
||||
|
||||
} // namespace blaze
|
||||
|
||||
+142
-6
@@ -38,8 +38,6 @@ void GlobalEnvironment::add()
|
||||
void GlobalEnvironment::sub()
|
||||
{
|
||||
auto sub = [](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
int64_t result = 0;
|
||||
|
||||
if (nodes.size() == 0) {
|
||||
return makePtr<Number>(0);
|
||||
}
|
||||
@@ -52,7 +50,7 @@ void GlobalEnvironment::sub()
|
||||
}
|
||||
|
||||
// Start with the first number
|
||||
result += std::static_pointer_cast<Number>(nodes[0])->number();
|
||||
int64_t result = std::static_pointer_cast<Number>(nodes[0])->number();
|
||||
|
||||
// Skip the first node
|
||||
for (auto it = std::next(nodes.begin()); it != nodes.end(); ++it) {
|
||||
@@ -88,8 +86,6 @@ void GlobalEnvironment::mul()
|
||||
void GlobalEnvironment::div()
|
||||
{
|
||||
auto div = [this](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
double result = 0;
|
||||
|
||||
if (nodes.size() == 0) {
|
||||
Error::the().addError(format("wrong number of arguments: {}, 0", m_current_key));
|
||||
return nullptr;
|
||||
@@ -103,7 +99,7 @@ void GlobalEnvironment::div()
|
||||
}
|
||||
|
||||
// Start with the first number
|
||||
result += std::static_pointer_cast<Number>(nodes[0])->number();
|
||||
double result = std::static_pointer_cast<Number>(nodes[0])->number();
|
||||
|
||||
// Skip the first node
|
||||
for (auto it = std::next(nodes.begin()); it != nodes.end(); ++it) {
|
||||
@@ -116,4 +112,144 @@ void GlobalEnvironment::div()
|
||||
m_values.emplace("/", makePtr<Function>(div));
|
||||
}
|
||||
|
||||
// -----------------------------------------
|
||||
|
||||
#define NUMBER_COMPARE(symbol, comparison_symbol) \
|
||||
auto lambda = [this](std::span<ASTNodePtr> nodes) -> ASTNodePtr { \
|
||||
bool result = true; \
|
||||
\
|
||||
if (nodes.size() < 2) { \
|
||||
Error::the().addError(format("wrong number of arguments: {}, {}", m_current_key, nodes.size() - 1)); \
|
||||
return nullptr; \
|
||||
} \
|
||||
\
|
||||
for (auto node : nodes) { \
|
||||
if (!is<Number>(node.get())) { \
|
||||
Error::the().addError(format("wrong argument type: number, '{}'", node)); \
|
||||
return nullptr; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
/* Start with the first number */ \
|
||||
int64_t number = std::static_pointer_cast<Number>(nodes[0])->number(); \
|
||||
\
|
||||
/* Skip the first node */ \
|
||||
for (auto it = std::next(nodes.begin()); it != nodes.end(); ++it) { \
|
||||
int64_t current_number = std::static_pointer_cast<Number>(*it)->number(); \
|
||||
if (number comparison_symbol current_number) { \
|
||||
result = false; \
|
||||
break; \
|
||||
} \
|
||||
number = current_number; \
|
||||
} \
|
||||
\
|
||||
return makePtr<Value>((result) ? Value::True : Value::False); \
|
||||
}; \
|
||||
\
|
||||
m_values.emplace(symbol, makePtr<Function>(lambda));
|
||||
|
||||
void GlobalEnvironment::lt()
|
||||
{
|
||||
NUMBER_COMPARE("<", >=);
|
||||
}
|
||||
|
||||
void GlobalEnvironment::lte()
|
||||
{
|
||||
NUMBER_COMPARE("<=", >);
|
||||
}
|
||||
|
||||
void GlobalEnvironment::gt()
|
||||
{
|
||||
NUMBER_COMPARE(">", <=);
|
||||
}
|
||||
|
||||
void GlobalEnvironment::gte()
|
||||
{
|
||||
NUMBER_COMPARE(">=", <);
|
||||
}
|
||||
|
||||
// -----------------------------------------
|
||||
|
||||
void GlobalEnvironment::list()
|
||||
{
|
||||
auto list = [](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
auto list = makePtr<List>();
|
||||
|
||||
for (auto node : nodes) {
|
||||
list->addNode(node);
|
||||
}
|
||||
|
||||
return list;
|
||||
};
|
||||
|
||||
m_values.emplace("list", makePtr<Function>(list));
|
||||
}
|
||||
|
||||
void GlobalEnvironment::is_list()
|
||||
{
|
||||
auto is_list = [](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
bool result = true;
|
||||
|
||||
for (auto node : nodes) {
|
||||
if (!is<List>(node.get())) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return makePtr<Value>((result) ? Value::True : Value::False);
|
||||
};
|
||||
|
||||
m_values.emplace("list?", makePtr<Function>(is_list));
|
||||
}
|
||||
|
||||
void GlobalEnvironment::is_empty()
|
||||
{
|
||||
auto is_empty = [](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
bool result = true;
|
||||
|
||||
for (auto node : nodes) {
|
||||
if (!is<List>(node.get())) {
|
||||
Error::the().addError(format("wrong argument type: list, '{}'", node));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!std::static_pointer_cast<List>(node)->empty()) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return makePtr<Value>((result) ? Value::True : Value::False);
|
||||
};
|
||||
|
||||
m_values.emplace("empty?", makePtr<Function>(is_empty));
|
||||
}
|
||||
|
||||
void GlobalEnvironment::count()
|
||||
{
|
||||
auto count = [this](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
if (nodes.size() > 1) {
|
||||
Error::the().addError(format("wrong number of arguments: {}, {}", m_current_key, nodes.size() - 1));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
size_t result = 0;
|
||||
|
||||
for (auto node : nodes) {
|
||||
if (!is<List>(node.get())) {
|
||||
Error::the().addError(format("wrong argument type: list, '{}'", node));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
result = std::static_pointer_cast<List>(node)->size();
|
||||
}
|
||||
|
||||
// FIXME: Add numeric_limits check for implicit cast: size_t > int64_t
|
||||
return makePtr<Number>((int64_t)result);
|
||||
};
|
||||
|
||||
m_values.emplace("count", makePtr<Function>(count));
|
||||
}
|
||||
|
||||
} // namespace blaze
|
||||
|
||||
+1
-1
@@ -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,
|
||||
};
|
||||
|
||||
@@ -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
@@ -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
@@ -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);
|
||||
|
||||
|
||||
+1
-1
@@ -16,7 +16,7 @@
|
||||
#include "readline.h"
|
||||
#include "settings.h"
|
||||
|
||||
#if 1
|
||||
#if 0
|
||||
static blaze::EnvironmentPtr env = blaze::makePtr<blaze::GlobalEnvironment>();
|
||||
|
||||
auto read(std::string_view input) -> blaze::ASTNodePtr
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
#include <csignal> // std::signal
|
||||
#include <cstdlib> // std::exit
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include "ruc/argparser.h"
|
||||
#include "ruc/format/color.h"
|
||||
|
||||
#include "ast.h"
|
||||
#include "environment.h"
|
||||
#include "error.h"
|
||||
#include "eval.h"
|
||||
#include "lexer.h"
|
||||
#include "printer.h"
|
||||
#include "reader.h"
|
||||
#include "readline.h"
|
||||
#include "settings.h"
|
||||
|
||||
#if 1
|
||||
static blaze::EnvironmentPtr env = blaze::makePtr<blaze::GlobalEnvironment>();
|
||||
|
||||
auto read(std::string_view input) -> blaze::ASTNodePtr
|
||||
{
|
||||
blaze::Lexer lexer(input);
|
||||
lexer.tokenize();
|
||||
if (blaze::Settings::the().get("dump-lexer") == "1") {
|
||||
lexer.dump();
|
||||
}
|
||||
|
||||
blaze::Reader reader(std::move(lexer.tokens()));
|
||||
reader.read();
|
||||
if (blaze::Settings::the().get("dump-reader") == "1") {
|
||||
reader.dump();
|
||||
}
|
||||
|
||||
return reader.node();
|
||||
}
|
||||
|
||||
auto eval(blaze::ASTNodePtr ast) -> blaze::ASTNodePtr
|
||||
{
|
||||
blaze::Eval eval(ast, env);
|
||||
eval.eval();
|
||||
|
||||
return eval.ast();
|
||||
}
|
||||
|
||||
auto print(blaze::ASTNodePtr exp) -> std::string
|
||||
{
|
||||
blaze::Printer printer;
|
||||
|
||||
return printer.print(exp);
|
||||
}
|
||||
|
||||
auto rep(std::string_view input) -> std::string
|
||||
{
|
||||
blaze::Error::the().clearErrors();
|
||||
blaze::Error::the().setInput(input);
|
||||
|
||||
return print(eval(read(input)));
|
||||
}
|
||||
|
||||
static auto cleanup(int signal) -> void
|
||||
{
|
||||
print("\033[0m\n");
|
||||
std::exit(signal);
|
||||
}
|
||||
|
||||
auto main(int argc, char* argv[]) -> int
|
||||
{
|
||||
bool dump_lexer = false;
|
||||
bool dump_reader = false;
|
||||
bool pretty_print = false;
|
||||
std::string_view history_path = "~/.mal-history";
|
||||
|
||||
// CLI arguments
|
||||
ruc::ArgParser arg_parser;
|
||||
arg_parser.addOption(dump_lexer, 'l', "dump-lexer", nullptr, nullptr);
|
||||
arg_parser.addOption(dump_reader, 'r', "dump-reader", nullptr, nullptr);
|
||||
arg_parser.addOption(pretty_print, 'c', "color", nullptr, nullptr);
|
||||
arg_parser.addOption(history_path, 'h', "history", nullptr, nullptr, nullptr, ruc::ArgParser::Required::Yes);
|
||||
arg_parser.parse(argc, argv);
|
||||
|
||||
// Set settings
|
||||
blaze::Settings::the().set("dump-lexer", dump_lexer ? "1" : "0");
|
||||
blaze::Settings::the().set("dump-reader", dump_reader ? "1" : "0");
|
||||
blaze::Settings::the().set("pretty-print", pretty_print ? "1" : "0");
|
||||
|
||||
// Signal callbacks
|
||||
std::signal(SIGINT, cleanup);
|
||||
std::signal(SIGTERM, cleanup);
|
||||
|
||||
blaze::Readline readline(pretty_print, history_path);
|
||||
|
||||
std::string input;
|
||||
while (readline.get(input)) {
|
||||
if (pretty_print) {
|
||||
print("\033[0m");
|
||||
}
|
||||
print("{}\n", rep(input));
|
||||
}
|
||||
|
||||
if (pretty_print) {
|
||||
print("\033[0m");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user