Skip to content

Commit

Permalink
unary negative works
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Feb 7, 2016
1 parent 0ab0eeb commit dc4cc84
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 83 deletions.
8 changes: 8 additions & 0 deletions eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ func TestEval(t *testing.T) {
ast.TypeString,
},

{
"foo ${-29}",
nil,
false,
"foo -29",
ast.TypeString,
},

{
"foo ${42+1}",
nil,
Expand Down
19 changes: 19 additions & 0 deletions lang.y
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,25 @@ expr:
Posx: $1.Pos,
}
}
| ARITH_OP expr
{
// This is REALLY jank. We assume that a singular ARITH_OP
// means 0 ARITH_OP expr, which... is weird. We don't want to
// support *, /, etc., only -. We should fix this later with a pure
// Go scanner/parser.
if $1.Value.(ast.ArithmeticOp) != ast.ArithmeticOpSub {
panic("Unary - is only allowed")
}

$$ = &ast.Arithmetic{
Op: $1.Value.(ast.ArithmeticOp),
Exprs: []ast.Node{
&ast.LiteralNode{Value: 0, Typex: ast.TypeInt},
$2,
},
Posx: $2.Pos(),
}
}
| expr ARITH_OP expr
{
$$ = &ast.Arithmetic{
Expand Down
7 changes: 7 additions & 0 deletions lex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ func TestLex(t *testing.T) {
PROGRAM_BRACKET_RIGHT, lexEOF},
},

{
"${bar(-42)}",
[]int{PROGRAM_BRACKET_LEFT,
IDENTIFIER, PAREN_LEFT, ARITH_OP, INTEGER, PAREN_RIGHT,
PROGRAM_BRACKET_RIGHT, lexEOF},
},

{
"${bar(42+1)}",
[]int{PROGRAM_BRACKET_LEFT,
Expand Down
82 changes: 52 additions & 30 deletions y.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const parserEofCode = 1
const parserErrCode = 2
const parserMaxDepth = 200

//line lang.y:165
//line lang.y:184

//line yacctab:1
var parserExca = [...]int{
Expand All @@ -62,51 +62,52 @@ var parserExca = [...]int{
-2, 0,
}

const parserNprod = 19
const parserNprod = 20
const parserPrivate = 57344

var parserTokenNames []string
var parserStates []string

const parserLast = 30
const parserLast = 34

var parserAct = [...]int{

9, 20, 16, 16, 7, 7, 3, 18, 10, 8,
1, 17, 14, 12, 13, 6, 6, 19, 8, 22,
15, 23, 24, 11, 2, 25, 16, 21, 4, 5,
9, 7, 3, 16, 22, 8, 17, 17, 20, 17,
1, 18, 6, 23, 8, 19, 25, 26, 21, 11,
2, 24, 7, 4, 5, 0, 10, 27, 0, 14,
15, 12, 13, 6,
}
var parserPact = [...]int{

1, -1000, 1, -1000, -1000, -1000, -1000, 0, -1000, 15,
0, 1, -1000, -1000, -1, -1000, 0, -8, 0, -1000,
-1000, 12, -9, -1000, 0, -9,
-3, -1000, -3, -1000, -1000, -1000, -1000, 18, -1000, -2,
18, -3, -1000, -1000, 18, 0, -1000, 18, -5, -1000,
18, -1000, -1000, 7, -4, -1000, 18, -4,
}
var parserPgo = [...]int{

0, 0, 29, 28, 23, 6, 27, 10,
0, 0, 24, 23, 19, 2, 13, 10,
}
var parserR1 = [...]int{

0, 7, 7, 4, 4, 5, 5, 2, 1, 1,
1, 1, 1, 1, 1, 6, 6, 6, 3,
1, 1, 1, 1, 1, 1, 6, 6, 6, 3,
}
var parserR2 = [...]int{

0, 0, 1, 1, 2, 1, 1, 3, 3, 1,
1, 1, 3, 1, 4, 0, 3, 1, 1,
1, 1, 2, 3, 1, 4, 0, 3, 1, 1,
}
var parserChk = [...]int{

-1000, -7, -4, -5, -3, -2, 15, 4, -5, -1,
8, -4, 13, 14, 12, 5, 11, -1, 8, -1,
9, -6, -1, 9, 10, -1,
8, -4, 13, 14, 11, 12, 5, 11, -1, -1,
8, -1, 9, -6, -1, 9, 10, -1,
}
var parserDef = [...]int{

1, -2, 2, 3, 5, 6, 18, 0, 4, 0,
0, 9, 10, 11, 13, 7, 0, 0, 15, 12,
8, 0, 17, 14, 0, 16,
1, -2, 2, 3, 5, 6, 19, 0, 4, 0,
0, 9, 10, 11, 0, 14, 7, 0, 0, 12,
16, 13, 8, 0, 18, 15, 0, 17,
}
var parserTok1 = [...]int{

Expand Down Expand Up @@ -567,48 +568,69 @@ parserdefault:
}
}
case 12:
parserDollar = parserS[parserpt-3 : parserpt+1]
parserDollar = parserS[parserpt-2 : parserpt+1]
//line lang.y:126
{
// This is REALLY jank. We assume that a singular ARITH_OP
// means 0 ARITH_OP expr, which... is weird. We don't want to
// support *, /, etc., only -. We should fix this later with a pure
// Go scanner/parser.
if parserDollar[1].token.Value.(ast.ArithmeticOp) != ast.ArithmeticOpSub {
panic("Unary - is only allowed")
}

parserVAL.node = &ast.Arithmetic{
Op: parserDollar[1].token.Value.(ast.ArithmeticOp),
Exprs: []ast.Node{
&ast.LiteralNode{Value: 0, Typex: ast.TypeInt},
parserDollar[2].node,
},
Posx: parserDollar[2].node.Pos(),
}
}
case 13:
parserDollar = parserS[parserpt-3 : parserpt+1]
//line lang.y:145
{
parserVAL.node = &ast.Arithmetic{
Op: parserDollar[2].token.Value.(ast.ArithmeticOp),
Exprs: []ast.Node{parserDollar[1].node, parserDollar[3].node},
Posx: parserDollar[1].node.Pos(),
}
}
case 13:
case 14:
parserDollar = parserS[parserpt-1 : parserpt+1]
//line lang.y:134
//line lang.y:153
{
parserVAL.node = &ast.VariableAccess{Name: parserDollar[1].token.Value.(string), Posx: parserDollar[1].token.Pos}
}
case 14:
case 15:
parserDollar = parserS[parserpt-4 : parserpt+1]
//line lang.y:138
//line lang.y:157
{
parserVAL.node = &ast.Call{Func: parserDollar[1].token.Value.(string), Args: parserDollar[3].nodeList, Posx: parserDollar[1].token.Pos}
}
case 15:
case 16:
parserDollar = parserS[parserpt-0 : parserpt+1]
//line lang.y:143
//line lang.y:162
{
parserVAL.nodeList = nil
}
case 16:
case 17:
parserDollar = parserS[parserpt-3 : parserpt+1]
//line lang.y:147
//line lang.y:166
{
parserVAL.nodeList = append(parserDollar[1].nodeList, parserDollar[3].node)
}
case 17:
case 18:
parserDollar = parserS[parserpt-1 : parserpt+1]
//line lang.y:151
//line lang.y:170
{
parserVAL.nodeList = append(parserVAL.nodeList, parserDollar[1].node)
}
case 18:
case 19:
parserDollar = parserS[parserpt-1 : parserpt+1]
//line lang.y:157
//line lang.y:176
{
parserVAL.node = &ast.LiteralNode{
Value: parserDollar[1].token.Value.(string),
Expand Down
Loading

6 comments on commit dc4cc84

@VladRassokhin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mitchellh It seems unary expressions introduced in hashicorp/terraform#3621 was lost during migration to separate repository.
Also originally both '+' and '-' was supported.
Maybe there was another changes between exporting HIL as separate repo (5 Dec 2015) and using this repo in Terraform (January 2016).

@mitchellh
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very very few changes. And we kept all existing tests so we shouldn't have any regressions. Not a single test was removed (many were added). If you found a difference, please report it!

@VladRassokhin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, tests was also lost :)
Will create issue.

@mitchellh
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests are all there (including unary) tests in the _test.go files. I see them!

"foo ${-29}",

@VladRassokhin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VladRassokhin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #4

Please sign in to comment.