/* 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 Compile AST using generic backend interface * * Compilation pass functions (lift/compile) that don't require direct use * of any specific backend API are implemented here. Others are implemented * in e.g. llvm.cpp. */ #include "tuplr.hpp" using namespace std; CValue ASymbol::compile(CEnv& cenv) { return cenv.vals.ref(this); } 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 (size_t i = 2; i < size(); ++i) at(i)->lift(cenv); cenv.pop(); AType* type = cenv.type(this); if (impls.find(type) || !type->concrete()) return; AType* protT = type->at(1)->as(); liftCall(cenv, *protT); } void ACall::lift(CEnv& cenv) { AFn* c = cenv.tenv.resolve(at(0))->to(); AType argsT(loc); // Lift arguments for (size_t i = 1; i < size(); ++i) { at(i)->lift(cenv); argsT.push_back(cenv.type(at(i))); } if (!c) return; // Primitive if (c->prot()->size() < size() - 1) throw Error(loc, (format("too many arguments to function `%1%'") % at(0)->str()).str()); if (c->prot()->size() > size() - 1) throw Error(loc, (format("too few arguments to function `%1%'") % at(0)->str()).str()); c->liftCall(cenv, argsT); // Lift called closure } CValue ACall::compile(CEnv& cenv) { AFn* c = cenv.tenv.resolve(at(0))->to(); if (!c) return NULL; // Primitive AType protT(loc); for (size_t i = 1; i < size(); ++i) protT.push_back(cenv.type(at(i))); TEnv::GenericTypes::const_iterator gt = cenv.tenv.genericTypes.find(c); assert(gt != cenv.tenv.genericTypes.end()); AType fnT(loc); fnT.push_back(cenv.penv.sym("Fn")); fnT.push_back(&protT); fnT.push_back(cenv.type(this)); CFunction f = c->impls.find(&fnT); THROW_IF(!f, loc, (format("callee failed to compile for type %1%") % fnT.str()).str()); vector args(size() - 1); for (size_t i = 0; i < args.size(); ++i) args[i] = cenv.compile(at(i + 1)); return cenv.engine()->compileCall(cenv, f, args); } void ADef::lift(CEnv& cenv) { // Define stub first for recursion cenv.def(sym(), at(2), cenv.type(at(2)), NULL); AFn* c = at(2)->to(); if (c) c->name = sym()->str(); at(2)->lift(cenv); } CValue ADef::compile(CEnv& cenv) { // Define stub first for recursion cenv.def(sym(), at(2), cenv.type(at(2)), NULL); CValue val = cenv.compile(at(size() - 1)); cenv.vals.def(sym(), val); return val; }