2008-06-06 15:27:34 -06:00
|
|
|
// $G $D/$F.go && $L $F.$A && ./$A.out
|
|
|
|
|
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
const nilchar = 0;
|
|
|
|
|
2009-01-16 17:12:14 -07:00
|
|
|
export type (
|
2008-10-07 13:59:54 -06:00
|
|
|
Atom struct;
|
|
|
|
List struct;
|
|
|
|
Slist struct;
|
|
|
|
)
|
|
|
|
|
2009-01-16 17:12:14 -07:00
|
|
|
export type Atom struct {
|
2008-06-06 15:27:34 -06:00
|
|
|
str string;
|
|
|
|
integer int;
|
|
|
|
next *Slist; /* in hash bucket */
|
|
|
|
}
|
|
|
|
|
2009-01-16 17:12:14 -07:00
|
|
|
export type List struct {
|
2008-06-06 15:27:34 -06:00
|
|
|
car *Slist;
|
|
|
|
cdr*Slist;
|
|
|
|
}
|
|
|
|
|
2009-01-16 17:12:14 -07:00
|
|
|
export type Slist struct {
|
2008-06-06 15:27:34 -06:00
|
|
|
isatom bool;
|
|
|
|
isstring bool;
|
|
|
|
//union {
|
|
|
|
atom Atom;
|
|
|
|
list List;
|
|
|
|
//} u;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Slist) Print();
|
|
|
|
|
|
|
|
func (this *Slist) Car() *Slist {
|
|
|
|
return this.list.car;
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Slist) Cdr() *Slist {
|
|
|
|
return this.list.cdr;
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Slist) String() string {
|
|
|
|
return this.atom.str;
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Slist) Integer() int {
|
|
|
|
return this.atom.integer;
|
|
|
|
}
|
|
|
|
|
|
|
|
func (slist *Slist) Free() {
|
|
|
|
if slist == nil {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if slist.isatom {
|
|
|
|
// free(slist.String());
|
|
|
|
} else {
|
|
|
|
slist.Car().Free();
|
|
|
|
slist.Cdr().Free();
|
|
|
|
}
|
|
|
|
// free(slist);
|
|
|
|
}
|
|
|
|
|
2009-01-16 17:12:14 -07:00
|
|
|
export func OpenFile();
|
|
|
|
export func Parse() *Slist;
|
2008-06-06 15:27:34 -06:00
|
|
|
|
|
|
|
//Slist* atom(byte *s, int i);
|
|
|
|
|
|
|
|
var token int;
|
|
|
|
var peekc int = -1;
|
|
|
|
var lineno int32 = 1;
|
|
|
|
|
|
|
|
var input string;
|
|
|
|
var inputindex int = 0;
|
|
|
|
var tokenbuf [100]byte;
|
2008-06-18 15:47:46 -06:00
|
|
|
var tokenlen int = 0;
|
2008-06-06 15:27:34 -06:00
|
|
|
|
2009-01-16 17:12:14 -07:00
|
|
|
export const EOF int = -1;
|
2008-06-06 15:27:34 -06:00
|
|
|
|
2008-06-27 15:15:06 -06:00
|
|
|
func main()
|
2008-06-06 15:27:34 -06:00
|
|
|
{
|
|
|
|
var list *Slist;
|
|
|
|
|
|
|
|
OpenFile();
|
|
|
|
for ;; {
|
|
|
|
list = Parse();
|
|
|
|
if list == nil {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
list.Print();
|
|
|
|
list.Free();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (slist *Slist) PrintOne(doparen bool)
|
|
|
|
{
|
|
|
|
if slist == nil {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if slist.isatom {
|
|
|
|
if slist.isstring {
|
2008-08-11 23:07:49 -06:00
|
|
|
print(slist.String());
|
2008-06-06 15:27:34 -06:00
|
|
|
} else {
|
2008-08-11 23:07:49 -06:00
|
|
|
print(slist.Integer());
|
2008-06-06 15:27:34 -06:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if doparen {
|
2008-08-11 23:07:49 -06:00
|
|
|
print("(" );
|
2008-06-06 15:27:34 -06:00
|
|
|
}
|
|
|
|
slist.Car().PrintOne(true);
|
|
|
|
if slist.Cdr() != nil {
|
2008-08-11 23:07:49 -06:00
|
|
|
print(" ");
|
2008-06-06 15:27:34 -06:00
|
|
|
slist.Cdr().PrintOne(false);
|
|
|
|
}
|
|
|
|
if doparen {
|
2008-08-11 23:07:49 -06:00
|
|
|
print(")");
|
2008-06-06 15:27:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (slist *Slist) Print()
|
|
|
|
{
|
|
|
|
slist.PrintOne(true);
|
2008-08-11 23:07:49 -06:00
|
|
|
print("\n");
|
2008-06-06 15:27:34 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 17:12:14 -07:00
|
|
|
export func Get() int
|
2008-06-06 15:27:34 -06:00
|
|
|
{
|
|
|
|
var c int;
|
|
|
|
|
|
|
|
if peekc >= 0 {
|
|
|
|
c = peekc;
|
|
|
|
peekc = -1;
|
|
|
|
} else {
|
2008-12-20 14:38:29 -07:00
|
|
|
c = int(input[inputindex]);
|
2008-10-07 13:31:31 -06:00
|
|
|
inputindex++;
|
2008-06-06 15:27:34 -06:00
|
|
|
if c == '\n' {
|
|
|
|
lineno = lineno + 1;
|
|
|
|
}
|
|
|
|
if c == nilchar {
|
|
|
|
inputindex = inputindex - 1;
|
|
|
|
c = EOF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2009-01-16 17:12:14 -07:00
|
|
|
export func WhiteSpace(c int) bool
|
2008-06-06 15:27:34 -06:00
|
|
|
{
|
|
|
|
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
|
|
|
|
}
|
|
|
|
|
2009-01-16 17:12:14 -07:00
|
|
|
export func NextToken()
|
2008-06-06 15:27:34 -06:00
|
|
|
{
|
|
|
|
var i, c int;
|
|
|
|
var backslash bool;
|
|
|
|
|
|
|
|
tokenbuf[0] = nilchar; // clear previous token
|
|
|
|
c = Get();
|
|
|
|
for WhiteSpace(c) {
|
|
|
|
c = Get();
|
|
|
|
}
|
|
|
|
switch c {
|
2008-08-12 15:04:03 -06:00
|
|
|
case EOF:
|
|
|
|
token = EOF;
|
|
|
|
case '(', ')':
|
|
|
|
token = c;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
for i = 0; i < 100 - 1; { // sizeof tokenbuf - 1
|
2008-12-20 14:38:29 -07:00
|
|
|
tokenbuf[i] = byte(c);
|
2008-08-12 15:04:03 -06:00
|
|
|
i = i + 1;
|
|
|
|
c = Get();
|
|
|
|
if c == EOF {
|
|
|
|
break;
|
2008-06-06 15:27:34 -06:00
|
|
|
}
|
2008-08-12 15:04:03 -06:00
|
|
|
if WhiteSpace(c) || c == ')' {
|
|
|
|
peekc = c;
|
|
|
|
break;
|
2008-06-06 15:27:34 -06:00
|
|
|
}
|
2008-08-12 15:04:03 -06:00
|
|
|
}
|
|
|
|
if i >= 100 - 1 { // sizeof tokenbuf - 1
|
|
|
|
panic("atom too long\n");
|
|
|
|
}
|
|
|
|
tokenlen = i;
|
|
|
|
tokenbuf[i] = nilchar;
|
|
|
|
if '0' <= tokenbuf[0] && tokenbuf[0] <= '9' {
|
|
|
|
token = '0';
|
|
|
|
} else {
|
|
|
|
token = 'A';
|
|
|
|
}
|
2008-06-06 15:27:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-16 17:12:14 -07:00
|
|
|
export func Expect(c int)
|
2008-06-06 15:27:34 -06:00
|
|
|
{
|
|
|
|
if token != c {
|
2008-08-11 23:07:49 -06:00
|
|
|
print("parse error: expected ", c, "\n");
|
|
|
|
panic("parse");
|
2008-06-06 15:27:34 -06:00
|
|
|
}
|
|
|
|
NextToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse a non-parenthesized list up to a closing paren or EOF
|
2009-01-16 17:12:14 -07:00
|
|
|
export func ParseList() *Slist
|
2008-06-06 15:27:34 -06:00
|
|
|
{
|
|
|
|
var slist, retval *Slist;
|
|
|
|
|
2009-01-06 16:19:02 -07:00
|
|
|
slist = new(Slist);
|
2008-06-06 15:27:34 -06:00
|
|
|
slist.list.car = nil;
|
|
|
|
slist.list.cdr = nil;
|
|
|
|
slist.isatom = false;
|
|
|
|
slist.isstring = false;
|
|
|
|
|
|
|
|
retval = slist;
|
|
|
|
for ;; {
|
|
|
|
slist.list.car = Parse();
|
2008-06-18 15:47:46 -06:00
|
|
|
if token == ')' || token == EOF { // empty cdr
|
2008-06-06 15:27:34 -06:00
|
|
|
break;
|
|
|
|
}
|
2009-01-06 16:19:02 -07:00
|
|
|
slist.list.cdr = new(Slist);
|
2008-06-06 15:27:34 -06:00
|
|
|
slist = slist.list.cdr;
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
func atom(i int) *Slist // BUG: uses tokenbuf; should take argument
|
|
|
|
{
|
|
|
|
var h, length int;
|
|
|
|
var slist, tail *Slist;
|
|
|
|
|
2009-01-06 16:19:02 -07:00
|
|
|
slist = new(Slist);
|
2008-06-06 15:27:34 -06:00
|
|
|
if token == '0' {
|
|
|
|
slist.atom.integer = i;
|
|
|
|
slist.isstring = false;
|
|
|
|
} else {
|
2008-06-18 15:47:46 -06:00
|
|
|
slist.atom.str = string(tokenbuf)[0:tokenlen];
|
2008-06-06 15:27:34 -06:00
|
|
|
slist.isstring = true;
|
|
|
|
}
|
|
|
|
slist.isatom = true;
|
|
|
|
return slist;
|
|
|
|
}
|
|
|
|
|
|
|
|
func atoi() int // BUG: uses tokenbuf; should take argument
|
|
|
|
{
|
|
|
|
var v int = 0;
|
2008-06-18 15:47:46 -06:00
|
|
|
for i := 0; i < tokenlen && '0' <= tokenbuf[i] && tokenbuf[i] <= '9'; i = i + 1 {
|
2008-12-20 14:38:29 -07:00
|
|
|
v = 10 * v + int(tokenbuf[i] - '0');
|
2008-06-06 15:27:34 -06:00
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2009-01-16 17:12:14 -07:00
|
|
|
export func Parse() *Slist
|
2008-06-06 15:27:34 -06:00
|
|
|
{
|
|
|
|
var slist *Slist;
|
|
|
|
|
|
|
|
if token == EOF || token == ')' {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
if token == '(' {
|
|
|
|
NextToken();
|
|
|
|
slist = ParseList();
|
|
|
|
Expect(')');
|
|
|
|
return slist;
|
|
|
|
} else {
|
|
|
|
// Atom
|
|
|
|
switch token {
|
2008-08-12 15:04:03 -06:00
|
|
|
case EOF:
|
|
|
|
return nil;
|
|
|
|
case '0':
|
|
|
|
slist = atom(atoi());
|
|
|
|
case '"', 'A':
|
|
|
|
slist = atom(0);
|
|
|
|
default:
|
|
|
|
slist = nil;
|
|
|
|
print("unknown token: ", token, "\n");
|
2008-06-06 15:27:34 -06:00
|
|
|
}
|
|
|
|
NextToken();
|
|
|
|
return slist;
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2009-01-16 17:12:14 -07:00
|
|
|
export func OpenFile()
|
2008-06-06 15:27:34 -06:00
|
|
|
{
|
|
|
|
input = "(defn foo (add 12 34))\n\x00";
|
|
|
|
inputindex = 0;
|
|
|
|
peekc = -1; // BUG
|
|
|
|
NextToken();
|
|
|
|
}
|