miller/internal/pkg/runtime/state.go
2022-03-15 23:19:39 -04:00

58 lines
1.7 KiB
Go

// ================================================================
// Tracks everything needed for statement evaluation/assignment in the Miller
// DSL runtimne: current record/context (the latter being NF, NR, etc);
// out-of-stream variables; local-variable stack; etc.
// ================================================================
package runtime
import (
"container/list"
"github.com/johnkerl/miller/internal/pkg/cli"
"github.com/johnkerl/miller/internal/pkg/lib"
"github.com/johnkerl/miller/internal/pkg/mlrval"
"github.com/johnkerl/miller/internal/pkg/types"
)
type State struct {
Inrec *mlrval.Mlrmap
Context *types.Context
ExitInfo *types.ExitInfo
Oosvars *mlrval.Mlrmap
FilterExpression *mlrval.Mlrval
Stack *Stack
OutputRecordsAndContexts *list.List // list of *types.RecordAndContext
// For holding "\0".."\9" between where they are set via things like
// '$x =~ "(..)_(...)"', and interpolated via things like '$y = "\2:\1"'.
RegexCaptures []string
Options *cli.TOptions
}
func NewEmptyState(options *cli.TOptions) *State {
oosvars := mlrval.NewMlrmap()
return &State{
Inrec: nil,
Context: nil,
ExitInfo: types.NewExitInfo(),
Oosvars: oosvars,
FilterExpression: mlrval.TRUE,
Stack: NewStack(),
// OutputRecordsAndContexts is assigned after construction
// See lib.MakeEmptyRegexCaptures for context.
RegexCaptures: lib.MakeEmptyRegexCaptures(),
Options: options,
}
}
func (state *State) Update(
inrec *mlrval.Mlrmap,
context *types.Context,
) {
state.Inrec = inrec
state.Context = context
state.RegexCaptures = lib.MakeEmptyRegexCaptures()
}