typ

package
v0.0.0-...-9cd5b15 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2024 License: BSD-2-Clause Imports: 10 Imported by: 9

README

xelf/typ

Xelf is a typed language. We use a kind bitset from package knd to describe many details. All types can have a type id and a name, type variables and references use these internally for resolution. Complex types need to store additional information in specialized type bodies.

Types are naturally enclosed in angular brackets. However the brackets can be omitted in contexts where a type is expected and the type description fits in one symbol.

Type alternatives represent type choices and are otherwise used internally for resolution. Nullable types are most common form of type alternative and have a special prefix notation using ?:

  (eq <alt int none> int?)

Type variables, references and selections use the at-sign '@' to separate kind from identifier.

  `@`       type variable with auto id
  `@1`      type variable with id 1
  `num@1`   type variable with constraint num
  `@ref`    type reference
  `num@ref` named num type
  `.sel`    local type selection

Variable and reference types must be instantiated by the type system by calling Sys.Inst, that means variables get fresh ids, and references and selections are resolved.

Types with element bodies are frequently used, we therefor provide a shorthand notation that joins the type and element with the pipe '|' character.

  (eq list|int@1? <list <alt@1 int none>>)
  sym|typ|list|dict|int

Parameter types like obj can have optional fields where names may ends in a question mark '?'. This means if the field has a zero value it can be omitted when serialized or sent to database. This is orthogonal to whether the type is optional.

  (obj name?:str opt?:str? (<> explicit optional (pointer to) string))

Documentation

Overview

Package typ provides a xelf type implementation, parser and inference system.

Index

Constants

This section is empty.

Variables

View Source
var (
	Void = Type{Kind: knd.Void}
	None = Type{Kind: knd.None}
	Bool = Type{Kind: knd.Bool}
	Num  = Type{Kind: knd.Num}
	Int  = Type{Kind: knd.Int}
	Real = Type{Kind: knd.Real}
	Char = Type{Kind: knd.Char}
	Str  = Type{Kind: knd.Str}
	Raw  = Type{Kind: knd.Raw}
	UUID = Type{Kind: knd.UUID}
	Time = Type{Kind: knd.Time}
	Span = Type{Kind: knd.Span}

	Lit    = Type{Kind: knd.Lit}
	Typ    = Type{Kind: knd.Typ}
	VarTyp = Type{Kind: knd.Typ | knd.Var, ID: -1}
	Sym    = Type{Kind: knd.Sym}
	Tag    = Type{Kind: knd.Tag}
	Tupl   = Type{Kind: knd.Tupl}
	Call   = Type{Kind: knd.Call}
	Exp    = Type{Kind: knd.Exp}

	Idxr = Type{Kind: knd.Idxr}
	Keyr = Type{Kind: knd.Keyr}
	List = Type{Kind: knd.List}
	Dict = Type{Kind: knd.Dict}

	Data = Type{Kind: knd.Data}
	Spec = Type{Kind: knd.Spec}
	All  = Type{Kind: knd.All}
	Any  = Type{Kind: knd.Any}
)
View Source
var (
	ErrAssign    = fmt.Errorf("cannot assign")
	ErrIdxBounds = fmt.Errorf("index out of bounds")
)
View Source
var WrapNull = func(t Type) *Wrap { return &Wrap{Typ: t} }

Functions

This section is empty.

Types

type AltBody

type AltBody struct {
	Alts []Type
}

AltBody contains compound type alternatives.

func (*AltBody) EqualBody

func (b *AltBody) EqualBody(o Body, h Hist) bool

type AstVal

type AstVal struct{ ast.Ast }

func (*AstVal) As

func (v *AstVal) As(t Type) (LitVal, error)

func (*AstVal) Assign

func (v *AstVal) Assign(o LitVal) error

func (*AstVal) MarshalJSON

func (v *AstVal) MarshalJSON() ([]byte, error)

func (*AstVal) Mut

func (v *AstVal) Mut() LitMut

func (*AstVal) New

func (v *AstVal) New() LitMut

func (*AstVal) Nil

func (v *AstVal) Nil() bool

