mirror of
https://github.com/johnkerl/miller.git
synced 2026-01-23 02:14:13 +00:00
CST mods
This commit is contained in:
parent
1ad522d888
commit
e7883442ff
3 changed files with 57 additions and 0 deletions
52
internal/pkg/dsl/cst/exit.go
Normal file
52
internal/pkg/dsl/cst/exit.go
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
// ================================================================
|
||||
// This handles exit statements.
|
||||
// ================================================================
|
||||
|
||||
package cst
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/johnkerl/miller/internal/pkg/dsl"
|
||||
"github.com/johnkerl/miller/internal/pkg/lib"
|
||||
"github.com/johnkerl/miller/internal/pkg/runtime"
|
||||
)
|
||||
|
||||
// ================================================================
|
||||
type ExitStatementNode struct {
|
||||
exitCodeEvaluable IEvaluable
|
||||
}
|
||||
|
||||
func (root *RootNode) BuildExitStatementNode(
|
||||
astNode *dsl.ASTNode,
|
||||
) (IExecutable, error) {
|
||||
lib.InternalCodingErrorIf(len(astNode.Children) != 1)
|
||||
exitCodeNode := astNode.Children[0]
|
||||
|
||||
exitCodeEvaluable, err := root.BuildEvaluableNode(exitCodeNode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
retval := &ExitStatementNode{
|
||||
exitCodeEvaluable: exitCodeEvaluable,
|
||||
}
|
||||
|
||||
return retval, nil
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
func (node *ExitStatementNode) Execute(state *runtime.State) (*BlockExitPayload, error) {
|
||||
exitCodeMlrval := node.exitCodeEvaluable.Evaluate(state)
|
||||
|
||||
intValue, isInt := exitCodeMlrval.GetIntValue()
|
||||
if !isInt {
|
||||
return nil, fmt.Errorf("expected exit statement int argument; got %d", exitCodeMlrval.GetTypeName())
|
||||
}
|
||||
|
||||
state.ExitInfo.HasExitCode = true
|
||||
state.ExitInfo.ExitCode = int(intValue)
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
|
|
@ -66,6 +66,9 @@ func (root *RootNode) BuildStatementNode(
|
|||
case dsl.NodeTypeEmitPStatement:
|
||||
return root.BuildEmitPStatementNode(astNode)
|
||||
|
||||
case dsl.NodeTypeExitStatement:
|
||||
return root.BuildExitStatementNode(astNode)
|
||||
|
||||
case dsl.NodeTypeBeginBlock:
|
||||
return nil, fmt.Errorf("mlr: begin blocks may only be declared at top level.")
|
||||
case dsl.NodeTypeEndBlock:
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ import (
|
|||
type State struct {
|
||||
Inrec *mlrval.Mlrmap
|
||||
Context *types.Context
|
||||
ExitInfo *types.ExitInfo
|
||||
Oosvars *mlrval.Mlrmap
|
||||
FilterExpression *mlrval.Mlrval
|
||||
Stack *Stack
|
||||
|
|
@ -34,6 +35,7 @@ func NewEmptyState(options *cli.TOptions) *State {
|
|||
return &State{
|
||||
Inrec: nil,
|
||||
Context: nil,
|
||||
ExitInfo: types.NewExitInfo(),
|
||||
Oosvars: oosvars,
|
||||
FilterExpression: mlrval.TRUE,
|
||||
Stack: NewStack(),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue