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
- Variables
- func Compile(files []*ast.File, builtins []string) (prog *bytecode.Program, err error)
- func CompileModule(files []*ast.File, src []byte, builtins []string, config *native.Config) (mod *bytecode.Module, err error)
- func CompileWith(files []*ast.File, builtins []string, config *native.Config) (prog *bytecode.Program, err error)
- func EvalBinary(op token.Kind, x, y value.Value) (value.Value, error)
- type Session
Constants ¶
const PreludeName = "<prelude>"
PreludeName is the file name of the prelude in positions and traces.
Variables ¶
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 ¶
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.
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
NewSession starts a session with the prelude.
func (*Session) Declare ¶ added in v0.7.0
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
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.