func (*AstVal) Parse

func (v *AstVal) Parse(a ast.Ast) error

func (*AstVal) Print

func (v *AstVal) Print(p *bfr.P) error

func (*AstVal) Ptr

func (v *AstVal) Ptr() interface{}

func (*AstVal) String

func (v *AstVal) String() string

func (*AstVal) Type

func (v *AstVal) Type() Type

func (*AstVal) UnmarshalJSON

func (v *AstVal) UnmarshalJSON(b []byte) (err error)

func (*AstVal) Value

func (v *AstVal) Value() LitVal

func (*AstVal) Zero

func (v *AstVal) Zero() bool

type Body

type Body interface {
	EqualBody(Body, Hist) bool
}

Body contains additional type information.

type BodyPair

type BodyPair struct{ A, B Body }

type Const

type Const struct {
	Name string
	Key  string
	Val  int64
}

Const describes a named constant value for an enum or bits type.

func C

func C(name string, v int64) Const

type ConstBody

type ConstBody struct {
	Consts []Const
}

ConstBody contains a name and a list of constants for the enum and bits types.

func (*ConstBody) EqualBody

func (b *ConstBody) EqualBody(o Body, h Hist) bool

func (*ConstBody) FindKeyIndex

func (b *ConstBody) FindKeyIndex(key string) int

func (*ConstBody) FindValIndex

func (b *ConstBody) FindValIndex(val int64) int

type EditFunc

type EditFunc func(e *Editor) (Type, error)

EditFunc edits a type and returns the result or an error. A copy editor expects returned types that differ from the input to be already copied.

type Editor

type Editor struct {
	Type
	Parent *Editor
	// contains filtered or unexported fields
}

Editor is type context used to edit type. It references the parents type context.

type Hist

type Hist []BodyPair

type LitMut

type LitMut interface {
	LitVal
	// New returns a fresh mutable value of the same type or an error.
	New() LitMut
	// Ptr returns a pointer to the underlying value for interfacing with other go tools.
	Ptr() interface{}
	// Assign assigns the given value to this mutable or returns an error.
	Assign(LitVal) error
	// Parse reads the given ast into this mutable or returns an error.
	// The registry parameter is strictly optional, proxies should bring a registry if required.
	Parse(ast.Ast) error
}

LitMut is the common interface of all mutable literal values see lit.Mut for more information. Mutable values should have an UnmarshalJSON method unless the base type is natively supported. This interface does in principle belong to the lit package.

type LitVal

type LitVal interface {
	Type() Type
	// Nil returns whether this is a null value.
	Nil() bool
	// Zero returns whether this is a zero value.
	Zero() bool
	// Value returns a simple value restricted to these types: Null, Bool, Int, Real, Str, Raw,
	// UUID, Time, Span, Type, Idxr, Keyr and *SpecRef.
	Value() LitVal
	// Mut returns the effective mutable itself or a new mutable for this value.
	Mut() LitMut
	// As returns the same or a new value with the given type or an error.
	// The value type must be convertible to the new type and the value conversion must succeed.
	As(Type) (LitVal, error)
	// String returns a string content for char literals and xelf format for other literals.
	// Use bfr.String(v) to get quoted char literals.
	String() string
	// Print writes this literal to the given printer or returns an error.
	Print(*bfr.P) error
	// MarshalJSON returns the literal as json bytes
	MarshalJSON() ([]byte, error)
}

LitVal is the common interface of all literal values see lit.Val for more information. This interface does in principle belong to the lit package.

type Lookup

type Lookup = func(string) (Type, error)

Lookup is the type registry interface used to look up type references.

type Null

type Null struct{}

func (Null) As

func (Null) As(t Type) (LitVal, error)

func (Null) Len

func (Null) Len() int

func (Null) MarshalJSON

func (Null) MarshalJSON() ([]byte, error)

func (Null) Mut

func (Null) Mut() LitMut

func (Null) Nil

func (Null) Nil() bool

func (Null) Print

func (Null) Print(p *bfr.P) error

func (Null) String

func (Null) String() string

