1 // $G $D/$F.go && $L $F.$A && ./$A.out
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
16 next *Slist /* in hash bucket */
34 func (this *Slist) Car() *Slist {
38 func (this *Slist) Cdr() *Slist {
42 func (this *Slist) String() string {
46 func (this *Slist) Integer() int {
47 return this.atom.integer
50 func (slist *Slist) Free() {
55 // free(slist.String());
63 //Slist* atom(byte *s, int i);
70 var inputindex int = 0
71 var tokenbuf [100]byte
87 if r != "(defn foo (add 12 34))" {
94 func (slist *Slist) PrintOne(doparen bool) string {
103 r = fmt.Sprintf("%v", slist.Integer())
109 r += slist.Car().PrintOne(true)
110 if slist.Cdr() != nil {
112 r += slist.Cdr().PrintOne(false)
121 func (slist *Slist) Print() string {
122 return slist.PrintOne(true)
132 c = int(input[inputindex])
138 inputindex = inputindex - 1
145 func WhiteSpace(c int) bool {
146 return c == ' ' || c == '\t' || c == '\r' || c == '\n'
152 tokenbuf[0] = nilchar // clear previous token
164 for i = 0; i < 100-1; { // sizeof tokenbuf - 1
165 tokenbuf[i] = byte(c)
171 if WhiteSpace(c) || c == ')' {
176 if i >= 100-1 { // sizeof tokenbuf - 1
177 panic("atom too long\n")
180 tokenbuf[i] = nilchar
181 if '0' <= tokenbuf[0] && tokenbuf[0] <= '9' {
191 print("parse error: expected ", c, "\n")
197 // Parse a non-parenthesized list up to a closing paren or EOF
198 func ParseList() *Slist {
199 var slist, retval *Slist
205 slist.isstring = false
209 slist.list.car = Parse()
210 if token == ')' || token == EOF { // empty cdr
213 slist.list.cdr = new(Slist)
214 slist = slist.list.cdr
219 func atom(i int) *Slist { // BUG: uses tokenbuf; should take argument)
224 slist.atom.integer = i
225 slist.isstring = false
227 slist.atom.str = string(tokenbuf[0:tokenlen])
228 slist.isstring = true
234 func atoi() int { // BUG: uses tokenbuf; should take argument)
236 for i := 0; i < tokenlen && '0' <= tokenbuf[i] && tokenbuf[i] <= '9'; i = i + 1 {
237 v = 10*v + int(tokenbuf[i]-'0')
242 func Parse() *Slist {
245 if token == EOF || token == ')' {
264 print("unknown token: ", token, "\n")
273 input = "(defn foo (add 12 34))\n\x00"