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

gen

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Overview

Package gen turns a notebook's code cells into a Go program: the worker the kernel compiles and drives.

Each cell becomes a function. The names a cell defines at its top level become package-level variables — so every cell, and every function, can read them, and their values outlive the call — which needs their types: gen type-checks the cells one by one in dependency order to learn them. The cells' own text is kept, edited in place (":=" becomes "= ", a final expression is wrapped in a call that shows it), under //line directives that point back into the notebook, so the compiler's errors and a panic's stack name the notebook's lines.

Index

Constants

View Source
const JuliaNotRun = analysis.JuliaNotRun

JuliaNotRun is analysis.JuliaNotRun, the failure of every Julia cell. Julia is a check-only language (analysis.Run): its cells are stubs that fail with it, and the cells that read them say which cell blocks them.

View Source
const RubyModule = analysis.RubyModule

RubyModule is the module RubyPath belongs to.

View Source
const RubyPath = analysis.RubyPath

RubyPath is the import path of the package that runs Ruby cells: the go-scikit Ruby bindings' cells, over the pure-Go Ruby go-embedded-ruby.

View Source
const RuntimePath = "github.com/go-notebook/notebook/runtime"

RuntimePath is the import path of the package the program links.

Variables

This section is empty.

Functions

func RegisterBackend added in v0.7.0

func RegisterBackend(lang string, b Backend)

RegisterBackend registers the Backend of a language that runs. It panics on an empty or duplicate name.

func UnregisterBackend added in v0.7.0

func UnregisterBackend(lang string)

UnregisterBackend removes a backend, for tests.

Types

type Backend added in v0.7.0

type Backend interface {
	// Infer declares the variables of cell i (Context.Declare), or defers it
	// (Context.Defer), and marks it done (Context.Done).
	Infer(c *Context, i int)

	// Write writes cell k's function, named Context.FuncName(k), whose
	// parameter is a *_nb.Cell: see Ruby's in ruby.go.
	Write(c *Context, b *strings.Builder, k int)

	// Runtime is the import name the generated program gives the language's
	// package (analysis.Runtime), and the declarations that follow the
	// program's variables.
	Runtime() (importName, prelude string)
}

A Backend generates the code of a non-Go language's cells that run. Go cells need none; a language that does not run (analysis.Run.Runs false) is check-only and needs none either: its cells are stubs that fail with its Run.NotRun. A language that runs registers its Backend under its name (RegisterBackend), and implements analysis.Runtime for the package the program imports: no edit of gen is needed.

type Code

type Code struct {
	Source string
	Line   int
	// Lang is the cell's language: "go" (or ""), "ruby" or "julia".
	Lang string
}

Code is one code cell: its source and the 1-based line of the notebook where that source starts.

type Context added in v0.7.0

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

Context is what a Backend sees of the program being generated.

func (*Context) Cell added in v0.7.0

func (c *Context) Cell(i int) *analysis.Cell

Cell is cell i's analysis.

func (*Context) Code added in v0.7.0

func (c *Context) Code(i int) Code

Code is cell i's source, language and line.

func (*Context) Declare added in v0.7.0

func (c *Context) Declare(v Var)

Declare adds a variable to the program.

func (*Context) Defer added in v0.7.0

func (c *Context) Defer(i int, reason string)

Defer defers cell i with a reason: its code is not emitted and it fails with the reason when run.

func (*Context) Done added in v0.7.0

func (c *Context) Done(i int)

Done marks the variables of cell i as known.

func (*Context) FuncName added in v0.7.0

func (c *Context) FuncName(i int) string

FuncName is the name of cell i's function in the program.

func (*Context) Inputs added in v0.7.0

func (c *Context) Inputs(i int) []Var

Inputs are the variables of other cells that cell i reads, in the order of its uses.

func (*Context) Options added in v0.7.0

func (c *Context) Options() Options

Options are the options GenerateWith was given.

func (*Context) Path added in v0.7.0

func (c *Context) Path() string

Path is the notebook's path, for position directives.

func (*Context) ResolveType added in v0.7.0

func (c *Context) ResolveType(t string) string

ResolveType writes a Go type, in the syntax of Options.RubyTypes ("*{github.com/go-plot/plot}.Figure"), with the program's import names; "" when it names a package the program cannot see.

func (*Context) Unknown added in v0.7.0

func (c *Context) Unknown(name string)

Unknown records that the Go type of variable name is not known yet: the Go cells that read it are deferred.

func (*Context) Vars added in v0.7.0

func (c *Context) Vars() []Var

Vars are the variables declared so far.

func (*Context) WaitsOnDeferred added in v0.7.0

func (c *Context) WaitsOnDeferred(i int) string

WaitsOnDeferred says why cell i must wait for a deferred cell it reads, "" when it need not.

type Diag

type Diag struct {
	Cell      int // -1 when it is not inside a cell
	Line, Col int // in the notebook
	Msg       string
}

Diag is a problem in the notebook, at a cell and a position in it.

func (Diag) String

func (d Diag) String() string

type Options added in v0.3.0

type Options struct {
	// RubyTypes maps a Ruby cell's variable to the Go type its value had the
	// last time the cell ran, as the cells package names it
	// ("[]float64", "*{github.com/go-plot/plot}.Figure"). A Go cell sees the
	// variable with that type. A variable not in it is declared any, and the
	// Go cells that read it are deferred until it is known.
	RubyTypes map[string]string
}

Options configure GenerateWith.

type Program

type Program struct {
	Source []byte
	Vars   []Var
	Graph  *analysis.Graph
	// Imports maps every package the program imports (but the runtime) to
	// the name it is imported under.
	Imports map[string]string
	// Diags are the notebook's errors. A program with diagnostics is still
	// returned, but will not compile.
	Diags []Diag
	// Deferred maps a cell that cannot be compiled yet to why: it reads a
	// Ruby cell's variable whose Go type is known only once that cell has
	// run (see Options.RubyTypes). It is emitted as a stub that says so.
	Deferred map[int]string
}

Program is a generated worker.

func Generate

func Generate(path string, cells []Code, imp types.Importer) *Program

Generate builds the program for a notebook's code cells. path is the notebook's file name, used in //line directives; imp resolves the packages the cells import.

func GenerateWith added in v0.3.0

func GenerateWith(path string, cells []Code, imp types.Importer, opt Options) *Program

GenerateWith is Generate with options.

type Var

type Var struct {
	Name string
	Type string // Go syntax, qualified with the program's import names
	Cell int
	// Ruby marks a Ruby cell's variable. Its Go holder is named Ident when
	// its name is no Go identifier (len, type): Go cells do not see it.
	Ruby  bool
	Ident string
}

Var is a notebook variable as the program declares it.

Jump to

Keyboard shortcuts

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