func (Null) Type

func (Null) Type() Type

func (Null) Value

func (Null) Value() LitVal

func (Null) Zero

func (Null) Zero() bool

type Param

type Param struct {
	Name string
	Key  string
	Type
}

Param describes a obj field or spec parameter.

func P

func P(name string, t Type) Param

func (Param) IsOpt

func (p Param) IsOpt() bool

type ParamBody

type ParamBody struct {
	Params []Param
}

ParamBody contains a name and a list of parameters for obj and spec types.

func (*ParamBody) EqualBody

func (b *ParamBody) EqualBody(o Body, h Hist) bool

func (*ParamBody) FindKeyIndex

func (b *ParamBody) FindKeyIndex(key string) int

type Sys

type Sys struct {
	MaxID int32
	Map   map[int32]Type
}

Sys is the resolution context used to instantiate, bind, update and unify types. Type unification is part of sys, because it needs close access to the type variable bindings.

func NewSys

func NewSys() *Sys

NewSys creates and returns an empty type system using the given type registry.

func (*Sys) Bind

func (sys *Sys) Bind(t Type) Type

Bind binds type t using an existing id or sets a new id and returns the update type.

func (*Sys) Free

func (c *Sys) Free(t Type, res []Type) []Type

Free appends all unbound type variables in t to res and returns the result.

func (*Sys) Get

func (sys *Sys) Get(id int32) Type

Get returns the bound type for id or void.

func (*Sys) Inst

func (sys *Sys) Inst(lup Lookup, t Type) (Type, error)

Inst instantiates all vars in t for sys and returns the result or an error.

func (*Sys) Unify

func (sys *Sys) Unify(t, h Type) (_ Type, err error)

Unify unifies type t and h and returns the result or an error. Type unification in this context means that we have two types that should describe the same type. We then check where these descriptions overlap and use the result instead of the input types.

func (*Sys) Update

func (sys *Sys) Update(t Type) (Type, error)

Update updates all vars and refs in t with the currently bound types and returns the result.

type Type

type Type struct {
	Kind knd.Kind
	ID   int32
	Ref  string
	Body
}

Type describes the shape of a xelf expression, literal or value.

func Alt

func Alt(ts ...Type) Type

func Bits

func Bits(n string, cs ...Const) Type

func CallOf

func CallOf(t Type) Type

func Clone

func Clone(r Type) Type

func ContEl

func ContEl(t Type) Type

func Deopt

func Deopt(t Type) Type

func DictOf

func DictOf(t Type) Type

func Edit

func Edit(t Type, f EditFunc) (Type, error)

Copy edits type t and all offspring types with f and returns the result or an error.

func El

func El(t Type) Type

func ElemTupl

func ElemTupl(t Type) Type

func Enum

func Enum(n string, cs ...Const) Type

func Form

func Form(name string, ps ...Param) Type

func Func

func Func(name string, ps ...Param) Type

func IdxrOf

func IdxrOf(t Type) Type

func KeyrOf

func KeyrOf(t Type) Type

func Last

func Last(t Type) Type

Last returns the last element type if t is a list or dict type otherwise t is returned as is.

func ListOf

func ListOf(t Type) Type

func LitOf

func LitOf(t Type) Type

func Obj

func Obj(n string, ps ...Param) Type

func Opt

func Opt(t Type) Type

func ParamTupl

func ParamTupl(ps ...Param) Type

func Parse

func Parse(str string) (Type, error)

Parse parses str and returns a type or an error.

func ParseAst

func ParseAst(a ast.Ast) (Type, error)

ParseAst parses a as type and returns it or an error.

func ParseSym

func ParseSym(raw string, src ast.Src) (Type, error)

func Read

func Read(r io.Reader, name string) (Type, error)

Read parses named reader r and returns a type or an error.

func Ref

func Ref(name string) Type

func Res

func Res(t Type) Type

func Sel

func Sel(sel string) Type

func Select

func Select(t Type, path string) (Type, error)

Select reads path and returns the selected type from t or an error.

func SelectIdx

func SelectIdx(t Type, idx int) (Type, error)

