From a80bdcc56bab969dbb214344c01a215e66b91f08 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 28 Jun 2009 23:35:44 +0000 Subject: write.cpp -> pprint.cpp. git-svn-id: http://svn.drobilla.net/resp/tuplr@162 ad02d1e2-f140-0410-9f75-f8b11f17cedd --- Makefile | 4 +-- src/pprint.cpp | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/typing.cpp | 8 ----- src/unify.cpp | 7 +++++ src/write.cpp | 97 ---------------------------------------------------------- 5 files changed, 106 insertions(+), 107 deletions(-) create mode 100644 src/pprint.cpp delete mode 100644 src/write.cpp diff --git a/Makefile b/Makefile index 6eff895..29061a4 100644 --- a/Makefile +++ b/Makefile @@ -15,10 +15,10 @@ OBJECTS = \ build/gc.o \ build/gclib.so \ build/llvm.so \ + build/pprint.o \ build/tuplr.o \ build/typing.o \ - build/unify.o \ - build/write.o + build/unify.o build/tuplr: $(OBJECTS) g++ -o $@ $^ $(LDFLAGS) diff --git a/src/pprint.cpp b/src/pprint.cpp new file mode 100644 index 0000000..04e3401 --- /dev/null +++ b/src/pprint.cpp @@ -0,0 +1,97 @@ +/* Tuplr Serialisation + * 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 "tuplr.hpp" + +ostream& +operator<<(ostream& out, const AST* ast) +{ + const ALiteral* flit = ast->to*>(); + if (flit) + return out << showpoint << flit->val; + + const ALiteral* ilit = ast->to*>(); + if (ilit) + return out << ilit->val; + + const ALiteral* blit = ast->to*>(); + if (blit) + return out << (blit->val ? "#t" : "#f"); + + const ASymbol* sym = ast->to(); + if (sym) + return out << sym->cppstr; + + const AType* type = ast->to(); + if (type) { + switch (type->kind) { + case AType::VAR: return out << "?" << type->id; + case AType::PRIM: return out << type->at(0); + case AType::EXPR: break; // will catch Tuple case below + } + } + + const ATuple* tup = ast->to(); + if (tup) { + out << "("; + for (size_t i = 0; i != tup->size(); ++i) + out << tup->at(i) << ((i != tup->size() - 1) ? " " : ""); + return out << ")"; + } + + return out << "?"; +} + +void +pprint_internal(ostream& out, const AST* ast, unsigned indent) +{ + const ATuple* tup = ast->to(); + if (tup && tup->size() > 0) { + const string head = tup->at(0)->str(); + ASymbol* headSym = tup->at(0)->to(); + out << "("; + pprint_internal(out, tup->at(0), indent); + unsigned child_indent = indent; + if (tup->size() > 1) { + out << " "; + if (headSym && headSym->cppstr == "fn") { + out << tup->at(1); + child_indent = indent + 4; + } else { + child_indent += head.length() + 1; + pprint_internal(out, tup->at(1), child_indent); + } + } + for (size_t i = 2; i < tup->size(); ++i) { + out << endl << string().insert(0, child_indent, ' '); + pprint_internal(out, tup->at(i), child_indent); + } + out << ")"; + if (headSym && headSym->cppstr == "fn") + out << endl << string().insert(0, indent + 4, ' '); + } else { + out << ast; + } +} + +void +pprint(ostream& out, const AST* ast) +{ + pprint_internal(out, ast, 0); + out << endl; +} + diff --git a/src/typing.cpp b/src/typing.cpp index 5791fdc..787f445 100644 --- a/src/typing.cpp +++ b/src/typing.cpp @@ -18,14 +18,6 @@ #include #include "tuplr.hpp" -void -Constraints::constrain(TEnv& tenv, const AST* o, AType* t) -{ - assert(!o->to()); - push_back(Constraint(tenv.var(o), t, o->loc)); -} - - /*************************************************************************** * AST Type Constraints * ***************************************************************************/ diff --git a/src/unify.cpp b/src/unify.cpp index 2a8e6a0..91d6fde 100644 --- a/src/unify.cpp +++ b/src/unify.cpp @@ -22,6 +22,13 @@ * Type Inference/Substitution * ***************************************************************************/ +void +Constraints::constrain(TEnv& tenv, const AST* o, AType* t) +{ + assert(!o->to()); + push_back(Constraint(tenv.var(o), t, o->loc)); +} + static void substitute(ATuple* tup, const AST* from, AST* to) { diff --git a/src/write.cpp b/src/write.cpp deleted file mode 100644 index 04e3401..0000000 --- a/src/write.cpp +++ /dev/null @@ -1,97 +0,0 @@ -/* Tuplr Serialisation - * 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 "tuplr.hpp" - -ostream& -operator<<(ostream& out, const AST* ast) -{ - const ALiteral* flit = ast->to*>(); - if (flit) - return out << showpoint << flit->val; - - const ALiteral* ilit = ast->to*>(); - if (ilit) - return out << ilit->val; - - const ALiteral* blit = ast->to*>(); - if (blit) - return out << (blit->val ? "#t" : "#f"); - - const ASymbol* sym = ast->to(); - if (sym) - return out << sym->cppstr; - - const AType* type = ast->to(); - if (type) { - switch (type->kind) { - case AType::VAR: return out << "?" << type->id; - case AType::PRIM: return out << type->at(0); - case AType::EXPR: break; // will catch Tuple case below - } - } - - const ATuple* tup = ast->to(); - if (tup) { - out << "("; - for (size_t i = 0; i != tup->size(); ++i) - out << tup->at(i) << ((i != tup->size() - 1) ? " " : ""); - return out << ")"; - } - - return out << "?"; -} - -void -pprint_internal(ostream& out, const AST* ast, unsigned indent) -{ - const ATuple* tup = ast->to(); - if (tup && tup->size() > 0) { - const string head = tup->at(0)->str(); - ASymbol* headSym = tup->at(0)->to(); - out << "("; - pprint_internal(out, tup->at(0), indent); - unsigned child_indent = indent; - if (tup->size() > 1) { - out << " "; - if (headSym && headSym->cppstr == "fn") { - out << tup->at(1); - child_indent = indent + 4; - } else { - child_indent += head.length() + 1; - pprint_internal(out, tup->at(1), child_indent); - } - } - for (size_t i = 2; i < tup->size(); ++i) { - out << endl << string().insert(0, child_indent, ' '); - pprint_internal(out, tup->at(i), child_indent); - } - out << ")"; - if (headSym && headSym->cppstr == "fn") - out << endl << string().insert(0, indent + 4, ' '); - } else { - out << ast; - } -} - -void -pprint(ostream& out, const AST* ast) -{ - pprint_internal(out, ast, 0); - out << endl; -} - -- cgit v1.2.1