/* Tuplr Type Inferencing * 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 . */ #include #include "tuplr.hpp" /*************************************************************************** * CPS Conversion * ***************************************************************************/ /** (cps x) => (cont x) */ AST* AST::cps(TEnv& tenv, AST* cont) { return new ACall(loc, cont, this, 0); } AST* ATuple::cps(TEnv& tenv, AST* cont) { ATuple* copy = new ATuple(loc, NULL); FOREACH(const_iterator, p, *this) copy->push_back((*p)->cps(tenv, cont)); return copy; } /** (cps (fn (a ...) body ...)) => */ AST* AFn::cps(TEnv& tenv, AST* cont) { AFn* copy = new AFn(loc, tenv.penv.sym("fn"), prot()); const_iterator p = begin(); ++(++p); for (; p != end(); ++p) copy->push_back((*p)->cps(tenv, cont)); return copy; return ATuple::cps(tenv, cont); } /** (cps (f a b ...)) => (a (fn (x) (b (fn (y) ... (cont (f x y ...)) */ AST* ACall::cps(TEnv& tenv, AST* cont) { return new ACall(loc, cont, this, 0); } /** (cps (def x y)) => (y (fn (x) (cont))) */ AST* ADef::cps(TEnv& tenv, AST* cont) { ADef* copy = new ADef(loc, ATuple(loc, tenv.penv.sym("def"), sym(), at(2)->cps(tenv, cont), 0)); return copy; /* ASymbol* fnSym = tenv.penv.sym("deff"); AFn* defFn = new AFn(loc, tenv.penv.sym("def"), new ATuple(at(1)->loc, sym(), new ACall(loc, cont, this))); ACall* defCall = new ACall(loc, at(2)->cps(tenv, defFn), 0); */ } /** (cps (if c t ... e)) => */ AST* AIf::cps(TEnv& tenv, AST* cont) { AFn* contFn = new AFn(loc, tenv.penv.sym("if0"), new ATuple(at(1)->loc, cont, 0)); //ACall* contCall = new ACall(loc, cont, this, 0); ACall* condCall = new ACall(loc, contFn, 0); return condCall; }