[prettyprint] r576 committed - Allow assignment to tuples from lists.

0 views
Skip to first unread message

prett...@googlecode.com

unread,
Jun 24, 2010, 8:51:15 PM6/24/10
to pp-d...@googlegroups.com
Revision: 576
Author: thockin
Date: Thu Jun 24 17:50:36 2010
Log: Allow assignment to tuples from lists.


http://code.google.com/p/prettyprint/source/detail?r=576

Added:
/trunk/language/TODO.language
Modified:
/trunk/language/environment.cpp
/trunk/language/environment.h
/trunk/language/errors.h
/trunk/language/syntax_tree.h
/trunk/language/tests/type_test.cpp
/trunk/language/type.cpp
/trunk/language/type.h

=======================================
--- /dev/null
+++ /trunk/language/TODO.language Thu Jun 24 17:50:36 2010
@@ -0,0 +1,28 @@
+Language issues to be resolved:
+-------------------------------------------------------------
+
+Define a standard lib for things like strcat()
+
+Define how to convert non-strings to strings, including formatting (hex,
etc)
+
+Tuple literals?
+
+auto x = [ 1, 2 ]; // detect x as list<int>
+list<auto> x = [ 1, 2 ]; // detect x as list<int>
+list x = [ 1, 2 ]; // detect x as list<int>
+tuple<auto, auto> x = [ 1, "2" ]; // detext x as tuple<int, string>
+tuple x = [ 1, "2" ]; // detext x as tuple<int, string>
+
+typedef
+
+struct
+
+Can I find a way to name tuple members?
+
+When calling functions is it pass by value or ref? How about assignments?
+
+Can a function redefine args[] in a local scope?
+
+Can a file have more than 1 'module' line?
+
+Can a file have 0 'module' lines?
=======================================
--- /trunk/language/environment.cpp Thu Jun 24 07:46:16 2010
+++ /trunk/language/environment.cpp Thu Jun 24 17:50:36 2010
@@ -80,3 +80,5 @@

} // namespace language
} // namespace pp
+
+// vim: set ai tabstop=4 shiftwidth=4 noexpandtab:
=======================================
--- /trunk/language/environment.h Thu Jun 24 07:46:16 2010
+++ /trunk/language/environment.h Thu Jun 24 17:50:36 2010
@@ -144,3 +144,5 @@
} // namespace pp

#endif // PP_LANGUAGE_ENVIRONMENT_H__
+
+// vim: set ai tabstop=4 shiftwidth=4 noexpandtab:
=======================================
--- /trunk/language/errors.h Thu Jun 24 07:46:16 2010
+++ /trunk/language/errors.h Thu Jun 24 17:50:36 2010
@@ -34,3 +34,5 @@
} // namespace pp

#endif // PP_LANGUAGE_ERRORS_H__
+
+// vim: set ai tabstop=4 shiftwidth=4 noexpandtab:
=======================================
--- /trunk/language/syntax_tree.h Thu Jun 24 07:46:16 2010
+++ /trunk/language/syntax_tree.h Thu Jun 24 17:50:36 2010
@@ -1080,8 +1080,8 @@
ltype);
}
if (!ltype.is_assignable_from(rtype)) {
- throw syntax_error("can't assign '%s' to '%s'",
- rtype, ltype);
+ throw syntax_error("can't assign '%s' from '%s'",
+ ltype, rtype);
}
set_result_type(ltype != Type::VAR ? ltype : rtype);
break;
@@ -1340,7 +1340,6 @@
util::NeverNullScopedPtr<Statement> m_body;
};

-//FIXME: tuple literal? assignable from list?
class ListLiteralExpression : public Expression {
public:
ListLiteralExpression(const Parser::Position &pos, ArgumentList *contents)
=======================================
--- /trunk/language/tests/type_test.cpp Thu Jun 24 07:46:16 2010
+++ /trunk/language/tests/type_test.cpp Thu Jun 24 17:50:36 2010
@@ -195,6 +195,18 @@
TEST_ASSERT(t1.is_assignable_from(t2));
TEST_ASSERT(t2.is_assignable_from(t1));
}
+ // tuple<int> = list<int>
+ {
+ Type t1(Type::TUPLE);
+ t1.add_argument(Type::INT);
+
+ Type t2(Type::LIST);
+ t2.add_argument(Type::INT);
+
+ TEST_ASSERT(!t1.is_equal_to(t2));
+ TEST_ASSERT(t1.is_assignable_from(t2));
+ TEST_ASSERT(!t2.is_assignable_from(t1));
+ }
// tuple<int,int> = tuple<int,int>
{
Type t1(Type::TUPLE);
@@ -209,6 +221,19 @@
TEST_ASSERT(t1.is_assignable_from(t2));
TEST_ASSERT(t2.is_assignable_from(t1));
}
+ // tuple<int,int> = list<int,int>
+ {
+ Type t1(Type::TUPLE);
+ t1.add_argument(Type::INT);
+ t1.add_argument(Type::INT);
+
+ Type t2(Type::LIST);
+ t2.add_argument(Type::INT);
+
+ TEST_ASSERT(!t1.is_equal_to(t2));
+ TEST_ASSERT(t1.is_assignable_from(t2));
+ TEST_ASSERT(!t2.is_assignable_from(t1));
+ }
// tuple<int> = tuple<bool>
{
Type t1(Type::TUPLE);
=======================================
--- /trunk/language/type.cpp Thu Jun 24 07:46:16 2010
+++ /trunk/language/type.cpp Thu Jun 24 17:50:36 2010
@@ -166,14 +166,23 @@
if (m_primitive == VAR || other.m_primitive == VAR) {
return true;
}
- // If the primitives don't match or the number of arguments
- // don't match, it can't possibly be assignable.
- if (m_primitive != other.m_primitive
- || m_arguments.size() != other.m_arguments.size()) {
- return false;
+ // Special case: tuple is assignable from list, as long as args match.
+ // Otherwise, we apply some other rules.
+ if (m_primitive == TUPLE && other.m_primitive == LIST) {
+ // The logic is easier to read this way.
+ } else {
+ // If the primitives don't match, it can't be assignable.
+ if (m_primitive != other.m_primitive) {
+ return false;
+ }
+ // If the number of arguments don't match, it can't be assignable.
+ if (m_arguments.size() != other.m_arguments.size()) {
+ return false;
+ }
}
// All arguments must also be assignable.
- for (size_t i = 0; i < m_arguments.size(); i++) {
+ size_t n_args = std::min(m_arguments.size(), other.m_arguments.size());
+ for (size_t i = 0; i < n_args; i++) {
const Type &lhs = m_arguments[i];
const Type &rhs = other.m_arguments[i];
if (!lhs.is_assignable_from(rhs, ignore_const)) {
@@ -220,3 +229,5 @@

} // namespace language
} // namespace pp
+
+// vim: set ai tabstop=4 shiftwidth=4 noexpandtab:
=======================================
--- /trunk/language/type.h Thu Jun 24 07:46:16 2010
+++ /trunk/language/type.h Thu Jun 24 17:50:36 2010
@@ -186,3 +186,5 @@
} // namespace pp

#endif // PP_LANGUAGE_TYPE_H__
+
+// vim: set ai tabstop=4 shiftwidth=4 noexpandtab:

Reply all
Reply to author
Forward
0 new messages