tcl

package
v0.0.0-...-75abb68 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2023 License: MIT Imports: 14 Imported by: 2

Documentation

Index

Constants

View Source
const (
	RETURN = StatusCode(iota + 2)
	BREAK
	CONTINUE
	USAGE // New in chirp; not in Tcl.
)

Variables

View Source
var AlphanumChars = "[A-Za-z0-9_]"
View Source
var BareWordPattern = "(" + InertChars + "+)"
View Source
var Debug [256]bool
View Source
var DumbDollarPattern = "[$](" + AlphanumChars + "+)"
View Source
var Empty = MkString("")
View Source
var False = Zero
View Source
var InertChars = "[!%-/0-9:<-@A-Z^_`a-z|~]"
View Source
var InvalidValue = *new(R.Value)
View Source
var MatchBareWord = regexp.MustCompile("^" + BareWordPattern + "$")
View Source
var MatchDumbDollar = regexp.MustCompile("^" + DumbDollarPattern + "$")
View Source
var MustFails int64
View Source
var MustMutex sync.Mutex
View Source
var MustSucceeds int64
View Source
var One = MkInt(1)
View Source
var Safes map[string]Command

Safes are builtin commands that safe subinterps can call. Conventionally these contain no hyphen.

View Source
var SayPrefix = "Say" // TODO: Global Prefix WILL BREAK WITH goROUTINES.
View Source
var True = One
View Source
var TypeT = R.TypeOf(new(T)).Elem()
View Source
var TypeType = R.TypeOf(TypeT)
View Source
var Unsafes map[string]Command

Unsafes are commands that only the trusted, toplevel terp can call. Conventionally these contain a hyphen.

View Source
var Zero = MkInt(0)

Functions

func Arg0

func Arg0(argv []T)

func Arg1v

func Arg1v(argv []T) (T, []T)

func Arg2

func Arg2(argv []T) (T, T)

func Arg2v

func Arg2v(argv []T) (T, T, []T)

func Arg3

func Arg3(argv []T) (T, T, T)

func Arg3Usage

func Arg3Usage(argv []T, usage string) (T, T, T)

func Arg3v

func Arg3v(argv []T) (T, T, T, []T)

func Arg4

func Arg4(argv []T) (T, T, T, T)

func Arg5

func Arg5(argv []T) (T, T, T, T, T)

func Arg6

func Arg6(argv []T) (T, T, T, T, T, T)

func Arg7

func Arg7(argv []T) (T, T, T, T, T, T, T)

func ArgDash2v

func ArgDash2v(argv []T) ([]string, T, T, []T)

ArgDash2v expects args to be (1) dash arguments (2) two required args (3) possibly some optional args.

func ArgDash2vUsage

func ArgDash2vUsage(argv []T, usage string) ([]string, T, T, []T)

func CHECK

func CHECK(b bool, rest ...interface{})

func CheckEQ

func CheckEQ[N NUMBER](a, b N, extra ...any)

func CheckGE

func CheckGE[N NUMBER](a, b N, extra ...any)

func CheckGT

func CheckGT[N NUMBER](a, b N, extra ...any)

func CheckLE

func CheckLE[N NUMBER](a, b N, extra ...any)

func CheckLT

func CheckLT[N NUMBER](a, b N, extra ...any)

func CheckNE

func CheckNE[N NUMBER](a, b N, extra ...any)

func ClearAllCounters

func ClearAllCounters()

func DropSpaces

func DropSpaces(s string) string

func FollowedByGap

func FollowedByGap(lex *Lex) bool

func GiveInfo

func GiveInfo(extra ...any) string

func IfNilArgvThenUsage

func IfNilArgvThenUsage(argv []T, usage string)

func IsGlobal

func IsGlobal(name string) bool

Initial capital letter for a variable means Global.

func IsLocal

func IsLocal(name string) bool

Initial capital letter for a variable means local.

func Log

func Log(fr *Frame, levelStr string, message string)

func LogAllCounters

func LogAllCounters()

func Logf

func Logf(fmt string, args ...interface{})

func MkFloat

func MkFloat(a float64) terpFloat

func MkHash

func MkHash(h Hash) *terpHash

func MkInt

func MkInt(a int64) terpInt

func MkList

func MkList(a []T) terpList

func MkMulti

func MkMulti(s string) *terpMulti

func MkMultiFr

func MkMultiFr(fr *Frame, a *terpMulti) *terpMulti

func MkString

func MkString(a string) terpString

