shell

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: May 5, 2024 License: BSD-3-Clause Imports: 11 Imported by: 0

README

Cogent Shell (cosh)

Cogent Shell (cosh) is a shell that combines the best parts of Go and bash to provide an integrated shell experience that allows you to easily run terminal commands while using Go for complicated logic. This allows you to write concise, elegant, and readable shell code that runs quickly on all platforms and transpiles to Go.

Syntax: Go vs. Exec

The critical extension from standard Go syntax is for lines that are processed by the Exec functions, used for running arbitrary programs on the user's executable path. Here are the rules (word = IDENT token):

  • Backticks "``" anywhere: Exec. Returns a string.
  • Within Exec, {}: Go
  • Line starts with Go Keyword: Go
  • Line is one word: Exec
  • Line starts with .: Exec
  • Line starts with word word: Exec
  • Line starts with word {: Exec
  • Otherwise: Go

Documentation

Overview

Package shell provides the Cogent Shell (cosh), which combines the best parts of Go and bash to provide an integrated shell experience that allows you to easily run terminal commands while using Go for complicated logic.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Shell

type Shell struct {

	// Builtins are all the builtin shell commands
	Builtins map[string]func(args ...string) error

	// Config is the [exec.Config] used to run commands.
	Config exec.Config

	// depth of parens at the end of the current line. if 0, was complete.
	ParenDepth int

	// depth of braces at the end of the current line. if 0, was complete.
	BraceDepth int

	// depth of brackets at the end of the current line. if 0, was complete.
	BrackDepth int

	// stack of transpiled lines
	Lines []string

	// stack of runtime errors
	Errors []error
}

Shell represents one running shell context.

func NewShell

func NewShell() *Shell

NewShell returns a new Shell with default options.

func (*Shell) AddError

func (sh *Shell) AddError(err error) error

AddError adds the given error to the error stack if it is non-nil. This is the main way that shell errors are handled. It also prints the error.

func (*Shell) AddLine

func (sh *Shell) AddLine(ln string)

AddLine adds line on the stack

func (*Shell) Cd

func (sh *Shell) Cd(args ...string) error

func (*Shell) Code

func (sh *Shell) Code() string

Code returns the current transpiled lines

func (*Shell) Exec

func (sh *Shell) Exec(cmd any, args ...any)

Exec executes the given command string, handling the given arguments appropriately. If there is any error, it adds it to the shell. It forwards output to exec.Config.Stdout and exec.Config.Stderr appropriately.

func (*Shell) InstallBuiltins

func (sh *Shell) InstallBuiltins()

func (*Shell) Output

func (sh *Shell) Output(cmd any, args ...any) string

Output executes the given command string, handling the given arguments appropriately. If there is any error, it adds it to the shell. It returns the stdout as a string and forwards stderr to exec.Config.Stderr appropriately.

func (*Shell) ResetLines

func (sh *Shell) ResetLines()

ResetLines resets the stack of transpiled lines

func (*Shell) RunBuiltin

func (sh *Shell) RunBuiltin(cmd string, args ...string) bool

func (*Shell) Tokens

func (sh *Shell) Tokens(ln string) Tokens

func (*Shell) TotalDepth

func (sh *Shell) TotalDepth() int

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

func (*Shell) TranspileCode

func (sh *Shell) TranspileCode(code string)

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

func (*Shell) TranspileExec

func (sh *Shell) TranspileExec(toks Tokens, output bool) Tokens

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

func (*Shell) TranspileExecString

func (sh *Shell) TranspileExecString(str string, output bool) Tokens

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

func (*Shell) TranspileFile

func (sh *Shell) TranspileFile(in string, out string) error

TranspileFile transpiles the given input cosh file to the given output Go file, adding package main and func main declarations.

func (*Shell) TranspileGo

func (sh *Shell) TranspileGo(toks Tokens) Tokens

TranspileGo returns transpiled tokens assuming Go code. Unpacks any backtick encapsulated shell commands.

func (*Shell) TranspileLine

func (sh *Shell) TranspileLine(ln string) string

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

func (*Shell) TranspileLineTokens

func (sh *Shell) TranspileLineTokens(ln string) Tokens

TranspileLineTokens returns the tokens for the full line

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) 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 (*Tokens) Add

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

Add adds a new token, for generated tokens without Pos

func (*Tokens) AddTokens

func (tk *Tokens) AddTokens(toks Tokens) *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) DeleteLastComma

func (tk *Tokens) DeleteLastComma()

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

func (Tokens) Last

func (tk Tokens) Last() *Token

Last returns the final token in the list

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
cmd
cosh
Command cosh is an interactive cli for running and compiling Cogent Shell (cosh).
Command cosh is an interactive cli for running and compiling Cogent Shell (cosh).

Jump to

Keyboard shortcuts

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