ternanry node

This commit is contained in:
z7zmey 2017-12-18 20:17:32 +02:00
parent 0751859395
commit ef39b9111e
3 changed files with 47 additions and 4 deletions

43
node/expr/ternary.go Normal file
View File

@ -0,0 +1,43 @@
package expr
import (
"fmt"
"io"
"github.com/z7zmey/php-parser/node"
)
type Ternary struct {
node.SimpleNode
condition node.Node
ifTrue node.Node
ifFalse node.Node
}
func NewTernary(condition node.Node, ifTrue node.Node, ifFalse node.Node) node.Node {
return Ternary{
node.SimpleNode{Name: "Ternary", Attributes: make(map[string]string)},
condition,
ifTrue,
ifFalse,
}
}
func (n Ternary) Print(out io.Writer, indent string) {
fmt.Fprintf(out, "\n%v%v [- -]", indent, n.Name)
if n.condition != nil {
fmt.Fprintf(out, "\n%vcondition:", indent+" ")
n.condition.Print(out, indent+" ")
}
if n.ifTrue != nil {
fmt.Fprintf(out, "\n%vifTrue:", indent+" ")
n.ifTrue.Print(out, indent+" ")
}
if n.ifFalse != nil {
fmt.Fprintf(out, "\n%vifFalse:", indent+" ")
n.ifFalse.Print(out, indent+" ")
}
}

View File

@ -4125,13 +4125,13 @@ yydefault:
yyDollar = yyS[yypt-5 : yypt+1]
//line parser/parser.y:878
{
yyVAL.node = node.NewSimpleNode("Ternary").Append(yyDollar[1].node).Append(yyDollar[3].node).Append(yyDollar[5].node)
yyVAL.node = expr.NewTernary(yyDollar[1].node, yyDollar[3].node, yyDollar[5].node)
}
case 348:
yyDollar = yyS[yypt-4 : yypt+1]
//line parser/parser.y:879
{
yyVAL.node = node.NewSimpleNode("Ternary").Append(yyDollar[1].node).Append(yyDollar[4].node)
yyVAL.node = expr.NewTernary(yyDollar[1].node, nil, yyDollar[4].node)
}
case 349:
yyDollar = yyS[yypt-3 : yypt+1]

View File

@ -875,8 +875,8 @@ expr_without_variable:
| expr T_INSTANCEOF class_name_reference { $$ = expr.NewInstanceOf($1, $3) }
| '(' expr ')' { $$ = $2; }
| new_expr { $$ = $1; }
| expr '?' expr ':' expr { $$ = node.NewSimpleNode("Ternary").Append($1).Append($3).Append($5); }
| expr '?' ':' expr { $$ = node.NewSimpleNode("Ternary").Append($1).Append($4); }
| expr '?' expr ':' expr { $$ = expr.NewTernary($1, $3, $5) }
| expr '?' ':' expr { $$ = expr.NewTernary($1, nil, $4) }
| expr T_COALESCE expr { $$ = binary_op.NewCoalesce($1, $3) }
| internal_functions_in_yacc { $$ = $1}
| T_INT_CAST expr { $$ = cast.NewCastInt($2) }