lang

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2015 License: MPL-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const ARITH_OP = 57353
View Source
const COMMA = 57352
View Source
const FLOAT = 57356
View Source
const IDENTIFIER = 57354
View Source
const INTEGER = 57355
View Source
const PAREN_LEFT = 57350
View Source
const PAREN_RIGHT = 57351
View Source
const PROGRAM_BRACKET_LEFT = 57346
View Source
const PROGRAM_BRACKET_RIGHT = 57347
View Source
const PROGRAM_STRING_END = 57349
View Source
const PROGRAM_STRING_START = 57348
View Source
const STRING = 57357

Variables

This section is empty.

Functions

func Eval

func Eval(root ast.Node, config *EvalConfig) (interface{}, ast.Type, error)

Eval evaluates the given AST tree and returns its output value, the type of the output, and any error that occurred.

func FixedValueTransform

func FixedValueTransform(root ast.Node, Value *ast.LiteralNode) ast.Node

FixedValueTransform transforms an AST to return a fixed value for all interpolations. i.e. you can make "hello ${anything}" always turn into "hello foo".

func Parse

func Parse(v string) (ast.Node, error)

Parse parses the given program and returns an executable AST tree.

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

type EvalNode interface {
	Eval(ast.Scope, *ast.Stack) (interface{}, ast.Type, error)
}

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

type IdentifierCheck struct {
	Scope ast.Scope
	// contains filtered or unexported fields
}

IdentifierCheck is a SemanticCheck that checks that all identifiers resolve properly and that the right number of arguments are passed to functions.

func (*IdentifierCheck) Visit

func (c *IdentifierCheck) Visit(root ast.Node) error

type SemanticChecker

type SemanticChecker func(ast.Node) error

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.

func (*TypeCheck) ImplicitConversion

func (v *TypeCheck) ImplicitConversion(
	actual ast.Type, expected ast.Type, n ast.Node) ast.Node

func (*TypeCheck) StackPop

func (v *TypeCheck) StackPop() ast.Type

func (*TypeCheck) StackPush

func (v *TypeCheck) StackPush(t ast.Type)

func (*TypeCheck) Visit

func (v *TypeCheck) Visit(root ast.Node) error

type TypeCheckNode

type TypeCheckNode interface {
	TypeCheck(*TypeCheck) (ast.Node, error)
}

TypeCheckNode is the interface that must be implemented by any ast.Node that wants to support type-checking. If the type checker encounters a node that doesn't implement this, it will error.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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