vm

package
v0.0.0-...-5e0759b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2017 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Overview

Package vm provides a compiler and virtual machine environment for executing mtail programs.

Index

Constants

View Source
const EOF = 0

Variables

View Source
var (
	// 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")
)
View Source
var (
	// LineCount counts the number of lines read by the virtual machine engine from the input channel.
	LineCount = expvar.NewInt("line_count")
)

Functions

func Check

func Check(node node) error

Check performs a semantic check of the ast node, and returns a boolean indicating OK; if ok is not true, then error is a list of errors found.

func CodeGen

func CodeGen(name string, ast node) (*object, error)

CodeGen is the function that compiles the program to bytecode and data.

func MergePosition

func MergePosition(a, b *position) *position

func Parse

func Parse(name string, input io.Reader) (node, error)

func Walk

func Walk(v Visitor, node node)

Types

type ErrorList

type ErrorList []*compileError

ErrorList contains a list of compile errors.

func (*ErrorList) Add

func (p *ErrorList) Add(pos *position, msg string)

Add appends an error at a position to the list of errors.

func (ErrorList) Error

func (p ErrorList) Error() string

ErrorList implements the error interface.

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

func (l *Loader) CompileAndRun(name string, input io.Reader) error

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) LoadProg

func (l *Loader) LoadProg(programPath string) error

LoadProg loads or reloads a program from the path specified. The name of the program is the basename of the file.

func (*Loader) LoadProgs

func (l *Loader) LoadProgs(programPath string) error

LoadProgs loads all programs in a directory and starts watching the directory for filesystem changes. The total number of program errors is returned.

func (*Loader) UnloadProgram

func (l *Loader) UnloadProgram(pathname string)

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 string
	W     watcher.Watcher // Not required, will use watcher.LogWatcher if zero.
	FS    afero.Fs        // Not required, will use afero.OsFs if zero.

	CompileOnly          bool
	DumpBytecode         bool
	SyslogUseCurrentYear bool
}

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.
}

Options contains all the parameters that affect the behaviour of the compiler.

type SymbolClass

type SymbolClass int
const (
	IDSymbol     SymbolClass = iota // Identifiers
	CaprefSymbol                    // Capture group references
	DefSymbol                       // Definitions

)

symtype enumerates the types of symbols found in the program text.

type SymbolTable

type SymbolTable []*scope

func (*SymbolTable) Add

func (s *SymbolTable) Add(name string, class SymbolClass, loc *position) (sym *symbol)

func (*SymbolTable) CurrentScope

func (s *SymbolTable) CurrentScope() *scope

func (*SymbolTable) EnterScope

func (s *SymbolTable) EnterScope(sc *scope) *scope

func (*SymbolTable) ExitScope

func (s *SymbolTable) ExitScope()

func (*SymbolTable) Lookup

func (s *SymbolTable) Lookup(name string, class SymbolClass) (*symbol, bool)

type Type

type Type int
const (
	Untyped Type = iota // Untyped indicates no type has been determined
	None
	String
	Int
	Float
)

type Unparser

type Unparser struct {
	// contains filtered or unexported fields
}

Unparser is for converting program syntax trees back to program text.

func (*Unparser) Unparse

func (u *Unparser) Unparse(n node) string

Unparse begins the unparsing of the syntax tree, returning the program text as a single string.

func (*Unparser) VisitAfter

func (u *Unparser) VisitAfter(n node)

func (*Unparser) VisitBefore

func (u *Unparser) VisitBefore(n node) Visitor

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

func Compile(name string, input io.Reader, o *Options) (*VM, error)

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

func New(name string, obj *object, syslogUseCurrentYear bool) *VM

New creates a new virtual machine with the given name, and compiler artifacts for executable and data segments.

func (*VM) DumpByteCode

func (v *VM) DumpByteCode(name string)

DumpByteCode emits the program disassembly and data to standard out.

func (*VM) Run

func (v *VM) Run(lines <-chan string, shutdown chan<- struct{})

Run executes the virtual machine on each line of input received. When the input closes, it signals to the loader that it has terminated by closing the shutdown channel.

type Visitor

type Visitor interface {
	VisitBefore(n node) (v Visitor)
	VisitAfter(n node)
}

a 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL