aboutsummaryrefslogtreecommitdiffstats
path: root/write.cpp
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2009-03-07 04:35:13 +0000
committerDavid Robillard <d@drobilla.net>2009-03-07 04:35:13 +0000
commitb754068a1e11cf480469836c6a04c6614f4d63c5 (patch)
tree7aa986f288e6b967db5b2de664acbe29a13690d3 /write.cpp
parent6d9b8a06e9d9fece731d045db2f815f261db09c3 (diff)
downloadresp-b754068a1e11cf480469836c6a04c6614f4d63c5.tar.gz
resp-b754068a1e11cf480469836c6a04c6614f4d63c5.tar.bz2
resp-b754068a1e11cf480469836c6a04c6614f4d63c5.zip
Stream based serialisation of AST nodes (can write arbitrarily large expressions without chewing memory).
git-svn-id: http://svn.drobilla.net/resp/tuplr@75 ad02d1e2-f140-0410-9f75-f8b11f17cedd
Diffstat (limited to 'write.cpp')
-rw-r--r--write.cpp55
1 files changed, 55 insertions, 0 deletions
diff --git a/write.cpp b/write.cpp
new file mode 100644
index 0000000..b5784ab
--- /dev/null
+++ b/write.cpp
@@ -0,0 +1,55 @@
+/* Tuplr Serialisation
+ * Copyright (C) 2008-2009 David Robillard <dave@drobilla.net>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include "tuplr.hpp"
+
+ostream&
+operator<<(ostream& out, const AST* ast)
+{
+#define TRY_WRITE_LITERAL(T) \
+ const ASTLiteral<T>* lit ## T = dynamic_cast<const ASTLiteral<T>*>(ast); \
+ if (lit ## T) \
+ return out << (lit ## T)->val;
+
+ TRY_WRITE_LITERAL(int32_t);
+ TRY_WRITE_LITERAL(float);
+ TRY_WRITE_LITERAL(bool);
+
+ const ASTSymbol* sym = dynamic_cast<const ASTSymbol*>(ast);
+ if (sym)
+ return out << sym->cppstr;
+
+ const AType* type = dynamic_cast<const AType*>(ast);
+ 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 ASTTuple* tup = dynamic_cast<const ASTTuple*>(ast);
+ if (tup) {
+ out << "(";
+ for (size_t i = 0; i != tup->size(); ++i)
+ out << tup->at(i) << ((i != tup->size() - 1) ? " " : "");
+ return out << ")";
+ }
+
+ return out << "?";
+}
+