pf

package
v0.0.0-...-b8dae14 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: MIT Imports: 20 Imported by: 0

README

This module declares a Service struct which wraps around the compiler/vm, hiding their internals.

This allows someone who imports this module into their own program to do anything to/with a service that the user can via the hub. This is enforced by the fact that the hub itself only sees the Service type and is ignorant of the compiler, vm, parser, etc.

This does mean that the API is rather large and somewhat cumbersome, because bits have been added on ad hoc to meet the specific needs of whatever it is I wanted the hub to do that week. It is also more unstable than most of the project.

Documentation

Index

Constants

This section is empty.

Variables

What we convert to when we convert to `any`.

Functions

func ExplainError

func ExplainError(es []*Error, i int) (string, error)

Provides the answer to `hub why <n>`.

func GetTraceReport

func GetTraceReport(e *err.Error) string

Gets the trace report of a runtime error, in the form of a string that can be passed to the `PrettyString` function for highlighting.

func ToGo

func ToGo[T any](sv *Service, pfValue Value) (T, error)

Wraps ToGoWithType for better ergonomics.

Types

type Error

type Error = err.Error

The representation of a Pipefish set in the `V` field of a `Value` with `T` = `ERROR`.

type InHandler

type InHandler = vm.InHandler

An interface with one method, `Get()`, which supplies a string when the Pipefish code does `get x from Input()`.

type List

type List = vector.Vector

The representation of a Pipefish list in the `V` field of a `Value` with `T` = `LIST`.

type LiteralOutHandler

type LiteralOutHandler = vm.LiteralOutHandler

An OutHandler which serializes the given value and writes it to an io.Writer supplied at its construction.

type Map

type Map = values.Map

The representation of a Pipefish map in the `V` field of a `Value` with `T` = `MAP`.

type OutHandler

type OutHandler = vm.OutHandler

An interface with two methods (1) `Out(v Value)` which takes the value supplied when the Pipefish code says `post x to Output()` and does something with it, presumably serializing it in some way and writing it somewhere; (2) a method `Write(s string)` which if necessary allows the user to write a string to the same place.

type Service

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

func NewService

func NewService() *Service

Returns a new service.

func (*Service) Api

func (sv *Service) Api(title string, fonts values.Map, width int) string

Returns a description of the API in rendered markdown. The `fonts` field is only used for rendering Pipefish code in code blocks, and so may be omitted if you don't want to do that.

func (*Service) CallMain

func (s *Service) CallMain() (Value, error)

Calls the `main` function.

func (*Service) Do

func (sv *Service) Do(line string) (Value, error)

Once the service is initialized, will interpret the string supplied as though it had been entered into the REPL of the service. The error field will be non-nil in the case of a compile-time error. In the case of a runtime error, it will be nil, and the error will be returned as the `Value`.

func (*Service) DumpCode

func (sv *Service) DumpCode(functionName string, showMemory bool) string

func (*Service) ErrorsExist

func (sv *Service) ErrorsExist() (bool, error)

Returns `true` if the last thing the service did produced errors, whether runtime or compile time.

func (*Service) GetErrorReport

func (sv *Service) GetErrorReport() (string, error)

Gets a summary of the errors produced by the last thing the service did, in the form of a string that can be passed to the `PrettyString` function for highlighting.

func (*Service) GetErrors

func (sv *Service) GetErrors() []*Error

Returns the errors produced by the last thing the service did, as a list of things of type `*Error`.

func (*Service) GetFilepath

func (sv *Service) GetFilepath() (string, error)

GetFilepath to the root file of the service.

func (*Service) GetMarkdowner

func (sv *Service) GetMarkdowner(leftMargin string, rightMargin int, fonts values.Map) func(string) string

Gets markdown with appropriate highlighting.

func (*Service) GetSources

func (sv *Service) GetSources() (map[string][]string, error)

Gets the source code of the service as a map from filenames to lists of strings (i.e. lines of source code).

func (*Service) GetTrackingReport

func (sv *Service) GetTrackingReport() (string, error)

