agora: github.com/PuerkitoBio/agora/runtime Index | Files | Directories

package runtime

import "github.com/PuerkitoBio/agora/runtime"

Package runtime defines the core of the agora language - the supported values, their behaviour, the execution context, modules, and VM.

Index

Package Files

bool.go builtin.go ctx.go doc.go funcdef.go funcval.go funcvm.go module.go nil.go number.go object.go string.go val.go

Constants

const (
    // The string representation of the nil value
    NilString = "nil"
)

Variables

var (
    // The one and only Nil instance
    Nil = null{}
)

func ExpectAtLeastNArgs Uses

func ExpectAtLeastNArgs(n int, args []Val)

ExpectAtLeastNArgs is a utility function for native modules implementation to ensure that the minimum number of arguments required are provided. It panics otherwise, which is the correct way to raise errors in the agora runtime.

func PanicToError Uses

func PanicToError(err *error)

PanicToError is a utility function for modules implementations to catch panics and translate them to an error interface. It should be called in a defer statement, with the address of an error variable (usually a named return value) as argument.

func Type Uses

func Type(v Val) string

Type returns the type of the value, which can be one of the following strings: * string * number * bool * func * object * nil * custom

type Arithmetic Uses

type Arithmetic interface {
    Add(Val, Val) Val
    Sub(Val, Val) Val
    Mul(Val, Val) Val
    Div(Val, Val) Val
    Mod(Val, Val) Val
    Unm(Val) Val
}

Arithmetic defines the methods required to compute all the supported arithmetic operations.

type Bool Uses

type Bool bool

Bool is the representation of the Boolean type. It is equivalent to Go's bool type.

func (Bool) Bool Uses

func (b Bool) Bool() bool

Bool returns the boolean value itself.

func (Bool) Dump Uses

func (b Bool) Dump() string

Dump pretty-prints the value for debugging purpose.

func (Bool) Float Uses

func (b Bool) Float() float64

Float returns 1 if true, 0 if false.

func (Bool) Int Uses

func (b Bool) Int() int64

Int returns 1 if true, 0 if false.

func (Bool) Native Uses

func (b Bool) Native() interface{}

Native returns the bool native Go representation.

func (Bool) String Uses

func (b Bool) String() string

String returns "true" if true, "false" otherwise.

type Comparer Uses

type Comparer interface {
    Cmp(Val, Val) int
}

Comparer defines the method required to compare two Values. Cmp() returns 1 if the first value is greater, 0 if it is equal, and -1 if it is lower.

type Compiler Uses

type Compiler interface {
    Compile(string, io.Reader) (*bytecode.File, error)
}

The Compiler interface defines the required behaviour for a Compiler.

type Converter Uses

type Converter interface {
    Int() int64
    Float() float64
    String() string
    Bool() bool
    Native() interface{}
}

Converter declares the required methods to convert a value to any one of the supported types (except Object and Func).

type Ctx Uses

type Ctx struct {
    // Public fields
    Stdout     io.ReadWriter  // The standard streams
    Stdin      io.ReadWriter  // ...
    Stderr     io.ReadWriter  // ...
    Arithmetic Arithmetic     // The arithmetic processor
    Comparer   Comparer       // The comparison processor
    Resolver   ModuleResolver // The module loading resolver (match a module to a string literal)
    Compiler   Compiler       // The source code compiler
    Debug      bool           // Debug mode outputs helpful messages
    // contains filtered or unexported fields
}

A Ctx represents the execution context. It is self-contained, share-nothing with other contexts. An execution context is *not* thread-safe, it should not be used concurrently. However, different instances of Ctx can be run concurrently, provided their components - Compiler, Resolver, etc. - are distinct instances too or do not rely on shared state or do so in a thread-safe way.

func NewCtx Uses

func NewCtx(resolver ModuleResolver, comp Compiler) *Ctx

NewCtx returns a new execution context, using the provided module resolver and compiler.

func (*Ctx) IsRunning Uses

