Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 67 additions & 68 deletions impls/cpp/Core.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#include "MAL.h"
#include "Environment.h"
#include "StaticList.h"
#include "Types.h"
#include "Core.h"
#include "Reader.h"
#include "ReadLine.h"
#include "String.h"
#include "Validation.h"

#include <gc/gc_allocator.h>

#include <chrono>
#include <fstream>
#include <iostream>
#include <vector>

#define CHECK_ARGS_IS(expected) \
checkArgsIs(name.c_str(), expected, \
Expand All @@ -19,19 +23,26 @@
checkArgsAtLeast(name.c_str(), expected, \
std::distance(argsBegin, argsEnd))

static String printValues(malValueIter begin, malValueIter end,
const String& sep, bool readably);
std::vector<malBuiltIn*, gc_allocator<malBuiltIn*>> handlers;
// We want to populate this list without duplication of the function
// names in the source (or source generation).
// This trick relies on static variables.
// The returned value is ignored, but void variables are forbidden.
bool addHandler(const String &name, malBuiltIn::ApplyFunc handler)
{
handlers.push_back(new malBuiltIn(name, handler));
return false;
}

static StaticList<malBuiltIn*> handlers;
#define BUILTIN_UNUSED_PARAMETER_NAME (void)name

#define ARG(type, name) type* name = VALUE_CAST(type, *argsBegin++)
#define ARG(type, var) type* var = VALUE_CAST(name, type, *argsBegin++)

#define FUNCNAME(uniq) builtIn ## uniq
#define HRECNAME(uniq) handler ## uniq
#define BUILTIN_DEF(uniq, symbol) \
static malBuiltIn::ApplyFunc FUNCNAME(uniq); \
static StaticList<malBuiltIn*>::Node HRECNAME(uniq) \
(handlers, new malBuiltIn(symbol, FUNCNAME(uniq))); \
static bool HRECNAME(uniq) = addHandler(symbol, FUNCNAME(uniq)); \
malValuePtr FUNCNAME(uniq)(const String& name, \
malValueIter argsBegin, malValueIter argsEnd)

Expand Down Expand Up @@ -81,9 +92,9 @@ BUILTIN_IS("nil?", nilValue);

BUILTIN("-")
{
int argCount = CHECK_ARGS_BETWEEN(1, 2);
CHECK_ARGS_BETWEEN(1, 2);
ARG(malInteger, lhs);
if (argCount == 1) {
if (argsBegin == argsEnd) {
return mal::integer(- lhs->value());
}

Expand Down Expand Up @@ -130,27 +141,27 @@ BUILTIN(">")
BUILTIN("=")
{
CHECK_ARGS_IS(2);
const malValue* lhs = (*argsBegin++).ptr();
const malValue* rhs = (*argsBegin++).ptr();
const malValue* lhs = (*argsBegin++);
const malValue* rhs = (*argsBegin++);

return mal::boolean(lhs->isEqualTo(rhs));
return mal::boolean(lhs->doIsEqualTo(rhs));
}

BUILTIN("apply")
{
CHECK_ARGS_AT_LEAST(2);
malValuePtr op = *argsBegin++; // this gets checked in APPLY
ARG(malApplicable, op);

// Copy the first N-1 arguments in.
malValueVec args(argsBegin, argsEnd-1);

// Then append the argument as a list.
const malSequence* lastArg = VALUE_CAST(malSequence, *(argsEnd-1));
const malSequence* lastArg = VALUE_CAST(name, malSequence, *(argsEnd-1));
for (int i = 0; i < lastArg->count(); i++) {
args.push_back(lastArg->item(i));
}

return APPLY(op, args.begin(), args.end());
return op->apply(args.begin(), args.end());
}

BUILTIN("assoc")
Expand All @@ -170,21 +181,15 @@ BUILTIN("atom")

BUILTIN("concat")
{
int count = 0;
auto items = new malList();
for (auto it = argsBegin; it != argsEnd; ++it) {
const malSequence* seq = VALUE_CAST(malSequence, *it);
count += seq->count();
}

malValueVec* items = new malValueVec(count);
int offset = 0;
for (auto it = argsBegin; it != argsEnd; ++it) {
const malSequence* seq = STATIC_CAST(malSequence, *it);
std::copy(seq->begin(), seq->end(), items->begin() + offset);
offset += seq->count();
const malSequence* seq = VALUE_CAST(name, malSequence, *it);
for (const auto &x : *seq) {
items->push_back(x);
}
}

return mal::list(items);
return malValuePtr(items);
}

BUILTIN("conj")
Expand All @@ -201,11 +206,13 @@ BUILTIN("cons")
malValuePtr first = *argsBegin++;
ARG(malSequence, rest);

malValueVec* items = new malValueVec(1 + rest->count());
items->at(0) = first;
std::copy(rest->begin(), rest->end(), items->begin() + 1);
auto items = new malList();
items->push_back(first);
for (const auto &x : *rest) {
items->push_back(x);
}

return mal::list(items);
return malValuePtr(items);
}

BUILTIN("contains?")
Expand Down Expand Up @@ -256,7 +263,7 @@ BUILTIN("empty?")
BUILTIN("eval")
{
CHECK_ARGS_IS(1);
return EVAL(*argsBegin, NULL);
return EVAL(*argsBegin, replEnv);
}

BUILTIN("first")
Expand Down Expand Up @@ -294,7 +301,7 @@ BUILTIN("get")

BUILTIN("hash-map")
{
return mal::hash(argsBegin, argsEnd, true);
return mal::hash(argsBegin, argsEnd, name);
}

BUILTIN("keys")
Expand All @@ -317,6 +324,7 @@ BUILTIN("keyword")

BUILTIN("list")
{
BUILTIN_UNUSED_PARAMETER_NAME;
return mal::list(argsBegin, argsEnd);
}

Expand All @@ -332,17 +340,17 @@ BUILTIN("macro?")
BUILTIN("map")
{
CHECK_ARGS_IS(2);
malValuePtr op = *argsBegin++; // this gets checked in APPLY
ARG(malApplicable, op);
ARG(malSequence, source);

const int length = source->count();
malValueVec* items = new malValueVec(length);
auto items = new malList();
auto it = source->begin();
for (int i = 0; i < length; i++) {
items->at(i) = APPLY(op, it+i, it+i+1);
items->push_back(op->apply(it+i, it+i+1));
}

return mal::list(items);
return malValuePtr(items);
}

BUILTIN("meta")
Expand All @@ -367,17 +375,20 @@ BUILTIN("nth")

BUILTIN("pr-str")
{
BUILTIN_UNUSED_PARAMETER_NAME;
return mal::string(printValues(argsBegin, argsEnd, " ", true));
}

BUILTIN("println")
{
BUILTIN_UNUSED_PARAMETER_NAME;
std::cout << printValues(argsBegin, argsEnd, " ", false) << "\n";
return mal::nilValue();
}

BUILTIN("prn")
{
BUILTIN_UNUSED_PARAMETER_NAME;
std::cout << printValues(argsBegin, argsEnd, " ", true) << "\n";
return mal::nilValue();
}
Expand All @@ -395,7 +406,11 @@ BUILTIN("readline")
CHECK_ARGS_IS(1);
ARG(malString, str);

return readline(str->value());
String input;
if (s_readLine_get(str->value(), input)) {
return mal::string(input);
}
return mal::nilValue();
}

BUILTIN("reset!")
Expand All @@ -409,7 +424,7 @@ BUILTIN("rest")
{
CHECK_ARGS_IS(1);
if (*argsBegin == mal::nilValue()) {
return mal::list(new malValueVec(0));
return mal::list();
}
ARG(malSequence, seq);
return seq->rest();
Expand All @@ -432,11 +447,11 @@ BUILTIN("seq")
if (length == 0)
return mal::nilValue();

malValueVec* items = new malValueVec(length);
auto items = new malList();
for (int i = 0; i < length; i++) {
(*items)[i] = mal::string(str.substr(i, 1));
items->push_back(mal::string(str.substr(i, 1)));
}
return mal::list(items);
return malValuePtr(items);
}
MAL_FAIL("%s is not a string or sequence", arg->print(true).c_str());
}
Expand All @@ -463,6 +478,7 @@ BUILTIN("slurp")

