### Interactive REPL Usage Source: https://pkg.go.dev/github.com/cosmos72/gomacro Example of starting the gomacro REPL and executing basic Go code. ```text $ gomacro [greeting message...] gomacro> import "fmt" gomacro> fmt.Println("hello, world!") hello, world! 14 // int // error gomacro> ``` -------------------------------- ### Installing gomacro Source: https://pkg.go.dev/github.com/cosmos72/gomacro Command to download, compile, and install the gomacro tool. ```bash go install github.com/cosmos72/gomacro@latest ``` -------------------------------- ### Selection String Examples Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/types Example output formats for selection strings. ```text "field (T) f int" "method (T) f(X) Y" "method expr (T) f(X) Y" ``` -------------------------------- ### MethodSet Output Example Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/types Example output demonstrating the method sets for Celsius types. ```text Method set of temperature.Celsius: method (temperature.Celsius) String() string Method set of *temperature.Celsius: method (*temperature.Celsius) SetF(f float64) method (*temperature.Celsius) String() string ``` -------------------------------- ### Example Info Output Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/types Example output showing recorded facts from a type-checked package, including definitions, references, types, and values. ```text Output: InitOrder: [c = "hello" b = S(c) a = len(b)] Defs and Uses of each named object: builtin len: defined at - used at 6:15 func fib(x int) int: defined at fib.go:8:6 used at 12:20, 12:9 type S string: defined at fib.go:4:6 used at 6:23 type int: defined at - used at 8:12, 8:17 type string: defined at - used at 4:8 var b S: defined at fib.go:6:8 used at 6:19 var c string: defined at fib.go:6:11 used at 6:25 var x int: defined at fib.go:8:10 used at 10:10, 12:13, 12:24, 9:5 Types and Values of each expression: 4: 8 | string | type : string 6:15 | len | builtin : func(string) int 6:15 | len(b) | value : int 6:19 | b | var : fib.S 6:23 | S | type : fib.S 6:23 | S(c) | value : fib.S 6:25 | c | var : string 6:29 | "hello" | value : string = "hello" 8:12 | int | type : int 8:17 | int | type : int 9: 5 | x | var : int 9: 5 | x < 2 | value : untyped bool 9: 9 | 2 | value : int = 2 10:10 | x | var : int 12: 9 | fib | value : func(x int) int 12: 9 | fib(x - 1) | value : int 12: 9 | fib(x-1) - fib(x-2) | value : int 12:13 | x | var : int 12:13 | x - 1 | value : int 12:15 | 1 | value : int = 1 12:20 | fib | value : func(x int) int 12:20 | fib(x - 2) | value : int 12:24 | x | var : int 12:24 | x - 2 | value : int 12:26 | 2 | value : int = 2 ``` -------------------------------- ### Execute program Source: https://pkg.go.dev/github.com/cosmos72/gomacro/_experiments/bytecode_values Executes the program starting at the given instruction pointer. ```go func (p *Prog) Exec(IP int) []r.Value ``` -------------------------------- ### Get instruction source Source: https://pkg.go.dev/github.com/cosmos72/gomacro/_experiments/bytecode_values Retrieves the source value from an instruction. ```go func (i Inst) Src() int ``` -------------------------------- ### Example usage of Fprint Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/printer Demonstrates printing an AST node body to a buffer and formatting the output string. ```go funcAST, fset := parseFunc("example_test.go", "ExampleFprint") var buf bytes.Buffer printer.Fprint(&buf, fset, funcAST.Body) s := buf.String() s = s[1 : len(s)-1] s = strings.TrimSpace(strings.Replace(s, "\n\t", "\n", -1)) fmt.Println(s) ``` -------------------------------- ### Match command prefix Source: https://pkg.go.dev/github.com/cosmos72/gomacro/fast Checks if the command name starts with the provided prefix. ```go func (cmd *Cmd) Match(prefix string) int ``` -------------------------------- ### Example Scope Tree Output Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/types A representation of a scope tree structure as printed by ExampleScope. ```text package "temperature" scope { . const temperature.Boiling temperature.Celsius . type temperature.Celsius float64 . func temperature.FToC(f float64) temperature.Celsius . func temperature.Unused() . func temperature.main() . main.go scope { . . package fmt . . function scope { . . . var freezing temperature.Celsius . . } . } . celsius.go scope { . . package fmt . . function scope { . . . var c temperature.Celsius . . } . . function scope { . . . var f float64 . . } . . function scope { . . . block scope { . . . } . . . block scope { . . . . block scope { . . . . . var x int . . . } . . } . } . } } ``` -------------------------------- ### Get compiler sizes Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/types Retrieves the sizing functions for a specific compiler and architecture. ```go func SizesFor(compiler, arch string) Sizes ``` -------------------------------- ### Initialize Cmd Source: https://pkg.go.dev/github.com/cosmos72/gomacro/cmd Creates a new instance of Cmd. ```go func New() *Cmd ``` -------------------------------- ### Get Methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns a pointer to the method values. Panics if the type is unnamed. ```go func (t Type) GetMethods() *[]r.Value ``` -------------------------------- ### Initialize Cmd instance Source: https://pkg.go.dev/github.com/cosmos72/gomacro/cmd Performs initialization tasks for the Cmd instance. ```go func (cmd *Cmd) Init() ``` -------------------------------- ### ClientSessionCache Get and Put Methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/imports Methods for interacting with the P_crypto_tls_ClientSessionCache proxy. ```go func (P *P_crypto_tls_ClientSessionCache) Get(sessionKey string) (session *tls.ClientSessionState, ok bool) ``` ```go func (P *P_crypto_tls_ClientSessionCache) Put(sessionKey string, cs *tls.ClientSessionState) ``` -------------------------------- ### Create Lit Instance Source: https://pkg.go.dev/github.com/cosmos72/gomacro/base/untyped Constructs a new Lit instance. ```go func MakeLit(kind Kind, val constant.Value, basicTypes *[]xr.Type) Lit ``` -------------------------------- ### Initialize Scanner Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/scanner Prepares the scanner for tokenizing source code. ```go func (s *Scanner) Init(file *etoken.File, src []byte, err ErrorHandler, mode Mode, macroChar rune) ``` -------------------------------- ### Comp.Quasiquote Source: https://pkg.go.dev/github.com/cosmos72/gomacro/fast Expands and compiles ~quasiquote, if Ast starts with it. ```go func (c *Comp) Quasiquote(in Ast) *Expr ``` -------------------------------- ### Package Type and Methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Package wrapper and accessors. ```go type Package types.Package ``` ```go func (pkg *Package) GoPackage() *types.Package ``` ```go func (pkg *Package) Name() string ``` ```go func (pkg *Package) Path() string ``` -------------------------------- ### func (*Prog) Exec Source: https://pkg.go.dev/github.com/cosmos72/gomacro/_experiments/bytecode_values Executes the program starting at the specified instruction pointer. ```APIDOC ## func (*Prog) Exec(IP int) []r.Value ### Description Executes the program starting at the specified instruction pointer. ### Signature `func (p *Prog) Exec(IP int) []r.Value` ``` -------------------------------- ### LoadPackage Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Loads a package from the given path. ```go func (v *Universe) LoadPackage(path string) *Package ``` -------------------------------- ### Get interface representation Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns the value as an interface{}. ```go func (v Value) Interface() interface{} ``` -------------------------------- ### Display usage Source: https://pkg.go.dev/github.com/cosmos72/gomacro/cmd Prints or returns the usage information for the command. ```go func (cmd *Cmd) Usage() error ``` -------------------------------- ### StartSignalHandler Source: https://pkg.go.dev/github.com/cosmos72/gomacro/base Starts a signal handler for the specified signal function. ```APIDOC ## func StartSignalHandler ### Description Starts a signal handler that executes the provided function when a signal is received. ### Signature `func StartSignalHandler(handler func(os.Signal)) chan os.Signal` ``` -------------------------------- ### Get Value type Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns the reflection type of the value. ```go func (v Value) Type() r.Type ``` -------------------------------- ### BadDecl structure and methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/ast2 Structure for BadDecl and its Get method. ```go type BadDecl struct{ X *ast.BadDecl } ``` ```go func (x BadDecl) Get(i int) Ast ``` -------------------------------- ### Get string representation Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns the string representation of the value. ```go func (v Value) String() string ``` -------------------------------- ### Get pointer address Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns the pointer address as a uintptr. ```go func (v Value) Pointer() uintptr ``` -------------------------------- ### Create Default Importer Source: https://pkg.go.dev/github.com/cosmos72/gomacro/base/genimport Initializes a new Importer with the provided output configuration. ```go func DefaultImporter(o *Output) *Importer ``` -------------------------------- ### Get number of fields Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns the number of fields in a struct. ```go func (v Value) NumField() int ``` -------------------------------- ### Parser Methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/parser Methods for configuring, initializing, and executing the parser. ```go func (p *Parser) Configure(mode Mode, macroChar rune) ``` ```go func (p *Parser) Init(fileset *etoken.FileSet, filename string, lineOffset int, src []byte) ``` ```go func (p *Parser) Parse() (list []ast.Node, err error) ``` -------------------------------- ### Get map index Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Retrieves a value from a map by key. ```go func (v Value) MapIndex(key Value) Value ``` -------------------------------- ### Internal Cmp Method Implementation Source: https://pkg.go.dev/github.com/cosmos72/gomacro Example of how basic types implement the Cmp method internally within gomacro. ```go func (a int) Cmp(b int) int { if a < b { return -1 } else if a > b { return 1 } else { return 0 } } ``` -------------------------------- ### Converter methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/types Methods to initialize the converter or perform package conversion. ```go func (c *Converter) Init(universe *Scope) func (c *Converter) Package(g *types.Package) *Package ``` -------------------------------- ### Get length of Value Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns the length of the underlying value. ```go func (v Value) Len() int ``` -------------------------------- ### Construct QName instances Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Functions to create QName instances from various sources. ```go func QName1(q QNameI) QName ``` ```go func QName2(name, pkgpath string) QName ``` ```go func QNameGo(obj types.Object) QName ``` ```go func QNameGo2(name string, pkg *types.Package) QName ``` -------------------------------- ### Get Value kind Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns the reflection kind of the value. ```go func (v Value) Kind() r.Kind ``` -------------------------------- ### Get integer value Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns the underlying int64 value. ```go func (v Value) Int() int64 ``` -------------------------------- ### Env Methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/classic Methods for interacting with the gomacro environment. ```go func (env *Env) ShowPackage(packageName string) ``` ```go func (env *Env) TopEnv() *Env ``` ```go func (env *Env) ValueOf(name string) (value r.Value) ``` -------------------------------- ### func (*Prog) Exec(IP int) []interface{} Source: https://pkg.go.dev/github.com/cosmos72/gomacro/_experiments/bytecode_interfaces Executes the bytecode program starting at the given instruction pointer. ```APIDOC ## func (*Prog) Exec(IP int) []interface{} ### Description Executes the bytecode program starting at the specified instruction pointer (IP). ### Parameters - **IP** (int) - Required - The starting instruction pointer. ### Returns - ([]interface{}) - The resulting state or output of the program execution. ``` -------------------------------- ### Get float value Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns the underlying float64 value. ```go func (v Value) Float() float64 ``` -------------------------------- ### Get complex value Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns the underlying complex128 value. ```go func (v Value) Complex() complex128 ``` -------------------------------- ### Package Methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/types Methods for interacting with and configuring Package objects. ```go func (pkg *Package) Complete() bool ``` ```go func (pkg *Package) Imports() []*Package ``` ```go func (pkg *Package) MarkComplete() ``` ```go func (pkg *Package) Name() string ``` ```go func (pkg *Package) Path() string ``` ```go func (pkg *Package) Scope() *Scope ``` ```go func (pkg *Package) SetImports(list []*Package) ``` ```go func (pkg *Package) SetName(name string) ``` ```go func (pkg *Package) String() string ``` -------------------------------- ### Get capacity of Value Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns the capacity of the underlying value. ```go func (v Value) Cap() int ``` -------------------------------- ### Create a new Hasher Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/typeutil Initializes a new Hasher instance. ```go func MakeHasher() Hasher ``` -------------------------------- ### Get Type Kind Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns the specific kind of the type. ```go func (t Type) Kind() r.Kind ``` -------------------------------- ### Environment and REPL management Source: https://pkg.go.dev/github.com/cosmos72/gomacro/fast Methods for configuring the interpreter environment and running REPL sessions. ```go func (ir *Interp) PrepareEnv() *Env ``` ```go func (ir *Interp) Repl(in *bufio.Reader) ``` ```go func (ir *Interp) ReplStdin() ``` -------------------------------- ### Get map values Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/typeutil Returns a slice of all values in the map. ```go func (m *Map) Values() []interface{} ``` -------------------------------- ### Interpreter Types and Initialization Source: https://pkg.go.dev/github.com/cosmos72/gomacro/classic Core interpreter structures and constructor. ```go type Function struct { // contains filtered or unexported fields } ``` ```go type Interp struct { *Env } ``` ```go func New() *Interp ``` -------------------------------- ### Get map length Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/typeutil Returns the number of entries in the map. ```go func (m *Map) Len() int ``` -------------------------------- ### Display command help Source: https://pkg.go.dev/github.com/cosmos72/gomacro/fast Displays help information for a specific command. ```go func (cmd *Cmd) ShowHelp(g *base.Globals) ``` -------------------------------- ### Get map keys Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/typeutil Returns a slice of all keys in the map. ```go func (m *Map) Keys() []types.Type ``` -------------------------------- ### Access QName properties Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Methods to retrieve name and package path information from a QName. ```go func (q QName) Name() string ``` ```go func (q QName) PkgPath() string ``` ```go func (q QName) String() string ``` -------------------------------- ### Get map hasher Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/typeutil Retrieves the hasher associated with the map. ```go func (m *Map) Hasher() Hasher ``` -------------------------------- ### FileComp Source: https://pkg.go.dev/github.com/cosmos72/gomacro/fast Returns the file compiler instance. ```go func (c *Comp) FileComp() *Comp ``` -------------------------------- ### File Structure and Methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/etoken Represents a source file handle and provides methods for position tracking and source management. ```go type File struct { *token.File // contains filtered or unexported fields } ``` ```go func (f *File) Position(p token.Pos) (pos token.Position) ``` ```go func (f *File) PositionFor(p token.Pos, adjusted bool) (pos token.Position) ``` ```go func (f *File) SetSource(source []string) ``` ```go func (f *File) SetSourceForContent(content []byte) ``` ```go func (f *File) Source(p token.Pos) (line string, pos token.Position) ``` -------------------------------- ### NewPackage Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/types Creates a new Package instance with the specified path and name. ```APIDOC ## func NewPackage(path, name string) *Package ### Description Returns a new Package for the given package path and name. The package is not complete and contains no explicit imports. ``` -------------------------------- ### Method Type and Methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Method structure and related utilities. ```go type Method struct { Name string Pkg *Package Type Type // method type Funs *[]r.Value // (*Funs)[Index] is the method, with receiver as first argument Index int // index for Type.Method FieldIndex []int // embedded fields index sequence for r.Type.FieldByIndex or r.Value.FieldByIndex GoFun *types.Func // for completeness } ``` ```go func MissingMethod(t, tinterf Type) *Method ``` ```go func (m Method) String() string ``` -------------------------------- ### File AST Methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/ast2 Methods for interacting with the File AST wrapper. ```go type File struct{ X *ast.File } ``` ```go func (x File) Append(child Ast) AstWithSlice ``` ```go func (x File) Get(i int) Ast ``` ```go func (x File) Interface() interface{} ``` ```go func (x File) New() Ast ``` ```go func (x File) Node() ast.Node ``` ```go func (x File) Op() token.Token ``` ```go func (x File) Set(i int, child Ast) ``` ```go func (x File) Size() int ``` ```go func (x File) Slice(lo, hi int) AstWithSlice ``` -------------------------------- ### Get Underlying Map Type Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/types Returns the underlying type of the map. ```go func (m *Map) Underlying() Type ``` -------------------------------- ### Define P_os_FileInfo structure and methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/imports Proxy structure for os.FileInfo and its associated methods. ```go type P_os_FileInfo struct { Object interface{} IsDir_ func(interface{}) bool ModTime_ func(interface{}) time.Time Mode_ func(interface{}) fs.FileMode Name_ func(interface{}) string Size_ func(interface{}) int64 Sys_ func(interface{}) any } ``` ```go func (P *P_os_FileInfo) IsDir() bool ``` ```go func (P *P_os_FileInfo) ModTime() time.Time ``` ```go func (P *P_os_FileInfo) Mode() fs.FileMode ``` ```go func (P *P_os_FileInfo) Name() string ``` ```go func (P *P_os_FileInfo) Size() int64 ``` ```go func (P *P_os_FileInfo) Sys() any ``` -------------------------------- ### Get unsigned integer value Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns the underlying uint64 value. ```go func (v Value) Uint() uint64 ``` -------------------------------- ### Configuration Options Source: https://pkg.go.dev/github.com/cosmos72/gomacro/base Bitmask options for controlling REPL behavior and debugging. ```go type Options uint ``` ```go const ( OptCollectDeclarations Options = 1 << iota OptCollectStatements OptCtrlCEnterDebugger // Ctrl+C enters the debugger instead of injecting a panic. requires OptDebugger OptDebugger // enable debugger support. "break" and _ = "break" are breakpoints and enter the debugger OptKeepUntyped OptMacroExpandOnly // do not compile or execute code, only parse and macroexpand it OptModuleImport // if built with Go >= 1.11, import "foo" will use modules OptPanicStackTrace OptTrapPanic OptDebugCallStack OptDebugDebugger // print debug information related to the debugger OptDebugField OptDebugFromReflect OptDebugMacroExpand OptDebugMethod OptDebugParse OptDebugRecover OptDebugQuasiquote OptDebugSleepOnSwitch // to torture-test "switch" implementation for race conditions OptDebugGenerics OptShowCompile OptShowEval OptShowEvalType OptShowMacroExpand OptShowParse OptShowPrompt OptShowTime ) ``` ```go func ParseOptions(str string) Options ``` ```go func (o Options) String() string ``` -------------------------------- ### Get underlying reflect.Value Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns the underlying standard library reflect.Value. ```go func (v Value) ReflectValue() r.Value ``` -------------------------------- ### Evaluate multiple files and directories Source: https://pkg.go.dev/github.com/cosmos72/gomacro/cmd Evaluates a variadic list of files and directories. ```go func (cmd *Cmd) EvalFilesAndDirs(filesAndDirs ...string) error ``` -------------------------------- ### Get number of methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns the number of methods associated with the value. ```go func (v Value) NumMethod() int ``` -------------------------------- ### Get element of Value Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns the value that the interface or pointer points to. ```go func (v Value) Elem() Value ``` -------------------------------- ### Package information Source: https://pkg.go.dev/github.com/cosmos72/gomacro/fast Methods to display information about packages. ```go func (ir *Interp) ShowAsPackage() ``` ```go func (ir *Interp) ShowImportedPackage(name string) ``` ```go func (ir *Interp) ShowPackage(name string) ``` -------------------------------- ### Get User Data Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Retrieves user-supplied data associated with the type. ```go func (t Type) GetUserData(key interface{}) (interface{}, bool) ``` -------------------------------- ### MakeTtyReadline constructor Source: https://pkg.go.dev/github.com/cosmos72/gomacro/base Initializes a new TtyReadline instance. ```go func MakeTtyReadline(historyfile string) (TtyReadline, error) ``` -------------------------------- ### Define P_testing_quick_Generator structure and methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/imports Proxy structure for testing/quick.Generator and its associated methods. ```go type P_testing_quick_Generator struct { Object interface{} Generate_ func(_proxy_obj_ interface{}, rand *rand.Rand, size int) reflect.Value } ``` ```go func (P *P_testing_quick_Generator) Generate(rand *rand.Rand, size int) reflect.Value ``` -------------------------------- ### Get PackageName String Representation Source: https://pkg.go.dev/github.com/cosmos72/gomacro/imports Returns the string representation of a PackageName type. ```go func (name PackageName) String() string ``` -------------------------------- ### Define P_os_DirEntry methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/imports Methods for the P_os_DirEntry proxy structure. ```go func (P *P_os_DirEntry) IsDir() bool ``` ```go func (P *P_os_DirEntry) Name() string ``` ```go func (P *P_os_DirEntry) Type() fs.FileMode ``` -------------------------------- ### Define QName interfaces Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Interfaces and proxy structures for QName compatibility. ```go type QNameI interface { Name() string PkgPath() string } ``` ```go type QNameI_github_com_cosmos72_gomacro_xreflect struct { Object interface{} Name_ func() string PkgPath_ func() string } ``` ```go func (Proxy *QNameI_github_com_cosmos72_gomacro_xreflect) Name() string ``` ```go func (Proxy *QNameI_github_com_cosmos72_gomacro_xreflect) PkgPath() string ``` -------------------------------- ### Get interface data Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns the raw interface data as uintptr array. ```go func (v Value) InterfaceData() [2]uintptr ``` -------------------------------- ### Import Structure and Methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/fast Represents an imported package and provides methods for displaying and string conversion. ```go type Import struct { // model as a combination of CompBinds and EnvBinds, because to support the command 'package PATH' // we must convert Comp+Env to Import and vice-versa. // This has the added benefit of allowing packages to freely mix // interpreted and compiled constants, functions, variables and types. CompBinds *EnvBinds // contains filtered or unexported fields } ``` ```go func (imp *Import) Show(g *CompGlobals, env *Env) ``` ```go func (imp *Import) String() string ``` -------------------------------- ### Binding and Compilation Entry Points Source: https://pkg.go.dev/github.com/cosmos72/gomacro/fast Methods for binding identifiers and initiating compilation of AST nodes. ```go func (c *Comp) Bind(bind *Bind) *Expr ``` ```go func (c *Comp) BindUntyped(kind untyped.Kind, value constant.Value) *Bind ``` ```go func (c *Comp) Compile(in Ast) *Expr ``` ```go func (c *Comp) CompileNode(node ast.Node) *Expr ``` -------------------------------- ### Get Go Type Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns the underlying go/types.Type corresponding to the given type. ```go func (t Type) GoType() types.Type ``` -------------------------------- ### Importing local filesystem packages Source: https://pkg.go.dev/github.com/cosmos72/gomacro Import unpublished packages from local directories using relative or absolute paths. ```go import ( "." // imports the package in current directory ".." // imports the package in parent directory "./some/relative/path" // "./" means relative to current directory "../some/other/relative/path" // "../" means relative to parent directory "/some/absolute/path" // "/" means absolute ) ``` -------------------------------- ### Import packages Source: https://pkg.go.dev/github.com/cosmos72/gomacro/fast Methods to import single or multiple packages into the interpreter environment. ```go func (ir *Interp) ImportPackage(alias PackageName, path string) *Import ``` ```go func (ir *Interp) ImportPackagesOrError(paths map[string]PackageName) (map[string]*Import, error) ``` -------------------------------- ### Get map string representation Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/typeutil Returns a string representation of all map entries. ```go func (m *Map) String() string ``` -------------------------------- ### Package AST Type and Methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/ast2 Definition and methods for the Package wrapper. ```go type Package struct{ X *ast.Package } ``` ```go func (x Package) Get(i int) Ast ``` ```go func (x Package) Interface() interface{} ``` ```go func (x Package) New() Ast ``` -------------------------------- ### Importing standard Go packages Source: https://pkg.go.dev/github.com/cosmos72/gomacro Use standard Go import syntax to include packages, including renaming imports. ```go import "fmt" import ( "io" "net/http" r "reflect" ) ``` -------------------------------- ### Signal Handling Functions Source: https://pkg.go.dev/github.com/cosmos72/gomacro/base Starts or stops a signal handler for the provided signal channel. ```go func StartSignalHandler(handler func(os.Signal)) chan os.Signal func StopSignalHandler(c chan os.Signal) ``` -------------------------------- ### Type Aliases Source: https://pkg.go.dev/github.com/cosmos72/gomacro/base/genimport Definitions for Output and PackageName types. ```go type Output = output.Output type PackageName = imports.PackageName // package default name, or package alias ``` -------------------------------- ### Get ErrorList length Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/scanner Returns the number of errors in the list, implementing the sort interface. ```go func (p ErrorList) Len() int ``` -------------------------------- ### ExecerContext Proxy Implementation Source: https://pkg.go.dev/github.com/cosmos72/gomacro/imports Proxy structure and ExecContext method for database/sql/driver.ExecerContext. ```go type P_database_sql_driver_ExecerContext struct { Object interface{} ExecContext_ func(_proxy_obj_ interface{}, ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) } ``` ```go func (P *P_database_sql_driver_ExecerContext) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) ``` -------------------------------- ### Get Array Length Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns an array type's length. Panics if the type is not an array. ```go func (t Type) Len() int ``` -------------------------------- ### Define and Invoke Generic Functions Source: https://pkg.go.dev/github.com/cosmos72/gomacro Demonstrates the syntax for generic functions using type constraints and explicit instantiation. ```go func Sum#[T: Addable] (args ...T) T { // to create the zero value of T, // one can write 'var sum T' or equivalently 'sum := T()' // Unluckily, that's not enough for math/big numbers, which require // the receiver of method calls to be created with a function `New()` // Once math/big numbers have such method, the following // will be fully general - currently it works only on basic types. sum := T().New() for _, elem := range args { // use the method T.Add(T, T) // // as an optimization, relevant at least for math/big numbers, // also use sum as the receiver where result of Add will be stored // if the method Add has pointer receiver. // // To cover the case where method Add has instead value receiver, // also assign the returned value to sum sum = sum.Add(sum, elem) } return sum } Sum#[int] // returns func(...int) int Sum#[int] (1,2,3) // returns int(6) Sum#[complex64] // returns func(...complex64) complex64 Sum#[complex64] (1.1+2.2i, 3.3) // returns complex64(4.4+2.2i) Sum#[string] // returns func(...string) string Sum#[string]("abc.","def.","xy","z") // returns "abc.def.xyz" ``` -------------------------------- ### Get Field Alignment Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns the alignment in bytes of a value of this type when used as a field in a struct. ```go func (t Type) FieldAlign() int ``` -------------------------------- ### Get string representation of keys Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/typeutil Returns a string representation of the map's key set. ```go func (m *Map) KeysString() string ``` -------------------------------- ### Package AST Methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/ast2 Methods for interacting with the Package AST node wrapper. ```go func (x Package) Node() ast.Node ``` ```go func (x Package) Op() token.Token ``` ```go func (x Package) Set(i int, child Ast) ``` ```go func (x Package) Size() int ``` -------------------------------- ### Env Structure and Methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/_experiments/closure_maps Defines the environment structure and methods for managing bindings and definitions. ```go type Env struct { Binds map[string]r.Value Outer *Env } ``` ```go func NewEnv(outer *Env) *Env ``` ```go func (env *Env) DefineFunc(name string, t r.Type, value r.Value) r.Value ``` ```go func (env *Env) DefineVar(name string, t r.Type, value r.Value) r.Value ``` -------------------------------- ### Get string representation with name Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/typeutil Returns a string representation of a type associated with a specific name. ```go func String2(name string, x types.Type) string ``` -------------------------------- ### Execute main logic Source: https://pkg.go.dev/github.com/cosmos72/gomacro/cmd Runs the main execution logic using the provided arguments. ```go func (cmd *Cmd) Main(args []string) (err error) ``` -------------------------------- ### func LookupSpecial(lit string) Token Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/etoken Maps an identifier starting with '~' to its corresponding keyword token. ```APIDOC ## func LookupSpecial(lit string) Token ### Description LookupSpecial maps an identifier starting with '~' to its keyword token. ### Signature `func LookupSpecial(lit string) Token` ### Parameters - **lit** (string) - The identifier string starting with '~' to look up. ``` -------------------------------- ### Get Explicit Method Count Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns the number of explicitly declared methods, excluding wrapper methods. ```go func (t Type) NumExplicitMethod() int ``` -------------------------------- ### Define ReadOptions type and constants Source: https://pkg.go.dev/github.com/cosmos72/gomacro/base Options for configuring readline behavior. ```go type ReadOptions int ``` ```go const ( ReadOptShowPrompt ReadOptions = 1 << iota ReadOptCollectAllComments // continue until non-comment is found. default is to return comments one by one ) ``` -------------------------------- ### Comp.Parse Source: https://pkg.go.dev/github.com/cosmos72/gomacro/fast Performs a combined Parse and MacroExpandCodeWalk. ```go func (c *Comp) Parse(src string) Ast ``` -------------------------------- ### Get Method by Index Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns the i-th method of an interface or named type. Panics if index is out of range. ```go func (t Type) Method(i int) Method ``` -------------------------------- ### Get string representation of a type Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/typeutil Returns a string representation of the given type, including its receiver if applicable. ```go func String(x types.Type) string ``` -------------------------------- ### Define the Packages variable Source: https://pkg.go.dev/github.com/cosmos72/gomacro/imports/syscall Initializes the global map used to store package definitions. ```go var Packages = make(map[string]Package) ``` -------------------------------- ### Lookup special identifier to keyword token Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/etoken Maps an identifier string starting with '~' to its corresponding keyword token. ```go func LookupSpecial(lit string) Token ``` -------------------------------- ### Comp.TopComp Source: https://pkg.go.dev/github.com/cosmos72/gomacro/fast Returns the top-level compiler instance. ```go func (c *Comp) TopComp() *Comp ``` -------------------------------- ### func (*Parser) Init Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/parser Initializes the parser with source file information. ```APIDOC ## func (*Parser) Init() ### Description Initializes the parser with a file set, filename, line offset, and the source code bytes. ### Signature `func (p *Parser) Init(fileset *etoken.FileSet, filename string, lineOffset int, src []byte)` ``` -------------------------------- ### Get Method Count Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns the number of methods for an interface or named type, with specific behavior for wrapper methods. ```go func (t Type) NumMethod() int ``` -------------------------------- ### Env Type and Constructor Source: https://pkg.go.dev/github.com/cosmos72/gomacro/_experiments/closure_ints Defines the environment structure for variable binding and scope management. ```go type Env struct { Binds []int Outer *Env } ``` ```go func NewEnv(outer *Env) *Env ``` -------------------------------- ### Get Struct Field Count Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns a struct type's field count. Panics if the type is not a struct. ```go func (t Type) NumField() int ``` -------------------------------- ### Interpreter configuration Source: https://pkg.go.dev/github.com/cosmos72/gomacro/fast Configuration structure and constructor for interpreter globals. ```go type IrGlobals struct { base.Globals // contains filtered or unexported fields } ``` ```go func NewIrGlobals() *IrGlobals ``` -------------------------------- ### Run expressions Source: https://pkg.go.dev/github.com/cosmos72/gomacro/fast Execute pre-parsed expressions. ```go func (ir *Interp) RunExpr(e *Expr) ([]xr.Value, []xr.Type) ``` ```go func (ir *Interp) RunExpr1(e *Expr) (xr.Value, xr.Type) ``` -------------------------------- ### Get Type Name Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns the type's name within its package, or an empty string for unnamed types. ```go func (t Type) Name() string ``` -------------------------------- ### P_io_fs_GlobFS Methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/imports Methods for the P_io_fs_GlobFS proxy. ```go func (P *P_io_fs_GlobFS) Glob(pattern string) ([]string, error) ``` ```go func (P *P_io_fs_GlobFS) Open(name string) (fs.File, error) ``` -------------------------------- ### Get Map Key Type Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns a map type's key type. Panics if the type is not a map. ```go func (t Type) Key() Type ``` -------------------------------- ### Get Function Input Count Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns a function type's input parameter count. Panics if the type is not a function. ```go func (t Type) NumIn() int ``` -------------------------------- ### File Source: https://pkg.go.dev/github.com/cosmos72/gomacro/fast Compiles a file, supporting out-of-order declarations. ```go func (c *Comp) File(node *ast.File) ``` -------------------------------- ### P_io_fs_FileInfo Methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/imports Methods for the P_io_fs_FileInfo proxy. ```go func (P *P_io_fs_FileInfo) IsDir() bool ``` ```go func (P *P_io_fs_FileInfo) ModTime() time.Time ``` ```go func (P *P_io_fs_FileInfo) Mode() fs.FileMode ``` ```go func (P *P_io_fs_FileInfo) Name() string ``` ```go func (P *P_io_fs_FileInfo) Size() int64 ``` ```go func (P *P_io_fs_FileInfo) Sys() any ``` -------------------------------- ### Get Total Method Count Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns the total number of methods for an interface or named type, including wrapper methods. ```go func (t Type) NumAllMethod() int ``` -------------------------------- ### Get Function Input Parameter Type Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Returns the type of a function type's i'th input parameter. Panics if the type is not a function or index is out of range. ```go func (t Type) In(i int) Type ``` -------------------------------- ### Goto Source: https://pkg.go.dev/github.com/cosmos72/gomacro/fast Compiles a goto statement. ```go func (c *Comp) Goto(node *ast.BranchStmt) ``` -------------------------------- ### Comp.Or Source: https://pkg.go.dev/github.com/cosmos72/gomacro/fast Compiles a logical OR binary expression. ```go func (c *Comp) Or(node *ast.BinaryExpr, xe *Expr, ye *Expr) *Expr ``` -------------------------------- ### Ast proxy methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/ast2 Methods for interacting with the base Ast proxy. ```go func (Proxy *Ast_github_com_cosmos72_gomacro_ast2) Get(i int) Ast ``` ```go func (Proxy *Ast_github_com_cosmos72_gomacro_ast2) Interface() interface{} ``` ```go func (Proxy *Ast_github_com_cosmos72_gomacro_ast2) New() Ast ``` ```go func (Proxy *Ast_github_com_cosmos72_gomacro_ast2) Op() token.Token ``` ```go func (Proxy *Ast_github_com_cosmos72_gomacro_ast2) Set(i int, child Ast) ``` ```go func (Proxy *Ast_github_com_cosmos72_gomacro_ast2) Size() int ``` -------------------------------- ### Execer Proxy Implementation Source: https://pkg.go.dev/github.com/cosmos72/gomacro/imports Proxy structure and Exec method for database/sql/driver.Execer. ```go func (P *P_database_sql_driver_Execer) Exec(query string, args []driver.Value) (driver.Result, error) ``` -------------------------------- ### Define P_os_Signal structure and methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/imports Proxy structure for os.Signal and its associated methods. ```go type P_os_Signal struct { Object interface{} Signal_ func(interface{}) String_ func(interface{}) string } ``` ```go func (P *P_os_Signal) Signal() ``` ```go func (P *P_os_Signal) String() string ``` -------------------------------- ### Key Type and Methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Type wrapper for map keys. ```go type Key struct { // contains filtered or unexported fields } ``` ```go func MakeKey(t Type) Key ``` ```go func (k Key) Type() Type ``` -------------------------------- ### PkgName Type and Methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/types Represents an imported Go package and provides methods to access its metadata. ```go type PkgName struct { // contains filtered or unexported fields } ``` ```go func NewPkgName(pos token.Pos, pkg *Package, name string, imported *Package) *PkgName ``` ```go func (obj *PkgName) Exported() bool ``` ```go func (obj *PkgName) Id() string ``` ```go func (obj *PkgName) Imported() *Package ``` ```go func (obj *PkgName) Name() string ``` ```go func (obj *PkgName) Parent() *Scope ``` ```go func (obj *PkgName) Pkg() *Package ``` ```go func (obj *PkgName) Pos() token.Pos ``` ```go func (obj *PkgName) String() string ``` ```go func (obj *PkgName) Type() Type ``` -------------------------------- ### Option Type and Constants Source: https://pkg.go.dev/github.com/cosmos72/gomacro/xreflect Option flags for type definitions. ```go type Option uint8 ``` ```go const ( OptDefault Option = iota // type is approximate because it's self-recursive // or part of a recursive cycle OptRecursive // type is still being defined OptIncomplete ) ``` ```go func (o Option) String() string ``` -------------------------------- ### Read method for P_github_com_cosmos72_gomacro_base_Readline Source: https://pkg.go.dev/github.com/cosmos72/gomacro/base Method signature for reading input via the proxy object. ```go func (P *P_github_com_cosmos72_gomacro_base_Readline) Read(prompt string) ([]byte, error) ``` -------------------------------- ### P_io_fs_File Methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/imports Methods for the P_io_fs_File proxy. ```go func (P *P_io_fs_File) Close() error ``` ```go func (P *P_io_fs_File) Read(unnamed0 []byte) (int, error) ``` ```go func (P *P_io_fs_File) Stat() (fs.FileInfo, error) ``` -------------------------------- ### Print AST node with Fprint Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/printer Prints an AST node to the provided output using default configuration settings. ```go func Fprint(output io.Writer, fset *token.FileSet, node interface{}) error ``` -------------------------------- ### Evaluate file Source: https://pkg.go.dev/github.com/cosmos72/gomacro/cmd Evaluates a single Go file. ```go func (cmd *Cmd) EvalFile(filename string) error ``` -------------------------------- ### Import Multiple Packages Source: https://pkg.go.dev/github.com/cosmos72/gomacro/base/genimport Imports multiple packages defined in a map of paths to aliases. ```go func (imp *Importer) ImportPackagesOrError(pathMap map[string]PackageName, enableModule bool) (map[string]*PackageRef, error) ``` -------------------------------- ### Create 16-bit instruction Source: https://pkg.go.dev/github.com/cosmos72/gomacro/_experiments/bytecode_values Constructs an instruction with a 16-bit source value. ```go func Inst16(op Op, dst uint8, src int16) Inst ``` -------------------------------- ### Importing third-party packages on Linux/Mac/BSD Source: https://pkg.go.dev/github.com/cosmos72/gomacro Gomacro automatically downloads and compiles third-party packages when imported. ```bash $ gomacro [greeting message...] gomacro> import ( "gonum.org/v1/gonum/floats"; "gonum.org/v1/plot" ) // debug: running "go get gonum.org/v1/plot gonum.org/v1/gonum/floats" ... go: downloading gonum.org/v1/gonum v0.12.0 go: downloading gonum.org/v1/plot v0.12.0 [ more "go: downloading " messages for dependencies...] go: added gonum.org/v1/gonum v0.12.0 go: added gonum.org/v1/plot v0.12.0 // debug: running "go mod tidy" ... go: downloading golang.org/x/exp v0.0.0-20220827204233-334a2380cb91 go: downloading github.com/go-fonts/latin-modern v0.2.0 go: downloading rsc.io/pdf v0.1.1 go: downloading github.com/go-fonts/dejavu v0.1.0 // debug: compiling plugin "/home/max/go/src/gomacro.imports/gomacro_pid_44092/import_1" ... gomacro> floats.Sum([]float64{1,2,3}) 6 // float64 ``` -------------------------------- ### Package Definition Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/types Defines the Package structure and related utilities. ```go type Package struct { // contains filtered or unexported fields } ``` ```go var Unsafe *Package ``` ```go func NewPackage(path, name string) *Package ``` -------------------------------- ### Comp.SelectorExpr Source: https://pkg.go.dev/github.com/cosmos72/gomacro/fast Compiles foo.bar, i.e. read access to methods, struct fields and imported packages. ```go func (c *Comp) SelectorExpr(node *ast.SelectorExpr) *Expr ``` -------------------------------- ### Evaluate file or directory Source: https://pkg.go.dev/github.com/cosmos72/gomacro/cmd Evaluates the provided path, whether it is a file or a directory. ```go func (cmd *Cmd) EvalFileOrDir(fileOrDir string) error ``` -------------------------------- ### P_database_sql_driver_Execer Proxy Source: https://pkg.go.dev/github.com/cosmos72/gomacro/imports Structure for the database/sql/driver.Execer proxy. ```go type P_database_sql_driver_Execer struct { Object interface{} } ``` -------------------------------- ### P_io_fs_SubFS Methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/imports Methods for the P_io_fs_SubFS proxy. ```go func (P *P_io_fs_SubFS) Open(name string) (fs.File, error) ``` ```go func (P *P_io_fs_SubFS) Sub(dir string) (fs.FS, error) ``` -------------------------------- ### X Expression Type and Constructors Source: https://pkg.go.dev/github.com/cosmos72/gomacro/_experiments/closure_values Core expression type and functions for building expressions. ```go type X func(*Env) (r.Value, []r.Value) func Block(list ...X) X func Const(value interface{}) X func DeclVar(idx int, expr X) X func For(init X, pred XBool, post X, body X) X func If(pred XBool, then, els X) X func IntToX(f XInt) X func Return(exprs ...X) X func ReturnInt(expr XInt) X func Var(upn, idx int) X func VarIncInt(idx int) X func VarSetInt(idx int, expr XInt) X ``` -------------------------------- ### P_io_fs_FS Open Method Source: https://pkg.go.dev/github.com/cosmos72/gomacro/imports Proxy method for opening a file within an io/fs.FS. ```go func (P *P_io_fs_FS) Open(name string) (fs.File, error) ``` -------------------------------- ### Auth Proxy Definition and Methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/imports Structure definition and methods for net/smtp.Auth proxy. ```go type P_net_smtp_Auth struct { Object interface{} Next_ func(_proxy_obj_ interface{}, fromServer []byte, more bool) (toServer []byte, err error) Start_ func(_proxy_obj_ interface{}, server *smtp.ServerInfo) (proto string, toServer []byte, err error) } ``` ```go func (P *P_net_smtp_Auth) Next(fromServer []byte, more bool) (toServer []byte, err error) ``` ```go func (P *P_net_smtp_Auth) Start(server *smtp.ServerInfo) (proto string, toServer []byte, err error) ``` -------------------------------- ### DirEntry Proxy Definition and Methods Source: https://pkg.go.dev/github.com/cosmos72/gomacro/imports Structure definition and methods for os.DirEntry proxy. ```go type P_os_DirEntry struct { Object interface{} Info_ func(interface{}) (fs.FileInfo, error) IsDir_ func(interface{}) bool Name_ func(interface{}) string Type_ func(interface{}) fs.FileMode } ``` ```go func (P *P_os_DirEntry) Info() (fs.FileInfo, error) ``` ```go func (P *P_os_DirEntry) IsDir() { ``` -------------------------------- ### Scanner.Init Source: https://pkg.go.dev/github.com/cosmos72/gomacro/go/scanner Initializes the scanner with the source text, file information, error handler, and scanning mode. ```APIDOC ## func (*Scanner) Init ### Description Prepares the scanner to tokenize the provided source text. It sets the scanner at the beginning of the source and configures error handling and mode settings. ### Signature func (s *Scanner) Init(file *etoken.File, src []byte, err ErrorHandler, mode Mode, macroChar rune) ``` -------------------------------- ### Comp.SelectorPlace Source: https://pkg.go.dev/github.com/cosmos72/gomacro/fast Compiles a.b returning a settable and/or addressable Place. ```go func (c *Comp) SelectorPlace(node *ast.SelectorExpr, opt PlaceOption) *Place ``` -------------------------------- ### Arg Function Source: https://pkg.go.dev/github.com/cosmos72/gomacro/_experiments/closure_ints Retrieves an integer argument from the environment. ```go func Arg(env *Env) int ```