/* Resp: A programming language * Copyright (C) 2008-2009 David Robillard * * Resp is free software: you can redistribute it and/or modify it under * the terms of the GNU Affero General Public License as published by the * Free Software Foundation, either version 3 of the License, or (at your * option) any later version. * * Resp is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General * Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with Resp. If not, see . */ /** @file * @brief Compile all code (compilation pass 2) */ #include "resp.hpp" using namespace std; #define COMPILE_LITERAL(CT) \ template<> CVal ALiteral::compile(CEnv& cenv) const throw() { \ return cenv.engine()->compileLiteral(cenv, this); \ } COMPILE_LITERAL(int32_t); COMPILE_LITERAL(float); COMPILE_LITERAL(bool); CVal AString::compile(CEnv& cenv) const throw() { return cenv.engine()->compileString(cenv, c_str()); } CVal AQuote::compile(CEnv& cenv) const throw() { return (*(begin() + 1))->compile(cenv); } CVal ALexeme::compile(CEnv& cenv) const throw() { return cenv.engine()->compileString(cenv, c_str()); } CVal ASymbol::compile(CEnv& cenv) const throw() { if (cenv.vals.topLevel(this) && cenv.type(this)->head()->str() != "Fn") { return cenv.engine()->getGlobal(cenv, cppstr, *cenv.vals.ref(this)); } else { return *cenv.vals.ref(this); } } CVal AFn::compile(CEnv& cenv) const throw() { const AType* type = cenv.type(this); CFunc f = cenv.findImpl(this, type); if (f) return f; // Write function declaration f = cenv.engine()->startFunction(cenv, name, prot(), type); // Create a new environment frame and bind argument values cenv.engine()->pushFunctionArgs(cenv, this, type, f); assert(!cenv.currentFn); cenv.currentFn = f; // Write function body CVal retVal = NULL; for (AFn::const_iterator i = begin() + 2; i != end(); ++i) retVal = (*i)->compile(cenv); // Write function conclusion cenv.engine()->finishFunction(cenv, f, retVal); // Pop environment frame cenv.pop(); cenv.currentFn = NULL; cenv.vals.def(cenv.penv.sym(name), f); cenv.addImpl(this, f); return f; } CVal ACall::compile(CEnv& cenv) const throw() { CFunc f = (*begin())->compile(cenv); if (!f) f = cenv.currentFn; // Recursive call (callee defined as a stub) vector args; for (const_iterator e = begin() + 1; e != end(); ++e) args.push_back((*e)->compile(cenv)); return cenv.engine()->compileCall(cenv, f, cenv.type(head()), args); } CVal ADef::compile(CEnv& cenv) const throw() { cenv.def(sym(), body(), cenv.type(body()), NULL); // define stub first for recursion CVal val = body()->compile(cenv); if (cenv.vals.size() == 1 && cenv.type(body())->head()->str() != "Fn") { val = cenv.engine()->compileGlobal( cenv, cenv.type(body()), sym()->str(), val); cenv.lock(this); } cenv.vals.def(sym(), val); return NULL; } CVal AIf::compile(CEnv& cenv) const throw() { return cenv.engine()->compileIf(cenv, this); } CVal ACons::compile(CEnv& cenv) const throw() { return ATuple::compile(cenv); } CVal ATuple::compile(CEnv& cenv) const throw() { AType* type = tup(loc, const_cast(head()->as()), 0); vector fields; for (const_iterator i = begin() + 1; i != end(); ++i) { type->push_back(const_cast(cenv.type(*i))); fields.push_back((*i)->compile(cenv)); } return cenv.engine()->compileTup(cenv, type, type->compile(cenv), fields); } CVal AType::compile(CEnv& cenv) const throw() { const ASymbol* sym = head()->as(); CVal* existing = cenv.vals.ref(sym); if (existing) { return *existing; } else { CVal compiled = cenv.engine()->compileString(cenv, (string("__T_") + head()->str()).c_str()); cenv.vals.def(sym, compiled); return compiled; } } CVal ADot::compile(CEnv& cenv) const throw() { const_iterator i = begin(); AST* tup = *++i; ALiteral* index = (*++i)->as*>(); CVal tupVal = tup->compile(cenv); return cenv.engine()->compileDot(cenv, tupVal, index->val); } CVal APrimitive::compile(CEnv& cenv) const throw() { return cenv.engine()->compilePrimitive(cenv, this); } CVal AMatch::compile(CEnv& cenv) const throw() { return cenv.engine()->compileMatch(cenv, this); }