func MkStringList

func MkStringList(a []string) terpList

func MkUint

func MkUint(a uint64) terpInt

func Must

func Must(a, b T)

Must takes 2 T values, and compares their Show()s.

func MustA

func MustA(a, b interface{})

MustA takes Any 2 values, and compares their Repr()s.

func MustB

func MustB(a, b byte)

MustB takes two bytes.

func MustNoSp

func MustNoSp(a, b interface{})

MustSp takes Any 2 values, and compares their Repr()s, without spaces.

func MustST

func MustST(a string, b T)

MustST takes a string and a T

func MustTok

func MustTok(a, b Token)

func NeedsOctalEscape

func NeedsOctalEscape(b byte) bool

func NonEmpty

func NonEmpty(v []string) []string

func OctalEscape

func OctalEscape(s string) string

func Regexp

func Regexp(exp string, nocase bool) *regexp.Regexp

func RegexpFindMatch

func RegexpFindMatch(exp, str string, nocase bool) (bool, string)

func RegexpFindSubmatch

func RegexpFindSubmatch(exp, str string, nocase bool) (bool, []string)

func RegexpMatch

func RegexpMatch(exp, str string, nocase bool) bool

func Repr

func Repr(a interface{}) string

func Say

func Say(args ...interface{})

Quick internal logging function that needs no Frame.

func Sayf

func Sayf(format string, args ...interface{})

func Show

func Show(a T) string

func ShowAllCounters

func ShowAllCounters() string

func ShowEnsembleItems

func ShowEnsembleItems(items []EnsembleItem) string

func ShowExprUnlessNull

func ShowExprUnlessNull(expr *PExpr) string

func ShowSeqUnlessNull

func ShowSeqUnlessNull(seq *PSeq) string

func Showv

func Showv(a []T) string

func SmartParseInt

func SmartParseInt(s string) int64

func SmartParseUint

func SmartParseUint(s string) uint64

func SortListByString

func SortListByString(list []T)

SortListByString is used by smilax-web/db.

func SortedKeysOfHash

func SortedKeysOfHash(h Hash) []string

func Str

func Str(a any) string

func StrEach

func StrEach(vec []any) string

func StringMatch

func StringMatch(pattern, str string) bool

func SubstStringOrOrig

func SubstStringOrOrig(fr *Frame, s string) (z string)

func ToListElementString

func ToListElementString(s string) string

func Where

func Where() string

func White

func White(ch uint8) bool

func WhiteOrSemi

func WhiteOrSemi(ch uint8) bool

Types

type BitsWord

type BitsWord uint32 // We cannot fit uint64 into the float -- until we support actual uint64, we must use shorter BitsWords.

type CmdNode

type CmdNode struct {
	Fn   Command
	Next *CmdNode
}

CmdNode makes a singly-linked-list of commands at different mixin levels, highest level first. A non-mixin command has level 0 and only one CmdNode.

type CmdScope

type CmdScope map[string]*CmdNode

type Command

type Command func(fr *Frame, argv []T) T

func MkEnsemble

func MkEnsemble(items []EnsembleItem) Command

type Counter

type Counter struct {
	// contains filtered or unexported fields
}
var Counters *Counter
var EvalSeqWithErrorLocationCounter Counter
var MkBoolCounter Counter
var MkFloatCounter Counter
var MkHackFloatCounter Counter
var MkHashCounter Counter
var MkIntCounter Counter
var MkListCounter Counter
var MkMultiCounter Counter
var MkNumCounter Counter
var MkStringCounter Counter
var MkStringListCounter Counter
var MkTCounter Counter
var MkUintCounter Counter
var MkValueCounter Counter
var MultiEvalExprCompileCounter Counter
var MultiEvalExprCounter Counter
var MultiEvalSeqCompileCounter Counter
var MultiEvalSeqCounter Counter
var NewFrameCounter Counter
var Parse2CmdCounter Counter
var Parse2CmdEvalCounter Counter
var Parse2DollarCounter Counter
var Parse2ExprEvalCounter Counter
var Parse2ExprTopCounter Counter
var Parse2QuoteCounter Counter
var Parse2SeqCounter Counter
var Parse2SeqEvalCounter Counter
var Parse2SquareCounter Counter
var Parse2WordEvalFastCounter0 Counter
var Parse2WordEvalFastCounter1 Counter
var Parse2WordEvalSlowCounter1 Counter
var Parse2WordEvalSlowCounter9 Counter
var ParseListCounter Counter
var SubstStringCounter Counter

