mtcl

package module
v0.0.0-...-0a82db0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2023 License: BSD-2-Clause Imports: 15 Imported by: 0

README

mtcl

mtcl is a small Tcl-like interpreter written primarily limited DSL use. It is not highly optimized, as the interpreter is just AST-walking for the time being. Currently, the language can be used for a handful of basic things (e.g., arithmetic functions are not built-in yet and only integers are supported for numerics right now), and is made specifically to ensure that more advanced features (such as user-defined functions, variables, and control flow like if and foreach) can be removed or replaced as needed for a given purpose.

Stability

This project is not considered stable and if you end up using it for anything, it is highly recommended you pin your use to a particular commit. While things are early I'm going to frequently make breaking changes that could inconvenience a regular user. Commit history is not guaranteed to be organized during early commits either. mtcl is still very much in the hacking-on-it phase.

License

mtcl is licensed under the 2-Clause BSD license (SPDX BSD-2-Clause). A copy of this license is included with the source code under LICENSE.txt.

Documentation

Index

Constants

View Source
const UnknownCmd = `*unknown*`

Variables

View Source
var (
	ErrNotFound    = errors.New("not found")
	ErrVarNotFound = fmt.Errorf("variable %w", ErrNotFound)
	ErrCmdNotFound = fmt.Errorf("command %w", ErrNotFound)
)

Functions

func IsEmpty

func IsEmpty(val Value) bool

func Prelude

func Prelude() map[string]Cmd

func Strings

func Strings[Slice ~[]E, E fmt.Stringer](vals Slice) []string

func Truthy

func Truthy(val Value) bool

Types

type Access

type Access struct {
	Access string `json:"access"`

	Braced bool        `json:"braced,omitempty"` // True if variable name enclosed in curly braces.
	Tok    token.Token `json:"-"`
}

Access is a variable access denoted as `$var` or `${var}`.

func (*Access) String

func (a *Access) String() string

func (*Access) Token

func (a *Access) Token() token.Token

type Block

type Block struct {
	Block []*Command `json:"block"`

	StartTok token.Token `json:"-"` // '[' token
	EndTok   token.Token `json:"-"` // ']' token
}

A Block is a set of Commands enclosed in square brackets, such as `[concat a b]`.

func (*Block) String

func (b *Block) String() string

func (*Block) Token

func (b *Block) Token() token.Token

type Bool

type Bool bool
const False Bool = false
const True Bool = true

func (Bool) Expand

func (b Bool) Expand() Values

func (Bool) Len

func (Bool) Len() int

func (Bool) String

func (b Bool) String() string

func (Bool) Type

func (b Bool) Type() string

type Cmd

type Cmd interface {
	Call(interp *Interp, args Values) (Values, error)
}

type CmdExprFunc

type CmdExprFunc func(interp *Interp, args []Expr) (Values, error)

func (CmdExprFunc) Call

func (cmd CmdExprFunc) Call(interp *Interp, args Values) (Values, error)

type CmdFunc

type CmdFunc func(interp *Interp, args Values) (Values, error)

func (CmdFunc) Call

func (cmd CmdFunc) Call(interp *Interp, args Values) (Values, error)

type Command

type Command struct {
	Command Expr   `json:"command"`
	Params  []Expr `json:"params"`
}

func (*Command) String

func (c *Command) String() string

func (*Command) Token

func (c *Command) Token() token.Token

type Error

type Error struct {
	Err error
}

func (*Error) Error

func (e *Error) Error() string

func (*Error) Expand

func (e *Error) Expand() Values

func (*Error) Len

func (*Error) Len() int

func (*Error) String

func (e *Error) String() string

func (*Error) Type

func (e *Error) Type() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

type EvalErrorFunc

type EvalErrorFunc func(Values, error) (values Values, err error, exit bool)

type Expr

type Expr interface {
	// Token returns the token from the first part of the Expr. The
	// concrete type of the Expr may contain additional tokens identifying
	// the remaining tokens making up the Expr.
	Token() token.Token
	// String returns the Expr as a string that is equivalent in value
	// to the source tokens (with differences allowed for comments
	// and whitespace).
	String() string
}

type Func

type Func struct {
	Fn    Cmd
	Binds Values
}

func (*Func) Call

func (fn *Func) Call(tcl *Interp, args Values) (Values, error)

func (*Func) Expand

func (fn *Func) Expand() Values

func (*Func) Len

func (*Func) Len() int

func (*Func) String

func (*Func) String() string

func (*Func) Type

func (*Func) Type() string

type Int

type Int struct {
	big.Int
}

func NewInt

func NewInt(x int) *Int

func (*Int) Expand

func (n *Int) Expand() Values

func (*Int) Len

func (*Int) Len() int

func (*Int) Type

