compiler

package
v0.0.0-...-a199d56 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bytecode

type Bytecode struct {
	Instructions code.Instructions
	Constants    []object.Object
}

Bytecode contains the Instructions our Compiler generated and the Constants the Compiler evaluated

type CompilationScope

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

CompilationScope - Before we start compiling a function's body (enter a new scope) we push a new CompilationScope on to the scopes stack. While compiling inside this scope, the emit method of the compiler will modify only the fields of the current CompilationScope. Once we're done compiling the function, we leave the scope by popping it off the scopes stack and putting the instructions in a new *object.CompiledFunction

type Compiler

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

Compiler defines our compiler with instructions which hold the generated bytecode, constants which serve as our constant pool, and the last and previous instructions

func New

func New() *Compiler

New creates and returns a pointer to a Compiler with initialized instructions & constants

func NewWithState

func NewWithState(s *SymbolTable, constants []object.Object) *Compiler

NewWithState creates a new compiler with the passed in symbol table and constants (needed in REPL)

func (*Compiler) Bytecode

func (c *Compiler) Bytecode() *Bytecode

Bytecode returns a pointer to a Bytecode intialized with our compilers instructions & constants

func (*Compiler) Compile

func (c *Compiler) Compile(node ast.Node) error

Compile walks the AST recursively and compiles nodes

type EmittedInstruction

type EmittedInstruction struct {
	Opcode   code.Opcode
	Position int
}

EmittedInstruction represents an instruction through an opcode and it's position

type Symbol

type Symbol struct {
	Name  string
	Scope SymbolScope
	Index int
}

Symbol - Holds all the necessary info about a symbol - Name, Scope, and Index

type SymbolScope

type SymbolScope string

SymbolScope - Type alias for a string. Used in a Symbol to define it's scope.

const (
	GlobalScope   SymbolScope = "GLOBAL"
	LocalScope    SymbolScope = "LOCAL"
	BuiltinScope  SymbolScope = "BUILTIN"
	FreeScope     SymbolScope = "FREE"
	FunctionScope SymbolScope = "FUNCTION"
)

Define all our scopes

type SymbolTable

type SymbolTable struct {
	Outer       *SymbolTable
	FreeSymbols []Symbol
	// contains filtered or unexported fields
}

SymbolTable holds a "store" which is a map of strings to Symbols, an int of number of definitions, and "Outer" which defines it's parent scope

func NewEnclosedSymbolTable

func NewEnclosedSymbolTable(outer *SymbolTable) *SymbolTable

NewEnclosedSymbolTable takes an outer parent symbol table and returns a pointer to the new enclosed SymbolTable after attaching the outer parent to the new one

func NewSymbolTable

func NewSymbolTable() *SymbolTable

NewSymbolTable creates and returns a pointer to a symbol table initialized with a "store"

func (*SymbolTable) Define

func (s *SymbolTable) Define(name string) Symbol

Define takes a name and creates a symbol with the name, an index, and assigns scope. It then assigns the symbol to the SymbolTable's store, increases numDefinitions and returns the symbol.

func (*SymbolTable) DefineBuiltin

func (s *SymbolTable) DefineBuiltin(index int, name string) Symbol

DefineBuiltin creates and returns a symbol within builtin scope

func (*SymbolTable) DefineFunctionName

func (s *SymbolTable) DefineFunctionName(name string) Symbol

DefineFunctionName creates a new Symbol with FunctionScope and adds it to the s.store

func (*SymbolTable) Resolve

func (s *SymbolTable) Resolve(name string) (Symbol, bool)

Resolve takes a name, looks for it in the SymbolTable's store, and returns it if found along with a boolean representing whether it was found

Jump to

Keyboard shortcuts

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