transpile

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: BSD-3-Clause Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddQuotes

func AddQuotes(str string) string

AddQuotes surrounds given string with quotes, also escaping any contained quotes

func AddYaegiTensorFuncs

func AddYaegiTensorFuncs()

AddYaegiTensorFuncs grabs all tensor* package functions registered in yaegicore and adds them to the `tensor.Funcs` map so we can properly convert symbols to either tensors or basic literals, depending on the arg types for the current function.

func EscapeQuotes

func EscapeQuotes(str string) string

EscapeQuotes replaces any " with \"

func ExecWordIsCommand

func ExecWordIsCommand(f string) bool

ExecWordIsCommand returns true if given exec word is a command-like string (excluding any paths)

func ExecWords

func ExecWords(ln string) ([]string, error)

Types

type State

type State struct {
	// FuncToVar translates function definitions into variable definitions,
	// which is the default for interactive use of random code fragments
	// without the complete go formatting.
	// For pure transpiling of a complete codebase with full proper Go formatting
	// this should be turned off.
	FuncToVar bool

	// MathMode is on when math mode is turned on.
	MathMode bool

	// MathRecord is state of auto-recording of data into current directory
	// in math mode.
	MathRecord bool

	// depth of delim at the end of the current line. if 0, was complete.
	ParenDepth, BraceDepth, BrackDepth, TypeDepth, DeclDepth int

	// Chunks of code lines that are accumulated during Transpile,
	// each of which should be evaluated separately, to avoid
	// issues with contextual effects from import, package etc.
	Chunks []string

	// current stack of transpiled lines, that are accumulated into
	// code Chunks.
	Lines []string

	// stack of runtime errors.
	Errors []error
	// contains filtered or unexported fields
}

State holds the transpiling state

func NewState

func NewState() *State

NewState returns a new transpiling state; mostly for testing

func (*State) AddChunk

func (st *State) AddChunk()

AddChunk adds current lines into a chunk of code that should be compiled separately.

func (*State) AddError

func (st *State) AddError(err error) error

AddError adds the given error to the error stack if it is non-nil, and calls the Cancel function if set, to stop execution. This is the main way that goal errors are handled. It also prints the error.

func (*State) AddLine

func (st *State) AddLine(ln string)

AddLine adds line on the stack

func (*State) Code

func (st *State) Code() string

Code returns the current transpiled lines, split into chunks that should be compiled separately.

func (*State) DepthError

func (st *State) DepthError() error

DepthError reports an error if any of the parsing depths are not zero, to be called at the end of transpiling a complete block of code.

func (*State) ResetCode

func (st *State) ResetCode()

ResetCode resets the stack of transpiled code

func (*State) ResetDepth

func (st *State) ResetDepth()

ResetDepth resets the current depths to 0

func (*State) TotalDepth

func (st *State) TotalDepth() int

TotalDepth returns the sum of any unresolved paren, brace, or bracket depths.

func (*State) TranspileCode

func (st *State) TranspileCode(code string)

TranspileCode processes each line of given code, adding the results to the LineStack

func (*State) TranspileExec

func (st *State) TranspileExec(ewords []string, output bool) Tokens

TranspileExec returns transpiled tokens assuming Exec code, with the given bools indicating the type of run to execute.

func (*State) TranspileExecString

func (st *State) TranspileExecString(str string, output bool) Tokens

TranspileExecString returns transpiled tokens assuming Exec code, from a string, with the given bool indicating whether [Output] is needed.

func (*State) TranspileExecTokens

func (st *State) TranspileExecTokens(toks Tokens, code string, output bool) Tokens

TranspileExecTokens returns transpiled tokens assuming Exec code, from given tokens, with the given bool indicating whether [Output] is needed.

func (*State) TranspileFile

func (st *State) TranspileFile(in string, out string) error

TranspileFile transpiles the given input goal file to the given output Go file. If no existing package declaration is found, then package main and func main declarations are added. This also affects how functions are interpreted.

func (*State) TranspileGo

func (st *State) TranspileGo(toks Tokens, code string) Tokens

TranspileGo returns transpiled tokens assuming Go code. Unpacks any encapsulated shell or math expressions.

func (*State) TranspileGoNDimIndex

func (st *State) TranspileGoNDimIndex(toks Tokens, code string, gtoks *Tokens, idIdx, rbIdx int) int

TranspileGoNDimIndex processes an ident[*] sequence of tokens, translating it into a corresponding tensor Value or Set expression, if it is a multi-dimensional indexing expression which is not valid in Go, to support simple n-dimensional tensor indexing in Go (not math) mode. Gets the current sequence of toks tokens, where the ident starts at idIdx and the ] is at rbIdx. It puts the results in gtoks generated tokens. Returns a positive index to resume processing at, if it is actually an n-dimensional expr, and -1 if not, in which case the normal process resumes.

func (*State) TranspileGoRange

func (st *State) TranspileGoRange(toks Tokens, code string, start, end int) Tokens

