Documentation ¶
Index ¶
- Constants
- func Eval(root ast.Node, config *EvalConfig) (interface{}, ast.Type, error)
- func FixedValueTransform(root ast.Node, Value *ast.LiteralNode) ast.Node
- func Parse(v string) (ast.Node, error)
- type EvalConfig
- type EvalNode
- type IdentifierCheck
- type SemanticChecker
- type TypeCheck
- type TypeCheckNode
Constants ¶
const ARITH_OP = 57353
const COMMA = 57352
const FLOAT = 57356
const IDENTIFIER = 57354
const INTEGER = 57355
const PAREN_LEFT = 57350
const PAREN_RIGHT = 57351
const PROGRAM_BRACKET_LEFT = 57346
const PROGRAM_BRACKET_RIGHT = 57347
const PROGRAM_STRING_END = 57349
const PROGRAM_STRING_START = 57348
const STRING = 57357
Variables ¶
This section is empty.
Functions ¶
func Eval ¶
Eval evaluates the given AST tree and returns its output value, the type of the output, and any error that occurred.
func FixedValueTransform ¶
FixedValueTransform transforms an AST to return a fixed value for all interpolations. i.e. you can make "hello ${anything}" always turn into "hello foo".
Types ¶
type EvalConfig ¶
type EvalConfig struct { // GlobalScope is the global scope of execution for evaluation. GlobalScope *ast.BasicScope // SemanticChecks is a list of additional semantic checks that will be run // on the tree prior to evaluating it. The type checker, identifier checker, // etc. will be run before these automatically. SemanticChecks []SemanticChecker }
EvalConfig is the configuration for evaluating.
type EvalNode ¶
EvalNode is the interface that must be implemented by any ast.Node to support evaluation. This will be called in visitor pattern order. The result of each call to Eval is automatically pushed onto the stack as a LiteralNode. Pop elements off the stack to get child values.
type IdentifierCheck ¶
IdentifierCheck is a SemanticCheck that checks that all identifiers resolve properly and that the right number of arguments are passed to functions.
type SemanticChecker ¶
SemanticChecker is the type that must be implemented to do a semantic check on an AST tree. This will be called with the root node.
type TypeCheck ¶
type TypeCheck struct { Scope ast.Scope // Implicit is a map of implicit type conversions that we can do, // and that shouldn't error. The key of the first map is the from type, // the key of the second map is the to type, and the final string // value is the function to call (which must be registered in the Scope). Implicit map[ast.Type]map[ast.Type]string // Stack of types. This shouldn't be used directly except by implementations // of TypeCheckNode. Stack []ast.Type // contains filtered or unexported fields }
TypeCheck implements ast.Visitor for type checking an AST tree. It requires some configuration to look up the type of nodes.
It also optionally will not type error and will insert an implicit type conversions for specific types if specified by the Implicit field. Note that this is kind of organizationally weird to put into this structure but we'd rather do that than duplicate the type checking logic multiple times.