Documentation ¶
Overview ¶
Package vm provides a compiler and virtual machine environment for executing mtail programs.
Package vm provides a compiler and virtual machine environment for executing mtail programs.
Index ¶
- Constants
- Variables
- func Check(node astNode) error
- func CodeGen(name string, ast astNode) (*object, error)
- func Equals(t1, t2 Type) bool
- func IsComplete(t Type) bool
- func IsDimension(t Type) bool
- func IsFunction(t Type) bool
- func MergePosition(a, b *position) *position
- func Parse(name string, input io.Reader) (astNode, error)
- func Unify(a, b Type) error
- func Walk(v Visitor, node astNode)
- type ErrorList
- type Loader
- type LoaderOptions
- type Options
- type Scope
- type Sexp
- type Symbol
- type SymbolKind
- type Type
- type TypeError
- type TypeOperator
- type TypeVariable
- type Unparser
- type VM
- type Visitor
Constants ¶
const EOF = 0
Variables ¶
var ( // LineCount counts the number of lines read by the program loader from the input channel. LineCount = expvar.NewInt("line_count") // ProgLoads counts the number of program load events. ProgLoads = expvar.NewMap("prog_loads_total") // ProgLoadErrors counts the number of program load errors. ProgLoadErrors = expvar.NewMap("prog_load_errors") )
var ( Undef = &TypeOperator{"Undef", []Type{}} Error = &TypeOperator{"Error", []Type{}} None = &TypeOperator{"None", []Type{}} Bool = &TypeOperator{"Bool", []Type{}} Int = &TypeOperator{"Int", []Type{}} Float = &TypeOperator{"Float", []Type{}} String = &TypeOperator{"String", []Type{}} Pattern = &TypeOperator{"Pattern", []Type{}} )
Builtin types
var Builtins = map[string]Type{ "int": Function(NewTypeVariable(), Int), "bool": Function(NewTypeVariable(), Bool), "float": Function(NewTypeVariable(), Float), "string": Function(NewTypeVariable(), String), "timestamp": Function(Int), "len": Function(String, Int), "settime": Function(Int, None), "strptime": Function(String, String, None), "strtol": Function(String, Int, Int), "tolower": Function(String, String), "getfilename": Function(String), }
Builtins is a mapping of the builtin language functions to their type definitions.
Functions ¶
func Check ¶
func Check(node astNode) error
Check performs a semantic check of the astNode, and returns a list of errors found, or nil if the program is semantically valid. At the completion of Check, the symbol table and type annotation are also complete.
func IsComplete ¶
IsComplete returns true if the type and all its arguments have non-variable exemplars.
func IsDimension ¶
IsDimension returns true if the given type is a Dimension type.
func IsFunction ¶
IsFunction returns true if the given type is a Function type.
func MergePosition ¶
func MergePosition(a, b *position) *position
func Unify ¶
Unify performs type unification of both parameter Types. It returns the least upper bound of both types, the smallest type that is capable of representing both parameters. If either type is a type variable, then that variable is unified with the LUB. In reporting errors, it is assumed that a is the expected type and b is the type observed.
Types ¶
type ErrorList ¶
type ErrorList []*compileError
ErrorList contains a list of compile errors.
type Loader ¶
type Loader struct { VMsDone chan struct{} // Notify mtail when all running VMs are shutdown. // contains filtered or unexported fields }
Loader handles the lifecycle of programs and virtual machines, by watching the configured program source directory, compiling changes to programs, and managing the running virtual machines that receive input from the lines channel.
func NewLoader ¶
func NewLoader(o LoaderOptions) (*Loader, error)
NewLoader creates a new program loader. It takes a filesystem watcher and a filesystem interface as arguments. If fs is nil, it will use the default filesystem interface.
func (*Loader) CompileAndRun ¶
CompileAndRun compiles a program read from the input, starting execution if it succeeds. If an existing virtual machine of the same name already exists, the previous virtual machine is terminated and the new loaded over it. If the new program fails to compile, any existing virtual machine with the same name remains running.
func (*Loader) LoadAllPrograms ¶
LoadAllPrograms loads all programs in a directory and starts watching the directory for filesystem changes. Any compile errors are stored for later retrieival. This function returns an error if an internal error occurs.
func (*Loader) LoadProgram ¶
LoadProgram loads or reloads a program from the path specified. The name of the program is the basename of the file.
func (*Loader) UnloadProgram ¶
UnloadProgram removes the named program from the watcher to prevent future updates, and terminates any currently running VM goroutine.
type LoaderOptions ¶
type LoaderOptions struct { Store *metrics.Store Lines <-chan *tailer.LogLine ProgramPath string // Path containing mtail programs W watcher.Watcher // Not required, will use watcher.LogWatcher if zero. FS afero.Fs // Not required, will use afero.OsFs if zero. OverrideLocation *time.Location // if not nil, overrides the timezone in strptime(). CompileOnly bool // Compile, don't start execution. ErrorsAbort bool // Compiler errors abort the loader. DumpAst bool // print the AST after type check DumpAstTypes bool // Instructs the loader to dump to stdout the compiled program after compilation. DumpBytecode bool // Instructs the loader to dump the program bytecode after compilation. SyslogUseCurrentYear bool // If true, override empty year with the current in strptime(). OmitMetricSource bool // Don't put the source in the metric when added to the Store. }
LoaderOptions contains the required and optional parameters for creating a new Loader.
type Options ¶
type Options struct { CompileOnly bool // Do not start the program after compilation. SyslogUseCurrentYear bool // Use the current year if no year is present in the log file timestamp. OverrideLocation *time.Location // EmitAst bool // Print the AST after parse EmitAstTypes bool // Print the AST with types after typechecking }
Options contains all the parameters that affect the behaviour of the compiler.
type Scope ¶
Scope maintains a record of the identifiers declared in the current program scope, and a link to the parent scope.
func (*Scope) Insert ¶
Insert attempts to insert a symbol into the scope. If the scope already contains an object alt with the same name, the scope is unchanged and the function returns alt. Otherwise the symbol is inserted, and returns nil.
func (*Scope) InsertAlias ¶
InsertAlias attempts to insert a duplicate name for an existing symbol into the scope. If the scope already contains an object alt with the alias, the scope is unchanged and the function returns alt. Otherwise, the symbol is inserted and the function returns nil.
type Sexp ¶
type Sexp struct {
// contains filtered or unexported fields
}
Sexp is for converting program syntax trees into typed s-expression for printing
func (*Sexp) Dump ¶
Dump begins the dumping of the syntax tree, returning the s-expression as a single string
func (*Sexp) VisitAfter ¶
func (s *Sexp) VisitAfter(node astNode)
func (*Sexp) VisitBefore ¶
type Symbol ¶
type Symbol struct { Name string // identifier name Kind SymbolKind // kind of program object Type Type // object's type Pos *position // Source file position of definition Binding interface{} // binding to storage allocated in runtime Addr int // Address offset in another structure, object specific Used bool // Optional marker that this symbol is used after declaration. }
Symbol describes a named program object.
func NewSymbol ¶
func NewSymbol(name string, kind SymbolKind, pos *position) (sym *Symbol)
NewSymbol creates a record of a given symbol kind, named name, found at loc
type SymbolKind ¶
type SymbolKind int
const ( VarSymbol SymbolKind = iota // Variables CaprefSymbol // Capture group references DecoSymbol // Decorators PatternSymbol // Named pattern constants )
SymbolKind enumerates the kinds of symbols found in the program text.
func (SymbolKind) String ¶
func (k SymbolKind) String() string
type Type ¶
type Type interface { // Root returns an exemplar Type after unification occurs. If the type // system is complete after unification, Root will be a TypeOperator. Root() Type // String returns a string representation of a Type. String() string }
Type represents a type in the mtail program.
func FreshType ¶
FreshType returns a new type from the provided type scheme, replacing any unbound type variables with new type variables.
func LeastUpperBound ¶
type TypeOperator ¶
type TypeOperator struct { // Name is a common name for this operator Name string // Args is the sequence of types that are parameters to this type. They // may be fully bound type operators, or partially defined (i.e. contain // TypeVariables) in which case they represent polymorphism in the operator // they are argyments to. Args []Type }
TypeOperator represents a type scheme in the type system.
func Dimension ¶
func Dimension(args ...Type) *TypeOperator
Dimension is a convenience method which instantiates a new Dimension type scheme, with the given args as the dimensions of the type.
func Function ¶
func Function(args ...Type) *TypeOperator
Function is a convenience method, which instantiates a new Function type scheme, with the given args as parameters.
func (*TypeOperator) Root ¶
func (t *TypeOperator) Root() Type
func (*TypeOperator) String ¶
func (t *TypeOperator) String() (s string)
type TypeVariable ¶
TypeVariable represents an unbound type variable in the type system.
func NewTypeVariable ¶
func NewTypeVariable() *TypeVariable
NewTypeVariable constructs a new unique TypeVariable.
func (*TypeVariable) Root ¶
func (t *TypeVariable) Root() Type
func (*TypeVariable) SetInstance ¶
func (t *TypeVariable) SetInstance(t1 *Type)
SetInstance sets the exemplar instance of this TypeVariable, during unification.
func (*TypeVariable) String ¶
func (t *TypeVariable) String() string
type Unparser ¶
type Unparser struct {
// contains filtered or unexported fields
}
Unparser is for converting program syntax trees back to program text.
func (*Unparser) Unparse ¶
Unparse begins the unparsing of the syntax tree, returning the program text as a single string.
func (*Unparser) VisitAfter ¶
func (u *Unparser) VisitAfter(n astNode)
func (*Unparser) VisitBefore ¶
type VM ¶
type VM struct {
// contains filtered or unexported fields
}
VM describes the virtual machine for each program. It contains virtual segments of the executable bytecode, constant data (string and regular expressions), mutable state (metrics), and a stack for the current thread of execution.
func Compile ¶
Compile compiles a program from the input into a virtual machine or a list of compile errors. It takes the program's name and the metric store as additional arguments to build the virtual machine.
func New ¶
New creates a new virtual machine with the given name, and compiler artifacts for executable and data segments.
func (*VM) DumpByteCode ¶
DumpByteCode emits the program disassembly and program objects to a string.
type Visitor ¶
type Visitor interface { VisitBefore(n astNode) (v Visitor) VisitAfter(n astNode) }
Visitor's VisitBefore method is invoked for each node encountered by Walk. If the result Visitor v is not nil, Walk visits each of the children of that node with v. VisitAfter is called on n at the end.