semantics

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2021 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Validate

func Validate(rootNode ast.Node, rootScope *Scope) []error

Types

type Builder

type Builder struct {
	RootScope    *Scope
	CurrentScope *Scope
	// contains filtered or unexported fields
}

func CreateBuilder

func CreateBuilder() *Builder

func (*Builder) Build

func (builder *Builder) Build(root ast.Node)

type Scope

type Scope struct {
	// ParentScope is the scope above this, if any (may be nil)
	ParentScope *Scope
	// ChildScopes are the scopes contained within this, in the order they're declared
	ChildScopes map[ast.Node]*Scope
	// SymbolTable is the table of symbols defined within the scope
	SymbolTable   *SymbolTable
	HasSeenReturn bool
	HasSeenBreak  bool
}

Scope is a scope within a program

func Build

func Build(root ast.Node) (*Scope, []error)

Build builds a symbol table from a parse tree (or sub tree)

func CreateScope

func CreateScope(parent *Scope) *Scope

func (*Scope) CreateScope

func (scope *Scope) CreateScope(node ast.Node) *Scope

func (*Scope) LookupByName

func (scope *Scope) LookupByName(name string) (*Symbol, *Scope, bool)

LookupByName checks for a symbol by name up the scope tree

func (*Scope) LookupNode

func (scope *Scope) LookupNode(node ast.Node) (*Symbol, *Scope, bool)

LookupNode checks for a symbol by name up the scope tree

type Symbol

type Symbol struct {
	// Name is the name of the symbol, such as the identifier of a declared variable
	Name string
	// Trait is the behavior of a symbol
	Trait Trait
	// ArgumentCount is the number of arguments the symbol may take if called. If < 0, assume infinite (or runtime specified) arguments
	ArgumentCount int

	// Node is the AST node the symbol corresponds to
	Node ast.Node
	// contains filtered or unexported fields
}

Symbol is a semantic unit

func CreateSymbol

func CreateSymbol(name string, trait Trait, node ast.Node) *Symbol

func (*Symbol) AddReference

func (symbol *Symbol) AddReference(line int)

type SymbolTable

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

SymbolTable keeps track of symbols within a scope

func CreateSymbolTable

func CreateSymbolTable() *SymbolTable

func (*SymbolTable) Insert

func (table *SymbolTable) Insert(symbol *Symbol)

Insert directly inserts a symbol

func (*SymbolTable) LookupByName

func (table *SymbolTable) LookupByName(name string) (*Symbol, bool)

LookupByName checks for a symbol by name

func (*SymbolTable) LookupNode

func (table *SymbolTable) LookupNode(node ast.Node) (*Symbol, bool)

LookupNode checks for a symbol for the node

func (*SymbolTable) Symbols

func (table *SymbolTable) Symbols() []*Symbol

Symbols returns all of the symbols in the order they were inserted

type Trait

type Trait uint64

Trait describes the behavior of a symbol

const (
	// TraitNumeric indicates that the symbol may be used for numeric operations
	TraitNumeric Trait = 1 << iota
	// TraitCallable indicates that the symbol may be called
	TraitCallable
	// TraitAny indicates that the symbol behaves like any trait (likely determined at runtime for non-typed variables)
	TraitAny
	// TraitAlias indicates that the symbol is an alias for other rules, aliases or functions
	TraitAlias
	// TraitImport indicates that the symbol is the root of an import package
	TraitImport
	// TraitString indicates that the symbol may be treated as a string
	TraitString
	// TraitObject indiciates that the symbol may be treated as a string
	TraitObject
)
const TraitNone Trait = 0

TraitNone indiciates an empty trait

func (Trait) Has

func (trait Trait) Has(other Trait) bool

Has checks whether or not a trait has the specified trait

func (Trait) String

func (i Trait) String() string

func (Trait) Strings

func (trait Trait) Strings() []string

type Validator

type Validator struct {
	RootNode     ast.Node
	RootScope    *Scope
	CurrentScope *Scope
	// contains filtered or unexported fields
}

func CreateValidator

func CreateValidator(rootNode ast.Node, rootScope *Scope) *Validator

func (*Validator) Validate

func (validator *Validator) Validate(root ast.Node)

Jump to

Keyboard shortcuts

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