/* Tuplr: A programming language * Copyright (C) 2008-2009 David Robillard * * Tuplr 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. * * Tuplr 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 Tuplr. If not, see . */ /** @file * @brief Lift functions (compilation pass 1) */ #include "tuplr.hpp" using namespace std; void AFn::lift(CEnv& cenv) { cenv.push(); for (const_iterator p = prot()->begin(); p != prot()->end(); ++p) cenv.def((*p)->as(), *p, NULL, NULL); // Lift body for (iterator i = begin() + 2; i != end(); ++i) (*i)->lift(cenv); cenv.pop(); AType* type = cenv.type(this); if (impls.find(type) || !type->concrete()) return; AType* protT = type->prot()->as(); cenv.engine()->compileFunction(cenv, this, *protT); } void ACall::lift(CEnv& cenv) { AFn* c = cenv.resolve(head())->to(); AType argsT(loc); // Lift arguments for (iterator i = begin() + 1; i != end(); ++i) { (*i)->lift(cenv); argsT.push_back(cenv.type(*i)); } if (!c) return; // Primitive if (c->prot()->size() < size() - 1) throw Error(loc, (format("too many arguments to function `%1%'") % head()->str()).str()); if (c->prot()->size() > size() - 1) throw Error(loc, (format("too few arguments to function `%1%'") % head()->str()).str()); cenv.engine()->compileFunction(cenv, c, argsT); // Lift called closure } void ADot::lift(CEnv& cenv) { (*(begin() + 1))->lift(cenv); } void ADef::lift(CEnv& cenv) { // Define stub first for recursion cenv.def(sym(), body(), cenv.type(body()), NULL); AFn* c = body()->to(); if (c) c->name = sym()->str(); body()->lift(cenv); }