func (c *Ctx) IsRunning(f Func) bool

IsRunning returns true if the specified function is currently executing.

func (*Ctx) Load Uses

func (c *Ctx) Load(id string) (Module, error)

Load resolves the module identified by the provided identifier, and loads it into memory. It returns a ready-to-run module, or an error.

The sequence for loading, compiling, and bootstrapping execution is the following:

* If id is empty string, return error. * If module is cached (ctx.loadedMods), return the Module, done. * If module is not cached, call ModuleResolver.Resolve(id string) (io.Reader, error) * If Resolve returns an error, return nil, error, done. * If file is already bytecode, just load it into memory using a decoder * If decoder returns an error, return nil, error, done. * Otherwise (if not bytecode) call Compiler.Compile(id string, r io.Reader) (*bytecode.File, error) * If Compile returns an error, return nil, error, done. * Create module from *bytecode.File * Cache module and return, do NOT execute the module.

func (*Ctx) RegisterNativeModule Uses

func (c *Ctx) RegisterNativeModule(m NativeModule)

RegisterNativeModule adds the provided native module to the list of loaded and cached modules in this execution context (replacing any other module with the same ID).

type CyclicDependencyError Uses

type CyclicDependencyError string

Error raised when a cyclic dependency is detected

func NewCyclicDependencyError Uses

func NewCyclicDependencyError(id string) CyclicDependencyError

Create a new CyclicDependencyError.

func (CyclicDependencyError) Error Uses

func (e CyclicDependencyError) Error() string

Error interface implementation.

type Dumper Uses

type Dumper interface {
    Dump() string
}

The Dumper interface defines the required behaviour to pretty-print the values in debug logs.

type EmptyModuleError Uses

type EmptyModuleError string

Error raised when a module has no function defined.

func NewEmptyModuleError Uses

func NewEmptyModuleError(id string) EmptyModuleError

Create a new EmptyModuleError

func (EmptyModuleError) Error Uses

func (e EmptyModuleError) Error() string

Error interface implementation.

type FileResolver Uses

type FileResolver struct{}

A FileResolver is a ModuleResolver that turns the module identifier into a file path to find the matching source code.

func (FileResolver) Resolve Uses

func (f FileResolver) Resolve(id string) (io.Reader, error)

Resolve matches the provided identifier with a source file.

If the identifier has no extension (which is recommended), Resolve looks for files in the following order:

1- .agorac (compiled bytecode) 2- .agoraa (agora assembly code) 3- .agora (agora source code)

TODO : This doesn't work, the Ctx has a single compiler, that may compile assembly or source, but not both. The Resolver should look for compiled bytecode or the same source code as the initial Ctx.Load.

type Func Uses

type Func interface {
    Val
    Call(this Val, args ...Val) Val
}

A Func value in Agora is a Val that also implements the Func interface.

type FuncFn Uses

type FuncFn func(...Val) Val

FuncFn represents the Func signature for native functions.

type Module Uses

type Module interface {
    ID() string
    Run(...Val) (Val, error)
}

The Module interface defines the required behaviours for a Module.

type ModuleNotFoundError Uses

type ModuleNotFoundError string

Error raised when a module ID is not found

func NewModuleNotFoundError Uses

func NewModuleNotFoundError(id string) ModuleNotFoundError

Create a new ModuleNotFoundError.

func (ModuleNotFoundError) Error Uses

func (e ModuleNotFoundError) Error() string

Error interface implementation.

type ModuleResolver Uses

type ModuleResolver interface {
    Resolve(string) (io.Reader, error)
}

A ModuleResolver interface represents the required behaviour for the component responsible for matching a module identifier to actual source code. Various implementations can be provided, for example by loading modules in a database, over http, compressed, secured and signed, etc.

type NativeFunc Uses

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

A NativeFunc represents a Go function exposed to agora.

func NewNativeFunc Uses

func NewNativeFunc(ctx *Ctx, nm string, fn FuncFn) *NativeFunc

NewNativeFunc returns a native function initialized with the specified context, name and function implementation.