Gets a report of the tracking if any, in the form of a string which can be passed to `PrettyString` for highlighting.

func (*Service) GetVariable

func (sv *Service) GetVariable(vname string) (values.Value, error)

Gets the value of a global variable given its name. Unlike using `Do` for the same purpose, this can get the value of private variables.

func (*Service) Highlight

func (sv *Service) Highlight(code []rune, fonts Map) string

This highlights the given string on the assumption that it's Pipefish code, and that the `fonts` map is a theme like in `user/themes.pf“.

func (*Service) InitializeFromCode

func (sv *Service) InitializeFromCode(code string) error

Initializes the service with the source code supplied in the string.

func (*Service) InitializeFromCodeWithStore

func (sv *Service) InitializeFromCodeWithStore(code string, store Map) error

The same as the previous two functions, except that we pass in a map of values to initialize $store. Initializes the service with the source code supplied in the string.

func (*Service) InitializeFromFilepath

func (sv *Service) InitializeFromFilepath(scriptFilepath string) error

Initializes the service with the source code supplied in the file indicated by the filepath.

func (*Service) InitializeFromFilepathWithStore

func (sv *Service) InitializeFromFilepathWithStore(scriptFilepath string, store Map) error

Initializes the service with the source code supplied in the file indicated by the filepath.

func (*Service) IsBroken

func (sv *Service) IsBroken() bool

Returns `true` if the service is uninitialized or failed to compile.

func (*Service) IsClone

func (sv *Service) IsClone(v Value) bool

Returns `true` if the `Value` is a clone.

func (*Service) IsEnum

func (sv *Service) IsEnum(v Value) bool

Returns `true` if the `Value` is an enum.

func (*Service) IsInitialized

func (sv *Service) IsInitialized() bool

Returns `true` if the service is initialized.

func (*Service) IsStruct

func (sv *Service) IsStruct(v Value) bool

Returns `true` if the `Value` is a struct.

func (*Service) MakeLiteralOutHandler

func (sv *Service) MakeLiteralOutHandler(out io.Writer) *LiteralOutHandler

An outhandler which always serializes the value, for use by external services.

func (*Service) MakeTerminalOutHandler

func (sv *Service) MakeTerminalOutHandler() *SimpleOutHandler

Method makes an `OutHandler` which applies Pipefish's `literal` or `string` function to the value and then writes the result to Stdout.

func (*Service) MakeWritingOutHandler

func (sv *Service) MakeWritingOutHandler(out io.Writer) *SimpleOutHandler

Method makes an `OutHandler` which applies Pipefish's `literal` or `string` function to the value and then writes the result to the supplied `io.Writer`.

func (*Service) NeedsUpdate

func (sv *Service) NeedsUpdate() (bool, error)

Checks whether the source code for a service has been changed since it was initialized.

func (*Service) Output

func (sv *Service) Output(v Value)

Outputs a value via the outhandler.

func (*Service) PostHappened

func (sv *Service) PostHappened() bool

Says whether post happened when we called Do.

func (*Service) SerializeApi

func (sv *Service) SerializeApi() string

Describes the API of a service in a custom reverse polish notation format.

TODO, WARNING! This is extremely unstable, it contains things it doesn't need to contain and omits things it should contain.

func (*Service) SetEnv

func (sv *Service) SetEnv(env values.Map) error

func (*Service) SetInHandler

func (sv *Service) SetInHandler(in InHandler) error

Sets an InHandler, i.e. the thing that decides what happens when you do `get x from Input()`.

func (*Service) SetLocalExternalServices

func (sv *Service) SetLocalExternalServices(svs map[string]*Service)

Makes other services visible to the service, as though they were running on the same hub: their `external` declarations can then allow them to use one another as external services.

func (*Service) SetOutHandler

func (sv *Service) SetOutHandler(out vm.OutHandler) error

Sets an OutHandler, i.e. the thing that decides what happens when you do `post x to Output()`.

func (*Service) SetPostHappened

func (sv *Service) SetPostHappened()

func (*Service) SetVariable