TranspileGoRange returns transpiled tokens assuming Go code, for given start, end (exclusive) range of given tokens and code. In general the positions in the tokens applies to the _original_ code so you should just keep the original code string. However, this is needed for a specific case.

func (*State) TranspileLine

func (st *State) TranspileLine(code string) string

TranspileLine is the main function for parsing a single line of goal input, returning a new transpiled line of code that converts Exec code into corresponding Go function calls.

func (*State) TranspileLineTokens

func (st *State) TranspileLineTokens(code string) Tokens

TranspileLineTokens returns the tokens for the full line

func (*State) TranspileMath

func (st *State) TranspileMath(toks Tokens, code string, fullLine bool) Tokens

TranspileMath does math mode transpiling. fullLine indicates code should be full statement(s).

type Token

type Token struct {
	// Go token classification
	Tok token.Token

	// Literal string
	Str string

	// position in the original string.
	// this is only set for the original parse,
	// not for transpiled additions.
	Pos token.Pos
}

Token provides full data for one token

func NewToken

func NewToken(tok token.Token, str ...string) *Token

NewToken returns a new token, for generated tokens without Pos

func (*Token) IsBacktickString

func (tk *Token) IsBacktickString() bool

IsBacktickString returns true if the given STRING uses backticks

func (*Token) IsBracket

func (tk *Token) IsBracket() bool

IsBracket returns true if the given token is a bracket delimiter: paren, brace, bracket

func (*Token) IsGo

func (tk *Token) IsGo() bool

IsGo returns true if the given token is a Go Keyword or Comment

func (*Token) IsOp

func (tk *Token) IsOp() bool

IsOp returns true if the given token is an operator

func (*Token) IsPathDelim

func (tk *Token) IsPathDelim() bool

func (*Token) IsPathExtraDelim

func (tk *Token) IsPathExtraDelim() bool

func (*Token) IsValidExecIdent

func (tk *Token) IsValidExecIdent() bool

IsValidExecIdent returns true if the given token is a valid component of an Exec mode identifier

func (*Token) IsWord

func (tk *Token) IsWord() bool

IsWord returns true if the token is some kind of word-like entity, including IDENT, STRING, CHAR, or one of the Go keywords. This is for exec filtering.

func (*Token) String

func (tk *Token) String() string

String returns the string for the token

type Tokens

type Tokens []*Token

Tokens is a slice of Token

func TokensFromString

func TokensFromString(ln string) Tokens

Tokens converts the string into tokens

func (*Tokens) Add

func (tk *Tokens) Add(tok token.Token, str ...string) *Token

Add adds a new token, for generated tokens without Pos

func (*Tokens) AddMulti

func (tk *Tokens) AddMulti(tok ...token.Token)

AddMulti adds new basic tokens (not IDENT).

func (*Tokens) AddTokens

func (tk *Tokens) AddTokens(toks ...*Token) *Tokens

AddTokens adds given tokens to our list

func (Tokens) BracketDepths

func (tk Tokens) BracketDepths() (paren, brace, brack int)

BracketDepths returns the depths for the three bracket delimiters [paren, bracket, brace], based on unmatched right versions.

func (Tokens) Code

func (tk Tokens) Code() string

Code returns concatenated Str values of the tokens, to generate a surface-valid code string.

func (Tokens) Contains

func (tk Tokens) Contains(toks ...token.Token) bool

Contains returns true if the token string contains any of the given token(s)

func (*Tokens) DeleteLastComma

func (tk *Tokens) DeleteLastComma()

DeleteLastComma removes any final Comma. easier to generate and delete at the end

func (*Tokens) Insert

func (tk *Tokens) Insert(i int, tok token.Token, str ...string) *Token

Insert inserts a new token at given position

func (Tokens) IsAssignExpr

func (tk Tokens) IsAssignExpr() bool

IsAssignExpr checks if there are any Go assignment or define tokens outside of { } Go code.

func (Tokens) Last

func (tk Tokens) Last() *Token

Last returns the final token in the list

func (Tokens) ModeEnd

func (tk Tokens) ModeEnd() int

ModeEnd returns the position (or -1 if not found) for the next ILLEGAL mode token ($ or #) given the starting one that is at the 0 position of the current set of tokens.

func (Tokens) Path

func (tk Tokens) Path(idx0 bool) (string, int)

Path extracts a standard path or URL expression from the current list of tokens (starting at index 0), returning the path string and the number of tokens included in the path. Restricts processing to contiguous elements with no spaces! If it is not a path, returns nil string, 0

func (Tokens) ReplaceIdentAt

func (tk Tokens) ReplaceIdentAt(at int, str string, n int) Tokens

ReplaceIdentAt replaces an identifier spanning n tokens starting at given index, with a single identifier with given string. This is used in Exec mode for dealing with identifiers and paths that are separately-parsed by Go.

func (Tokens) RightMatching

func (tk Tokens) RightMatching() int

RightMatching returns the position (or -1 if not found) for the right matching [paren, bracket, brace] given the left one that is at the 0 position of the current set of tokens.

func (Tokens) String

func (tk Tokens) String() string

String is the stringer version which includes the token ID in addition to the string literal

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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