[pure-lang-svn] SF.net SVN: pure-lang:[518] pure/trunk
Status: Beta
Brought to you by:
agraef
|
From: <ag...@us...> - 2008-08-17 11:25:25
|
Revision: 518
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=518&view=rev
Author: agraef
Date: 2008-08-17 11:25:34 +0000 (Sun, 17 Aug 2008)
Log Message:
-----------
Revised list-of-tuple syntax.
Modified Paths:
--------------
pure/trunk/ChangeLog
pure/trunk/expr.cc
pure/trunk/expr.hh
pure/trunk/interpreter.cc
pure/trunk/parser.yy
pure/trunk/printer.cc
pure/trunk/test/test015.log
Modified: pure/trunk/ChangeLog
===================================================================
--- pure/trunk/ChangeLog 2008-08-17 09:51:09 UTC (rev 517)
+++ pure/trunk/ChangeLog 2008-08-17 11:25:34 UTC (rev 518)
@@ -1,5 +1,9 @@
2008-08-17 Albert Graef <Dr....@t-...>
+ * parser.yy, printer.cc et al: Revised list-of-tuples syntax. In
+ order to include a tuple in a proper list value you can simply put
+ the tuple inside parentheses now.
+
* parser.yy, lexer.ll: Revised 'using' syntax so that script names
are now separated with a comma. Updated library and sample scripts
accordingly.
Modified: pure/trunk/expr.cc
===================================================================
--- pure/trunk/expr.cc 2008-08-17 09:51:09 UTC (rev 517)
+++ pure/trunk/expr.cc 2008-08-17 11:25:34 UTC (rev 518)
@@ -175,15 +175,6 @@
return is_nil();
}
-bool expr::is_listx() const
-{
- expr x, y;
- if (is_cons(x, y))
- return !x.is_pair() && y.is_listx();
- else
- return is_nil();
-}
-
bool expr::is_voidx() const
{
return tag() == interpreter::g_interp->symtab.void_sym().f;
@@ -226,25 +217,6 @@
}
}
-bool expr::is_listx(exprl &xs) const
-{
- expr x, y;
- if (is_cons(x, y)) {
- if (x.is_pair()) {
- xs.clear();
- return false;
- } else {
- xs.push_back(x);
- return y.is_listx(xs);
- }
- } else if (is_nil())
- return true;
- else {
- xs.clear();
- return false;
- }
-}
-
bool expr::is_pair(expr &x, expr &y) const
{
expr u, v;
@@ -280,6 +252,17 @@
}
}
+bool expr::is_tuplexxx(exprl &xs) const
+{
+ expr x, y;
+ if (is_pair(x, y) && !(flags()&EXPR::PAREN))
+ return x.is_tuplexxx(xs) && y.is_tuplexxx(xs);
+ else {
+ xs.push_back(*this);
+ return true;
+ }
+}
+
env_info::env_info(const env_info& e) : t(e.t), temp(e.temp) {
switch (t) {
case none:
Modified: pure/trunk/expr.hh
===================================================================
--- pure/trunk/expr.hh 2008-08-17 09:51:09 UTC (rev 517)
+++ pure/trunk/expr.hh 2008-08-17 11:25:34 UTC (rev 518)
@@ -117,7 +117,8 @@
// special flag values used during compilation:
enum {
- OVF = 1, // overflowed int constant -> bigint
+ OVF = 1, // overflowed int constant -> bigint
+ PAREN = 1<<1, // parenthesized expression
};
uint32_t refc; // reference counter
@@ -469,9 +470,6 @@
bool is_nil() const;
bool is_cons() const;
bool is_list() const;
- // Check for lists which don't contain tuple elements, so that they can be
- // printed in standard list format.
- bool is_listx() const;
bool is_voidx() const;
bool is_pair() const;
// This is always true, as we consider a singleton as a tuple, too. Use
@@ -481,12 +479,13 @@
bool is_tuplex() const;
bool is_cons(expr &x, expr &y) const;
bool is_list(exprl &xs) const;
- bool is_listx(exprl &xs) const;
bool is_pair(expr &x, expr &y) const;
// Always true (see note above). Use is_pair() && istuple(xs) to test for a
// "real" tuple instead.
bool is_tuple(exprl &xs) const;
bool is_tuplex(exprl &xs) const;
+ // Special check for tuples used in list construction.
+ bool is_tuplexxx(exprl &xs) const;
};
/* Rules of the form: lhs -> rhs [if qual]. */
Modified: pure/trunk/interpreter.cc
===================================================================
--- pure/trunk/interpreter.cc 2008-08-17 09:51:09 UTC (rev 517)
+++ pure/trunk/interpreter.cc 2008-08-17 11:25:34 UTC (rev 518)
@@ -1920,7 +1920,7 @@
{
expr *u;
exprl xs;
- if (x->is_pair() && x->is_tuple(xs))
+ if (x->is_pair() && x->is_tuplexxx(xs))
u = new expr(expr::list(xs));
else
u = new expr(expr::cons(*x, expr::nil()));
Modified: pure/trunk/parser.yy
===================================================================
--- pure/trunk/parser.yy 2008-08-17 09:51:09 UTC (rev 517)
+++ pure/trunk/parser.yy 2008-08-17 11:25:34 UTC (rev 518)
@@ -515,7 +515,8 @@
| '[' expr ']' { $$ = interp.mklist_expr($2); }
| '[' expr ';' comp_clauses ']'
{ $$ = interp.mklistcomp_expr($2, $4); }
-| '(' expr ')' { $$ = $2; }
+| '(' expr ')' { $$ = $2;
+ if ($$->is_pair()) $$->flags() |= EXPR::PAREN; }
| '(' op ')' { $$ = $2; }
;
Modified: pure/trunk/printer.cc
===================================================================
--- pure/trunk/printer.cc 2008-08-17 09:51:09 UTC (rev 517)
+++ pure/trunk/printer.cc 2008-08-17 11:25:34 UTC (rev 518)
@@ -84,7 +84,7 @@
case EXPR::APP: {
expr u, v, w;
prec_t p;
- if (x.is_listx())
+ if (x.is_list())
return 100;
else if (x.is_app(u, v))
if (u.tag() > 0 && (p = sym_nprec(u.tag())) < 100 && p%10 >= 3)
@@ -248,11 +248,11 @@
expr u, v, w, y;
exprl xs;
prec_t p;
- if (x.is_listx(xs)) {
+ if (x.is_list(xs)) {
// proper list value
size_t n = xs.size();
os << "[";
- if (n>1) {
+ if (n>1 || n==1 && xs.front().is_pair()) {
// list elements at a precedence not larger than ',' have to be
// parenthesized
p = sym_nprec(interpreter::g_interp->symtab.pair_sym().f) + 1;
@@ -528,8 +528,7 @@
static bool pure_is_cons(const pure_expr *x)
{
if (x->tag == EXPR::APP && x->data.x[0]->tag == EXPR::APP)
- return
- x->data.x[0]->data.x[0]->tag == interpreter::g_interp->symtab.cons_sym().f;
+ return x->data.x[0]->data.x[0]->tag == interpreter::g_interp->symtab.cons_sym().f;
else
return false;
}
@@ -537,8 +536,7 @@
static bool pure_is_pair(const pure_expr *x)
{
if (x->tag == EXPR::APP && x->data.x[0]->tag == EXPR::APP)
- return
- x->data.x[0]->data.x[0]->tag == interpreter::g_interp->symtab.pair_sym().f;
+ return x->data.x[0]->data.x[0]->tag == interpreter::g_interp->symtab.pair_sym().f;
else
return false;
}
@@ -546,22 +544,16 @@
static bool pure_is_list(const pure_expr *x)
{
while (pure_is_cons(x))
- if (pure_is_pair(x->data.x[0]->data.x[1]))
- return false;
- else
- x = x->data.x[1];
+ x = x->data.x[1];
return pure_is_nil(x);
}
static bool pure_is_list(const pure_expr *x, list<const pure_expr*>& xs)
{
- while (pure_is_cons(x))
- if (pure_is_pair(x->data.x[0]->data.x[1]))
- return false;
- else {
- xs.push_back(x->data.x[0]->data.x[1]);
- x = x->data.x[1];
- }
+ while (pure_is_cons(x)) {
+ xs.push_back(x->data.x[0]->data.x[1]);
+ x = x->data.x[1];
+ }
return pure_is_nil(x);
}
@@ -681,7 +673,7 @@
// proper list value
size_t n = xs.size();
os << "[";
- if (n>1) {
+ if (n>1 || n==1 && pure_is_pair(xs.front())) {
// list elements at a precedence not larger than ',' have to be
// parenthesized
p = sym_nprec(interpreter::g_interp->symtab.pair_sym().f) + 1;
Modified: pure/trunk/test/test015.log
===================================================================
--- pure/trunk/test/test015.log 2008-08-17 09:51:09 UTC (rev 517)
+++ pure/trunk/test/test015.log 2008-08-17 11:25:34 UTC (rev 518)
@@ -196,25 +196,25 @@
state 1: #0
}) (11..20));
{
- rule #0: c = hdict$zipwith (=>) (catmap (\i -> (i,double i,str i):[]) (1..10)) (1..10)
+ rule #0: c = hdict$zipwith (=>) (catmap (\i -> [(i,double i,str i)]) (1..10)) (1..10)
state 0: #0
<var> state 1
state 1: #0
}
-let c = hdict$zipwith (=>) (catmap (\i/*0:*/ -> (i/*0:*/,double i/*0:*/,str i/*0:*/):[] {
- rule #0: i = (i,double i,str i):[]
+let c = hdict$zipwith (=>) (catmap (\i/*0:*/ -> [(i/*0:*/,double i/*0:*/,str i/*0:*/)] {
+ rule #0: i = [(i,double i,str i)]
state 0: #0
<var> state 1
state 1: #0
}) (1..10)) (1..10);
{
- rule #0: d = hdict$zipwith (=>) (catmap (\i -> (i,double i,str i):[]) (11..20)) (11..20)
+ rule #0: d = hdict$zipwith (=>) (catmap (\i -> [(i,double i,str i)]) (11..20)) (11..20)
state 0: #0
<var> state 1
state 1: #0
}
-let d = hdict$zipwith (=>) (catmap (\i/*0:*/ -> (i/*0:*/,double i/*0:*/,str i/*0:*/):[] {
- rule #0: i = (i,double i,str i):[]
+let d = hdict$zipwith (=>) (catmap (\i/*0:*/ -> [(i/*0:*/,double i/*0:*/,str i/*0:*/)] {
+ rule #0: i = [(i,double i,str i)]
state 0: #0
<var> state 1
state 1: #0
@@ -238,8 +238,8 @@
Dict (bin "4" "4" 0 (bin "2" "2" 1 (bin "1" "1" (-1) nil (bin "10" "10" 0 nil nil)) (bin "3" "3" 0 nil nil)) (bin "6" "6" (-1) (bin "5" "5" 0 nil nil) (bin "8" "8" 0 (bin "7" "7" 0 nil nil) (bin "9" "9" 0 nil nil))))
mkdict 1000 (1..10);
Dict (bin 4 1000 (-1) (bin 2 1000 0 (bin 1 1000 0 nil nil) (bin 3 1000 0 nil nil)) (bin 8 1000 0 (bin 6 1000 0 (bin 5 1000 0 nil nil) (bin 7 1000 0 nil nil)) (bin 9 1000 (-1) nil (bin 10 1000 0 nil nil))))
-mkhdict 1000 (catmap (\i/*0:*/ -> (i/*0:*/,double i/*0:*/,str i/*0:*/):[] {
- rule #0: i = (i,double i,str i):[]
+mkhdict 1000 (catmap (\i/*0:*/ -> [(i/*0:*/,double i/*0:*/,str i/*0:*/)] {
+ rule #0: i = [(i,double i,str i)]
state 0: #0
<var> state 1
state 1: #0
@@ -297,8 +297,8 @@
<stdin>:166.39-59: unhandled exception 'out_of_bounds' while evaluating 'c!(50,50.0,"50")'
a!!(5..15);
[5.0,6.0,7.0,8.0,9.0,10.0]
-c!!catmap (\i/*0:*/ -> (i/*0:*/,double i/*0:*/,str i/*0:*/):[] {
- rule #0: i = (i,double i,str i):[]
+c!!catmap (\i/*0:*/ -> [(i/*0:*/,double i/*0:*/,str i/*0:*/)] {
+ rule #0: i = [(i,double i,str i)]
state 0: #0
<var> state 1
state 1: #0
@@ -311,7 +311,7 @@
keys a;
[1,2,3,4,5,6,7,8,9,10]
keys c;
-(1,1.0,"1"):(2,2.0,"2"):(3,3.0,"3"):(4,4.0,"4"):(5,5.0,"5"):(6,6.0,"6"):(7,7.0,"7"):(8,8.0,"8"):(9,9.0,"9"):(10,10.0,"10"):[]
+[(1,1.0,"1"),(2,2.0,"2"),(3,3.0,"3"),(4,4.0,"4"),(5,5.0,"5"),(6,6.0,"6"),(7,7.0,"7"),(8,8.0,"8"),(9,9.0,"9"),(10,10.0,"10")]
vals a;
[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0]
vals c;
@@ -328,8 +328,8 @@
Dict nil
delete a 5000;
Dict (bin 4 4.0 (-1) (bin 2 2.0 0 (bin 1 1.0 0 nil nil) (bin 3 3.0 0 nil nil)) (bin 8 8.0 0 (bin 6 6.0 0 (bin 5 5.0 0 nil nil) (bin 7 7.0 0 nil nil)) (bin 9 9.0 (-1) nil (bin 10 10.0 0 nil nil))))
-foldl delete c (catmap (\i/*0:*/ -> (i/*0:*/,double i/*0:*/,str i/*0:*/):[] {
- rule #0: i = (i,double i,str i):[]
+foldl delete c (catmap (\i/*0:*/ -> [(i/*0:*/,double i/*0:*/,str i/*0:*/)] {
+ rule #0: i = [(i,double i,str i)]
state 0: #0
<var> state 1
state 1: #0
@@ -519,13 +519,13 @@
<stdin>:292.24-36: unhandled exception 'out_of_bounds' while evaluating 'c!(20,30)'
a!!(5..15);
[6,7,8,9,10]
-c!!catmap (\i/*0:*/ -> catmap (\j/*0:*/ -> (i/*1:*/,j/*0:*/):[] {
- rule #0: j = (i,j):[]
+c!!catmap (\i/*0:*/ -> catmap (\j/*0:*/ -> [(i/*1:*/,j/*0:*/)] {
+ rule #0: j = [(i,j)]
state 0: #0
<var> state 1
state 1: #0
}) (3..15) {
- rule #0: i = catmap (\j -> (i,j):[]) (3..15)
+ rule #0: i = catmap (\j -> [(i,j)]) (3..15)
state 0: #0
<var> state 1
state 1: #0
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|