2019-12-20 22:40:38 +03:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
// CompilationScope represents a compiled instructions and the last two
|
|
|
|
// instructions that were emitted.
|
|
|
|
type CompilationScope struct {
|
|
|
|
Instructions []byte
|
|
|
|
SymbolInit map[string]bool
|
|
|
|
SourceMap map[int]Pos
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop represents a loop construct that the compiler uses to track the current
|
|
|
|
// loop.
|
|
|
|
type Loop struct {
|
|
|
|
Continues []int
|
|
|
|
Breaks []int
|
|
|
|
}
|
|
|
|
|
|
|
|
// CompilerError represents a compiler error.
|
|
|
|
type CompilerError struct {
|
|
|
|
FileSet *SourceFileSet
|
|
|
|
Node Node
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *CompilerError) Error() string {
|
|
|
|
filePos := e.FileSet.Position(e.Node.Pos())
|
|
|
|
return fmt.Sprintf("Compile Error: %s\n\tat %s", e.Err.Error(), filePos)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SymbolScope represents a symbol scope.
|
|
|
|
type SymbolScope string
|
|
|
|
|
|
|
|
// List of symbol scopes
|
|
|
|
const (
|
|
|
|
ScopeGlobal SymbolScope = "GLOBAL"
|
|
|
|
ScopeLocal SymbolScope = "LOCAL"
|
|
|
|
ScopeBuiltin SymbolScope = "BUILTIN"
|
|
|
|
ScopeFree SymbolScope = "FREE"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Symbol represents a symbol in the symbol table.
|
|
|
|
type Symbol struct {
|
|
|
|
Name string
|
|
|
|
Scope SymbolScope
|
|
|
|
Index int
|
|
|
|
LocalAssigned bool // if the local symbol is assigned at least once
|
|
|
|
}
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-01-15 09:24:33 +03:00
|
|
|
// SymbolTable represents a symbol table.
|
2019-01-09 10:17:42 +03:00
|
|
|
type SymbolTable struct {
|
2019-03-01 05:41:29 +03:00
|
|
|
parent *SymbolTable
|
|
|
|
block bool
|
|
|
|
store map[string]*Symbol
|
|
|
|
numDefinition int
|
|
|
|
maxDefinition int
|
|
|
|
freeSymbols []*Symbol
|
|
|
|
builtinSymbols []*Symbol
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
|
|
|
|
2019-01-15 09:24:33 +03:00
|
|
|
// NewSymbolTable creates a SymbolTable.
|
2019-01-09 10:17:42 +03:00
|
|
|
func NewSymbolTable() *SymbolTable {
|
|
|
|
return &SymbolTable{
|
2019-02-02 10:27:29 +03:00
|
|
|
store: make(map[string]*Symbol),
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-15 09:24:33 +03:00
|
|
|
// Define adds a new symbol in the current scope.
|
2019-02-02 10:27:29 +03:00
|
|
|
func (t *SymbolTable) Define(name string) *Symbol {
|
|
|
|
symbol := &Symbol{Name: name, Index: t.nextIndex()}
|
2019-01-09 10:17:42 +03:00
|
|
|
t.numDefinition++
|
|
|
|
|
|
|
|
if t.Parent(true) == nil {
|
|
|
|
symbol.Scope = ScopeGlobal
|
|
|
|
} else {
|
|
|
|
symbol.Scope = ScopeLocal
|
|
|
|
}
|
|
|
|
t.store[name] = symbol
|
|
|
|
t.updateMaxDefs(symbol.Index + 1)
|
|
|
|
return symbol
|
|
|
|
}
|
|
|
|
|
2019-01-15 09:24:33 +03:00
|
|
|
// DefineBuiltin adds a symbol for builtin function.
|
2019-02-02 10:27:29 +03:00
|
|
|
func (t *SymbolTable) DefineBuiltin(index int, name string) *Symbol {
|
2019-03-01 05:41:29 +03:00
|
|
|
if t.parent != nil {
|
|
|
|
return t.parent.DefineBuiltin(index, name)
|
|
|
|
}
|
|
|
|
|
2019-02-02 10:27:29 +03:00
|
|
|
symbol := &Symbol{
|
2019-01-09 10:17:42 +03:00
|
|
|
Name: name,
|
|
|
|
Index: index,
|
|
|
|
Scope: ScopeBuiltin,
|
|
|
|
}
|
|
|
|
t.store[name] = symbol
|
2019-03-01 05:41:29 +03:00
|
|
|
t.builtinSymbols = append(t.builtinSymbols, symbol)
|
2019-01-09 10:17:42 +03:00
|
|
|
return symbol
|
|
|
|
}
|
|
|
|
|
2019-01-15 09:24:33 +03:00
|
|
|
// Resolve resolves a symbol with a given name.
|
2019-12-20 22:40:38 +03:00
|
|
|
func (t *SymbolTable) Resolve(
|
|
|
|
name string,
|
|
|
|
) (symbol *Symbol, depth int, ok bool) {
|
2019-01-09 10:17:42 +03:00
|
|
|
symbol, ok = t.store[name]
|
|
|
|
if !ok && t.parent != nil {
|
|
|
|
symbol, depth, ok = t.parent.Resolve(name)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2019-03-24 12:23:38 +03:00
|
|
|
depth++
|
2019-01-09 10:17:42 +03:00
|
|
|
|
|
|
|
// if symbol is defined in parent table and if it's not global/builtin
|
|
|
|
// then it's free variable.
|
2019-12-20 22:40:38 +03:00
|
|
|
if !t.block && depth > 0 &&
|
|
|
|
symbol.Scope != ScopeGlobal &&
|
|
|
|
symbol.Scope != ScopeBuiltin {
|
2019-01-09 10:17:42 +03:00
|
|
|
return t.defineFree(symbol), depth, true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-15 09:24:33 +03:00
|
|
|
// Fork creates a new symbol table for a new scope.
|
2019-01-09 10:17:42 +03:00
|
|
|
func (t *SymbolTable) Fork(block bool) *SymbolTable {
|
|
|
|
return &SymbolTable{
|
2019-02-02 10:27:29 +03:00
|
|
|
store: make(map[string]*Symbol),
|
2019-01-09 10:17:42 +03:00
|
|
|
parent: t,
|
|
|
|
block: block,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-15 09:24:33 +03:00
|
|
|
// Parent returns the outer scope of the current symbol table.
|
2019-01-09 10:17:42 +03:00
|
|
|
func (t *SymbolTable) Parent(skipBlock bool) *SymbolTable {
|
|
|
|
if skipBlock && t.block {
|
|
|
|
return t.parent.Parent(skipBlock)
|
|
|
|
}
|
|
|
|
return t.parent
|
|
|
|
}
|
|
|
|
|
2019-01-15 09:24:33 +03:00
|
|
|
// MaxSymbols returns the total number of symbols defined in the scope.
|
2019-01-09 10:17:42 +03:00
|
|
|
func (t *SymbolTable) MaxSymbols() int {
|
|
|
|
return t.maxDefinition
|
|
|
|
}
|
|
|
|
|
2019-01-15 09:24:33 +03:00
|
|
|
// FreeSymbols returns free symbols for the scope.
|
2019-02-02 10:27:29 +03:00
|
|
|
func (t *SymbolTable) FreeSymbols() []*Symbol {
|
2019-01-09 10:17:42 +03:00
|
|
|
return t.freeSymbols
|
|
|
|
}
|
|
|
|
|
2019-03-01 05:41:29 +03:00
|
|
|
// BuiltinSymbols returns builtin symbols for the scope.
|
|
|
|
func (t *SymbolTable) BuiltinSymbols() []*Symbol {
|
|
|
|
if t.parent != nil {
|
|
|
|
return t.parent.BuiltinSymbols()
|
|
|
|
}
|
|
|
|
return t.builtinSymbols
|
|
|
|
}
|
|
|
|
|
2019-01-15 09:24:33 +03:00
|
|
|
// Names returns the name of all the symbols.
|
2019-01-09 10:17:42 +03:00
|
|
|
func (t *SymbolTable) Names() []string {
|
|
|
|
var names []string
|
|
|
|
for name := range t.store {
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *SymbolTable) nextIndex() int {
|
|
|
|
if t.block {
|
|
|
|
return t.parent.nextIndex() + t.numDefinition
|
|
|
|
}
|
|
|
|
return t.numDefinition
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *SymbolTable) updateMaxDefs(numDefs int) {
|
|
|
|
if numDefs > t.maxDefinition {
|
|
|
|
t.maxDefinition = numDefs
|
|
|
|
}
|
|
|
|
if t.block {
|
|
|
|
t.parent.updateMaxDefs(numDefs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-02 10:27:29 +03:00
|
|
|
func (t *SymbolTable) defineFree(original *Symbol) *Symbol {
|
2019-01-09 10:17:42 +03:00
|
|
|
// TODO: should we check duplicates?
|
|
|
|
t.freeSymbols = append(t.freeSymbols, original)
|
2019-02-02 10:27:29 +03:00
|
|
|
symbol := &Symbol{
|
2019-01-09 10:17:42 +03:00
|
|
|
Name: original.Name,
|
|
|
|
Index: len(t.freeSymbols) - 1,
|
|
|
|
Scope: ScopeFree,
|
|
|
|
}
|
|
|
|
t.store[original.Name] = symbol
|
|
|
|
return symbol
|
|
|
|
}
|