Browse Source

Main+Eval+AST: Implement step7

master
Riyyi 2 years ago
parent
commit
80e0437a5c
  1. 4
      CMakeLists.txt
  2. 21
      src/ast.cpp
  3. 7
      src/ast.h
  4. 142
      src/eval.cpp
  5. 3
      src/eval.h
  6. 63
      src/functions.cpp
  7. 2
      src/step6_file.cpp
  8. 159
      src/step7_quote.cpp

4
CMakeLists.txt

@ -107,3 +107,7 @@ add_dependencies(test5 ${PROJECT})
add_custom_target(test6
COMMAND env STEP=step_env MAL_IMPL=js ../vendor/mal/runtest.py --deferrable --optional ../vendor/mal/tests/step6_file.mal -- ./${PROJECT})
add_dependencies(test6 ${PROJECT})
add_custom_target(test7
COMMAND env STEP=step_env MAL_IMPL=js ../vendor/mal/runtest.py --deferrable --optional ../vendor/mal/tests/step7_quote.mal -- ./${PROJECT})
add_dependencies(test7 ${PROJECT})

21
src/ast.cpp

@ -16,6 +16,11 @@
namespace blaze {
Collection::Collection(const std::list<ValuePtr>& nodes)
: m_nodes(nodes)
{
}
void Collection::add(ValuePtr node)
{
if (node == nullptr) {
@ -27,6 +32,20 @@ void Collection::add(ValuePtr node)
// -----------------------------------------
List::List(const std::list<ValuePtr>& nodes)
: Collection(nodes)
{
}
// -----------------------------------------
Vector::Vector(const std::list<ValuePtr>& nodes)
: Collection(nodes)
{
}
// -----------------------------------------
void HashMap::add(const std::string& key, ValuePtr value)
{
if (value == nullptr) {
@ -81,7 +100,7 @@ Function::Function(const std::string& name, FunctionType function)
// -----------------------------------------
Lambda::Lambda(std::vector<std::string> bindings, ValuePtr body, EnvironmentPtr env)
Lambda::Lambda(const std::vector<std::string>& bindings, ValuePtr body, EnvironmentPtr env)
: m_bindings(bindings)
, m_body(body)
, m_env(env)

7
src/ast.h

@ -64,6 +64,7 @@ public:
protected:
Collection() = default;
Collection(const std::list<ValuePtr>& nodes);
private:
virtual bool isCollection() const override { return true; }
@ -77,6 +78,7 @@ private:
class List final : public Collection {
public:
List() = default;
List(const std::list<ValuePtr>& nodes);
virtual ~List() = default;
private:
@ -89,6 +91,7 @@ private:
class Vector final : public Collection {
public:
Vector() = default;
Vector(const std::list<ValuePtr>& nodes);
virtual ~Vector() = default;
private:
@ -236,10 +239,10 @@ private:
class Lambda final : public Callable {
public:
Lambda(std::vector<std::string> bindings, ValuePtr body, EnvironmentPtr env);
Lambda(const std::vector<std::string>& bindings, ValuePtr body, EnvironmentPtr env);
virtual ~Lambda() = default;
std::vector<std::string> bindings() const { return m_bindings; }
const std::vector<std::string>& bindings() const { return m_bindings; }
ValuePtr body() const { return m_body; }
EnvironmentPtr env() const { return m_env; }

142
src/eval.cpp

@ -79,6 +79,16 @@ ValuePtr Eval::evalImpl()
evalLet(nodes, env);
continue; // TCO
}
if (symbol == "quote") {
return evalQuote(nodes, env);
}
if (symbol == "quasiquote") {
evalQuasiQuote(nodes, env);
continue; // TCO
}
if (symbol == "quasiquoteexpand") {
return evalQuasiQuoteExpand(nodes, env);
}
if (symbol == "do") {
evalDo(nodes, env);
continue; // TCO
@ -195,6 +205,136 @@ ValuePtr Eval::evalDef(const std::list<ValuePtr>& nodes, EnvironmentPtr env)
return env->set(symbol, value);
}
ValuePtr Eval::evalQuote(const std::list<ValuePtr>& nodes, EnvironmentPtr env)
{
if (nodes.size() != 1) {
Error::the().add(format("wrong number of arguments: quote, {}", nodes.size()));
return nullptr;
}
return nodes.front();
}
static bool isSymbol(ValuePtr value, const std::string& symbol)
{
if (!is<Symbol>(value.get())) {
return false;
}
auto valueSymbol = std::static_pointer_cast<Symbol>(value)->symbol();
if (valueSymbol != symbol) {
return false;
}
return true;
}
static ValuePtr startsWith(ValuePtr ast, const std::string& symbol)
{
if (!is<List>(ast.get())) {
return nullptr;
}
auto nodes = std::static_pointer_cast<List>(ast)->nodes();
if (nodes.empty() || !isSymbol(nodes.front(), symbol)) {
return nullptr;
}
if (nodes.size() != 2) {
Error::the().add(format("wrong number of arguments: {}, {}", symbol, nodes.size() - 1));
return nullptr;
}
return *std::next(nodes.begin());
}
static ValuePtr evalQuasiQuoteImpl(ValuePtr ast)
{
if (is<HashMap>(ast.get()) || is<Symbol>(ast.get())) {
auto quoted_list = makePtr<List>();
quoted_list->add(makePtr<Symbol>("quote"));
quoted_list->add(ast);
return quoted_list;
}
if (!is<Collection>(ast.get())) {
return ast;
}
// `~2 or `(unquote 2)
const auto unquote = startsWith(ast, "unquote"); // x
if (unquote) {
return unquote;
}
// `~@(list 2 2 2) or `(splice-unquote (list 2 2 2))
const auto splice_unquote = startsWith(ast, "splice-unquote"); // (list 2 2 2)
if (splice_unquote) {
return splice_unquote;
}
ValuePtr result = makePtr<List>();
auto nodes = std::static_pointer_cast<Collection>(ast)->nodes();
// `() or `(1 ~2 3) or `(1 ~@(list 2 2 2) 3)
for (auto it = nodes.rbegin(); it != nodes.rend(); ++it) {
const auto elt = *it;
auto list = makePtr<List>();
const auto splice_unquote = startsWith(elt, "splice-unquote"); // (list 2 2 2)
if (splice_unquote) {
list->add(makePtr<Symbol>("concat"));
list->add(splice_unquote);
list->add(result);
result = list; // (cons 1 (concat (list 2 2 2) (cons 3 ())))
continue;
}
list->add(makePtr<Symbol>("cons"));
list->add(evalQuasiQuoteImpl(elt));
list->add(result);
result = list; // (cons 1 (cons 2 (cons 3 ())))
}
if (is<List>(ast.get())) {
return result;
}
// Wrap Vector in (vec)
auto vector = makePtr<List>();
vector->add(makePtr<Symbol>("vec"));
vector->add(result);
return vector;
}
void Eval::evalQuasiQuote(const std::list<ValuePtr>& nodes, EnvironmentPtr env)
{
if (nodes.size() != 1) {
Error::the().add(format("wrong number of arguments: quasiquote, {}", nodes.size()));
return;
}
auto result = evalQuasiQuoteImpl(nodes.front());
m_ast_stack.push(result);
m_env_stack.push(env);
return; // TCO
}
ValuePtr Eval::evalQuasiQuoteExpand(const std::list<ValuePtr>& nodes, EnvironmentPtr env)
{
if (nodes.size() != 1) {
Error::the().add(format("wrong number of arguments: quasiquoteexpand, {}", nodes.size()));
return nullptr;
}
return evalQuasiQuoteImpl(nodes.front());
}
void Eval::evalLet(const std::list<ValuePtr>& nodes, EnvironmentPtr env)
{
if (nodes.size() != 2) {
@ -207,7 +347,7 @@ void Eval::evalLet(const std::list<ValuePtr>& nodes, EnvironmentPtr env)
// First argument needs to be a List or Vector
if (!is<Collection>(first_argument.get())) {
Error::the().add(format("wrong argument type: collection, '{}'", first_argument));
Error::the().add(format("wrong argument type: list, '{}'", first_argument));
return;
}

3
src/eval.h

@ -30,6 +30,9 @@ private:
ValuePtr evalAst(ValuePtr ast, EnvironmentPtr env);
ValuePtr evalDef(const std::list<ValuePtr>& nodes, EnvironmentPtr env);
void evalLet(const std::list<ValuePtr>& nodes, EnvironmentPtr env);
ValuePtr evalQuote(const std::list<ValuePtr>& nodes, EnvironmentPtr env);
void evalQuasiQuote(const std::list<ValuePtr>& nodes, EnvironmentPtr env);
ValuePtr evalQuasiQuoteExpand(const std::list<ValuePtr>& nodes, EnvironmentPtr env);
void evalDo(const std::list<ValuePtr>& nodes, EnvironmentPtr env);
void evalIf(const std::list<ValuePtr>& nodes, EnvironmentPtr env);
ValuePtr evalFn(const std::list<ValuePtr>& nodes, EnvironmentPtr env);

63
src/functions.cpp

@ -497,7 +497,7 @@ ADD_FUNCTION(
"swap!",
{
if (nodes.size() < 2) {
Error::the().add(format("wrong number of arguments: reset!, {}", nodes.size()));
Error::the().add(format("wrong number of arguments: swap!, {}", nodes.size()));
return nullptr;
}
@ -534,6 +534,67 @@ ADD_FUNCTION(
return atom->reset(value);
});
// (cons 1 (list 2 3))
ADD_FUNCTION(
"cons",
{
if (nodes.size() != 2) {
Error::the().add(format("wrong number of arguments: cons, {}", nodes.size()));
return nullptr;
}
auto first_argument = *nodes.begin();
auto second_argument = *std::next(nodes.begin());
if (!is<Collection>(second_argument.get())) {
Error::the().add(format("wrong argument type: list, '{}'", second_argument));
return nullptr;
}
auto result_nodes = std::static_pointer_cast<Collection>(second_argument)->nodes();
result_nodes.push_front(first_argument);
return makePtr<List>(result_nodes);
});
// (concat (list 1) (list 2 3))
ADD_FUNCTION(
"concat",
{
std::list<ValuePtr> result_nodes;
for (auto node : nodes) {
if (!is<Collection>(node.get())) {
Error::the().add(format("wrong argument type: list, '{}'", node));
return nullptr;
}
auto argument_nodes = std::static_pointer_cast<Collection>(node)->nodes();
result_nodes.splice(result_nodes.end(), argument_nodes);
}
return makePtr<List>(result_nodes);
});
// (vec (list 1 2 3))
ADD_FUNCTION(
"vec",
{
if (nodes.size() != 1) {
Error::the().add(format("wrong number of arguments: vec, {}", nodes.size()));
return nullptr;
}
if (!is<Collection>(nodes.front().get())) {
Error::the().add(format("wrong argument type: list, '{}'", nodes.front()));
return nullptr;
}
auto result_nodes = std::static_pointer_cast<Collection>(nodes.front())->nodes();
return makePtr<Vector>(result_nodes);
});
// -----------------------------------------
void installFunctions(EnvironmentPtr env)

2
src/step6_file.cpp

@ -24,7 +24,7 @@
#include "readline.h"
#include "settings.h"
#if 1
#if 0
namespace blaze {
static EnvironmentPtr s_outer_env = Environment::create();

159
src/step7_quote.cpp

@ -0,0 +1,159 @@
/*
* Copyright (C) 2023 Riyyi
*
* SPDX-License-Identifier: MIT
*/
#include <csignal> // std::signal
#include <cstdlib> // std::exit
#include <string>
#include <string_view>
#include <vector>
#include "ruc/argparser.h"
#include "ruc/format/color.h"
#include "ast.h"
#include "environment.h"
#include "error.h"
#include "eval.h"
#include "forward.h"
#include "lexer.h"
#include "printer.h"
#include "reader.h"
#include "readline.h"
#include "settings.h"
#if 1
namespace blaze {
static EnvironmentPtr s_outer_env = Environment::create();
static auto cleanup(int signal) -> void
{
print("\033[0m\n");
std::exit(signal);
}
auto read(std::string_view input) -> ValuePtr
{
Lexer lexer(input);
lexer.tokenize();
if (Settings::the().get("dump-lexer") == "1") {
lexer.dump();
}
Reader reader(std::move(lexer.tokens()));
reader.read();
if (Settings::the().get("dump-reader") == "1") {
reader.dump();
}
return reader.node();
}
auto eval(ValuePtr ast, EnvironmentPtr env) -> ValuePtr
{
if (env == nullptr) {
env = s_outer_env;
}
Eval eval(ast, env);
eval.eval();
return eval.ast();
}
static auto print(ValuePtr exp) -> std::string
{
Printer printer;
return printer.print(exp, true);
}
static auto rep(std::string_view input, EnvironmentPtr env) -> std::string
{
Error::the().clearErrors();
Error::the().setInput(input);
return print(eval(read(input), env));
}
static std::string_view lambdaTable[] = {
"(def! not (fn* (cond) (if cond false true)))",
"(def! load-file (fn* (filename) \
(eval (read-string (str \"(do \" (slurp filename) \"\nnil)\")))))",
};
static auto installLambdas(EnvironmentPtr env) -> void
{
for (auto function : lambdaTable) {
rep(function, env);
}
}
static auto makeArgv(EnvironmentPtr env, std::vector<std::string> arguments) -> void
{
auto list = makePtr<List>();
if (arguments.size() > 1) {
for (auto it = arguments.begin() + 1; it != arguments.end(); ++it) {
list->add(makePtr<String>(*it));
}
}
env->set("*ARGV*", list);
}
} // namespace blaze
auto main(int argc, char* argv[]) -> int
{
bool dump_lexer = false;
bool dump_reader = false;
bool pretty_print = false;
std::string_view history_path = "~/.blaze-history";
std::vector<std::string> arguments;
// 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-path", nullptr, nullptr, nullptr, ruc::ArgParser::Required::Yes);
arg_parser.addArgument(arguments, "arguments", nullptr, nullptr, ruc::ArgParser::Required::No);
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, blaze::cleanup);
std::signal(SIGTERM, blaze::cleanup);
installFunctions(blaze::s_outer_env);
installLambdas(blaze::s_outer_env);
makeArgv(blaze::s_outer_env, arguments);
if (arguments.size() > 0) {
rep(format("(load-file \"{}\")", arguments.front()), blaze::s_outer_env);
return 0;
}
blaze::Readline readline(pretty_print, history_path);
std::string input;
while (readline.get(input)) {
if (pretty_print) {
print("\033[0m");
}
print("{}\n", rep(input, blaze::s_outer_env));
}
if (pretty_print) {
print("\033[0m");
}
return 0;
}
#endif
Loading…
Cancel
Save