Skip to content
This repository has been archived by the owner on Jan 16, 2021. It is now read-only.

Commit

Permalink
1903 - experiment: suppress paren-insertion inside parens
Browse files Browse the repository at this point in the history
Inspiration: David Wheeler (http://sourceforge.net/mailarchive/message.php?msg_id=29538201)
Gone is the single-space rule.

This change was verified in two ways:
a) The tests all pass.
b) I log all expressions as read after paren-insertion, and make sure the logs
remain the same after these changes.
  • Loading branch information
akkartik committed Jul 14, 2012
1 parent 7db6114 commit 3320d25
Show file tree
Hide file tree
Showing 18 changed files with 63 additions and 170 deletions.
12 changes: 2 additions & 10 deletions 002parenthesize.cc
Expand Up @@ -71,12 +71,11 @@ list<Token> nextExpr(CodeStream& c) {
list<Token> result;
stack<long> explicitParenStack; // parens in the original
stack<long> implicitParenStack; // parens we inserted
long argParenCount=0; // depth of unprocessed parens not at start of line
for (list<Token> line = nextLine(c); !line.empty(); line=nextLine(c)) {
long thisLineIndent=line.front().indentLevel, nextLineIndent=line.back().indentLevel;

bool insertedParenThisLine = false;
if (!argParenCount && numWordsInLine(line) > 1 && noParenAtStart(line) && !continuationLine(thisLineIndent, explicitParenStack)) {
if (explicitParenStack.empty() && numWordsInLine(line) > 1 && noParenAtStart(line)) {
// open paren
add(result, Token::of("("));
implicitParenStack.push(thisLineIndent);
Expand All @@ -94,15 +93,8 @@ list<Token> nextExpr(CodeStream& c) {
RAISE << "Unbalanced )" << endl << DIE;
explicitParenStack.pop();
}

if (*q == "(" && (argParenCount || q != firstNonQuote(line)))
++argParenCount; // no more paren-insertion until it closes
if (*q == ")" && argParenCount) // it closed
--argParenCount;
}

if (argParenCount) continue;

if (nextLineIndent <= thisLineIndent && insertedParenThisLine) {
// close paren for this line
add(result, Token::of(")"));
Expand All @@ -116,7 +108,7 @@ list<Token> nextExpr(CodeStream& c) {
implicitParenStack.pop();
}

if (implicitParenStack.empty() && explicitParenStack.empty() && argParenCount == 0) {
if (implicitParenStack.empty() && explicitParenStack.empty()) {
if (!c.fd.eof())
// Clean up indent state for the next call.
for (int i = 0; i < nextLineIndent; ++i)
Expand Down
69 changes: 4 additions & 65 deletions 002parenthesize.test.cc
Expand Up @@ -431,63 +431,57 @@ void test_parenthesize_groups_across_comments() {
check(c.fd.eof());
}

void test_parenthesize_groups_inside_parens() {
void test_parenthesize_does_not_group_inside_parens() {
CodeStream c(stream("(def foo\n ;a b c\n d e)\nnewdef"));
list<Token> tokens = nextExpr(c);
list<Token>::iterator p = tokens.begin();
checkEq(*p, "("); ++p;
checkEq(*p, "def"); ++p;
checkEq(*p, "foo"); ++p;
checkEq(*p, "("); ++p;
checkEq(*p, "d"); ++p;
checkEq(*p, "e"); ++p;
checkEq(*p, ")"); ++p;
checkEq(*p, ")"); ++p;
check(p == tokens.end());
tokens = nextExpr(c); p = tokens.begin();
checkEq(*p, "newdef"); ++p;
check(p == tokens.end());
check(c.fd.eof());
}

void test_parenthesize_groups_inside_parens2() {
void test_parenthesize_does_not_group_inside_parens2() {
CodeStream c(stream("`(def foo\n ;a b c\n d e)\nnewdef"));
list<Token> tokens = nextExpr(c);
list<Token>::iterator p = tokens.begin();
checkEq(*p, "`"); ++p;
checkEq(*p, "("); ++p;
checkEq(*p, "def"); ++p;
checkEq(*p, "foo"); ++p;
checkEq(*p, "("); ++p;
checkEq(*p, "d"); ++p;
checkEq(*p, "e"); ++p;
checkEq(*p, ")"); ++p;
checkEq(*p, ")"); ++p;
check(p == tokens.end());
tokens = nextExpr(c); p = tokens.begin();
checkEq(*p, "newdef"); ++p;
check(p == tokens.end());
check(c.fd.eof());
}

void test_parenthesize_groups_inside_indented_parens() {
void test_parenthesize_does_not_group_inside_parens3() {
CodeStream c(stream(" (a b c\n d e)"));
list<Token> tokens = nextExpr(c);
list<Token>::iterator p = tokens.begin();
checkEq(*p, "("); ++p;
checkEq(*p, "a"); ++p;
checkEq(*p, "b"); ++p;
checkEq(*p, "c"); ++p;
checkEq(*p, "("); ++p;
checkEq(*p, "d"); ++p;
checkEq(*p, "e"); ++p;
checkEq(*p, ")"); ++p;
checkEq(*p, ")"); ++p;
check(p == tokens.end());
check(c.fd.eof());
}

void test_parenthesize_passes_through_arglists() {
void test_parenthesize_does_not_group_inside_arglists() {
CodeStream c(stream("def foo(a (b)\n c d)\n d e\nnewdef"));
list<Token> tokens = nextExpr(c);
list<Token>::iterator p = tokens.begin();
Expand All @@ -514,61 +508,6 @@ void test_parenthesize_passes_through_arglists() {
check(c.fd.eof());
}

void test_parenthesize_passes_through_when_indented_by_one_space() {
CodeStream c(stream(" (a b c\n d e\n f g)"));
list<Token> tokens = nextExpr(c);
list<Token>::iterator p = tokens.begin();
checkEq(*p, "("); ++p;
checkEq(*p, "a"); ++p;
checkEq(*p, "b"); ++p;
checkEq(*p, "c"); ++p;
checkEq(*p, "d"); ++p;
checkEq(*p, "e"); ++p;
checkEq(*p, "f"); ++p;
checkEq(*p, "g"); ++p;
checkEq(*p, ")"); ++p;
check(p == tokens.end());
check(c.fd.eof());
}

void test_parenthesize_passes_through_when_indented_by_one_space2() {
CodeStream c(stream(" '(a b c\n d e\n f g)"));
list<Token> tokens = nextExpr(c);
list<Token>::iterator p = tokens.begin();
checkEq(*p, "'"); ++p;
checkEq(*p, "("); ++p;
checkEq(*p, "a"); ++p;
checkEq(*p, "b"); ++p;
checkEq(*p, "c"); ++p;
checkEq(*p, "d"); ++p;
checkEq(*p, "e"); ++p;
checkEq(*p, "f"); ++p;
checkEq(*p, "g"); ++p;
checkEq(*p, ")"); ++p;
check(p == tokens.end());
check(c.fd.eof());
}

void test_parenthesize_passes_through_when_indented_by_one_space3() {
CodeStream c(stream("a\n (a b c\n d e)"));
list<Token> tokens = nextExpr(c);
list<Token>::iterator p = tokens.begin();
checkEq(*p, "a"); ++p;
check(p == tokens.end());

tokens = nextExpr(c);
p = tokens.begin();
checkEq(*p, "("); ++p;
checkEq(*p, "a"); ++p;
checkEq(*p, "b"); ++p;
checkEq(*p, "c"); ++p;
checkEq(*p, "d"); ++p;
checkEq(*p, "e"); ++p;
checkEq(*p, ")"); ++p;
check(p == tokens.end());
check(c.fd.eof());
}

void test_parenthesize_passes_through_unbalanced_open_paren() {
CodeStream c(stream("("));
list<Token> tokens = nextExpr(c);
Expand Down
1 change: 1 addition & 0 deletions 003parse.cc
Expand Up @@ -93,5 +93,6 @@ AstNode nextAstNode(CodeStream& c) {
parseNext(tokens.begin(), tokens.end(), result);
if (result.size() > 1) RAISE << "parse error\n" << DIE;
if (result.empty()) return AstNode::of(Token::of("nil"));
cout << result.front() << endl;
return result.front();
}
2 changes: 2 additions & 0 deletions 029loader.cc
@@ -1,11 +1,13 @@
#include<dirent.h>

void loadFile(const char* filename) {
cout << "=== " << filename << endl;
ifstream f(filename);
CodeStream c(f);
bool old_interactive = interactive; interactive = false;
while (!c.fd.eof()) {
Cell* cell = read(c);
cout << "..... " << cell << endl;
rmref(eval(cell));
rmref(cell);
}
Expand Down
4 changes: 2 additions & 2 deletions 030.wart
Expand Up @@ -14,8 +14,8 @@ mac! do body
def! prn args
(if args
(do
pr car.args
prn @cdr.args
(pr car.args)
(prn @cdr.args)
car.args)
(pr "
"))
Expand Down
10 changes: 5 additions & 5 deletions 033check.wart
Expand Up @@ -9,9 +9,9 @@ let $if if
mac or args
if args
`(let $x ,car.args
if $x
(if $x
$x
(or ,@cdr.args))
(or ,@cdr.args)))

mac and args
if !args
Expand Down Expand Up @@ -53,9 +53,9 @@ def only(f)

mac check(x test else)
`(let $x ,x
if (,test $x)
$x
,else)
(if (,test $x)
$x
,else))

def maybe(f a b/to)
if a
Expand Down
4 changes: 2 additions & 2 deletions 034type.wart
Expand Up @@ -30,8 +30,8 @@ def coerce(x 'dest-type)

mac defcoerce(src dest f)
`(do
if (~table_get coercions* ',dest)
(table_set coercions* ',dest (table))
(if (~table_get coercions* ',dest)
(table_set coercions* ',dest (table)))
(table_set (table_get coercions* ',dest) ',src ,f))

defcoerce nil list
Expand Down
10 changes: 5 additions & 5 deletions 035generic.wart
Expand Up @@ -11,14 +11,14 @@ let $mac mac
`(,$mac ,name ,params ,@body)
`(let $super ,name
(mac! ,name ,params
if ,cadr.body
(if ,cadr.body
(do ,@cddr.body)
,(construct-macro-call `($super ,@params)))) ; call super with params
,(construct-macro-call `($super ,@params))))) ; call super with params

mac def(name params . body) :case (iso :case car.body)
`(let $old ,name
(def! ,name $params
let ,params $params
if ,cadr.body
(let ,params $params
(if ,cadr.body
(do ,@body)
($old @$params)))
($old @$params)))))
24 changes: 12 additions & 12 deletions 036control.wart
Expand Up @@ -24,11 +24,11 @@ mac iflet(var expr . branches)
if !branches
expr
`(let $tmp ,expr
if $tmp
(if $tmp
(let ,var $tmp
,car.branches)
,(if cdr.branches
`(iflet ,var ,@cdr.branches)))
`(iflet ,var ,@cdr.branches))))

mac aif(expr . branches)
`(iflet it ,expr ,@branches)
Expand Down Expand Up @@ -56,33 +56,33 @@ mac aand args
mac while(test . body)
`(when ,test
,@body
while ,test
,@body)
(while ,test
,@body))

mac whilet(var test . body)
`(let ,var nil
while (= ,var ,test)
,@body)
(while (= ,var ,test)
,@body))

mac awhile(test . body)
`(whilet it ,test
,@body)

mac for(var start test update . body)
`(let ,var ,start
while ,test
(while ,test
,@body
,update)
,update))

mac each(var expr . body)
`(for $i (as list ,expr) $i (zap cdr $i)
let ,var car.$i
,@body)
(let ,var car.$i
,@body))

mac on(var expr . body)
`(for ($i index) (list ,expr 0) $i (do (zap cdr $i) ++.index)
let ,var car.$i
,@body)
(let ,var car.$i
,@body))

mac forlen(var expr . body)
`(for ,var 0 (< ,var len.,expr) ++.,var
Expand Down
2 changes: 1 addition & 1 deletion 037assign.wart
Expand Up @@ -8,7 +8,7 @@ mac =(lhs rhs) :case list?.lhs
mac defset(type params . body)
`(defcoerce ,type function=
(fn ',params
eval (do ,@body) caller-scope))
(eval (do ,@body) caller-scope)))



Expand Down
2 changes: 1 addition & 1 deletion 039mutate.wart
Expand Up @@ -19,7 +19,7 @@ mac shift places ; multiple-eval to maintain places

mac rotate places
`(let $tmp ,car.places
shift ,@places $tmp)
(shift ,@places $tmp))

mac swap(x y)
`(rotate ,x ,y)
Expand Down
6 changes: 3 additions & 3 deletions 041string.wart
Expand Up @@ -23,9 +23,9 @@ defcoerce string symbol

def str args
(w/outstring
each arg args
if arg
pr.arg)
(each arg args
(if arg
pr.arg)))

defcoerce symbol string
str
Expand Down
6 changes: 3 additions & 3 deletions 042table.wart
Expand Up @@ -13,9 +13,9 @@ let $table table ; ignore later refinements

defcoerce list table
(fn(_)
ret h ($table)
each (k v) _
(= h.k v))
(ret h ($table)
(each (k v) _
(= h.k v))))

def! table args
(as table pair.args)
Expand Down
4 changes: 2 additions & 2 deletions 050patmatch.wart
@@ -1,7 +1,7 @@
mac matching(vars vals . body)
`(if (is-match ',vars ,vals)
let ,strip_underscores.vars ,vals
,@body)
(let ,strip_underscores.vars ,vals
,@body))



Expand Down
8 changes: 4 additions & 4 deletions 060fork.wart
Expand Up @@ -6,9 +6,9 @@ let $fork fork

mac preforking(n . body)
`(do
repeat ,n
(fork ,@body)
repeat :forever
(repeat ,n
(fork ,@body))
(repeat :forever
(wait_for_child)
(prn "restart")
(fork ,@body))
(fork ,@body)))

0 comments on commit 3320d25

Please sign in to comment.