BUILTIN("str")
{
BUILTIN_UNUSED_PARAMETER_NAME;
return mal::string(printValues(argsBegin, argsEnd, "", false));
}

Expand All @@ -471,13 +487,13 @@ BUILTIN("swap!")
CHECK_ARGS_AT_LEAST(2);
ARG(malAtom, atom);

malValuePtr op = *argsBegin++; // this gets checked in APPLY
ARG(malApplicable, op);

malValueVec args(1 + argsEnd - argsBegin);
args[0] = atom->deref();
std::copy(argsBegin, argsEnd, args.begin() + 1);

malValuePtr value = APPLY(op, args.begin(), args.end());
malValuePtr value = op->apply(args.begin(), args.end());
return atom->reset(value);
}

Expand Down Expand Up @@ -522,6 +538,7 @@ BUILTIN("vec")

BUILTIN("vector")
{
BUILTIN_UNUSED_PARAMETER_NAME;
return mal::vector(argsBegin, argsEnd);
}

Expand All @@ -530,30 +547,12 @@ BUILTIN("with-meta")
CHECK_ARGS_IS(2);
malValuePtr obj = *argsBegin++;
malValuePtr meta = *argsBegin++;
return obj->withMeta(meta);
return obj->doWithMeta(meta);
}

void installCore(malEnvPtr env) {
void installCore() {
for (auto it = handlers.begin(), end = handlers.end(); it != end; ++it) {
malBuiltIn* handler = *it;
env->set(handler->name(), handler);
replEnv->set(handler->name(), handler);
}
}

static String printValues(malValueIter begin, malValueIter end,
const String& sep, bool readably)
{
String out;

if (begin != end) {
out += (*begin)->print(readably);
++begin;
}

for ( ; begin != end; ++begin) {
out += sep;
out += (*begin)->print(readably);
}

return out;
}
9 changes: 9 additions & 0 deletions impls/cpp/Core.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include "Environment.h"
#include "Types.h"

void installCore();

extern const malEnvPtr replEnv; // step*.cpp
extern malValuePtr EVAL(malValuePtr, malEnvPtr); // step*.cpp
30 changes: 2 additions & 28 deletions impls/cpp/Debug.h
Original file line number Diff line number Diff line change
@@ -1,35 +1,11 @@
#ifndef INCLUDE_DEBUG_H
#define INCLUDE_DEBUG_H
#pragma once

#include <stdio.h>
#include <stdlib.h>

#define DEBUG_TRACE 1
//#define DEBUG_OBJECT_LIFETIMES 1
//#define DEBUG_ENV_LIFETIMES 1

#define DEBUG_TRACE_FILE stderr

#define NOOP do { } while (false)
#define NOTRACE(...) NOOP

#if DEBUG_TRACE
#define TRACE(...) fprintf(DEBUG_TRACE_FILE, __VA_ARGS__)
#else
#define TRACE NOTRACE
#endif

#if DEBUG_OBJECT_LIFETIMES
#define TRACE_OBJECT TRACE
#else
#define TRACE_OBJECT NOTRACE
#endif

#if DEBUG_ENV_LIFETIMES
#define TRACE_ENV TRACE
#else
#define TRACE_ENV NOTRACE
#endif
#define TRACE(...) fprintf(DEBUG_TRACE_FILE, __VA_ARGS__)

#define _ASSERT(file, line, condition, ...) \
if (!(condition)) { \
Expand All @@ -41,5 +17,3 @@

#define ASSERT(condition, ...) \
_ASSERT(__FILE__, __LINE__, condition, __VA_ARGS__)

#endif // INCLUDE_DEBUG_H
2 changes: 1 addition & 1 deletion impls/cpp/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ WORKDIR /mal
##########################################################

# Install g++ for any C/C++ based implementations
RUN apt-get -y install g++ libreadline-dev
RUN apt-get -y install g++ libreadline-dev libgc-dev
Loading
Loading