-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathif.go
151 lines (136 loc) · 4.49 KB
/
if.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// ================================================================
// This is for if/elif/elif/else chains.
// ================================================================
package cst
import (
"fmt"
"github.com/johnkerl/miller/v6/pkg/dsl"
"github.com/johnkerl/miller/v6/pkg/lib"
"github.com/johnkerl/miller/v6/pkg/mlrval"
"github.com/johnkerl/miller/v6/pkg/parsing/token"
"github.com/johnkerl/miller/v6/pkg/runtime"
)
// ----------------------------------------------------------------
type IfChainNode struct {
ifItems []*IfItem
}
func NewIfChainNode(ifItems []*IfItem) *IfChainNode {
return &IfChainNode{
ifItems: ifItems,
}
}
// ----------------------------------------------------------------
// For each if/elif/elif/else portion: the conditional part (...) and the
// statement-block part {...}. For "else", the conditional is nil.
type IfItem struct {
conditionNode IEvaluable
conditionToken *token.Token
statementBlockNode *StatementBlockNode
}
// ----------------------------------------------------------------
// Sample AST:
// DSL EXPRESSION:
// if (NR == 1) { $z = 100 } elif (NR == 2) { $z = 200 } elif (NR == 3) { $z = 300 } else { $z = 900 }
// AST:
// * StatementBlock
// * IfChain
// * IfItem "if"
// * Operator "=="
// * ContextVariable "NR"
// * IntLiteral "1"
// * StatementBlock
// * Assignment "="
// * DirectFieldValue "z"
// * IntLiteral "100"
// * IfItem "elif"
// * Operator "=="
// * ContextVariable "NR"
// * IntLiteral "2"
// * StatementBlock
// * Assignment "="
// * DirectFieldValue "z"
// * IntLiteral "200"
// * IfItem "elif"
// * Operator "=="
// * ContextVariable "NR"
// * IntLiteral "3"
// * StatementBlock
// * Assignment "="
// * DirectFieldValue "z"
// * IntLiteral "300"
// * IfItem "else"
// * StatementBlock
// * Assignment "="
// * DirectFieldValue "z"
// * IntLiteral "900"
func (root *RootNode) BuildIfChainNode(astNode *dsl.ASTNode) (*IfChainNode, error) {
lib.InternalCodingErrorIf(astNode.Type != dsl.NodeTypeIfChain)
ifItems := make([]*IfItem, 0)
astChildren := astNode.Children
for _, astChild := range astChildren {
lib.InternalCodingErrorIf(astChild.Type != dsl.NodeTypeIfItem)
token := string(astChild.Token.Lit) // "if", "elif", "else"
if token == "if" || token == "elif" {
lib.InternalCodingErrorIf(len(astChild.Children) != 2)
conditionNode, err := root.BuildEvaluableNode(astChild.Children[0])
if err != nil {
return nil, err
}
statementBlockNode, err := root.BuildStatementBlockNode(astChild.Children[1])
if err != nil {
return nil, err
}
ifItem := &IfItem{
conditionNode: conditionNode,
conditionToken: astChild.Children[0].Token,
statementBlockNode: statementBlockNode,
}
ifItems = append(ifItems, ifItem)
} else if token == "else" {
lib.InternalCodingErrorIf(len(astChild.Children) != 1)
var conditionNode IEvaluable = nil
statementBlockNode, err := root.BuildStatementBlockNode(astChild.Children[0])
if err != nil {
return nil, err
}
ifItem := &IfItem{
conditionNode: conditionNode,
statementBlockNode: statementBlockNode,
}
ifItems = append(ifItems, ifItem)
} else {
lib.InternalCodingErrorIf(true)
}
}
ifChainNode := NewIfChainNode(ifItems)
return ifChainNode, nil
}
// ----------------------------------------------------------------
func (node *IfChainNode) Execute(state *runtime.State) (*BlockExitPayload, error) {
for _, ifItem := range node.ifItems {
condition := mlrval.TRUE
if ifItem.conditionNode != nil {
condition = ifItem.conditionNode.Evaluate(state)
}
boolValue, isBool := condition.GetBoolValue()
if !isBool {
return nil, fmt.Errorf(
"mlr: conditional expression did not evaluate to boolean%s",
dsl.TokenToLocationInfo(ifItem.conditionToken),
)
}
if boolValue {
blockExitPayload, err := ifItem.statementBlockNode.Execute(state)
if err != nil {
return nil, err
}
// Pass break/continue out of the if-block since they apply to the
// containing for/while/etc.
if blockExitPayload != nil {
return blockExitPayload, nil
}
break
}
}
return nil, nil
}