calc

package module
v0.0.0-...-8d43dbe Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2020 License: MIT Imports: 8 Imported by: 0

README

calc

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

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 IsWhitespace

func IsWhitespace(ch rune) bool

func Solve

func Solve(s string) float64

func SolveFunction

func SolveFunction(s string) string

SolveFunction returns the answer of a function found within an expression

func SolvePostfix

func SolvePostfix(tokens Stack) float64

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() (tok Token)

func (*Parser) ScanIgnoreWhitespace

func (p *Parser) ScanIgnoreWhitespace() (tok Token)

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

func (*Scanner) Scan

func (s *Scanner) Scan() Token

func (*Scanner) ScanNumber

func (s *Scanner) ScanNumber() Token

func (*Scanner) ScanWhitespace

func (s *Scanner) ScanWhitespace() Token

func (*Scanner) ScanWord

func (s *Scanner) ScanWord() Token

func (*Scanner) Unread

func (s *Scanner) Unread()

type Stack

type Stack struct {
	Values []Token
}

Stack is a LIFO data structure

func ShuntingYard

func ShuntingYard(s Stack) Stack

func (*Stack) EmptyInto

func (self *Stack) EmptyInto(s *Stack)

EmptyInto dumps all tokens from one stack to another

func (*Stack) IsEmpty

func (self *Stack) IsEmpty() bool

IsEmpty checks if there are any tokens in the stack

func (*Stack) Length

func (self *Stack) Length() int

Length returns the amount of tokens in the stack

func (*Stack) Peek

func (self *Stack) Peek() Token

Peek returns the token at the top of the stack

func (*Stack) Pop

func (self *Stack) Pop() Token

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

func (*Stack) Push

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

Push adds tokens to the top of the stack

type Token

type Token struct {
	Type  TokenType
	Value string
}

type TokenType

type TokenType int
const (
	NUMBER TokenType = iota
	LPAREN
	RPAREN
	CONSTANT
	FUNCTION
	OPERATOR
	WHITESPACE
	ERROR
	EOF
)

Jump to

Keyboard shortcuts

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