calculathor

package
v0.0.0-...-e54fe13 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2024 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareToken

func CompareToken(t1, t2 *Token) bool

Compare two tokens and return true if they are equal or false if not

@param t1 *Token @param t2 *Token

@return bool True if tokens are equal, false if not equal or any of it is nil

func CompareTokenString

func CompareTokenString(t1, t2 []Token) bool

Compare two token strings and return false if they don't match

@param t1 []Token @param t2 []Token

@return bool

func IsBracket

func IsBracket(s string) bool

func IsComparisonOp

func IsComparisonOp(s string) bool

func IsLeftBracket

func IsLeftBracket(s string) bool

func IsOperator

func IsOperator(s string) bool

func IsRightBracket

func IsRightBracket(s string) bool

func OperatorPriority

func OperatorPriority(op Token) uint8

Returns priority of operator based on its type

@param op Token Operator token

@return uint8 Priority

func StringifyTokenString

func StringifyTokenString(tokens []Token, v bool) string

Print array of tokens (eg. variable content)

@param []Token Slice of tokens @param bool Verbose mode - prints token type along with its content

func StringifyTokenType

func StringifyTokenType(t TokenType) string

Convert token type to human readable string to print it out

func TokenTypeOneOf

func TokenTypeOneOf(token *Token, types ...TokenType) bool

Types

type Token

type Token struct {
	Type   TokenType
	Hidden bool        // token and its ancestors should be hidden from user (added for calculation purposes, eg. * (multiplication) before polynomial)
	Value  interface{} // string representation of value
}

general token

func PostfixToValue

func PostfixToValue(postfix []Token) ([]Token, error)

Calculate value of postfix token string, we got only operands and operators, no brackets

@param postfix []Token Array of tokens in postfix notation

@return Token Resulting token

@return error Error if any

func TokenStringToPostfix

func TokenStringToPostfix(in []Token) ([]Token, error)

Convert token string from infix to postfix notation for easier calculation

@param in []Token Array of tokens in infix notation

@return []Token Array of tokens in postfix notation

@return error Error if any

func (*Token) String

func (t *Token) String() string

Format token as string to print it out

@param *Token Pointer to a token @param bool Return the token type before it's content (forces new line with \n)

@return string

type TokenStack

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

Token stack

func NewTokenStack

func NewTokenStack() *TokenStack

Create new token stack

@return *TokenStack

func (*TokenStack) Peek

func (s *TokenStack) Peek() Token

Look at the top token on the stack

@return Token

func (*TokenStack) Pop

func (s *TokenStack) Pop() Token

Pop token from stack

@return Token

func (*TokenStack) Push

func (s *TokenStack) Push(t Token)

Push token to stack

@param Token

type TokenType

type TokenType uint8
const (
	TokenTypeVariable TokenType = iota + 10 // start at 10, because 0-3 are already reserved for variable types and may expand in time
	TokenTypeFunction
	TokenTypeOperator
	TokenTypeBracket
	TokenTypeInt // simple number
	TokenTypeFloat
	TokenTypeEqualSign
	TokenTypeUnknownIdent // unknown identifier
	TokenTypeUnknown
)

func (*TokenType) String

func (tt *TokenType) String() string

Jump to

Keyboard shortcuts

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