func (*Counter) Incr

func (p *Counter) Incr()

func (*Counter) Register

func (p *Counter) Register(name string)

func (*Counter) Show

func (p *Counter) Show() string

type Either

type Either struct {
	Bad  interface{}
	Good T
}

Either Bad or Good value.

type EnsembleItem

type EnsembleItem struct {
	Name string
	Cmd  Command
	Doc  string
}

type Frame

type Frame struct {
	Vars Scope // local variables
	Cred Hash  // credentials

	Prev *Frame
	G    *Global

	DebugName string
}

Frame is a local variable frame. There is one for the global variables in the Global struct, and a new one is created for each proc or yproc invocation (but not for every Command; non-proc commands do not make Frames).

func NewInterpreter

func NewInterpreter() *Frame

NewInterpreter() makes a new full interpreter.

func NewSafeInterpreter

func NewSafeInterpreter() *Frame

NewSafeInterpreter() makes a new safe interpreter.

func (*Frame) Apply

func (fr *Frame) Apply(argv []T) T

Apply a command with its arguments.

func (*Frame) DefineUpVar

func (fr *Frame) DefineUpVar(name string, remFr *Frame, remName string)

func (*Frame) Eval

func (fr *Frame) Eval(a T) (result T)

func (*Frame) EvalExpr

func (fr *Frame) EvalExpr(a T) (result T)

Takes a single word that represents an expression and returns the result.

func (*Frame) EvalSeqWithErrorLocation

func (fr *Frame) EvalSeqWithErrorLocation(seq *PSeq) (result T)

func (*Frame) EvalString

func (fr *Frame) EvalString(a string) (result T)

func (*Frame) FindCommand

func (fr *Frame) FindCommand(name T, callSuper bool) Command

func (*Frame) GetVar

func (fr *Frame) GetVar(name string) T

func (*Frame) GetVarScope

func (fr *Frame) GetVarScope(name string) Scope

func (*Frame) HasVar

func (fr *Frame) HasVar(name string) bool

func (*Frame) NewFrame

func (fr *Frame) NewFrame() *Frame

NewFrame makes a frame for calling another proc.

func (*Frame) SetVar

func (fr *Frame) SetVar(name string, x T)

func (*Frame) SubstString

func (fr *Frame) SubstString(s string, flags SubstFlags) string

type Global

type Global struct {
	Cmds   CmdScope
	Macros MacroScope
	Fr     Frame // global scope

	IsSafe    bool // Set true for safe subinterpreter.
	Logger    *log.Logger
	Verbosity int    // Log if message level <= verbosity.
	LogName   string // for logging
}

Global holds the global state of an interpreter, mainly the Commands and global variables.

type Hash

type Hash map[string]T

type Jump

type Jump struct {
	Status StatusCode
	Result T
}

Jump structs are panicked for return, break, and continue.

type Lex

type Lex struct {
	Str  string
	Len  int   // len(s)
	Next int   // next position to scan
	Pos  int   // position of token
	Tok  Token // type of token
}

func NewLex

func NewLex(s string) *Lex

func (*Lex) Advance

func (x *Lex) Advance()

func (*Lex) AdvanceCurly

func (x *Lex) AdvanceCurly() string

AdvanceCurly returns the string decoded from the Curly clause and leaves Next on the close-curly.

func (*Lex) AdvanceIfAlfaNum

func (x *Lex) AdvanceIfAlfaNum()

AdvanceIfAlfaNum will either take a TokAlfaNum (no white space first), or not advance.

func (*Lex) Current

func (x *Lex) Current() string

func (*Lex) PeekNext

func (x *Lex) PeekNext() byte

func (*Lex) Show

func (x *Lex) Show() string

func (*Lex) SkipComment

func (x *Lex) SkipComment()

func (*Lex) Stretch1

func (lex *Lex) Stretch1()

func (*Lex) StretchBackslashEscaped

func (lex *Lex) StretchBackslashEscaped() byte

StretchBackslashEscaped stretches the Next pointer across \C or \ooo for octal.

type Loc

type Loc interface {
	Has() bool
	Get() T
	Set(T)
}

Loc is protocol for a variable location.

type MacroNode

type MacroNode struct {
	Args []string
	Body *PSeq
}

Macros (for now) are not defined by mixins; they must be global.

type MacroScope

type MacroScope map[string]*MacroNode

type NUMBER