func (sv *Service) SetVariable(vname string, ty values.ValueType, v any) error

Sets the value of a global variable given its name. Unlike using `Do` for the same purpose, this can set the value of private variables.

func (*Service) ToGoWithType

func (sv *Service) ToGoWithType(pfValue Value, goType reflect.Type) (any, error)

Tries to turn a given Pipefish value into a given Go type. As it necessarily has return type `any` (plus an error if the coercion is impossible) the value returned will then still need downcasting to the type it was coerced to.

E.g:

func TwoPlusTwo() int {
    v, _ := fooService.Do(`2 + 2`)                   // `v` has type `Value`.
    i := fooService.ToGoWithType(v, reflect.TypeFor[int]())  // `i` has type `any`.
    return i.(int)                                   // We return an integer as required.
}

The error will be non-nil if the coercion is impossible.

func (*Service) ToLiteral

func (sv *Service) ToLiteral(v Value) string

Converts a `Value` to a string using Pipefish's `literal` function.

func (*Service) ToString

func (sv *Service) ToString(v Value) string

Converts a `Value` to a string using Pipefish's `string` function.

func (*Service) TypeNameToType

func (sv *Service) TypeNameToType(s string) (Type, error)

Returns the `Type` associated with a given type name.

func (*Service) TypeToTypeName

func (sv *Service) TypeToTypeName(t Type) (string, error)

Returns the type name associated with a given `Type`.

func (*Service) UnderlyingType

func (sv *Service) UnderlyingType(v Value) Type

Returns the underlying `Type` of a `Value`, i.e. its parent type if it's a clone, otherwise its own type.

func (*Service) WriteSecret

func (sv *Service) WriteSecret(store values.Map, password string) string

Serializes a Map of Values into newline-separated key-value pairs, encrypting if the password is non-empty, and heading the result with "PLAINTEXT\n" if the password is empty.

type Set

type Set = values.Set

The representation of a Pipefish set in the `V` field of a `Value` with `T` = `SET`.

type SimpleInHandler

type SimpleInHandler = vm.SimpleInHandler

An InHandler which just gets an input from an io.Reader supplied at its construction.

func MakeSimpleInHandler

func MakeSimpleInHandler(in io.Reader) *SimpleInHandler

Makes an InHandler which does nothing but get a string from terminal input and return it.

type SimpleOutHandler

type SimpleOutHandler = vm.SimpleOutHandler

An OutHandler which stringifies/serializes the given value and writes it to an io.Writer supplied at its construction.

type TerminalInHandler

type TerminalInHandler = vm.StandardInHandler

An InHandler which supplies a prompt and then gets its input from the terminal.

func MakeTerminalInHandler

func MakeTerminalInHandler(prompt string) *TerminalInHandler

Makes an `InHandler` which will get input from the terminal using the string supplied to prompt the end user.

type Type

type Type = values.ValueType
const (
	UNDEFINED_TYPE          Type = values.UNDEFINED_TYPE
	BLING                   Type = values.BLING
	UNSATISFIED_CONDITIONAL Type = values.UNSATISFIED_CONDITIONAL
	OK                      Type = values.SUCCESSFUL_VALUE
	TUPLE                   Type = values.TUPLE
	ERROR                   Type = values.ERROR
	NULL                    Type = values.NULL
	INT                     Type = values.INT
	BOOL                    Type = values.BOOL
	STRING                  Type = values.STRING
	RUNE                    Type = values.RUNE
	FLOAT                   Type = values.FLOAT
	TYPE                    Type = values.TYPE
	FUNC                    Type = values.FUNC
	PAIR                    Type = values.PAIR
	LIST                    Type = values.LIST
	MAP                     Type = values.MAP
	SET                     Type = values.SET
	LABEL                   Type = values.LABEL
	SNIPPET                 Type = values.SNIPPET
)

Constants representing Pipefish types.

type Value

type Value = values.Value

A struct representing a Pipefish value, consisting of fields `T“, a `Type`, and V, the payload, of type `any`.

Jump to

Keyboard shortcuts

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