-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathblock_exit.go
90 lines (76 loc) · 2.61 KB
/
block_exit.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
// ================================================================
// This is for things that get us out of statement blocks: break, continue,
// return.
// ================================================================
package cst
import (
"fmt"
"github.com/johnkerl/miller/v6/pkg/dsl"
"github.com/johnkerl/miller/v6/pkg/lib"
"github.com/johnkerl/miller/v6/pkg/runtime"
)
// ----------------------------------------------------------------
type BreakNode struct {
}
func (root *RootNode) BuildBreakNode(astNode *dsl.ASTNode) (*BreakNode, error) {
lib.InternalCodingErrorIf(astNode.Type != dsl.NodeTypeBreak)
lib.InternalCodingErrorIf(astNode.Children == nil)
lib.InternalCodingErrorIf(len(astNode.Children) != 0)
return &BreakNode{}, nil
}
func (node *BreakNode) Execute(state *runtime.State) (*BlockExitPayload, error) {
return &BlockExitPayload{
BLOCK_EXIT_BREAK,
nil,
}, nil
}
// ----------------------------------------------------------------
type ContinueNode struct {
}
func (root *RootNode) BuildContinueNode(astNode *dsl.ASTNode) (*ContinueNode, error) {
lib.InternalCodingErrorIf(astNode.Type != dsl.NodeTypeContinue)
lib.InternalCodingErrorIf(astNode.Children == nil)
lib.InternalCodingErrorIf(len(astNode.Children) != 0)
return &ContinueNode{}, nil
}
func (node *ContinueNode) Execute(state *runtime.State) (*BlockExitPayload, error) {
return &BlockExitPayload{
BLOCK_EXIT_CONTINUE,
nil,
}, nil
}
// ----------------------------------------------------------------
type ReturnNode struct {
returnValueExpression IEvaluable
}
func (root *RootNode) BuildReturnNode(astNode *dsl.ASTNode) (*ReturnNode, error) {
lib.InternalCodingErrorIf(astNode.Type != dsl.NodeTypeReturn)
lib.InternalCodingErrorIf(astNode.Children == nil)
if len(astNode.Children) == 0 {
return &ReturnNode{returnValueExpression: nil}, nil
} else if len(astNode.Children) == 1 {
returnValueExpression, err := root.BuildEvaluableNode(astNode.Children[0])
if err != nil {
return nil, err
}
return &ReturnNode{returnValueExpression: returnValueExpression}, nil
} else {
lib.InternalCodingErrorIf(true)
}
return nil, fmt.Errorf("internal coding error: statement should not be reached")
}
func (node *ReturnNode) Execute(state *runtime.State) (*BlockExitPayload, error) {
if node.returnValueExpression == nil {
return &BlockExitPayload{
BLOCK_EXIT_RETURN_VOID,
nil,
}, nil
} else {
// The return value can be of type MT_ERROR but we do not use Go-level error return here.
returnValue := node.returnValueExpression.Evaluate(state)
return &BlockExitPayload{
BLOCK_EXIT_RETURN_VALUE,
returnValue.Copy(),
}, nil
}
}