type NUMBER interface {
	byte | rune | int | int64 | float64
}

type PCmd

type PCmd struct {
	Words []*PWord
}

One command made of one or more words.

func Parse2Cmd

func Parse2Cmd(lex *Lex) *PCmd

Returns next command, or else nil.

func (*PCmd) CloneAndSubst

func (me *PCmd) CloneAndSubst(params map[string][]*PWord) *PCmd

func (*PCmd) Eval

func (me *PCmd) Eval(fr *Frame) T

func (*PCmd) ExpandMacros

func (me *PCmd) ExpandMacros(fr *Frame, maxSubCompile int) []*PCmd

func (*PCmd) Show

func (me *PCmd) Show() string

type PExpr

type PExpr struct {
	Op      Token
	A, B, C *PExpr
	Word    *PWord // Op '"': for Primatives: "quoted", [square], 3.14 literal, {literal}, $var, $var(index).
}

An expr command

func Parse2ExprConjunction

func Parse2ExprConjunction(lex *Lex) *PExpr

func Parse2ExprDisjunction

func Parse2ExprDisjunction(lex *Lex) *PExpr

func Parse2ExprPrimative

func Parse2ExprPrimative(lex *Lex) *PExpr

func Parse2ExprProduct

func Parse2ExprProduct(lex *Lex) *PExpr

func Parse2ExprRelation

func Parse2ExprRelation(lex *Lex) *PExpr

func Parse2ExprStr

func Parse2ExprStr(s string) *PExpr

func Parse2ExprSum

func Parse2ExprSum(lex *Lex) *PExpr

func Parse2ExprTop

func Parse2ExprTop(lex *Lex) *PExpr

func Parse2ExprUnary

func Parse2ExprUnary(lex *Lex) *PExpr

func (*PExpr) Eval

func (me *PExpr) Eval(fr *Frame) T

func (*PExpr) Show

func (me *PExpr) Show() string

type PPart

type PPart struct {
	VarName string
	Multi   *terpMulti
	Word    *PWord // for DOLLAR2
	Seq     *PSeq  // for SQUARE
	Type    PartType
}

func Parse2Dollar

func Parse2Dollar(lex *Lex) *PPart

Parse a variable name after a '$', returning *PPart. Leaves Next immediately after the consumed part.

func Parse2Square

func Parse2Square(lex *Lex) *PPart

Parse Square Bracketed subcommand, returning result and new position

func (*PPart) CloneAndSubst

func (me *PPart) CloneAndSubst(params map[string][]*PWord) []*PPart

func (*PPart) Eval

func (me *PPart) Eval(fr *Frame) T

func (*PPart) ExpandMacros

func (me *PPart) ExpandMacros(fr *Frame, maxSubCompile int) *PPart

func (*PPart) Show

func (me *PPart) Show() string

type PSeq

type PSeq struct {
	Cmds []*PCmd
	Src  string
}

Any piece of tcl code, a sequence of commands.

func CompileSequence

func CompileSequence(fr *Frame, s string) *PSeq

func MaybeCompileSequence

func MaybeCompileSequence(fr *Frame, s string) (seq *PSeq)

func Parse2Seq

func Parse2Seq(lex *Lex) *PSeq

func Parse2SeqStr

func Parse2SeqStr(s string) *PSeq

func (*PSeq) CloneAndSubst

func (me *PSeq) CloneAndSubst(params map[string][]*PWord) *PSeq

func (*PSeq) Eval

func (me *PSeq) Eval(fr *Frame) T

func (*PSeq) ExpandMacros

func (me *PSeq) ExpandMacros(fr *Frame, maxSubCompile int) *PSeq

func (*PSeq) Show

func (me *PSeq) Show() string

type PWord

type PWord struct {
	Parts             []*PPart
	Multi             *terpMulti // If not null, value is fixed and precompiled.
	ExpandAsMultiWord bool
}

One words, composed of parts that may require substitions.

func Parse2Curly

func Parse2Curly(lex *Lex) *PWord

Parse nested curlies, returning contents.

func Parse2DollarKey

func Parse2DollarKey(lex *Lex) *PWord

Parse the Key for a Dollar with Parens, e.g. $x(key). Dollar, Square, and Backslash substitutions occur. White space and DQuotes are not special. Terminates with ")".

func Parse2Quote

func Parse2Quote(lex *Lex) *PWord

func Parse2Word

func Parse2Word(lex *Lex) *PWord

Parse a word, returning result and new position

