calc

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2021 License: MIT Imports: 9 Imported by: 0

README

calc

Calc is a package that provides the tools to evaluate simple numerical expressions.

Fork from github.com/marcak/calc, a bit refactored, with errors, with tests and as go module.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContainsLetter

func ContainsLetter(s string) bool

ContainsLetter checks if a string contains a letter

func IsOperator

func IsOperator(r rune) bool

func Solve

func Solve(s string) (float64, error)

Solve a mathematical calculation.

func SolveFunction

func SolveFunction(s string) (string, error)

SolveFunction returns the answer of a function found within an expression

func SolvePostfix

func SolvePostfix(tokens Stack) (float64, error)

SolvePostfix evaluates and returns the answer of the expression converted to postfix

Types

type Parser

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

func NewParser

func NewParser(r io.Reader) *Parser

func (*Parser) Parse

func (p *Parser) Parse() (Stack, error)

func (*Parser) Scan

func (p *Parser) Scan() (Token, error)

func (*Parser) ScanIgnoreWhitespace

func (p *Parser) ScanIgnoreWhitespace() (Token, error)

func (*Parser) Unscan

func (p *Parser) Unscan()

type Scanner

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

func NewScanner

func NewScanner(r io.Reader) *Scanner

func (*Scanner) Read

func (s *Scanner) Read() (rune, error)

func (*Scanner) Scan

func (s *Scanner) Scan() (Token, error)

func (*Scanner) ScanNumber

func (s *Scanner) ScanNumber() (Token, error)

func (*Scanner) ScanWhitespace

func (s *Scanner) ScanWhitespace() (Token, error)

func (*Scanner) ScanWord

func (s *Scanner) ScanWord() (Token, error)

func (*Scanner) Unread

func (s *Scanner) Unread() error

type Stack

type Stack []Token

Stack is a LIFO data structure.

func ShuntingYard

func ShuntingYard(s Stack) (Stack, error)

func (*Stack) EmptyInto

func (s *Stack) EmptyInto(dest *Stack)

EmptyInto dumps all tokens from one stack to another.

func (*Stack) IsEmpty

func (s *Stack) IsEmpty() bool

IsEmpty checks if there are any tokens in the stack.

func (*Stack) Length

func (s *Stack) Length() int

Length returns the amount of tokens in the stack.

func (*Stack) Peek

func (s *Stack) Peek() Token

Peek returns the token at the top of the stack.

func (*Stack) Pop

func (s *Stack) Pop() Token

Pop removes the token at the top of the stack and returns its value.

func (*Stack) Push

func (s *Stack) Push(i ...Token)

Push adds tokens to the top of the stack.

type Token

type Token struct {
	// Type of the token.
	// This can be used to parse the Value accordingly.
	Type TokenType

	// Value of the Token.
	// It should match the Type.
	Value string
}

Token represents a pair of a type and a value which matches that type.

type TokenType

type TokenType int

TokenType defines what the token is.

const (
	Number TokenType = iota
	Lparen
	Rparen
	Constant
	Function
	Operator
	Whitespace
)

These constants are all possible TokenType values.

Jump to

Keyboard shortcuts

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