func (n *Int) Type() string

type Interp

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

func NewInterp

func NewInterp() *Interp

func (*Interp) Bind

func (tcl *Interp) Bind(name string, cmd Cmd)

func (*Interp) BindCmds

func (tcl *Interp) BindCmds(cmds map[string]Cmd)

func (*Interp) Call

func (tcl *Interp) Call(name string, args ...Value) (Values, error)

func (*Interp) Cmd

func (tcl *Interp) Cmd(name string) (Cmd, error)

func (*Interp) Do

func (tcl *Interp) Do(e Expr) (result Values, err error)

func (*Interp) Eval

func (tcl *Interp) Eval(e Expr, lookup *Interp, handleErr EvalErrorFunc) (result Values, err error)

func (*Interp) Fork

func (tcl *Interp) Fork() *Interp

func (*Interp) Global

func (tcl *Interp) Global(name string) error

func (*Interp) GlobalScope

func (tcl *Interp) GlobalScope() *Interp

func (*Interp) Parent

func (tcl *Interp) Parent(nth uint) (*Interp, error)

func (*Interp) Return

func (tcl *Interp) Return() (*Interp, error)

func (*Interp) SetPrelude

func (tcl *Interp) SetPrelude(cmds map[string]Cmd)

func (*Interp) SetVar

func (tcl *Interp) SetVar(name string, vals Values)

func (*Interp) Unset

func (tcl *Interp) Unset(name string)

func (*Interp) Upvar

func (tcl *Interp) Upvar(name string) error

func (*Interp) Var

func (tcl *Interp) Var(name string) (Values, error)

func (*Interp) VarStorage

func (tcl *Interp) VarStorage(name string) (*Values, error)

type Iterable

type Iterable interface {
	Value
	Iterator() Iterator
}

type Iterator

type Iterator func(vals Values) (bool, error)

func EmptyIterator

func EmptyIterator() Iterator

func OnceIterator

func OnceIterator(val Value) Iterator

type ListExpr

type ListExpr struct {
	List []Expr `json:"list"`

	StartTok token.Token `json:"-"` // '(' token
	EndTok   token.Token `json:"-"` // ')' token
}

A ListExpr is a sequence of expressions that results in a list. This is functionally equivalent to calling [list exprs...] in the source script, assuming [list] is provided..

func (*ListExpr) String

func (l *ListExpr) String() string

func (*ListExpr) Token

func (l *ListExpr) Token() token.Token

type Literal

type Literal struct {
	Literal string      `json:"literal"`
	Tok     token.Token `json:"-"`
}

A Literal is literal text that has no evaluated form other than itself.

func (*Literal) String

func (l *Literal) String() string

func (*Literal) Token

func (l *Literal) Token() token.Token

type Map

type Map map[String]Value

func (Map) Expand

func (m Map) Expand() Values

func (Map) Iterator

func (m Map) Iterator() Iterator

func (Map) Len

func (m Map) Len() int

func (Map) String

func (m Map) String() string

func (Map) Type

func (m Map) Type() string

type OnceIterable

type OnceIterable struct {
	Value
}

func (OnceIterable) Iterator

func (o OnceIterable) Iterator() Iterator

type Parser

type Parser struct {
	LogFunc func(...any)
	// contains filtered or unexported fields
}

func NewParser

func NewParser(r TokenReader) *Parser

func (*Parser) Parse

func (p *Parser) Parse() (cmds []*Command, err error)

func (*Parser) ParseCommand

func (p *Parser) ParseCommand() (cmd *Command, err error)

type QuoteString

type QuoteString struct {
	QuoteString *Word `json:"quote_string"`
}

A QuoteString is a string starting and ending in double quotes made up of Words, Accesses, and Blocks.

func (*QuoteString) String

func (qs *QuoteString) String() string

func (*QuoteString) Token

func (qs *QuoteString) Token() token.Token

type RawString

type RawString struct {
	RawString string      `json:"raw_string"`
	Tok       token.Token `json:"-"`

	// Exprs contains parsed commands from the RawString. May be empty if the string cannot be parsed as a set of expressions.
	Cmds []*Command `json:"cmds"`
}

A RawString is a string wrapped in curly braces that has no interpreted value other than itself, minus escaped curly braces.

func (*RawString) String

func (rs *RawString) String() string

func (*RawString) Token

func (rs *RawString) Token() token.Token

type ReturnCode

type ReturnCode int
const (
	ReturnOK       ReturnCode = 0
	ReturnError    ReturnCode = 1
	ReturnOuter    ReturnCode = 2
	ReturnBreak    ReturnCode = 3
	ReturnContinue ReturnCode = 4
)

func (ReturnCode) Error

func (rc ReturnCode) Error() string

type Seq

