SIGN IN SIGN UP
#include "MAL.h"
#include "Environment.h"
#include "ReadLine.h"
#include "Types.h"
#include <iostream>
#include <memory>
malValuePtr READ(const String& input);
String PRINT(malValuePtr ast);
static ReadLine s_readLine("~/.mal-history");
static malBuiltIn::ApplyFunc
builtIn_add, builtIn_sub, builtIn_mul, builtIn_div;
int main(int argc, char* argv[])
{
String prompt = "user> ";
String input;
2015-03-27 11:03:28 +11:00
malEnvPtr replEnv(new malEnv);
replEnv->set("+", mal::builtin("+", &builtIn_add));
replEnv->set("-", mal::builtin("-", &builtIn_sub));
replEnv->set("*", mal::builtin("+", &builtIn_mul));
replEnv->set("/", mal::builtin("/", &builtIn_div));
while (s_readLine.get(prompt, input)) {
String out;
try {
out = rep(input, replEnv);
}
catch (malEmptyInputException&) {
continue; // no output
}
catch (String& s) {
out = s;
};
std::cout << out << "\n";
}
return 0;
}
2015-03-27 11:03:28 +11:00
String rep(const String& input, malEnvPtr env)
{
return PRINT(EVAL(READ(input), env));
}
malValuePtr READ(const String& input)
{
return readStr(input);
}
2015-03-27 11:03:28 +11:00
malValuePtr EVAL(malValuePtr ast, malEnvPtr env)
{
Merge eval-ast and macro expansion into EVAL, add DEBUG-EVAL See issue #587. * Merge eval-ast and eval into a single conditional. * Expand macros during the apply phase, removing lots of duplicate tests, and increasing the overall consistency by allowing the macro to be computed instead of referenced by name (`((defmacro! cond (...)))` is currently illegal for example). * Print "EVAL: $ast" at the top of EVAL if DEBUG-EVAL exists in the MAL environment. * Remove macroexpand and quasiquoteexpand special forms. * Use pattern-matching style in process/step*.txt. Unresolved issues: c.2: unable to reproduce with gcc 11.12.0. elm: the directory is unchanged. groovy: sometimes fail, but not on each rebuild. nasm: fails some new soft tests, but the issue is unreproducible when running the interpreter manually. objpascal: unreproducible with fpc 3.2.2. ocaml: unreproducible with 4.11.1. perl6: unreproducible with rakudo 2021.09. Unrelated changes: Reduce diff betweens steps. Prevent defmacro! from mutating functions: c forth logo miniMAL vb. dart: fix recent errors and warnings ocaml: remove metadata from symbols. Improve the logo implementation. Encapsulate all representation in types.lg and env.lg, unwrap numbers. Replace some manual iterations with logo control structures. Reduce the diff between steps. Use native iteration in env_get and env_map Rewrite the reader with less temporary strings. Reduce the number of temporary lists (for example, reverse iteration with butlast requires O(n^2) allocations). It seems possible to remove a few exceptions: GC settings (Dockerfile), NO_SELF_HOSTING (IMPLS.yml) and step5_EXCLUDES (Makefile.impls) .
2022-01-10 00:15:40 +01:00
// std::cout << "EVAL: " << PRINT(ast) << "\n";
return ast->eval(env);
}
String PRINT(malValuePtr ast)
{
return ast->print(true);
}
malValuePtr APPLY(malValuePtr op, malValueIter argsBegin, malValueIter argsEnd)
{
const malApplicable* handler = DYNAMIC_CAST(malApplicable, op);
MAL_CHECK(handler != NULL,
"\"%s\" is not applicable", op->print(true).c_str());
return handler->apply(argsBegin, argsEnd);
}
#define ARG(type, name) type* name = VALUE_CAST(type, *argsBegin++)
#define CHECK_ARGS_IS(expected) \
checkArgsIs(name.c_str(), expected, std::distance(argsBegin, argsEnd))
#define CHECK_ARGS_BETWEEN(min, max) \
checkArgsBetween(name.c_str(), min, max, std::distance(argsBegin, argsEnd))
static malValuePtr builtIn_add(const String& name,
malValueIter argsBegin, malValueIter argsEnd)
{
CHECK_ARGS_IS(2);
ARG(malInteger, lhs);
ARG(malInteger, rhs);
return mal::integer(lhs->value() + rhs->value());
}
static malValuePtr builtIn_sub(const String& name,
malValueIter argsBegin, malValueIter argsEnd)
{
int argCount = CHECK_ARGS_BETWEEN(1, 2);
ARG(malInteger, lhs);
if (argCount == 1) {
return mal::integer(- lhs->value());
}
ARG(malInteger, rhs);
return mal::integer(lhs->value() - rhs->value());
}
static malValuePtr builtIn_mul(const String& name,
malValueIter argsBegin, malValueIter argsEnd)
{
CHECK_ARGS_IS(2);
ARG(malInteger, lhs);
ARG(malInteger, rhs);
return mal::integer(lhs->value() * rhs->value());
}
static malValuePtr builtIn_div(const String& name,
malValueIter argsBegin, malValueIter argsEnd)
{
CHECK_ARGS_IS(2);
ARG(malInteger, lhs);
ARG(malInteger, rhs);
MAL_CHECK(rhs->value() != 0, "Division by zero"); \
return mal::integer(lhs->value() / rhs->value());
}