symtable

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2022 License: BSD-3-Clause Imports: 5 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnalyzeCells

func AnalyzeCells(scopes Scopes, free StringSet)

If a name is defined in free and also in locals, then this block

provides the binding for the free variable.  The name should be
marked CELL in this block and removed from the free list.

Note that the current block's free variables are included in free.
That's safe because no name can be free and local in the same scope.

Types

type BlockType

type BlockType uint8

BlockType for SymTable

const (
	FunctionBlock BlockType = iota
	ClassBlock
	ModuleBlock
)

BlockTypes

func (BlockType) String

func (i BlockType) String() string

type Children

type Children []*SymTable

type DefUseFlags

type DefUseFlags uint8

Def-use flag information

const (
	DefGlobal    DefUseFlags = 1 << iota // global stmt
	DefLocal                             // assignment in code block
	DefParam                             // formal parameter
	DefNonlocal                          // nonlocal stmt
	DefUse                               // name is used
	DefFree                              // name used but not defined in nested block
	DefFreeClass                         // free variable from class's method
	DefImport                            // assignment occurred via import

	DefBound = (DefLocal | DefParam | DefImport)

	// ScopeGlobalExplicit and ScopeGlobalImplicit are used internally by the symbol
	// table.  GLOBAL is returned from PyST_GetScope() for either of them.
	// It is stored in ste_symbols at bits 12-15.
	DefScopeMask = (DefGlobal | DefLocal | DefParam | DefNonlocal)
)

Flags for def-use information

type LookupChild

type LookupChild map[ast.Ast]*SymTable

type OptType

type OptType uint8

OptType for SymTable

type Scope

type Scope uint8

Scope

const (
	ScopeInvalid Scope = iota
	ScopeLocal
	ScopeGlobalExplicit
	ScopeGlobalImplicit
	ScopeFree
	ScopeCell
)

Scope definitions

func (Scope) String

func (i Scope) String() string

type Scopes

type Scopes map[string]Scope

Accumulate Scopes

type StringSet

type StringSet map[string]struct{}

StringSet for storing strings

func (StringSet) Add

func (s StringSet) Add(elem string)

Add adds elem to the set, returning the set

If the element already exists then it has no effect

func (StringSet) Contains

func (s StringSet) Contains(k string) bool

Contains returns true if k is in s

func (StringSet) Copy

func (s StringSet) Copy() StringSet

Copy makes a shallow copy of s

func (StringSet) Discard

func (s StringSet) Discard(k string) bool

Discard ensures k is not in the set returning true if it was found

func (StringSet) Update

func (s StringSet) Update(other StringSet)

Update adds all the elements from the other set to this set.

type SymTable

type SymTable struct {
	Type              BlockType // 'class', 'module', and 'function'
	Name              string    // name of the class if the table is for a class, the name of the function if the table is for a function, or 'top' if the table is global (get_type() returns 'module').
	Filename          string    // filename that this is being parsed from
	Lineno            int       // number of the first line in the block this table represents.
	Unoptimized       OptType   // false if namespace is optimized
	Nested            bool      // true if the block is a nested class or function.
	Free              bool      // true if block has free variables
	ChildFree         bool      // true if a child block has free vars, including free refs to globals
	Generator         bool      // true if namespace is a generator
	Varargs           bool      // true if block has varargs
	Varkeywords       bool      // true if block has varkeywords
	ReturnsValue      bool      // true if namespace uses return with an argument
	NeedsClassClosure bool      // for class scopes, true if a closure over __class__ should be created
	// col_offset        int       // offset of first line of block
	// opt_lineno        int       // lineno of last exec or import *
	// opt_col_offset    int       // offset of last exec or import *
	TmpName int    // counter for listcomp temp vars
	Private string // name of current class or ""

	Symbols     Symbols
	Global      *SymTable // symbol table entry for module
	Parent      *SymTable
	Varnames    []string    // list of function parameters
	Children    Children    // Child SymTables
	LookupChild LookupChild // Child symtables keyed by ast
}

func NewSymTable

func NewSymTable(Ast ast.Ast, filename string) (st *SymTable, err error)

Make a new top symbol table from the ast supplied

func (*SymTable) AddDef

func (st *SymTable) AddDef(node ast.Ast, name ast.Identifier, flags DefUseFlags)

Add a symbol into the symble table

func (*SymTable) Analyze

func (st *SymTable) Analyze()

Analyze the SymTable

func (*SymTable) AnalyzeBlock

func (st *SymTable) AnalyzeBlock(bound, free, global StringSet)

Make final symbol table decisions for block of ste.

Arguments:
st -- current symtable entry (input/output)
bound -- set of variables bound in enclosing scopes (input).  bound
    is nil for module blocks.
free -- set of free variables in enclosed scopes (output)
globals -- set of declared global variables in enclosing scopes (input)

The implementation uses two mutually recursive functions,
analyze_block() and analyze_child_block().  analyze_block() is
responsible for analyzing the individual names defined in a block.
analyze_child_block() prepares temporary namespace dictionaries
used to evaluated nested blocks.

The two functions exist because a child block should see the name
bindings of its enclosing blocks, but those bindings should not
propagate back to a parent block.

func (*SymTable) AnalyzeChildBlock

func (st *SymTable) AnalyzeChildBlock(bound, free, global, child_free StringSet)

func (*SymTable) AnalyzeName

func (st *SymTable) AnalyzeName(scopes Scopes, name string, symbol Symbol, bound, local, free, global StringSet)

Decide on scope of name, given flags.

The namespace dictionaries may be modified to record information
about the new name.  For example, a new global will add an entry to
global.  A name that was global can be changed to local.

func (*SymTable) DropClassFree

func (st *SymTable) DropClassFree(free StringSet)

func (*SymTable) Find

func (st *SymTable) Find(scopeType Scope, flag DefUseFlags) (out []string)

Return a sorted list of symbol names if the scope of a name matches either scopeType or flag is set

func (*SymTable) FindChild

func (st *SymTable) FindChild(Ast ast.Ast) *SymTable

FindChild finds SymTable attached to Ast - returns nil if not found

func (*SymTable) GetScope

func (st *SymTable) GetScope(name string) Scope

GetScope finds the scope for the name, returns ScopeInvalid if not found

func (*SymTable) Parse

func (st *SymTable) Parse(Ast ast.Ast)

Parse the ast into the symbol table

type Symbol

type Symbol struct {
	Scope     Scope
	Flags     DefUseFlags
	Lineno    int
	ColOffset int
}

Info about a symbol

type Symbols

type Symbols map[string]Symbol

func (Symbols) Update

func (symbols Symbols) Update(scopes Scopes, bound, free StringSet, classflag bool)

Enter the final scope information into the st.Symbols dict. * * All arguments are dicts. Modifies symbols, others are read-only.

Jump to

Keyboard shortcuts

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