From b53da9531cf3021d8585ef1bc1ab5434cf780543 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sun, 24 Jul 2016 15:04:06 -0600 Subject: [PATCH] implement expr.Macro --- expr/expr.go | 28 ++++++++++++++++++++++++++-- expr/expr_test.go | 5 +++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/expr/expr.go b/expr/expr.go index 914bbb6..bddb928 100644 --- a/expr/expr.go +++ b/expr/expr.go @@ -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) } diff --git a/expr/expr_test.go b/expr/expr_test.go index 9019702..f377e9d 100644 --- a/expr/expr_test.go +++ b/expr/expr_test.go @@ -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) {