Compare commits
7
Commits
6573ac0b22
...
1e76d4599a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1e76d4599a | ||
|
|
bbced6f487 | ||
|
|
38d8daa9d0 | ||
|
|
35a32678d0 | ||
|
|
de2a207fcb | ||
|
|
9c1c5114a9 | ||
|
|
c1e4b6c6d1 |
@@ -95,3 +95,7 @@ add_dependencies(test2 ${PROJECT})
|
||||
add_custom_target(test3
|
||||
COMMAND env STEP=step_env MAL_IMPL=js ../vendor/mal/runtest.py --deferrable --optional ../vendor/mal/tests/step3_env.mal -- ./${PROJECT})
|
||||
add_dependencies(test3 ${PROJECT})
|
||||
|
||||
add_custom_target(test4
|
||||
COMMAND env STEP=step_env MAL_IMPL=js ../vendor/mal/runtest.py --deferrable --optional ../vendor/mal/tests/step4_if_fn_do.mal -- ./${PROJECT})
|
||||
add_dependencies(test4 ${PROJECT})
|
||||
|
||||
@@ -8,13 +8,13 @@
|
||||
|
||||
#include <cstdint> // int64_t, uint8_t
|
||||
#include <functional> // std::function
|
||||
#include <memory> // std::shared_ptr
|
||||
#include <list>
|
||||
#include <memory> // std::shared_ptr
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <typeinfo> // typeid
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "ruc/format/formatter.h"
|
||||
|
||||
@@ -33,9 +33,9 @@ public:
|
||||
bool fastIs() const = delete;
|
||||
|
||||
virtual bool isCollection() const { return false; }
|
||||
virtual bool isList() const { return false; }
|
||||
virtual bool isVector() const { return false; }
|
||||
virtual bool isHashMap() const { return false; }
|
||||
virtual bool isList() const { return false; }
|
||||
virtual bool isString() const { return false; }
|
||||
virtual bool isKeyword() const { return false; }
|
||||
virtual bool isNumber() const { return false; }
|
||||
@@ -57,7 +57,7 @@ public:
|
||||
|
||||
void addNode(ASTNodePtr node);
|
||||
|
||||
const std::vector<ASTNodePtr>& nodes() const { return m_nodes; }
|
||||
const std::list<ASTNodePtr>& nodes() const { return m_nodes; }
|
||||
size_t size() const { return m_nodes.size(); }
|
||||
bool empty() const { return m_nodes.size() == 0; }
|
||||
|
||||
@@ -65,7 +65,7 @@ protected:
|
||||
Collection() {}
|
||||
|
||||
private:
|
||||
std::vector<ASTNodePtr> m_nodes;
|
||||
std::list<ASTNodePtr> m_nodes;
|
||||
};
|
||||
|
||||
// -----------------------------------------
|
||||
@@ -161,22 +161,6 @@ private:
|
||||
|
||||
// -----------------------------------------
|
||||
|
||||
// Symbols
|
||||
class Symbol final : public ASTNode {
|
||||
public:
|
||||
explicit Symbol(const std::string& symbol);
|
||||
virtual ~Symbol() = default;
|
||||
|
||||
virtual bool isSymbol() const override { return true; }
|
||||
|
||||
const std::string& symbol() const { return m_symbol; }
|
||||
|
||||
private:
|
||||
std::string m_symbol;
|
||||
};
|
||||
|
||||
// -----------------------------------------
|
||||
|
||||
// true, false, nil
|
||||
class Value final : public ASTNode {
|
||||
public:
|
||||
@@ -199,7 +183,23 @@ private:
|
||||
|
||||
// -----------------------------------------
|
||||
|
||||
using Lambda = std::function<ASTNodePtr(std::span<ASTNodePtr>)>;
|
||||
// Symbols
|
||||
class Symbol final : public ASTNode {
|
||||
public:
|
||||
explicit Symbol(const std::string& symbol);
|
||||
virtual ~Symbol() = default;
|
||||
|
||||
virtual bool isSymbol() const override { return true; }
|
||||
|
||||
const std::string& symbol() const { return m_symbol; }
|
||||
|
||||
private:
|
||||
std::string m_symbol;
|
||||
};
|
||||
|
||||
// -----------------------------------------
|
||||
|
||||
using Lambda = std::function<ASTNodePtr(std::list<ASTNodePtr>)>;
|
||||
|
||||
class Function final : public ASTNode {
|
||||
public:
|
||||
@@ -247,10 +247,10 @@ template<>
|
||||
inline bool ASTNode::fastIs<Number>() const { return isNumber(); }
|
||||
|
||||
template<>
|
||||
inline bool ASTNode::fastIs<Symbol>() const { return isSymbol(); }
|
||||
inline bool ASTNode::fastIs<Value>() const { return isValue(); }
|
||||
|
||||
template<>
|
||||
inline bool ASTNode::fastIs<Value>() const { return isValue(); }
|
||||
inline bool ASTNode::fastIs<Symbol>() const { return isSymbol(); }
|
||||
|
||||
template<>
|
||||
inline bool ASTNode::fastIs<Function>() const { return isFunction(); }
|
||||
|
||||
+9
-2
@@ -64,9 +64,16 @@ GlobalEnvironment::GlobalEnvironment()
|
||||
gte();
|
||||
|
||||
list();
|
||||
is_list();
|
||||
is_empty();
|
||||
isList();
|
||||
isEmpty();
|
||||
count();
|
||||
|
||||
str();
|
||||
prStr();
|
||||
prn();
|
||||
println();
|
||||
|
||||
equal();
|
||||
}
|
||||
|
||||
} // namespace blaze
|
||||
|
||||
+10
-5
@@ -48,12 +48,17 @@ private:
|
||||
void gt(); // >
|
||||
void gte(); // >=
|
||||
|
||||
void list(); // list
|
||||
void is_list(); // list?
|
||||
void is_empty(); // empty?
|
||||
void count(); // count
|
||||
void list(); // list
|
||||
void isList(); // list?
|
||||
void isEmpty(); // empty?
|
||||
void count(); // count
|
||||
|
||||
// void equal(); // =
|
||||
void str(); // str
|
||||
void prStr(); // pr-str
|
||||
void prn(); // prn
|
||||
void println(); // println
|
||||
|
||||
void equal(); // =
|
||||
};
|
||||
|
||||
} // namespace blaze
|
||||
|
||||
+81
-30
@@ -4,10 +4,11 @@
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#include <iterator> // sd::advance, std::next, std::prev
|
||||
#include <list>
|
||||
#include <memory> // std::static_pointer_cast
|
||||
#include <span> // std::span
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "ast.h"
|
||||
#include "environment.h"
|
||||
@@ -47,14 +48,21 @@ ASTNodePtr Eval::evalImpl(ASTNodePtr ast, EnvironmentPtr env)
|
||||
|
||||
// Environment
|
||||
auto nodes = list->nodes();
|
||||
if (is<Symbol>(nodes[0].get())) {
|
||||
auto symbol = std::static_pointer_cast<Symbol>(nodes[0])->symbol();
|
||||
if (is<Symbol>(nodes.front().get())) {
|
||||
auto symbol = std::static_pointer_cast<Symbol>(nodes.front())->symbol();
|
||||
nodes.pop_front();
|
||||
if (symbol == "def!") {
|
||||
return evalDef(nodes, env);
|
||||
}
|
||||
if (symbol == "let*") {
|
||||
return evalLet(nodes, env);
|
||||
}
|
||||
if (symbol == "do") {
|
||||
return evalDo(nodes, env);
|
||||
}
|
||||
if (symbol == "if") {
|
||||
return evalIf(nodes, env);
|
||||
}
|
||||
}
|
||||
|
||||
// Function call
|
||||
@@ -116,21 +124,24 @@ ASTNodePtr Eval::evalAst(ASTNodePtr ast, EnvironmentPtr env)
|
||||
return ast;
|
||||
}
|
||||
|
||||
ASTNodePtr Eval::evalDef(const std::vector<ASTNodePtr>& nodes, EnvironmentPtr env)
|
||||
ASTNodePtr Eval::evalDef(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
|
||||
{
|
||||
if (nodes.size() != 3) {
|
||||
Error::the().addError(format("wrong number of arguments: def!, {}", nodes.size() - 1));
|
||||
if (nodes.size() != 2) {
|
||||
Error::the().addError(format("wrong number of arguments: def!, {}", nodes.size()));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto first_argument = *nodes.begin();
|
||||
auto second_argument = *std::next(nodes.begin());
|
||||
|
||||
// First element needs to be a Symbol
|
||||
if (!is<Symbol>(nodes[1].get())) {
|
||||
Error::the().addError(format("wrong type argument: symbol, {}", nodes[1]));
|
||||
if (!is<Symbol>(first_argument.get())) {
|
||||
Error::the().addError(format("wrong type argument: symbol, {}", first_argument));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::string symbol = std::static_pointer_cast<Symbol>(nodes[1])->symbol();
|
||||
ASTNodePtr value = evalImpl(nodes[2], env);
|
||||
std::string symbol = std::static_pointer_cast<Symbol>(first_argument)->symbol();
|
||||
ASTNodePtr value = evalImpl(second_argument, env);
|
||||
|
||||
// Dont overwrite symbols after an error
|
||||
if (Error::the().hasAnyError()) {
|
||||
@@ -141,27 +152,30 @@ ASTNodePtr Eval::evalDef(const std::vector<ASTNodePtr>& nodes, EnvironmentPtr en
|
||||
return env->set(symbol, value);
|
||||
}
|
||||
|
||||
ASTNodePtr Eval::evalLet(const std::vector<ASTNodePtr>& nodes, EnvironmentPtr env)
|
||||
ASTNodePtr Eval::evalLet(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
|
||||
{
|
||||
if (nodes.size() != 3) {
|
||||
Error::the().addError(format("wrong number of arguments: let*, {}", nodes.size() - 1));
|
||||
if (nodes.size() != 2) {
|
||||
Error::the().addError(format("wrong number of arguments: let*, {}", nodes.size()));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto first_argument = *nodes.begin();
|
||||
auto second_argument = *std::next(nodes.begin());
|
||||
|
||||
// First argument needs to be a List or Vector
|
||||
if (!is<List>(nodes[1].get()) && !is<Vector>(nodes[1].get())) {
|
||||
Error::the().addError(format("wrong argument type: list, '{}'", nodes[1]));
|
||||
if (!is<List>(first_argument.get()) && !is<Vector>(first_argument.get())) {
|
||||
Error::the().addError(format("wrong argument type: list, '{}'", first_argument));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Get the nodes out of the List or Vector
|
||||
std::vector<ASTNodePtr> binding_nodes;
|
||||
if (is<List>(nodes[1].get())) {
|
||||
auto bindings = std::static_pointer_cast<List>(nodes[1]);
|
||||
std::list<ASTNodePtr> binding_nodes;
|
||||
if (is<List>(first_argument.get())) {
|
||||
auto bindings = std::static_pointer_cast<List>(first_argument);
|
||||
binding_nodes = bindings->nodes();
|
||||
}
|
||||
else {
|
||||
auto bindings = std::static_pointer_cast<Vector>(nodes[1]);
|
||||
auto bindings = std::static_pointer_cast<Vector>(first_argument);
|
||||
binding_nodes = bindings->nodes();
|
||||
}
|
||||
|
||||
@@ -175,21 +189,58 @@ ASTNodePtr Eval::evalLet(const std::vector<ASTNodePtr>& nodes, EnvironmentPtr en
|
||||
// Create new environment
|
||||
auto let_env = makePtr<Environment>(env);
|
||||
|
||||
for (size_t i = 0; i < count; i += 2) {
|
||||
for (auto it = binding_nodes.begin(); it != binding_nodes.end(); std::advance(it, 2)) {
|
||||
// First element needs to be a Symbol
|
||||
if (!is<Symbol>(binding_nodes[i].get())) {
|
||||
Error::the().addError(format("wrong argument type: symbol, '{}'", binding_nodes[i]));
|
||||
if (!is<Symbol>(*it->get())) {
|
||||
Error::the().addError(format("wrong argument type: symbol, '{}'", *it));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::string key = std::static_pointer_cast<Symbol>(binding_nodes[i])->symbol();
|
||||
ASTNodePtr value = evalImpl(binding_nodes[i + 1], let_env);
|
||||
std::string key = std::static_pointer_cast<Symbol>(*it)->symbol();
|
||||
ASTNodePtr value = evalImpl(*std::next(it), let_env);
|
||||
let_env->set(key, value);
|
||||
}
|
||||
|
||||
// TODO: Remove limitation of 3 arguments
|
||||
// Eval all values in this new env, return last sexp of the result
|
||||
return evalImpl(nodes[2], let_env);
|
||||
return evalImpl(second_argument, let_env);
|
||||
}
|
||||
|
||||
ASTNodePtr Eval::evalDo(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
|
||||
{
|
||||
if (nodes.size() == 0) {
|
||||
Error::the().addError(format("wrong number of arguments: do, {}", nodes.size()));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Evaluate all nodes except the last
|
||||
for (auto it = nodes.begin(); it != std::prev(nodes.end(), 1); ++it) {
|
||||
evalImpl(*it, env);
|
||||
}
|
||||
|
||||
// Eval and return last node
|
||||
return evalImpl(nodes.back(), env);
|
||||
}
|
||||
|
||||
ASTNodePtr Eval::evalIf(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
|
||||
{
|
||||
if (nodes.size() != 2 && nodes.size() != 3) {
|
||||
Error::the().addError(format("wrong number of arguments: if, {}", nodes.size()));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
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>(Value::Nil);
|
||||
|
||||
auto first_evaluated = evalImpl(first_argument, env);
|
||||
if (!is<Value>(first_evaluated.get())
|
||||
|| std::static_pointer_cast<Value>(first_evaluated)->state() == Value::True) {
|
||||
return evalImpl(second_argument, env);
|
||||
}
|
||||
else {
|
||||
return evalImpl(third_argument, env);
|
||||
}
|
||||
}
|
||||
|
||||
ASTNodePtr Eval::apply(std::shared_ptr<List> evaluated_list)
|
||||
@@ -200,17 +251,17 @@ ASTNodePtr Eval::apply(std::shared_ptr<List> evaluated_list)
|
||||
|
||||
auto nodes = evaluated_list->nodes();
|
||||
|
||||
if (!is<Function>(nodes[0].get())) {
|
||||
Error::the().addError(format("invalid function: {}", nodes[0]));
|
||||
if (!is<Function>(nodes.front().get())) {
|
||||
Error::the().addError(format("invalid function: {}", nodes.front()));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// car
|
||||
auto lambda = std::static_pointer_cast<Function>(nodes[0])->lambda();
|
||||
auto lambda = std::static_pointer_cast<Function>(nodes.front())->lambda();
|
||||
// cdr
|
||||
std::span<ASTNodePtr> span { nodes.data() + 1, nodes.size() - 1 };
|
||||
nodes.pop_front();
|
||||
|
||||
return lambda(span);
|
||||
return lambda(nodes);
|
||||
}
|
||||
|
||||
} // namespace blaze
|
||||
|
||||
+5
-3
@@ -6,7 +6,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <list>
|
||||
|
||||
#include "ast.h"
|
||||
#include "environment.h"
|
||||
@@ -25,8 +25,10 @@ public:
|
||||
private:
|
||||
ASTNodePtr evalImpl(ASTNodePtr ast, EnvironmentPtr env);
|
||||
ASTNodePtr evalAst(ASTNodePtr ast, EnvironmentPtr env);
|
||||
ASTNodePtr evalDef(const std::vector<ASTNodePtr>& nodes, EnvironmentPtr env);
|
||||
ASTNodePtr evalLet(const std::vector<ASTNodePtr>& nodes, EnvironmentPtr env);
|
||||
ASTNodePtr evalDef(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env);
|
||||
ASTNodePtr evalLet(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env);
|
||||
ASTNodePtr evalDo(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env);
|
||||
ASTNodePtr evalIf(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env);
|
||||
ASTNodePtr apply(std::shared_ptr<List> evaluated_list);
|
||||
|
||||
ASTNodePtr m_ast;
|
||||
|
||||
+178
-23
@@ -5,19 +5,23 @@
|
||||
*/
|
||||
|
||||
#include <memory> // std::static_pointer_cast
|
||||
#include <string>
|
||||
|
||||
#include "ruc/format/color.h"
|
||||
#include "ruc/format/format.h"
|
||||
|
||||
#include "ast.h"
|
||||
#include "environment.h"
|
||||
#include "error.h"
|
||||
#include "printer.h"
|
||||
#include "types.h"
|
||||
#include "util.h"
|
||||
|
||||
namespace blaze {
|
||||
|
||||
void GlobalEnvironment::add()
|
||||
{
|
||||
auto add = [](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
auto add = [](std::list<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
int64_t result = 0;
|
||||
|
||||
for (auto node : nodes) {
|
||||
@@ -37,7 +41,7 @@ void GlobalEnvironment::add()
|
||||
|
||||
void GlobalEnvironment::sub()
|
||||
{
|
||||
auto sub = [](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
auto sub = [](std::list<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
if (nodes.size() == 0) {
|
||||
return makePtr<Number>(0);
|
||||
}
|
||||
@@ -50,7 +54,7 @@ void GlobalEnvironment::sub()
|
||||
}
|
||||
|
||||
// Start with the first number
|
||||
int64_t result = std::static_pointer_cast<Number>(nodes[0])->number();
|
||||
int64_t result = std::static_pointer_cast<Number>(nodes.front())->number();
|
||||
|
||||
// Skip the first node
|
||||
for (auto it = std::next(nodes.begin()); it != nodes.end(); ++it) {
|
||||
@@ -65,7 +69,7 @@ void GlobalEnvironment::sub()
|
||||
|
||||
void GlobalEnvironment::mul()
|
||||
{
|
||||
auto mul = [](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
auto mul = [](std::list<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
int64_t result = 1;
|
||||
|
||||
for (auto node : nodes) {
|
||||
@@ -85,7 +89,7 @@ void GlobalEnvironment::mul()
|
||||
|
||||
void GlobalEnvironment::div()
|
||||
{
|
||||
auto div = [this](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
auto div = [this](std::list<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
if (nodes.size() == 0) {
|
||||
Error::the().addError(format("wrong number of arguments: {}, 0", m_current_key));
|
||||
return nullptr;
|
||||
@@ -99,7 +103,7 @@ void GlobalEnvironment::div()
|
||||
}
|
||||
|
||||
// Start with the first number
|
||||
double result = std::static_pointer_cast<Number>(nodes[0])->number();
|
||||
double result = std::static_pointer_cast<Number>(nodes.front())->number();
|
||||
|
||||
// Skip the first node
|
||||
for (auto it = std::next(nodes.begin()); it != nodes.end(); ++it) {
|
||||
@@ -115,7 +119,7 @@ void GlobalEnvironment::div()
|
||||
// -----------------------------------------
|
||||
|
||||
#define NUMBER_COMPARE(symbol, comparison_symbol) \
|
||||
auto lambda = [this](std::span<ASTNodePtr> nodes) -> ASTNodePtr { \
|
||||
auto lambda = [this](std::list<ASTNodePtr> nodes) -> ASTNodePtr { \
|
||||
bool result = true; \
|
||||
\
|
||||
if (nodes.size() < 2) { \
|
||||
@@ -131,7 +135,7 @@ void GlobalEnvironment::div()
|
||||
} \
|
||||
\
|
||||
/* Start with the first number */ \
|
||||
int64_t number = std::static_pointer_cast<Number>(nodes[0])->number(); \
|
||||
int64_t number = std::static_pointer_cast<Number>(nodes.front())->number(); \
|
||||
\
|
||||
/* Skip the first node */ \
|
||||
for (auto it = std::next(nodes.begin()); it != nodes.end(); ++it) { \
|
||||
@@ -172,7 +176,7 @@ void GlobalEnvironment::gte()
|
||||
|
||||
void GlobalEnvironment::list()
|
||||
{
|
||||
auto list = [](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
auto list = [](std::list<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
auto list = makePtr<List>();
|
||||
|
||||
for (auto node : nodes) {
|
||||
@@ -185,9 +189,9 @@ void GlobalEnvironment::list()
|
||||
m_values.emplace("list", makePtr<Function>(list));
|
||||
}
|
||||
|
||||
void GlobalEnvironment::is_list()
|
||||
void GlobalEnvironment::isList()
|
||||
{
|
||||
auto is_list = [](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
auto is_list = [](std::list<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
bool result = true;
|
||||
|
||||
for (auto node : nodes) {
|
||||
@@ -203,9 +207,9 @@ void GlobalEnvironment::is_list()
|
||||
m_values.emplace("list?", makePtr<Function>(is_list));
|
||||
}
|
||||
|
||||
void GlobalEnvironment::is_empty()
|
||||
void GlobalEnvironment::isEmpty()
|
||||
{
|
||||
auto is_empty = [](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
auto is_empty = [](std::list<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
bool result = true;
|
||||
|
||||
for (auto node : nodes) {
|
||||
@@ -228,21 +232,25 @@ void GlobalEnvironment::is_empty()
|
||||
|
||||
void GlobalEnvironment::count()
|
||||
{
|
||||
auto count = [this](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
if (nodes.size() > 1) {
|
||||
auto count = [this](std::list<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();
|
||||
if (is<Value>(nodes.front().get()) && std::static_pointer_cast<Value>(nodes.front())->state() == Value::Nil) {
|
||||
// result = 0
|
||||
}
|
||||
else if (!is<List>(nodes.front().get())) {
|
||||
result = std::static_pointer_cast<List>(nodes.front())->size();
|
||||
}
|
||||
else if (!is<Vector>(nodes.front().get())) {
|
||||
result = std::static_pointer_cast<Vector>(nodes.front())->size();
|
||||
}
|
||||
else {
|
||||
Error::the().addError(format("wrong argument type: list, '{}'", nodes));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// FIXME: Add numeric_limits check for implicit cast: size_t > int64_t
|
||||
@@ -252,4 +260,151 @@ void GlobalEnvironment::count()
|
||||
m_values.emplace("count", makePtr<Function>(count));
|
||||
}
|
||||
|
||||
// -----------------------------------------
|
||||
|
||||
#define PRINTER_STRING(symbol, concatenation, print_readably) \
|
||||
auto lambda = [](std::list<ASTNodePtr> nodes) -> ASTNodePtr { \
|
||||
std::string result; \
|
||||
\
|
||||
Printer printer; \
|
||||
for (auto it = nodes.begin(); it != nodes.end(); ++it) { \
|
||||
result += format("{}", printer.printNoErrorCheck(*it, print_readably)); \
|
||||
\
|
||||
if (!isLast(it, nodes)) { \
|
||||
result += concatenation; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
return makePtr<String>(result); \
|
||||
}; \
|
||||
\
|
||||
m_values.emplace(symbol, makePtr<Function>(lambda));
|
||||
|
||||
void GlobalEnvironment::str()
|
||||
{
|
||||
PRINTER_STRING("str", "", false);
|
||||
}
|
||||
|
||||
void GlobalEnvironment::prStr()
|
||||
{
|
||||
PRINTER_STRING("pr-str", " ", true);
|
||||
}
|
||||
|
||||
#define PRINTER_PRINT(symbol, print_readably) \
|
||||
auto lambda = [](std::list<ASTNodePtr> nodes) -> ASTNodePtr { \
|
||||
Printer printer; \
|
||||
for (auto it = nodes.begin(); it != nodes.end(); ++it) { \
|
||||
print("{}", printer.printNoErrorCheck(*it, print_readably)); \
|
||||
\
|
||||
if (!isLast(it, nodes)) { \
|
||||
print(" "); \
|
||||
} \
|
||||
} \
|
||||
print("\n"); \
|
||||
\
|
||||
return makePtr<Value>(Value::Nil); \
|
||||
}; \
|
||||
\
|
||||
m_values.emplace(symbol, makePtr<Function>(lambda));
|
||||
|
||||
void GlobalEnvironment::prn()
|
||||
{
|
||||
PRINTER_PRINT("prn", true);
|
||||
}
|
||||
|
||||
void GlobalEnvironment::println()
|
||||
{
|
||||
PRINTER_PRINT("println", false);
|
||||
}
|
||||
|
||||
// -----------------------------------------
|
||||
|
||||
void GlobalEnvironment::equal()
|
||||
{
|
||||
auto lambda = [this](std::list<ASTNodePtr> nodes) -> ASTNodePtr {
|
||||
if (nodes.size() < 2) {
|
||||
Error::the().addError(format("wrong number of arguments: {}, {}", m_current_key, nodes.size() - 1));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::function<bool(ASTNodePtr, ASTNodePtr)> equal =
|
||||
[&equal](ASTNodePtr lhs, ASTNodePtr rhs) -> bool {
|
||||
if ((is<List>(lhs.get()) && is<List>(rhs.get()))
|
||||
|| (is<Vector>(lhs.get()) && is<Vector>(rhs.get()))) {
|
||||
auto lhs_nodes = std::static_pointer_cast<List>(lhs)->nodes();
|
||||
auto rhs_nodes = std::static_pointer_cast<List>(rhs)->nodes();
|
||||
|
||||
if (lhs_nodes.size() != rhs_nodes.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto lhs_it = lhs_nodes.begin();
|
||||
auto rhs_it = rhs_nodes.begin();
|
||||
for (; lhs_it != lhs_nodes.end(); ++lhs_it, ++rhs_it) {
|
||||
if (!equal(*lhs_it, *rhs_it)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (is<HashMap>(lhs.get()) && is<HashMap>(rhs.get())) {
|
||||
auto lhs_nodes = std::static_pointer_cast<HashMap>(lhs)->elements();
|
||||
auto rhs_nodes = std::static_pointer_cast<HashMap>(rhs)->elements();
|
||||
|
||||
if (lhs_nodes.size() != rhs_nodes.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const auto& [key, value] : lhs_nodes) {
|
||||
auto it = rhs_nodes.find(key);
|
||||
if (it == rhs_nodes.end() || !equal(value, it->second)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (is<String>(lhs.get()) && is<String>(rhs.get())
|
||||
&& std::static_pointer_cast<String>(lhs)->data() == std::static_pointer_cast<String>(rhs)->data()) {
|
||||
return true;
|
||||
}
|
||||
if (is<Keyword>(lhs.get()) && is<Keyword>(rhs.get())
|
||||
&& std::static_pointer_cast<Keyword>(lhs)->keyword() == std::static_pointer_cast<Keyword>(rhs)->keyword()) {
|
||||
return true;
|
||||
}
|
||||
if (is<Number>(lhs.get()) && is<Number>(rhs.get())
|
||||
&& std::static_pointer_cast<Number>(lhs)->number() == std::static_pointer_cast<Number>(rhs)->number()) {
|
||||
return true;
|
||||
}
|
||||
if (is<Value>(lhs.get()) && is<Value>(rhs.get())
|
||||
&& std::static_pointer_cast<Value>(lhs)->state() == std::static_pointer_cast<Value>(rhs)->state()) {
|
||||
return true;
|
||||
}
|
||||
if (is<Symbol>(lhs.get()) && is<Symbol>(rhs.get())
|
||||
&& std::static_pointer_cast<Symbol>(lhs)->symbol() == std::static_pointer_cast<Symbol>(rhs)->symbol()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
bool result = true;
|
||||
auto it = nodes.begin();
|
||||
auto it_next = std::next(nodes.begin());
|
||||
for (; it_next != nodes.end(); ++it, ++it_next) {
|
||||
if (!equal(*it, *it_next)) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return makePtr<Value>((result) ? Value::True : Value::False);
|
||||
};
|
||||
|
||||
m_values.emplace("=", makePtr<Function>(lambda));
|
||||
}
|
||||
|
||||
} // namespace blaze
|
||||
|
||||
+13
-12
@@ -124,7 +124,7 @@ bool Lexer::consumeSpliceUnquoteOrUnquote()
|
||||
bool Lexer::consumeString()
|
||||
{
|
||||
size_t column = m_column;
|
||||
std::string text = "\"";
|
||||
std::string text;
|
||||
|
||||
static std::unordered_set<char> exit = {
|
||||
'"',
|
||||
@@ -134,12 +134,11 @@ bool Lexer::consumeString()
|
||||
};
|
||||
|
||||
bool escape = false;
|
||||
char character = consume();
|
||||
for (;;) {
|
||||
char character = consume(); // "
|
||||
while (true) {
|
||||
character = peek();
|
||||
|
||||
if (!escape && character == '\\') {
|
||||
text += '\\';
|
||||
ignore();
|
||||
escape = true;
|
||||
continue;
|
||||
@@ -149,16 +148,18 @@ bool Lexer::consumeString()
|
||||
break;
|
||||
}
|
||||
|
||||
text += character;
|
||||
if (escape && character == 'n') {
|
||||
text += 0xa; // 10 or \n
|
||||
}
|
||||
else {
|
||||
text += character;
|
||||
}
|
||||
ignore();
|
||||
|
||||
escape = false;
|
||||
}
|
||||
|
||||
if (character == '"') {
|
||||
text += character;
|
||||
}
|
||||
else {
|
||||
if (character != '"') {
|
||||
Error::the().addError({ Token::Type::Error, m_line, column, "expected '\"', got EOF" });
|
||||
}
|
||||
|
||||
@@ -195,7 +196,7 @@ bool Lexer::consumeKeyword()
|
||||
};
|
||||
|
||||
char character = 0;
|
||||
for (;;) {
|
||||
while (true) {
|
||||
character = peek();
|
||||
|
||||
if (exit.find(character) != exit.end()) {
|
||||
@@ -238,7 +239,7 @@ bool Lexer::consumeValue()
|
||||
};
|
||||
|
||||
char character = 0;
|
||||
for (;;) {
|
||||
while (true) {
|
||||
character = peek();
|
||||
|
||||
if (exit.find(character) != exit.end()) {
|
||||
@@ -270,7 +271,7 @@ bool Lexer::consumeComment()
|
||||
};
|
||||
|
||||
char character = 0;
|
||||
for (;;) {
|
||||
while (true) {
|
||||
character = peek();
|
||||
|
||||
if (exit.find(character) != exit.end()) {
|
||||
|
||||
+19
-12
@@ -15,6 +15,7 @@
|
||||
#include "lexer.h"
|
||||
#include "printer.h"
|
||||
#include "types.h"
|
||||
#include "util.h"
|
||||
|
||||
namespace blaze {
|
||||
|
||||
@@ -28,7 +29,7 @@ Printer::~Printer()
|
||||
|
||||
// -----------------------------------------
|
||||
|
||||
std::string Printer::print(ASTNodePtr node)
|
||||
std::string Printer::print(ASTNodePtr node, bool print_readably)
|
||||
{
|
||||
if (Error::the().hasAnyError()) {
|
||||
init();
|
||||
@@ -36,10 +37,10 @@ std::string Printer::print(ASTNodePtr node)
|
||||
return m_print;
|
||||
}
|
||||
|
||||
return printNoErrorCheck(node);
|
||||
return printNoErrorCheck(node, print_readably);
|
||||
}
|
||||
|
||||
std::string Printer::printNoErrorCheck(ASTNodePtr node)
|
||||
std::string Printer::printNoErrorCheck(ASTNodePtr node, bool print_readably)
|
||||
{
|
||||
init();
|
||||
|
||||
@@ -47,7 +48,7 @@ std::string Printer::printNoErrorCheck(ASTNodePtr node)
|
||||
return {};
|
||||
}
|
||||
|
||||
printImpl(node);
|
||||
printImpl(node, print_readably);
|
||||
|
||||
return m_print;
|
||||
}
|
||||
@@ -61,7 +62,7 @@ void Printer::init()
|
||||
m_print = "";
|
||||
}
|
||||
|
||||
void Printer::printImpl(ASTNodePtr node)
|
||||
void Printer::printImpl(ASTNodePtr node, bool print_readably)
|
||||
{
|
||||
auto printSpacing = [this]() -> void {
|
||||
if (!m_first_node && !m_previous_node_is_list) {
|
||||
@@ -76,8 +77,8 @@ void Printer::printImpl(ASTNodePtr node)
|
||||
m_first_node = false;
|
||||
m_previous_node_is_list = true;
|
||||
auto nodes = std::static_pointer_cast<List>(node)->nodes();
|
||||
for (size_t i = 0; i < nodes.size(); ++i) {
|
||||
printImpl(nodes[i]);
|
||||
for (auto node : nodes) {
|
||||
printImpl(node);
|
||||
m_previous_node_is_list = false;
|
||||
}
|
||||
m_print += ')';
|
||||
@@ -88,8 +89,8 @@ void Printer::printImpl(ASTNodePtr node)
|
||||
m_first_node = false;
|
||||
m_previous_node_is_list = true;
|
||||
auto nodes = std::static_pointer_cast<Vector>(node)->nodes();
|
||||
for (size_t i = 0; i < nodes.size(); ++i) {
|
||||
printImpl(nodes[i]);
|
||||
for (auto node : nodes) {
|
||||
printImpl(node);
|
||||
m_previous_node_is_list = false;
|
||||
}
|
||||
m_print += ']';
|
||||
@@ -104,7 +105,7 @@ void Printer::printImpl(ASTNodePtr node)
|
||||
m_print += format("{} ", it->first.front() == 0x7f ? ":" + it->first.substr(1) : it->first); // 127
|
||||
printImpl(it->second);
|
||||
|
||||
if (it != elements.end() && std::next(it) != elements.end()) {
|
||||
if (isLast(it, elements)) {
|
||||
m_print += ' ';
|
||||
}
|
||||
}
|
||||
@@ -112,9 +113,15 @@ void Printer::printImpl(ASTNodePtr node)
|
||||
m_print += '}';
|
||||
}
|
||||
else if (is<String>(node_raw_ptr)) {
|
||||
// TODO: Implement string readably printing
|
||||
std::string text = std::static_pointer_cast<String>(node)->data();
|
||||
if (print_readably) {
|
||||
text = replaceAll(text, "\\", "\\\\");
|
||||
text = replaceAll(text, "\"", "\\\"");
|
||||
text = replaceAll(text, "\n", "\\n");
|
||||
text = "\"" + text + "\"";
|
||||
}
|
||||
printSpacing();
|
||||
m_print += format("{}", std::static_pointer_cast<String>(node)->data());
|
||||
m_print += format("{}", text);
|
||||
}
|
||||
else if (is<Keyword>(node_raw_ptr)) {
|
||||
printSpacing();
|
||||
|
||||
+3
-3
@@ -17,12 +17,12 @@ public:
|
||||
Printer();
|
||||
virtual ~Printer();
|
||||
|
||||
std::string print(ASTNodePtr node);
|
||||
std::string printNoErrorCheck(ASTNodePtr node);
|
||||
std::string print(ASTNodePtr node, bool print_readably = true);
|
||||
std::string printNoErrorCheck(ASTNodePtr node, bool print_readably = true);
|
||||
|
||||
private:
|
||||
void init();
|
||||
void printImpl(ASTNodePtr node);
|
||||
void printImpl(ASTNodePtr node, bool print_readably = true);
|
||||
void printError();
|
||||
|
||||
bool m_first_node { true };
|
||||
|
||||
+3
-8
@@ -296,11 +296,6 @@ ASTNodePtr Reader::readString()
|
||||
{
|
||||
std::string symbol = consume().symbol;
|
||||
|
||||
// Unbalanced string
|
||||
if (symbol.size() < 2 || symbol.front() != '"' || symbol.back() != '"') {
|
||||
Error::the().addError("expected '\"', got EOF");
|
||||
}
|
||||
|
||||
return makePtr<String>(symbol);
|
||||
}
|
||||
|
||||
@@ -383,15 +378,15 @@ void Reader::dumpImpl(ASTNodePtr node)
|
||||
|
||||
ASTNode* node_raw_ptr = node.get();
|
||||
if (is<List>(node_raw_ptr)) {
|
||||
auto list = std::static_pointer_cast<List>(node);
|
||||
auto nodes = std::static_pointer_cast<List>(node)->nodes();
|
||||
print("{}", indentation);
|
||||
print(fg(ruc::format::TerminalColor::Blue), "ListContainer");
|
||||
print(" <");
|
||||
print(fg(ruc::format::TerminalColor::Blue), "()");
|
||||
print(">\n");
|
||||
m_indentation++;
|
||||
for (size_t i = 0; i < list->nodes().size(); ++i) {
|
||||
dumpImpl(list->nodes()[i]);
|
||||
for (auto node : nodes) {
|
||||
dumpImpl(node);
|
||||
}
|
||||
m_indentation--;
|
||||
return;
|
||||
|
||||
@@ -48,7 +48,7 @@ auto print(blaze::ASTNodePtr exp) -> std::string
|
||||
{
|
||||
blaze::Printer printer;
|
||||
|
||||
return printer.print(exp);
|
||||
return printer.print(exp, true);
|
||||
}
|
||||
|
||||
auto rep(std::string_view input) -> std::string
|
||||
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (C) 2023 Riyyi
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace blaze {
|
||||
|
||||
template<typename It, typename C>
|
||||
inline bool isLast(It it, const C& container)
|
||||
{
|
||||
return (it != container.end()) && (next(it) == container.end());
|
||||
}
|
||||
|
||||
inline std::string replaceAll(std::string text, std::string_view search, std::string_view replace)
|
||||
{
|
||||
size_t search_length = search.length();
|
||||
size_t replace_length = replace.length();
|
||||
size_t position = text.find(search, 0);
|
||||
while (position != std::string::npos) {
|
||||
text.replace(position, search_length, replace);
|
||||
position += replace_length;
|
||||
position = text.find(search, position);
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
} // namespace blaze
|
||||
Reference in New Issue
Block a user