func (NativeFunc) Bool Uses

func (f NativeFunc) Bool() bool

Bool returns true.

func (*NativeFunc) Call Uses

func (n *NativeFunc) Call(_ Val, args ...Val) Val

Call executes the native function and returns its return value.

func (NativeFunc) Dump Uses

func (f NativeFunc) Dump() string

Dump pretty-prints the value for debugging purpose.

func (NativeFunc) Float Uses

func (f NativeFunc) Float() float64

Float is an invalid conversion.

func (NativeFunc) Int Uses

func (f NativeFunc) Int() int64

Int is an invalid conversion.

func (*NativeFunc) Native Uses

func (n *NativeFunc) Native() interface{}

Native returns the Go native representation of the native function type.

func (NativeFunc) String Uses

func (f NativeFunc) String() string

String prints the function representation

type NativeModule Uses

type NativeModule interface {
    Module
    SetCtx(*Ctx)
}

A NativeModule is a Module with added behaviour required for supporting native Go modules.

type NoSuchMethodError Uses

type NoSuchMethodError string

This error is raised if a non-existing method is called.

func NewNoSuchMethodError Uses

func NewNoSuchMethodError(m string) NoSuchMethodError

Create a new NoSuchMethodError.

func (NoSuchMethodError) Error Uses

func (e NoSuchMethodError) Error() string

Error interface implementation.

type Number Uses

type Number float64

Number is the representation of the Number type. It is equivalent to Go's float64 type.

func (Number) Bool Uses

func (f Number) Bool() bool

Bool returns true if the float value is non-zero, false otherwise.

func (Number) Dump Uses

func (f Number) Dump() string

Dump pretty-prints the value for debugging purpose.

func (Number) Float Uses

func (f Number) Float() float64

Float returns the float value itself.

func (Number) Int Uses

func (f Number) Int() int64

Int returns the integer part of the float value.

func (Number) Native Uses

func (f Number) Native() interface{}

Native returns the Go native representation of the value.

func (Number) String Uses

func (f Number) String() string

String returns a string representation of the float value.

type Object Uses

type Object interface {
    Val
    Get(Val) Val
    Set(Val, Val)
    Len() Val
    Keys() Val
    // contains filtered or unexported methods
}

The Object interface represents an agora object, which is an associative array. It can get and set keys, retrieve the length, the list of keys, and call methods and meta-methods.

func NewObject Uses

func NewObject() Object

NewObject returns a new instance of an object.

type String Uses

type String string

String is the representation of the String type. It is equivalent to Go's string type.

func (String) Bool Uses

func (s String) Bool() bool

Bool returns true if the string value is not empty, false otherwise.

func (String) Dump Uses

func (s String) Dump() string

Pretty-prints the string value.

func (String) Float Uses

func (s String) Float() float64

Float converts the string representation of a float to a float value. If the string doesn't hold a valid float representation, it panics.

func (String) Int Uses

func (s String) Int() int64

Int converts the string representation of an integer to an integer value. If the string doesn't hold a valid integer representation, it panics.

func (String) Native Uses

func (s String) Native() interface{}

Native returns the Go native representation of the value.

func (String) String Uses

func (s String) String() string

String returns itself.

type TypeError Uses

type TypeError string

The TypeError is raised if an invalid type is used for a specific action.

func NewTypeError Uses

func NewTypeError(t1, t2, op string) TypeError

Create a new TypeError.

func (TypeError) Error Uses

func (te TypeError) Error() string

Error interface implementation.

type Val Uses

type Val interface {
    Converter
}

Val is the representation of a value, any value, in the language. The supported value types are the following: * Number (float64) * String * Bool (bool) * Nil (null) * Object (interface) * Func (interface) * Custom (any other Val impl)

Directories

PathSynopsis
stdlibPackage stdlib provides the standard library for agora.

Package runtime imports 11 packages (graph) and is imported by 15 packages. Updated 2016-07-21. Refresh now. Tools for package owners.