aboutsummaryrefslogtreecommitdiffstats
path: root/src/parse.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/parse.cpp')
-rw-r--r--src/parse.cpp30
1 files changed, 17 insertions, 13 deletions
diff --git a/src/parse.cpp b/src/parse.cpp
index e37f095..a6af50c 100644
--- a/src/parse.cpp
+++ b/src/parse.cpp
@@ -31,22 +31,27 @@ using namespace std;
inline SExp
macDef(PEnv& penv, const SExp& exp)
{
- THROW_IF(exp.size() < 3, exp.loc, "[MAC] `def' requires at least 2 arguments")
- if (exp.at(1).type == SExp::ATOM) {
+ SExp::const_iterator i = exp.begin();
+ THROW_IF(i == exp.end(), exp.loc, "Unexpected end of `def' macro call");
+ const SExp& name = *(++i);
+ THROW_IF(i == exp.end(), name.loc, "Unexpected end of `def' macro call");
+ if (name.type == SExp::ATOM) {
return exp;
} else {
// (def (f x) y) => (def f (fn (x) y))
SExp argsExp(exp.loc);
- for (size_t i = 1; i < exp.at(1).size(); ++i)
- argsExp.push_back(exp.at(1).at(i));
- SExp fnExp(exp.at(2).loc);
- fnExp.push_back(SExp(exp.at(2).loc, "fn"));
+ SExp::const_iterator j = name.begin();
+ for (++j; j != name.end(); ++j)
+ argsExp.push_back(*j);
+ const SExp& body = *(++i);
+ SExp fnExp(body.loc);
+ fnExp.push_back(SExp(body.loc, "fn"));
fnExp.push_back(argsExp);
- for (size_t i = 2; i < exp.size(); ++i)
- fnExp.push_back(exp.at(i));
+ for (; i != exp.end(); ++i)
+ fnExp.push_back(*i);
SExp ret(exp.loc);
- ret.push_back(exp.at(0));
- ret.push_back(exp.at(1).at(0));
+ ret.push_back(exp.front());
+ ret.push_back(name.front());
ret.push_back(fnExp);
return ret;
}
@@ -74,9 +79,8 @@ parseLiteral(PEnv& penv, const SExp& exp, void* arg)
inline AST*
parseFn(PEnv& penv, const SExp& exp, void* arg)
{
- THROW_IF(exp.size() < 2,exp.loc, "Missing function parameters and body");
- THROW_IF(exp.size() < 3, exp.loc, "Missing function body");
- SExp::const_iterator a = exp.begin(); ++a;
+ SExp::const_iterator a = exp.begin();
+ THROW_IF(++a == exp.end(), exp.loc, "Unexpected end of `fn' form");
AFn* ret = tup<AFn>(exp.loc, penv.sym("fn"), new ATuple(penv.parseTuple(*a++)), 0);
while (a != exp.end())
ret->push_back(penv.parse(*a++));