list nodes parse now

master
Jordan Orelli 10 years ago
parent 7cd65a4025
commit 88c013b2bc

@ -18,6 +18,7 @@ const (
n_assignment
n_string
n_number
n_list
)
var indent = " "
@ -291,5 +292,55 @@ func (n *numberNode) eval(ctx map[string]interface{}) (interface{}, error) {
}
}
type listNode []node
func (l *listNode) Type() nodeType {
return n_list
}
func (l *listNode) parse(p *parser) error {
if p.peek().t == t_list_end {
p.next()
return nil
}
if n, err := p.parseValue(); err != nil {
return err
} else {
*l = append(*l, n)
}
switch t := p.peek(); t.t {
case t_list_end:
p.next()
return nil
default:
return l.parse(p)
}
}
func (l *listNode) pretty(w io.Writer, prefix string) error {
fmt.Fprintf(w, "%slist{\n", prefix)
for _, n := range *l {
if err := n.pretty(w, prefix+indent); err != nil {
return err
}
}
fmt.Fprintf(w, "%s}\n", prefix)
return nil
}
func (l *listNode) eval(ctx map[string]interface{}) (interface{}, error) {
out := make([]interface{}, 0, len(*l))
for _, n := range *l {
v, err := n.eval(ctx)
if err != nil {
return nil, err
}
out = append(out, v)
}
return out, nil
}
type list []interface{}
type object map[string]interface{}

@ -92,7 +92,13 @@ func (p *parser) parseValue() (node, error) {
return nil, err
}
return n, nil
// case t_list_start:
case t_list_start:
p.next()
n := new(listNode)
if err := n.parse(p); err != nil {
return nil, err
}
return n, nil
// return p.parseList(make(list, 0, 4))
// case t_object_start:
// return p.parseObject(make(object))
@ -102,27 +108,6 @@ func (p *parser) parseValue() (node, error) {
}
}
func (p *parser) parseList(l list) (list, error) {
if p.peek().t == t_list_end {
p.next()
return l, nil
}
if v, err := p.parseValue(); err != nil {
return nil, err
} else {
l = append(l, v)
}
switch t := p.peek(); t.t {
case t_list_end:
p.next()
return l, nil
default:
return p.parseList(l)
}
}
func (p *parser) parseObject(obj object) (object, error) {
if p.peek().t == t_object_end {
p.next()

@ -0,0 +1 @@
items: [1 2 3]

@ -0,0 +1,10 @@
root{
assign{
name: items
value: list{
value: 1
value: 2
value: 3
value: }
}
}
Loading…
Cancel
Save