type Seq struct {
	Start *Int
	End   *Int
	Step  *Int
}

func (*Seq) Expand

func (s *Seq) Expand() (values Values)

func (*Seq) Iterator

func (s *Seq) Iterator() Iterator

func (*Seq) Len

func (s *Seq) Len() int

func (*Seq) String

func (s *Seq) String() string

func (*Seq) Type

func (*Seq) Type() string

type String

type String string

func (String) Expand

func (s String) Expand() Values

func (String) Len

func (s String) Len() int

func (String) String

func (s String) String() string

func (String) Type

func (String) Type() string

type TokenReader

type TokenReader interface {
	Pos() token.Location
	ReadToken() (token.Token, error)
}

type UnexpectedTokenError

type UnexpectedTokenError struct {
	Token token.Token
	Err   error
}

func (*UnexpectedTokenError) Error

func (e *UnexpectedTokenError) Error() string

func (*UnexpectedTokenError) Unwrap

func (e *UnexpectedTokenError) Unwrap() error

type Value

type Value interface {
	Len() int
	String() string
	Type() string
	Expand() Values
	// contains filtered or unexported methods
}

type Values

type Values []Value

func Empty

func Empty() Values

func PreludeAnd

func PreludeAnd(tcl *Interp, args []Expr) (last Values, err error)

func PreludeBind

func PreludeBind(tcl *Interp, args Values) (Values, error)

func PreludeBreak

func PreludeBreak(tcl *Interp, args Values) (Values, error)

func PreludeCatch

func PreludeCatch(tcl *Interp, args []Expr) (last Values, err error)

func PreludeConcat

func PreludeConcat(tr *Interp, args Values) (Values, error)

func PreludeContinue

func PreludeContinue(tcl *Interp, args Values) (Values, error)

func PreludeDict

func PreludeDict(tcl *Interp, args Values) (Values, error)

func PreludeExpand

func PreludeExpand(tcl *Interp, args Values) (Values, error)

func PreludeFalse

func PreludeFalse(tcl *Interp, args Values) (Values, error)

func PreludeFn

func PreludeFn(tcl *Interp, args []Expr) (Values, error)

func PreludeForeach

func PreludeForeach(tcl *Interp, args []Expr) (Values, error)

func PreludeIf

func PreludeIf(tcl *Interp, args []Expr) (results Values, err error)

func PreludeIndex

func PreludeIndex(tr *Interp, args Values) (Values, error)

func PreludeInt

func PreludeInt(tcl *Interp, args Values) (Values, error)

func PreludeIsEmpty

func PreludeIsEmpty(tcl *Interp, args Values) (Values, error)

func PreludeIsError

func PreludeIsError(tcl *Interp, args Values) (Values, error)

func PreludeIsTrue

func PreludeIsTrue(tcl *Interp, args Values) (Values, error)

func PreludeLen

func PreludeLen(tcl *Interp, args Values) (Values, error)

func PreludeList

func PreludeList(tcl *Interp, args Values) (Values, error)

func PreludeNot

func PreludeNot(tcl *Interp, args Values) (Values, error)

func PreludeOr

func PreludeOr(tcl *Interp, args []Expr) (last Values, err error)

func PreludePuts

func PreludePuts(tr *Interp, args Values) (Values, error)

func PreludeReturn

func PreludeReturn(tcl *Interp, args Values) (Values, error)

func PreludeSeq

func PreludeSeq(tcl *Interp, args Values) (Values, error)

func PreludeSet

func PreludeSet(tcl *Interp, args Values) (Values, error)

func PreludeStr

func PreludeStr(tcl *Interp, args Values) (Values, error)

func PreludeTap

func PreludeTap(tcl *Interp, args Values) (Values, error)

func PreludeTrue

func PreludeTrue(tcl *Interp, args Values) (Values, error)

func PreludeType

func PreludeType(tcl *Interp, args Values) (Values, error)

func PreludeUplevel

func PreludeUplevel(tcl *Interp, args []Expr) (results Values, err error)

func PreludeUpvar

func PreludeUpvar(tcl *Interp, args Values) (Values, error)

func PreludeWhile

func PreludeWhile(tcl *Interp, args []Expr) (results Values, err error)

func (Values) Expand

func (vs Values) Expand() Values

func (Values) Iterator

func (vs Values) Iterator() Iterator

func (Values) Len

func (vs Values) Len() int

func (Values) String

func (vs Values) String() string

func (Values) Type

func (vs Values) Type() string

type Word

type Word struct {
	Word []Expr      `json:"word"`
	Tok  token.Token `json:"-"`
}

A Word is any set of expressions that run together without whitespace breaking them up.

func (*Word) String

func (w *Word) String() string

func (*Word) Token

func (w *Word) Token() token.Token

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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