cparse

package
v0.1905062139.0 Latest Latest
Warning

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

Go to latest
Published: May 6, 2019 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLexer

func NewLexer(input string) *lexer

func NewParser

func NewParser(lex *lexer, scope Scope) *parser

Types

type EvalContext

type EvalContext interface{}

EvalContext is a context in which to evaluate an Expression. It is not used directly by cparse, but is passed to the Get methods of Function and Variable objects to get their value in the current context.

type Expression

type Expression interface {
	// Evaluate the Expression in a given context.  Can be called multiple times
	// with different contexts.  If IsConstant() returns true, can be called with
	// nil context.
	Value(ctx EvalContext) Value
	// Return a string representation of the result of parsing the expression for
	// debugging.
	Dump() string
	// Returns true if the expression is constant (no function calls, all referenced
	// variables are ConstantVariables).  If true, Value() will always return the
	// same value for any context, including nil.
	IsConstant() bool
}

Expression is a parsed expression. Use Value(ctx) to evaluate it in a given context.

func CallFunction

func CallFunction(function Function, name string, args []Expression) Expression

CallFunction returns an Expression that evaluates to the the value of the function called with the given argument expression.

func CastExpression

func CastExpression(val Expression, size int, signed bool) Expression

CastExpression returns an Expression that evaluates to the the value of the given expression cast to the given integer type.

func Parse

func Parse(input string, scope Scope) ([]Expression, error)

Parse takes a string representing comma separated C expressions and a Scope object, and returns a slice of Expression objects.

type Function

type Function interface {
	// Called during Expression.Value(context) with the evaluation context and
	// the values of the arguments, and returns the value of the result
	Get(ctx EvalContext, args []Value) Value
}

A Function object is a handle to call a function when an Expression is being evaluated

type Scope

type Scope interface {
	GetVariable(name string) Variable
	GetFunction(name string) Function
	GetType(name string) string
}

Scope is the scope in which to parse and Expression. Unknown symbols are passed to the methods of the Scope object to get a Variable or Function object, which is used during evaluation to get the value in the evaluation context.

type Value

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

a placeholder for a string, an int64, an array of values, or a valueError

func NewValueBool

func NewValueBool(b bool) Value

Boolean, always promoted to int for now

func NewValueError

func NewValueError(error string, args ...interface{}) Value

Error

func NewValueInt

func NewValueInt(val uint64, size int, signed bool) Value

Integer

func NewValueList

func NewValueList(vals []Value) Value

List

func NewValueString

func NewValueString(s string) Value

String

func (Value) AsBool

func (v Value) AsBool() bool

func (Value) AsError

func (v Value) AsError() error

func (Value) AsInt

func (v Value) AsInt() int64

func (Value) AsInterface

func (v Value) AsInterface() interface{}

As interface (for use in sprintf)

func (Value) AsList

func (v Value) AsList() []Value

func (Value) AsString

func (v Value) AsString() string

func (Value) AsUint64

func (v Value) AsUint64() uint64

func (Value) Dump

func (v Value) Dump() string

func (Value) IsError

func (v Value) IsError() bool

func (Value) IsInt

func (v Value) IsInt() bool

func (Value) IsList

func (v Value) IsList() bool

func (Value) IsString

func (v Value) IsString() bool

type Variable

type Variable interface {
	// Called during Expression.Value(context) with the evaluation context returns
	// the value of the variable in the evaluation context
	Get(ctx EvalContext) Value
}

A Function object is a handle to get the value of a variable when an Expression is being evaluated

func NewConstantVariable

func NewConstantVariable(value Value) Variable

NewConstantVariable is a helper for use inside Scope.GetVariable if the value of the variable never changes. It allows the parser to optimize the expression by collapsing operators on constant values at parse time instead of evaluating them at evaluation time.

Jump to

Keyboard shortcuts

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