func SelectKey

func SelectKey(t Type, key string) (Type, error)

func SelectList

func SelectList(t Type, p cor.Path) (r Type, err error)

func SelectPath

func SelectPath(t Type, path cor.Path) (r Type, err error)

SelectPath returns the selected type from t or an error.

func SymOf

func SymOf(t Type) Type

func TagOf

func TagOf(t Type) Type

func ToType

func ToType(v LitVal) (t Type, err error)

func TuplEl

func TuplEl(t Type) (Type, int)

func TypOf

func TypOf(t Type) Type

func Var

func Var(id int32, t Type) Type

func WithID

func WithID(id int32, t Type) Type

func WithRef

func WithRef(r string, t Type) Type

func (Type) As

func (t Type) As(o Type) (LitVal, error)

func (*Type) Assign

func (t *Type) Assign(p LitVal) error

func (Type) AssignableTo

func (t Type) AssignableTo(dst Type) bool

AssignableTo returns whether *all* values represented by type t can be assigned to dst.

func (Type) ConvertibleTo

func (t Type) ConvertibleTo(dst Type) bool

ConvertibleTo returns whether *any* value represented by type t can be assigned to dst. That means char is convertible to time, but str is not.

func (Type) Equal

func (t Type) Equal(o Type) bool

Equal returns whether type t and o are identical.

func (*Type) EqualBody

func (t *Type) EqualBody(b Body, h Hist) bool

A *Type itself can be used as element body for expression and container types.

func (Type) EqualHist

func (t Type) EqualHist(o Type, h Hist) bool

func (Type) MarshalJSON

func (t Type) MarshalJSON() ([]byte, error)

func (Type) Mut

func (t Type) Mut() LitMut

func (*Type) New

func (*Type) New() LitMut

func (Type) Nil

func (Type) Nil() bool

func (*Type) Parse

func (t *Type) Parse(a ast.Ast) error

func (Type) Print

func (t Type) Print(b *bfr.P) error

func (*Type) Ptr

func (t *Type) Ptr() interface{}

func (Type) ResolvableTo

func (t Type) ResolvableTo(dst Type) bool

ResolvableTo returns whether the resolved value of t is convertible to the resolved dest. That call|char, call or exp are all possibly resolvable to time, but not call|str.

func (Type) String

func (t Type) String() string

func (Type) Type

func (t Type) Type() Type

func (*Type) UnmarshalJSON

func (t *Type) UnmarshalJSON(b []byte) error

func (Type) Value

func (t Type) Value() LitVal

func (Type) Zero

func (t Type) Zero() bool

type Wrap

type Wrap struct {
	Typ Type
	OK  bool
	Val LitMut
}

Wrap is versatile literal value wrapper. It can generalize the type of a mutable value. It does also provide automatic optional behaviour, a mutable 'any' value and lazy value support.

func (Wrap) As

func (w Wrap) As(t Type) (LitVal, error)

func (*Wrap) Assign

func (w *Wrap) Assign(v LitVal) error

func (*Wrap) MarshalJSON

func (w *Wrap) MarshalJSON() ([]byte, error)

func (*Wrap) Mut

func (w *Wrap) Mut() LitMut

func (Wrap) New

func (w Wrap) New() (v LitMut)

func (*Wrap) Nil

func (w *Wrap) Nil() bool

func (*Wrap) Parse

func (w *Wrap) Parse(a ast.Ast) error

func (*Wrap) Print

func (w *Wrap) Print(p *bfr.P) error

func (*Wrap) Ptr

func (w *Wrap) Ptr() interface{}

func (*Wrap) String

func (w *Wrap) String() string

func (*Wrap) Type

func (w *Wrap) Type() Type

func (*Wrap) UnmarshalJSON

func (w *Wrap) UnmarshalJSON(b []byte) error

func (*Wrap) Unwrap

func (w *Wrap) Unwrap() LitVal

func (*Wrap) Value

func (w *Wrap) Value() LitVal

func (*Wrap) Zero

func (w *Wrap) Zero() bool

Jump to

Keyboard shortcuts

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