1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
package sexp
import "bufio"
// Cons is an S-Expression cons cell
type Cons struct {
first Value
second Value
}
// First returns the first value of the cons
func (l Cons) First() Value {
return l.first
}
// Second returns the second value of the cons
func (l Cons) Second() Value {
return l.second
}
// Value returns the first value inside this Cons
func (l Cons) Value() interface{} {
return l.first
}
// String returns the Cons formatted for printing in an S-Expression
func (l Cons) String() string {
return "(" + l.First().String() + " . " + l.Second().String() + ")"
}
// List creates a chain of Cons cells ended with a nil
func List(values ...Value) Value {
var result Value = snil
l := len(values)
for i := l - 1; i >= 0; i-- {
result = Cons{values[i], result}
}
return result
}
// ReadListStart will expect the start character for an S-Expression list and return false if not encountered
func ReadListStart(r *bufio.Reader) bool {
return expect(r, '(')
}
// ReadListEnd will expect the end character for an S-Expression list and return false if not encountered
func ReadListEnd(r *bufio.Reader) bool {
return expect(r, ')')
}
// ReadList will read a list and return it
func ReadList(r *bufio.Reader) Value {
ReadWhitespace(r)
if !ReadListStart(r) {
return nil
}
result := ReadListItem(r)
if !ReadListEnd(r) {
return nil
}
return result
}
// ReadListItem recursively read a list item and the next potential item
func ReadListItem(r *bufio.Reader) Value {
ReadWhitespace(r)
val, end := ReadValue(r)
if end {
return Snil{}
}
return Cons{val, ReadListItem(r)}
}
|