Add support for simple expressions

Add support for the + operator to concatentate strings and lists.

Change-Id: I6b6d74122e8f9e509d411c82250f440b09dc5765
This commit is contained in:
Colin Cross 2015-01-02 15:47:54 -08:00
parent c0dbc553a2
commit 82b7d51dfd

View file

@ -137,7 +137,7 @@ func (p *parser) parseAssignment(name string,
if !p.accept('=') {
return
}
value := p.parseValue()
value := p.parseExpression()
assignment.Name = name
assignment.Value = value
@ -191,7 +191,7 @@ func (p *parser) parseProperty() (property *Property) {
if !p.accept(scanner.Ident, ':') {
return
}
value := p.parseValue()
value := p.parseExpression()
property.Name = name
property.Value = value
@ -200,6 +200,46 @@ func (p *parser) parseProperty() (property *Property) {
return
}
func (p *parser) parseExpression() (value Value) {
value = p.parseValue()
switch p.tok {
case '+':
return p.parseOperator(value)
default:
return value
}
}
func (p *parser) parseOperator(value1 Value) (value Value) {
operator := p.tok
p.accept(operator)
value2 := p.parseExpression()
if value1.Type != value2.Type {
p.errorf("mismatched type in operator %c: %s != %s", operator,
value1.Type, value2.Type)
return
}
switch operator {
case '+':
switch value1.Type {
case String:
value1.StringValue += value2.StringValue
case List:
value1.ListValue = append(value1.ListValue, value2.ListValue...)
default:
p.errorf("operator %c not supported on type %s", operator, value1.Type)
return
}
default:
panic("unknown operator " + string(operator))
}
return value1
}
func (p *parser) parseValue() (value Value) {
switch p.tok {
case scanner.Ident:
@ -260,7 +300,7 @@ func (p *parser) parseListValue() (value Value) {
var elements []Value
for p.tok != ']' {
element := p.parseValue()
element := p.parseExpression()
if element.Type != String {
p.errorf("Expected string in list, found %s", element.String())
return