Help us shape the future of Go! Take the 2026 Go Developer Survey

compiler

package
v0.38.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package compiler translates syntax trees into bytecode for the stack machine. It resolves names (locals, parameters, captured variables, members, then top-level names), checks the language's rules (the "Rule:" comments of syntax.ebnf), evaluates constants and generates the code of every function, member function and function literal, together with the prelude that defines class Error.

Index

Constants

View Source
const PreludeName = "<prelude>"

PreludeName is the file name of the prelude in positions and traces.

Variables

View Source
var Fuse = true

Fuse makes the compiler use the instructions that do the work of several (ARITH_LK, CMP_JUMP_LK, ARITH_LL, CMP_JUMP_LL, ARITH_K, CMP_JUMP_K, INDEX_LL, GET_MEMBER_L, ADD_LOCAL, INC_LOCAL, DEC_LOCAL). Tests turn it off to check that a program does the same without them.

Functions

func Compile

func Compile(files []*ast.File, builtins []string) (prog *bytecode.Program, err error)

Compile compiles the files of one program, in order. builtins lists the names of the built-in functions. A file with a Module name belongs to that module; a module must come before the files that use it (the loader orders them so).

func CompileModule added in v0.7.0

func CompileModule(files []*ast.File, src []byte, builtins []string, config *native.Config) (mod *bytecode.Module, err error)

CompileModule compiles the last of files, a module (its File.Module is set) whose source is src, on its own; the files before it are the modules it uses.

func CompileWith added in v0.2.0

func CompileWith(files []*ast.File, builtins []string, config *native.Config) (prog *bytecode.Program, err error)

CompileWith is Compile for a program limited by config: its own native modules, the registered modules it may use, the built-ins it may not.

func EvalBinary added in v0.28.0

func EvalBinary(op token.Kind, x, y value.Value) (value.Value, error)

EvalBinary applies a binary operator (not && or ||) to two values, as the program would: for constant expressions, and the debugger's.

Types

type Session added in v0.7.0

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

Session compiles a program piece by piece, as kukel repl reads it: each piece adds to the same Program, so that a VM can run the pieces one after the other and keep its state (vm.Extend). A piece is declarations, or statements; a local variable declared by a statement at the top level of a piece is a variable of the session, a global of the program.

A piece with an error adds nothing: the session compiles the pieces before it again, into the same indexes.

func NewSession added in v0.7.0

func NewSession(builtins []string, config *native.Config) (*Session, error)

NewSession starts a session with the prelude.

func (*Session) Declare added in v0.7.0

func (s *Session) Declare(files []*ast.File, input *ast.File) (*bytecode.Function, error)

Declare compiles declarations: the files of modules (those already in the session are skipped) and of the program, then the declarations typed at the prompt (input, or nil), which share one scope. It gives the function that runs the initializers of the new global variables.

func (*Session) Program added in v0.7.0

func (s *Session) Program() *bytecode.Program

Program is the program compiled so far. After an error it is a new Program, with the same globals, classes and functions as before.

func (*Session) Statements added in v0.7.0

func (s *Session) Statements(name string, stmts []ast.Stmt) (fn *bytecode.Function, show bool, err error)

Statements compiles statements typed at the prompt into a function that runs them. A local variable declared at their top level becomes a variable of the session (declared again, it keeps its slot). If show is set, the last statement is an expression other than an assignment: the function returns its value.

Jump to

Keyboard shortcuts

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