hey look, variables

master
Jordan Orelli 10 years ago
parent 89b5bbcb6f
commit 239b63b3d6

@ -32,6 +32,5 @@ other_hash: {
item_one: "this is item one"
# we should be able to reference variables defined earlier
# (this doesn't work yet)
# item_two: item_one
# we may reference an item that was defined earlier
item_two: item_one

@ -21,6 +21,7 @@ const (
n_number
n_list
n_object
n_variable
)
var indent = " "
@ -410,3 +411,32 @@ func (o *objectNode) eval(ctx map[string]interface{}) (interface{}, error) {
}
return out, nil
}
type variableNode string
func (v *variableNode) Type() nodeType {
return n_variable
}
func (v *variableNode) parse(p *parser) error {
t := p.next()
if t.t != t_name {
return fmt.Errorf("unexpected %s token when parsing variable", t.t)
}
*v = variableNode(t.s)
return nil
}
func (v *variableNode) pretty(w io.Writer, prefix string) error {
fmt.Fprintf(w, "%svariable:\n", prefix)
fmt.Fprintf(w, "%s%s\n", prefix+indent, string(*v))
return nil
}
func (v *variableNode) eval(ctx map[string]interface{}) (interface{}, error) {
value, ok := ctx[string(*v)]
if !ok {
return nil, fmt.Errorf("undefined variable: %s", *v)
}
return value, nil
}

@ -106,6 +106,12 @@ func (p *parser) parseValue() (node, error) {
return nil, err
}
return n, nil
case t_name:
n := new(variableNode)
if err := n.parse(p); err != nil {
return nil, err
}
return n, nil
default:
return nil, fmt.Errorf("parse error: unexpected %v token while looking for value", t.t)
}

@ -0,0 +1,2 @@
an_item: "this is the value of the item"
a_variable: an_item

@ -0,0 +1,13 @@
root:
assign:
name:
an_item
value:
string:
this is the value of the item
assign:
name:
a_variable
value:
variable:
an_item
Loading…
Cancel
Save