-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start creating, printing and testing s-expressions!
- Loading branch information
1 parent
a7a0412
commit b3a84e1
Showing
3 changed files
with
119 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math/big" | ||
) | ||
|
||
// sexpr is a general-purpose data structure for representing | ||
// S-expressions; for now, it has String only, but it may have | ||
// evaluable or other methods added later. | ||
type sexpr interface { | ||
String() string | ||
} | ||
|
||
// consCell is a cons cell. Use Cons to create one. | ||
type consCell struct { | ||
car sexpr | ||
cdr sexpr | ||
} | ||
|
||
// Nil is the empty list / cons cell. Cons with Nil to create a list | ||
// of one item. | ||
var Nil *consCell = nil | ||
|
||
func (j *consCell) String() string { | ||
ret := "(" | ||
for car := j; car != Nil; car = car.cdr.(*consCell) { | ||
if car.car == Nil { | ||
return ret + ")" | ||
} | ||
ret += car.car.String() | ||
if car.cdr != Nil { | ||
ret += " " | ||
} | ||
} | ||
return ret + ")" | ||
} | ||
|
||
// number is a big.Int, but narrower in its string representation. | ||
type number struct { | ||
big.Int | ||
} | ||
|
||
// String returns the string representation of the number. | ||
func (n number) String() string { | ||
return n.Text(10) | ||
} | ||
|
||
func Num(ob interface{}) number { | ||
var n number | ||
switch s := ob.(type) { | ||
case string: | ||
n.SetString(s, 10) | ||
case int: | ||
n.SetInt64(int64(s)) | ||
default: | ||
panic(fmt.Sprintf("Num: unknown type %T\n", ob)) | ||
} | ||
return n | ||
} | ||
|
||
func Cons(i sexpr, cdr *consCell) *consCell { | ||
return &consCell{i, cdr} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestNumberStrings(T *testing.T) { | ||
var tests = []struct { | ||
input interface{} | ||
output string | ||
}{ | ||
{1, "1"}, | ||
{"1", "1"}, | ||
{666, "666"}, | ||
{"12948712498129877", "12948712498129877"}, | ||
} | ||
for _, test := range tests { | ||
fmt.Printf("Checking that %T-valued %v -> %q\n", | ||
test.input, test.input, test.output) | ||
if Num(test.input).String() != test.output { | ||
T.Errorf("Num(%q).String() != %q", test.input, test.output) | ||
} | ||
} | ||
} | ||
|
||
func TestSexprStrings(T *testing.T) { | ||
var tests = []struct { | ||
input sexpr | ||
want string | ||
}{ | ||
{Nil, "()"}, | ||
{Num(1), "1"}, | ||
{Num("2"), "2"}, | ||
{Cons(Num(1), Cons(Num("2"), Nil)), "(1 2)"}, | ||
{Cons(Num(1), Cons(Num("2"), Cons(Num(3), Nil))), "(1 2 3)"}, | ||
{Cons( | ||
Cons( | ||
Num(3), | ||
Cons( | ||
Num("1309875618907812098"), | ||
Nil)), | ||
Cons(Num(5), Cons(Num("6"), Nil))), "((3 1309875618907812098) 5 6)"}, | ||
} | ||
for _, test := range tests { | ||
fmt.Printf("Checking string representation: %v\n", test.input) | ||
if test.input.String() != test.want { | ||
T.Errorf("%T-valued %v.String() != %s", | ||
test.input, test.input, test.want) | ||
} | ||
} | ||
} |