knd

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: 2 Imported by: 8

README

xelf/knd

Kind is a bitset that describes language elements and types in a compact and practical way.

Then we have expressions and types as part of the language:

  • lit for literals
  • typ for types
  • sym for symbols
  • tag for key expression pairs
  • tupl for expression lists
  • call for resolved expressions

We want to support unmodified JSON literals:

  • none for null
  • bool for boolean
  • num for number
  • char for string
  • idxr for array
  • keyr for object

We want specialized sub types:

  • num: int, real, bits
  • char: str, raw, uuid, span, time, enum
  • idxr: list
  • keyr: dict, obj

We have functions, type variables and references, and alternative types.

  • meta: alt, var, ref, sel, mod
  • spec: func, form

And some super types:

  • exp: lit, typ, sym, tag, tupl, call
  • prim: bool, num, char
  • cont: list, dict
  • data: prim, cont, obj
  • all: data, typ, spec
  • any: all, none

All individual bits signify concrete types. Abstract and base types use a mask of all the allowed concrete type bits.

For compatibility reasons we use only 32 bits, because all bitwise operations in javascript truncate numbers to 32 bits.

The none kind describes the type of the null literal. It is often used in type alternatives and has a special suffix notation using ?. The corresponding some kind with the ! suffix explicitly flagging the type as not null.

(if (ne <str?> <alt str none>) (fail))
<form@dflt val:@1 default?:@1! @1!>

Kinds can be marked either as lit|T or typ|T. Kinds without prefix are treated as lit|T. One special rule for variables, selections or references is to transform the target type. The lit prefix is usually omitted except for identifying expression kinds or to transform a type.

<form@make typ|@1 … lit|@1>
<form@typof lit|@1 typ|@1>

The none bit and prim bits can be combined to a primitive alternative type that does not need the alt bit. The bit is explicitly used whenever an alternative has an id, ref or a type body. In that case all primitive alternatives are still combined into the kind bitset.

The expression bits sym, tag and call can contain an element type. These bits are used as expression kinds and may in a type context be otherwise used as hints for the automatic resolution process for custom specs.

A conditional spec for example does not want the automatic resolver to evaluate branches but does want the type interference to work:

<form@if ifs:<list|tupl cond:any act:exp|@1> els?:exp|@1 @1>

Documentation

Index

Constants

View Source
const (
	Void = Kind(0)
	Exp  = Lit | Sym | Tag | Tupl | Call
	Meta = Alt | Var | Ref | Sel

	Num  = Int | Real | Bits
	Char = Str | Raw | UUID | Span | Time | Enum
	Prim = Bool | Num | Char
	Cont = List | Dict
	Idxr = List | Obj
	Keyr = Dict | Obj
	Data = Prim | Cont | Obj
	Spec = Func | Form
	All  = Data | Typ | Spec
	Any  = All | None
)

Variables

View Source
var ErrInvalid = fmt.Errorf("invalid")
View Source
var Infos = []Info{
	{"void", Void},
	{"none", None},
	{"some", Some},
	{"lit", Lit},
	{"typ", Typ},
	{"sym", Sym},
	{"tag", Tag},
	{"tupl", Tupl},
	{"call", Call},
	{"bool", Bool},
	{"int", Int},
	{"real", Real},
	{"bits", Bits},
	{"str", Str},
	{"raw", Raw},
	{"uuid", UUID},
	{"span", Span},
	{"time", Time},
	{"enum", Enum},
	{"list", List},
	{"dict", Dict},
	{"obj", Obj},
	{"func", Func},
	{"form", Form},
	{"alt", Alt},
	{"var", Var},
	{"ref", Ref},
	{"sel", Sel},

	{"exp", Exp},
	{"meta", Meta},

	{"num", Num},
	{"char", Char},
	{"prim", Prim},
	{"cont", Cont},
	{"idxr", Idxr},
	{"keyr", Keyr},
	{"data", Data},
	{"spec", Spec},
	{"all", All},
	{"any", Any},
}

Infos is a list of all named kinds.

Functions

func Name

func Name(k Kind) string

Name returns the simple name of kind k or an empty string.

Types

type Info

type Info struct {
	Name string
	Kind Kind
}

type Kind

type Kind uint32

Kind is a bitset describing a language element.

const (
	// modifier bits
	None Kind = 1 << iota
	Some

	// exp bits
	Lit
	Typ
	Sym
	Tag
	Tupl
	Call

	// prim bits
	Bool

	Int
	Real
	Bits

	Str
	Raw
	UUID
	Span
	Time
	Enum

	// cont
	List
	Dict
	Obj

	// spec
	Func
	Form

	// meta
	Alt
	Var
	Ref
	Sel
)

func ParseName

func ParseName(str string) (Kind, error)

Parse returns the kind for str or an error.

func (Kind) Count

func (k Kind) Count() int

func (Kind) IsAlt

func (k Kind) IsAlt() bool

Jump to

Keyboard shortcuts

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