Documentation ¶
Index ¶
- Constants
- Variables
- func Ceil(n *big.Rat) *big.Rat
- func Eval(expr string) (*big.Rat, error)
- func Exec(expr string, vars map[string]*big.Rat) (*big.Rat, error)
- func Factorial(n *big.Rat) *big.Rat
- func Floor(n *big.Rat) *big.Rat
- func Gcd(x, y *big.Rat) *big.Rat
- func IsValidIdent(s string) bool
- func Max(a, b *big.Rat) *big.Rat
- func Min(a, b *big.Rat) *big.Rat
- func Mod(x, y *big.Rat) *big.Rat
- func RationalToInteger(n *big.Rat) *big.Int
- type Parser
- type Token
- type TokenType
- type Tokens
Constants ¶
const ( AssocLeft association = iota AssocRight )
Variables ¶
var ( // RatTrue represents true in boolean operations RatTrue = big.NewRat(1, 1) // RatFalse represents false in boolean operations RatFalse = big.NewRat(0, 1) ErrUnmatchedParentheses = errors.New("Unmatched parentheses") ErrMisplacedComma = errors.New("Misplaced ‘,’") ErrAssignToLiteral = errors.New("Can't assign to literal") )
var ErrDivisionByZero = errors.New("Division by zero")
var FunctionNames []string
FunctionNames holds all the function names that are available for use
Functions ¶
func Eval ¶
Eval evaluates an expression and returns its result and any errors found.
Example:
res, err := mathcat.Eval("2 * 2 * 2") // 8
func Exec ¶
Exec executes an expression with a given map of variables.
Example:
res, err := mathcat.Exec("a + b * b", map[string]*big.Rat{ "a": big.NewRat(1, 1), "b": big.NewRat(3, 1), }) // 10
func IsValidIdent ¶
IsValidIdent checks if a string qualifies as a valid identifier.
Types ¶
type Parser ¶
type Parser struct { Tokens Tokens Variables map[string]*big.Rat // contains filtered or unexported fields }
Parser holds the lexed tokens, token position, declared variables and stacks used throughout the parsing of an expression.
By default, variables always contains the constants defined below. These can however be overwritten.
func New ¶
func New() *Parser
New initializes a new Parser instance, useful when you want to run multiple expression and/or use variables.
type Token ¶
Token is an entity in an expression
func (Token) IsAssignment ¶
IsAssignment checks if the token is an assignment operator
func (Token) IsOperator ¶
IsOperator checks if the token is an operator
type TokenType ¶
type TokenType int
TokenType represents the type of token
const ( Eol TokenType = iota // end of line Ident // x Decimal // 3 Hex // 0xDEADBEEF Binary // 0b10101101100 Octal // 0o666 Add // + Sub // - Div // / Mul // * Pow // ** Rem // % UnaryMin // - And // & Or // | Xor // ^ Lsh // << Rsh // >> Not // ~ AndEq // &= OrEq // |= XorEq // ^= LshEq // <<= RshEq // >>= Eq // = AddEq // += SubEq // -= DivEq // /= MulEq // *= PowEq // **= RemEq // %= NotEq // != EqEq // == Gt // > GtEq // >= Lt // < LtEq // <= Lparen // ( Rparen // ) Comma // , )