gocalc

package module
v0.0.0-...-3996bc6 Latest Latest
Warning

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

Go to latest
Published: May 30, 2021 License: MIT Imports: 6 Imported by: 0

README

Gocalc

godoc license

Simple calculator on Go!

Install

go get github.com/Rollylni/Gocalc

Example

package example

import (
     "fmt"
     gocalc "github.com/Rollylni/Gocalc"
)

func calc(input string) {
     calc := gocalc.NewCalculator(input)
     res, err := calc.Process()
     if err != nil {
         fmt.Println(err)
     } else {
         fmt.Println(res.Value()) 
     }
}

func main() {
     calc("2 + 2 * 2")
     calc("PI + 1.0")
     calc("1 + 24.00")
}

Output

> 6
> 4.141593
> Operation error: Type Error!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsIdentifier

func IsIdentifier(chr rune) bool

func ToFloat

func ToFloat(tok *Token, bsize int) (float64, error)

func ToInt

func ToInt(tok *Token) (int, error)

func ToRune

func ToRune(chr string) rune

Types

type Calculator

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

func NewCalculator

func NewCalculator(input string) *Calculator

func (*Calculator) Process

func (calc *Calculator) Process() (*Token, error)

type Lexer

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

func NewLexer

func NewLexer(input string) *Lexer

func (*Lexer) Apply

func (lex *Lexer) Apply(length ...int)

func (*Lexer) Consume

func (lex *Lexer) Consume(length ...int) string

func (Lexer) Has

func (lex Lexer) Has() bool

func (*Lexer) Look

func (lex *Lexer) Look(length ...int) string

func (Lexer) Parse

func (lex Lexer) Parse() [](*Token)

type Priority

type Priority int
var (
	RIGHT Priority = 0
	LEFT  Priority = 1
)

type Token

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

func Back

func Back(arr []*Token) *Token

func ExecuteFloat

func ExecuteFloat(op *Token, num1 *Token, num2 *Token) *Token

func ExecuteInt

func ExecuteInt(op *Token, num1 *Token, num2 *Token) *Token

func MakeFloat

func MakeFloat(val float64) *Token

func MakeInt

func MakeInt(val int) *Token

func PopBack

func PopBack(arr []*Token) []*Token

func (Token) Pos

func (tok Token) Pos() int

func (*Token) SetType

func (tok *Token) SetType(val TokenType)

func (*Token) SetValue

func (tok *Token) SetValue(val string)

func (Token) Type

func (tok Token) Type() TokenType

func (Token) Value

func (tok Token) Value() string

type TokenType

type TokenType int
const (
	TT_UNKNOWN TokenType = iota
	TT_IDENTIFIER
	TT_INTEGER
	TT_FLOAT
	TT_DIVIDE
	TT_MODDIV
	TT_MULTI
	TT_MINUS
	TT_EXPON
	TT_PLUS
	TT_LPAREN
	TT_RPAREN
	TT_IGNORE
)

Jump to

Keyboard shortcuts

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