func (*PWord) CloneAndSubst

func (me *PWord) CloneAndSubst(params map[string][]*PWord) []*PWord

func (*PWord) Eval

func (me *PWord) Eval(fr *Frame) (z T)

func (*PWord) ExpandMacros

func (me *PWord) ExpandMacros(fr *Frame, maxSubCompile int) *PWord

func (*PWord) Show

func (me *PWord) Show() string

type PartType

type PartType int
const (
	BARE    PartType = iota + 1 // Does not need substitions (backslash subs aready done).
	DOLLAR1                     // $x, variable subs without index
	DOLLAR2                     // $x(...), variable subs with index
	SQUARE                      // [...], subcommand eval and replace.
)

type Scope

type Scope map[string]Loc

type Shower

type Shower interface {
	Show() string
}

type Slot

type Slot struct {
	Elem T
}

Slot stores a variable value.

func (*Slot) Get

func (p *Slot) Get() T

func (*Slot) Has

func (p *Slot) Has() bool

func (*Slot) Set

func (p *Slot) Set(t T)

type SortListByStringTSlice

type SortListByStringTSlice []T

func (SortListByStringTSlice) Len

func (p SortListByStringTSlice) Len() int

func (SortListByStringTSlice) Less

func (p SortListByStringTSlice) Less(i, j int) bool

func (SortListByStringTSlice) Swap

func (p SortListByStringTSlice) Swap(i, j int)

type StatusCode

type StatusCode int

StatusCode are the same integers as Tcl/C uses for return, break, and continue.

type SubstFlags

type SubstFlags int
const (
	NoDollar SubstFlags = 1 << iota
	NoSquare
	NoBackslash
)

type T

type T interface {
	String() string
	Float() float64
	Int() int64
	Uint() uint64
	ListElementString() string
	IsQuickString() bool
	IsQuickList() bool
	IsQuickHash() bool
	Bool() bool    // Like Python, empty values and 0 values are false.
	IsEmpty() bool // Would String() return ""?
	List() []T
	IsPreservedByList() bool
	IsQuickInt() bool
	IsQuickNumber() bool
	HeadTail() (hd, tl T)
	Hash() Hash
	GetAt(key T) T
	PutAt(value T, key T)
	EvalSeq(fr *Frame) T
	EvalExpr(fr *Frame) T
	Apply(fr *Frame, args []T) T
}

T is an interface to any Tcl value. Use them only through these methods, or fix these methods.

func Arg0v

func Arg0v(argv []T) []T

func Arg1

func Arg1(argv []T) T

func Arg1Usage

func Arg1Usage(argv []T, usage string) T

func ConcatLists

func ConcatLists(lists []T) []T

func EvalOrApplyLists

func EvalOrApplyLists(fr *Frame, lists []T) T

func MkBool

func MkBool(a bool) T

func MkNum

func MkNum(s string) T

func Parse2EvalExprStr

func Parse2EvalExprStr(fr *Frame, s string) T

func Parse2EvalSeqStr

func Parse2EvalSeqStr(fr *Frame, s string) T

func ParseList

func ParseList(s string) []T

func ParseListOrRecover

func ParseListOrRecover(s string) (recs []T, err interface{})

func RemoveHeadDashArgs

func RemoveHeadDashArgs(argv []T) (dashes []string, newArgv []T)

RemoveHeadDashArgs removes dash args from argv (only if they are QuickString), returning the dash args and modified argv. Argv[0] is preserved.

type Token

type Token uint8
const (
	TokEnd Token = iota // end of string
	TokNewline
	TokAlfaNum
	TokNumber
	TokOther // Non-Ascii or not special.

	TokBoolOr  // ||
	TokBoolAnd // &&

	TokNumEq // ==
	TokNumNe
	TokNumLe
	TokNumGe

	TokStrEq // eq
	TokStrNe
	TokStrLt
	TokStrLe
	TokStrGt
	TokStrGe

	TokShiftLeft  // <<
	TokShiftRight // >>

	TokExpandSquare // {*}[
	TokExpandDollar // {*}$

)

type UpSlot

type UpSlot struct {
	Fr         *Frame
	RemoteName string
}

UpSlot forwards a variable to another variable.

func (*UpSlot) Get

func (p *UpSlot) Get() T

func (*UpSlot) Has

func (p *UpSlot) Has() bool

func (*UpSlot) Set

func (p *UpSlot) Set(t T)

Jump to

Keyboard shortcuts

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