sscal

package
v0.3.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const VersionDate = "2022-09-05"

VersionDate is SSCal's current version date.

View Source
const VersionNumber = "0.3.0"

VersionNumber is SSCal's current version number.

Variables

View Source

Banner is SSCal's interactive banner string.

View Source
var Operators = map[string]*Operator{

	"+": Op2(func(a, b float64) float64 { return a + b }),
	"-": Op2(func(a, b float64) float64 { return a - b }),
	"*": Op2(func(a, b float64) float64 { return a * b }),
	"/": Op2(func(a, b float64) float64 { return a / b }),
	"%": Op2(math.Mod),
	"^": Op2(math.Pow),

	"ceil":  Op1(math.Ceil),
	"floor": Op1(math.Floor),
	"sqrt":  Op1(math.Sqrt),
	"min":   Op2(math.Min),
	"max":   Op2(math.Max),

	"sum": OpFold(func(a, b float64) float64 { return a + b }),

	"dupe": OpStack(1, func(st *Stack) { st.Push(st.Peek()) }),
	"roll": OpStack(1, func(st *Stack) { st.Insert(0, st.Pop()) }),
	"swap": OpStack(2, func(st *Stack) { st.Insert(st.End()-1, st.Pop()) }),
	"wipe": OpStack(0, func(st *Stack) { st.Clear() }),
}

Operators is a map of all available Operators.

Reader is SSCal's default standard input Reader.

Writer is SSCal's default standard input Writer.

Functions

func AssertOp

func AssertOp(t *testing.T, s string, st *Stack, fs ...float64)

AssertOp asserts the results of an Operator.

func Cycle added in v0.3.0

func Cycle(s string, fl *Flags, st *Stack) error

Cycle runs the main SSCal program for one loop cycle.

func Eval

func Eval(a any, st *Stack) error

Eval evaluates one float or Operator against a Stack.

func EvalAll

func EvalAll(as []any, st *Stack) error

EvalAll evaluates a slice of floats and Operators against a Stack.

func Input

func Input() (string, error)

Input returns a trimmed input string.

func Main

func Main(fl *Flags, st *Stack)

Main runs the main SSCal program.

func Output

func Output(s string, as ...any)

Output writes a formatted output string.

func Parse

func Parse(s string) (any, error)

Parse returns a float or Operator from a string.

func ParseAll

func ParseAll(s string) ([]any, error)

ParseAll returns a slice of floats and Operators from a string.

func Prompt

func Prompt(s string) (string, error)

Prompt writes a padded prompt string and returns an input string.

func TestBufs added in v0.3.0

func TestBufs(s string) *bytes.Buffer

TestBufs replaces Reader and Writer with test buffers, populates Reader's buffer from a string and returns Writer's buffer.

Types

type Flags added in v0.3.0

type Flags struct {
	Command string
	Quiet   bool
}

Flags is a container of parsed command-line arguments.

func NewFlags added in v0.3.0

func NewFlags(c string, q bool) *Flags

NewFlags returns a pointer to a new Flags.

func ParseFlags added in v0.3.0

func ParseFlags() *Flags

ParseFlags returns a pointer to a new Flags using parsed arguments.

type OperFunc

type OperFunc func(*Stack) error

OperFunc is a function that operates on a Stack.

type Operator

type Operator struct {
	Size int
	Func OperFunc
}

Operator is a callable Stack operator.

func NewOperator

func NewOperator(i int, fn OperFunc) *Operator

NewOperator returns a pointer to a new Operator.

func Op1

func Op1(fn func(float64) float64) *Operator

Op1 returns an Operator from a function that takes one float argument.

func Op2

func Op2(fn func(float64, float64) float64) *Operator

Op2 returns an Operator from a function that takes two float arguments.

func OpFold added in v0.3.0

func OpFold(fn func(float64, float64) float64) *Operator

OpFold returns an Operator from a function that takes two float arguments, converting the function to operate on an entire Stack.

func OpStack added in v0.3.0

func OpStack(i int, fn func(st *Stack)) *Operator

OpStack returns an Operator from a function that takes a Stack argument and does not return an error.

func (*Operator) Run

func (op *Operator) Run(st *Stack) error

Run applies the Operator's callable function to a Stack.

type Stack

type Stack struct {
	Floats []float64
}

Stack is a last-in-first-out stack of floats.

func NewStack

func NewStack(fs ...float64) *Stack

NewStack returns a pointer to a new Stack.

func (*Stack) Clear added in v0.3.0

func (st *Stack) Clear()

Clear removes all floats on the Stack,

func (*Stack) Empty

func (st *Stack) Empty() bool

Empty returns true if the Stack has no floats.

func (*Stack) End

func (st *Stack) End() int

End returns the index of the top float on the Stack.

func (*Stack) Insert added in v0.3.0

func (st *Stack) Insert(i int, f float64)

Insert inserts a float at an index on the Stack.

func (*Stack) Len

func (st *Stack) Len() int

Len returns the number of floats on the Stack.

func (*Stack) Peek

func (st *Stack) Peek() float64

Peek returns the top float on the Stack,

func (*Stack) Pop

func (st *Stack) Pop() float64

Pop removes and returns the top float on the Stack.

func (*Stack) Push

func (st *Stack) Push(f float64)

Push appends a float to the top of the Stack.

func (*Stack) String

func (st *Stack) String() string

String returns the Stack as a space-separated string.

Jump to

Keyboard shortcuts

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