Commit 3df8e0a1 authored by Rob Pike's avatar Rob Pike

clean up ken/rob2.go to eliminate a few workarounds

SVN=123442
parent 74aa77fb
...@@ -77,7 +77,7 @@ Hello World! ...@@ -77,7 +77,7 @@ Hello World!
=========== ken/rob1.go =========== ken/rob1.go
=========== ken/rob2.go =========== ken/rob2.go
(hello hello (hello 12 34)) (defn foo (add 12 34))
=========== ken/robfor.go =========== ken/robfor.go
......
...@@ -73,6 +73,7 @@ var lineno int32 = 1; ...@@ -73,6 +73,7 @@ var lineno int32 = 1;
var input string; var input string;
var inputindex int = 0; var inputindex int = 0;
var tokenbuf [100]byte; var tokenbuf [100]byte;
var tokenlen int = 0;
const EOF int = -1; const EOF int = -1;
...@@ -135,7 +136,7 @@ func Get() int ...@@ -135,7 +136,7 @@ func Get() int
peekc = -1; peekc = -1;
} else { } else {
c = convert(int, input[inputindex]); c = convert(int, input[inputindex]);
inputindex = inputindex + 1; // BUG should be incr one expr inputindex++
if c == '\n' { if c == '\n' {
lineno = lineno + 1; lineno = lineno + 1;
} }
...@@ -185,6 +186,7 @@ func NextToken() ...@@ -185,6 +186,7 @@ func NextToken()
if i >= 100 - 1 { // sizeof tokenbuf - 1 if i >= 100 - 1 { // sizeof tokenbuf - 1
panic "atom too long\n"; panic "atom too long\n";
} }
tokenlen = i;
tokenbuf[i] = nilchar; tokenbuf[i] = nilchar;
if '0' <= tokenbuf[0] && tokenbuf[0] <= '9' { if '0' <= tokenbuf[0] && tokenbuf[0] <= '9' {
token = '0'; token = '0';
...@@ -217,10 +219,7 @@ func ParseList() *Slist ...@@ -217,10 +219,7 @@ func ParseList() *Slist
retval = slist; retval = slist;
for ;; { for ;; {
slist.list.car = Parse(); slist.list.car = Parse();
if token == ')' { // empty cdr if token == ')' || token == EOF { // empty cdr
break;
}
if token == EOF { // empty cdr BUG SHOULD USE ||
break; break;
} }
slist.list.cdr = new(Slist); slist.list.cdr = new(Slist);
...@@ -239,7 +238,7 @@ func atom(i int) *Slist // BUG: uses tokenbuf; should take argument ...@@ -239,7 +238,7 @@ func atom(i int) *Slist // BUG: uses tokenbuf; should take argument
slist.atom.integer = i; slist.atom.integer = i;
slist.isstring = false; slist.isstring = false;
} else { } else {
slist.atom.str = "hello"; slist.atom.str = string(tokenbuf)[0:tokenlen];
slist.isstring = true; slist.isstring = true;
} }
slist.isatom = true; slist.isatom = true;
...@@ -249,7 +248,7 @@ func atom(i int) *Slist // BUG: uses tokenbuf; should take argument ...@@ -249,7 +248,7 @@ func atom(i int) *Slist // BUG: uses tokenbuf; should take argument
func atoi() int // BUG: uses tokenbuf; should take argument func atoi() int // BUG: uses tokenbuf; should take argument
{ {
var v int = 0; var v int = 0;
for i := 0; '0' <= tokenbuf[i] && tokenbuf[i] <= '9'; i = i + 1 { for i := 0; i < tokenlen && '0' <= tokenbuf[i] && tokenbuf[i] <= '9'; i = i + 1 {
v = 10 * v + convert(int, tokenbuf[i] - '0'); v = 10 * v + convert(int, tokenbuf[i] - '0');
} }
return v; return v;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment