aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2009-06-29 00:34:16 +0000
committerDavid Robillard <d@drobilla.net>2009-06-29 00:34:16 +0000
commitf5ea52df50d4b199904c42cadff3917621f336db (patch)
tree67332e0b8bd1ea32542e755289c536949f673494
parent314482b95ff83f1f88e41d45fadd05a7e625bb0c (diff)
downloadresp-f5ea52df50d4b199904c42cadff3917621f336db.tar.gz
resp-f5ea52df50d4b199904c42cadff3917621f336db.tar.bz2
resp-f5ea52df50d4b199904c42cadff3917621f336db.zip
Split code up further.
git-svn-id: http://svn.drobilla.net/resp/tuplr@166 ad02d1e2-f140-0410-9f75-f8b11f17cedd
-rw-r--r--Makefile1
-rw-r--r--src/repl.cpp151
-rw-r--r--src/tuplr.cpp155
-rw-r--r--tuplr.dox2
4 files changed, 154 insertions, 155 deletions
diff --git a/Makefile b/Makefile
index 9388f95..8adb8f7 100644
--- a/Makefile
+++ b/Makefile
@@ -19,6 +19,7 @@ OBJECTS = \
build/llvm.so \
build/parse.o \
build/pprint.o \
+ build/repl.o \
build/tuplr.o \
build/unify.o
diff --git a/src/repl.cpp b/src/repl.cpp
new file mode 100644
index 0000000..df85293
--- /dev/null
+++ b/src/repl.cpp
@@ -0,0 +1,151 @@
+/* Tuplr: A programming language
+ * 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/>.
+ */
+
+/** @file
+ * @brief REPL and eval
+ */
+
+#include <cerrno>
+#include <cstring>
+#include <fstream>
+#include "tuplr.hpp"
+
+using namespace std;
+
+int
+eval(CEnv& cenv, const string& name, istream& is)
+{
+ AST* result = NULL;
+ AType* resultType = NULL;
+ list< pair<SExp, AST*> > exprs;
+ Cursor cursor(name);
+ try {
+ while (true) {
+ SExp exp = readExpression(cursor, is);
+ if (exp.type == SExp::LIST && exp.empty())
+ break;
+
+ result = cenv.penv.parse(exp); // Parse input
+ Constraints c;
+ result->constrain(cenv.tenv, c); // Constrain types
+ cenv.tsubst = Subst::compose(cenv.tsubst, TEnv::unify(c)); // Solve type constraints
+ resultType = cenv.type(result);
+ result->lift(cenv); // Lift functions
+ exprs.push_back(make_pair(exp, result));
+
+ // Add definitions as GC roots
+ if (result->to<ADef*>())
+ cenv.lock(result);
+
+ // Add types in type substition as GC roots
+ for (Subst::iterator i = cenv.tsubst.begin(); i != cenv.tsubst.end(); ++i) {
+ Object::pool.addRoot(i->first);
+ Object::pool.addRoot(i->second);
+ }
+ }
+
+ // Print CPS form
+ CValue val = NULL;
+ /*for (list< pair<SExp, AST*> >::const_iterator i = exprs.begin(); i != exprs.end(); ++i) {
+ cout << "; CPS" << endl;
+ pprint(cout, i->second->cps(cenv.tenv, cenv.penv.sym("cont")));
+ }*/
+
+ if (resultType->concrete()) {
+ // Create function for top-level of program
+ CFunction f = cenv.engine()->startFunction(cenv, "main", resultType, ATuple(cursor));
+
+ // Compile all expressions into it
+ for (list< pair<SExp, AST*> >::const_iterator i = exprs.begin(); i != exprs.end(); ++i)
+ val = cenv.compile(i->second);
+
+ // Finish and call it
+ cenv.engine()->finishFunction(cenv, f, resultType, val);
+ cenv.out << cenv.engine()->call(cenv, f, resultType);
+ }
+ cenv.out << " : " << resultType << endl;
+
+ Object::pool.collect(Object::pool.roots());
+
+ if (cenv.args.find("-d") != cenv.args.end())
+ cenv.engine()->writeModule(cenv, cenv.out);
+
+ } catch (Error& e) {
+ cenv.err << e.what() << endl;
+ return 1;
+ }
+ return 0;
+}
+
+int
+repl(CEnv& cenv)
+{
+ while (1) {
+ cenv.out << "() ";
+ cenv.out.flush();
+ Cursor cursor("(stdin)");
+
+ try {
+ SExp exp = readExpression(cursor, std::cin);
+ if (exp.type == SExp::LIST && exp.empty())
+ break;
+
+ AST* body = cenv.penv.parse(exp); // Parse input
+ Constraints c;
+ body->constrain(cenv.tenv, c); // Constrain types
+
+ Subst oldSubst = cenv.tsubst;
+ cenv.tsubst = Subst::compose(cenv.tsubst, TEnv::unify(c)); // Solve type constraints
+
+ AType* bodyT = cenv.type(body);
+ THROW_IF(!bodyT, cursor, "call to untyped body")
+
+ body->lift(cenv);
+
+ CFunction f = NULL;
+ try {
+ // Create anonymous function to insert code into
+ f = cenv.engine()->startFunction(cenv, cenv.penv.gensymstr("_repl"), bodyT, ATuple(cursor));
+ CValue retVal = cenv.compile(body);
+ cenv.engine()->finishFunction(cenv, f, bodyT, retVal);
+ cenv.out << cenv.engine()->call(cenv, f, bodyT);
+ } catch (Error& e) {
+ ADef* def = body->to<ADef*>();
+ if (def)
+ cenv.out << def->sym();
+ else
+ cenv.out << "?";
+ cenv.engine()->eraseFunction(cenv, f);
+ }
+ cenv.out << " : " << cenv.type(body) << endl;
+
+ // Add definitions as GC roots
+ if (body->to<ADef*>())
+ cenv.lock(body);
+
+ Object::pool.collect(Object::pool.roots());
+
+ cenv.tsubst = oldSubst;
+ if (cenv.args.find("-d") != cenv.args.end())
+ cenv.engine()->writeModule(cenv, cenv.out);
+
+ } catch (Error& e) {
+ cenv.err << e.what() << endl;
+ }
+ }
+ return 0;
+}
diff --git a/src/tuplr.cpp b/src/tuplr.cpp
index a0c001d..3bd90f6 100644
--- a/src/tuplr.cpp
+++ b/src/tuplr.cpp
@@ -22,167 +22,12 @@
#include <cerrno>
#include <cstring>
#include <fstream>
-#include <set>
-#include <sstream>
#include "tuplr.hpp"
using namespace std;
-using boost::format;
GC Object::pool;
-template<typename Atom>
-ostream&
-operator<<(ostream& out, const Exp<Atom>& exp)
-{
- switch (exp.type) {
- case Exp<Atom>::ATOM:
- out << exp.atom;
- break;
- case Exp<Atom>::LIST:
- out << "(";
- for (size_t i = 0; i != exp.size(); ++i)
- out << exp.at(i) << ((i != exp.size() - 1) ? " " : "");
- out << ")";
- break;
- }
- return out;
-}
-
-
-/***************************************************************************
- * EVAL/REPL *
- ***************************************************************************/
-
-int
-eval(CEnv& cenv, const string& name, istream& is)
-{
- AST* result = NULL;
- AType* resultType = NULL;
- list< pair<SExp, AST*> > exprs;
- Cursor cursor(name);
- try {
- while (true) {
- SExp exp = readExpression(cursor, is);
- if (exp.type == SExp::LIST && exp.empty())
- break;
-
- result = cenv.penv.parse(exp); // Parse input
- Constraints c;
- result->constrain(cenv.tenv, c); // Constrain types
- cenv.tsubst = Subst::compose(cenv.tsubst, TEnv::unify(c)); // Solve type constraints
- resultType = cenv.type(result);
- result->lift(cenv); // Lift functions
- exprs.push_back(make_pair(exp, result));
-
- // Add definitions as GC roots
- if (result->to<ADef*>())
- cenv.lock(result);
-
- // Add types in type substition as GC roots
- for (Subst::iterator i = cenv.tsubst.begin(); i != cenv.tsubst.end(); ++i) {
- Object::pool.addRoot(i->first);
- Object::pool.addRoot(i->second);
- }
- }
-
- // Print CPS form
- CValue val = NULL;
- /*for (list< pair<SExp, AST*> >::const_iterator i = exprs.begin(); i != exprs.end(); ++i) {
- cout << "; CPS" << endl;
- pprint(cout, i->second->cps(cenv.tenv, cenv.penv.sym("cont")));
- }*/
-
- if (resultType->concrete()) {
- // Create function for top-level of program
- CFunction f = cenv.engine()->startFunction(cenv, "main", resultType, ATuple(cursor));
-
- // Compile all expressions into it
- for (list< pair<SExp, AST*> >::const_iterator i = exprs.begin(); i != exprs.end(); ++i)
- val = cenv.compile(i->second);
-
- // Finish and call it
- cenv.engine()->finishFunction(cenv, f, resultType, val);
- cenv.out << cenv.engine()->call(cenv, f, resultType);
- }
- cenv.out << " : " << resultType << endl;
-
- Object::pool.collect(Object::pool.roots());
-
- if (cenv.args.find("-d") != cenv.args.end())
- cenv.engine()->writeModule(cenv, cenv.out);
-
- } catch (Error& e) {
- cenv.err << e.what() << endl;
- return 1;
- }
- return 0;
-}
-
-int
-repl(CEnv& cenv)
-{
- while (1) {
- cenv.out << "() ";
- cenv.out.flush();
- Cursor cursor("(stdin)");
-
- try {
- SExp exp = readExpression(cursor, std::cin);
- if (exp.type == SExp::LIST && exp.empty())
- break;
-
- AST* body = cenv.penv.parse(exp); // Parse input
- Constraints c;
- body->constrain(cenv.tenv, c); // Constrain types
-
- Subst oldSubst = cenv.tsubst;
- cenv.tsubst = Subst::compose(cenv.tsubst, TEnv::unify(c)); // Solve type constraints
-
- AType* bodyT = cenv.type(body);
- THROW_IF(!bodyT, cursor, "call to untyped body")
-
- body->lift(cenv);
-
- CFunction f = NULL;
- try {
- // Create anonymous function to insert code into
- f = cenv.engine()->startFunction(cenv, cenv.penv.gensymstr("_repl"), bodyT, ATuple(cursor));
- CValue retVal = cenv.compile(body);
- cenv.engine()->finishFunction(cenv, f, bodyT, retVal);
- cenv.out << cenv.engine()->call(cenv, f, bodyT);
- } catch (Error& e) {
- ADef* def = body->to<ADef*>();
- if (def)
- cenv.out << def->sym();
- else
- cenv.out << "?";
- cenv.engine()->eraseFunction(cenv, f);
- }
- cenv.out << " : " << cenv.type(body) << endl;
-
- // Add definitions as GC roots
- if (body->to<ADef*>())
- cenv.lock(body);
-
- Object::pool.collect(Object::pool.roots());
-
- cenv.tsubst = oldSubst;
- if (cenv.args.find("-d") != cenv.args.end())
- cenv.engine()->writeModule(cenv, cenv.out);
-
- } catch (Error& e) {
- cenv.err << e.what() << endl;
- }
- }
- return 0;
-}
-
-
-/***************************************************************************
- * MAIN *
- ***************************************************************************/
-
int
print_usage(char* name, bool error)
{
diff --git a/tuplr.dox b/tuplr.dox
index 456d159..9e793a7 100644
--- a/tuplr.dox
+++ b/tuplr.dox
@@ -570,7 +570,9 @@ INPUT = src/constrain.cpp \
src/gclib.cpp \
src/lex.cpp \
src/llvm.cpp \
+ src/parse.cpp \
src/pprint.cpp \
+ src/repl.cpp \
src/tuplr.cpp \
src/tuplr.hpp \
src/unify.cpp