Skip to content

Clean up. #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
52 changes: 5 additions & 47 deletions include/lisp/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,28 +369,6 @@ inline auto tryMacroCall(ExprPtr const& expr, std::shared_ptr<Env> const& env) -
return expr;
}

inline auto parseAndEvalMacroCall(ExprPtr const& expr, std::shared_ptr<Env> const& env) -> ExprPtr
{
auto cons = dynamic_cast<Cons*>(expr.get());
if (!cons)
{
return expr;
}
auto car = cons->car();
auto cdr = cons->cdr();
auto carStr = asString(car);
if (!carStr.has_value())
{
// do nothing
return expr;
}
if (env->variableDefined(carStr.value()))
{
return parse(expr)->eval(env);
}
return expr;
}

inline auto parse(ExprPtr const& expr) -> ExprPtr
{
if (auto e = tryCons(expr))
Expand Down Expand Up @@ -466,34 +444,14 @@ inline auto parseAsQuoted(ExprPtr const& expr, std::optional<int32_t> quasiquote
return consToQuoted(expr, quasiquoteLevel);
}

template <typename Func>
void forEach(ExprPtr const& expr, Func func)
{
if (auto e = dynamic_cast<Cons const*>(expr.get()))
{
forEach(e->car(), func);
forEach(e->cdr(), func);
}
func(expr);
}

inline auto defineMacros(ExprPtr const& expr, std::shared_ptr<Env> const& env) -> ExprPtr
{
auto func = [&env](auto const& expr)
{
if (auto macroDefinition = parseMacroDefinition(expr))
{
macroDefinition->eval(env);
return nil();
}
return expr;
};
return transform(expr, func);
}

// FIXME
inline auto expandMacros(ExprPtr const& expr, std::shared_ptr<Env> const& env) -> ExprPtr
{
if (auto macroDefinition = parseMacroDefinition(expr))
{
macroDefinition->eval(env);
return nil();
}
if (auto e = tryMacroCall(expr, env))
{
return e;
Expand Down
4 changes: 1 addition & 3 deletions sample/loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,11 @@ auto eval(std::string const& input, std::shared_ptr<Env> const& env)
Lexer lex(input);
MetaParser p(lex);
std::string result;
// auto macroEnv = std::make_shared<Env>();
auto macroEnv = env;
do
{
auto me = p.sexpr();
auto de = defineMacros(me, macroEnv);
auto ee = expandMacros(de, macroEnv);
auto ee = expandMacros(me, macroEnv);
#if DEBUG
std::cout << "ee ## " << ee->toString() << std::endl;
#endif // DEBUG
Expand Down