implement expr.Macro

This commit is contained in:
Brian Picciano 2016-07-24 15:04:06 -06:00
parent 9f53060b0c
commit b53da9531c
2 changed files with 31 additions and 2 deletions

View File

@ -90,6 +90,27 @@ func (id Identifier) Equal(e Actual) bool {
////////////////////////////////////////////////////////////////////////////////
// Macro is an identifier for a macro which can be used to transform
// expressions. The tokens for macros start with a '%', but the Macro identifier
// itself has that stripped off
type Macro string
// String returns the Macro with a '%' prepended to it
func (m Macro) String() string {
return "%" + string(m)
}
// Equal implements the Actual method
func (m Macro) Equal(e Actual) bool {
mm, ok := e.(Macro)
if !ok {
return false
}
return m == mm
}
////////////////////////////////////////////////////////////////////////////////
// Tuple represents a fixed set of expressions which are interacted with as if
// they were a single value
type Tuple []Expr
@ -391,12 +412,15 @@ func parseIdentifier(t lexer.Token) (Expr, error) {
}
e.Actual = Int(n)
} else if t.Val == "true" {
} else if t.Val == "%true" {
e.Actual = Bool(true)
} else if t.Val == "false" {
} else if t.Val == "%false" {
e.Actual = Bool(false)
} else if t.Val[0] == '%' {
e.Actual = Macro(t.Val[1:])
} else {
e.Actual = Identifier(t.Val)
}

View File

@ -43,7 +43,9 @@ func assertParse(t *T, in []lexer.Token, expExpr Expr, expOut []lexer.Token) {
func TestParseSingle(t *T) {
foo := lexer.Token{TokenType: lexer.Identifier, Val: "foo"}
fooM := lexer.Token{TokenType: lexer.Identifier, Val: "%foo"}
fooExpr := Expr{Actual: Identifier("foo")}
fooMExpr := Expr{Actual: Macro("foo")}
toks := []lexer.Token{foo}
assertParse(t, toks, fooExpr, []lexer.Token{})
@ -56,6 +58,9 @@ func TestParseSingle(t *T) {
toks = []lexer.Token{openParen, openParen, foo, closeParen, closeParen, foo}
assertParse(t, toks, fooExpr, []lexer.Token{foo})
toks = []lexer.Token{fooM, foo}
assertParse(t, toks, fooMExpr, []lexer.Token{foo})
}
func TestParseTuple(t *T) {