calc

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 27, 2025 License: MIT Imports: 3 Imported by: 0

README

calc-go

A flexible command-line calculator written in Go. Supports arithmetic, scientific functions, constants, and custom grouping with (), [], {}.

Features

  • Basic arithmetic: +, -, *, /, ^
  • Grouping: (), [], {} (can be mixed)
  • Scientific functions: sin, cos, tan, asin, acos, atan, ln, log, sqrt, abs, ceil, floor, round, exp, max, min, gcd, lcm, factorial, and more
  • Constants: pi, e
  • Multi-argument functions: e.g. max(2, 5), lcm(3, 4)
  • Clean output: prints integers without a decimal, trims trailing zeros for floats

Usage

Build the binary:

go build -o calc ./cmd/calc

Run interactively:

./calc

Or pass an expression directly:

./calc "sin(pi/2) + max(2, 5)"

Example Expressions

1 + 2 * 3
sin(pi/2)
lcm(3, 4)
max(2, 5)
(2 + 3) * [4 - 1]
log(100)
2.5 + 3.1

Releasing

This project uses GoReleaser for multi-platform releases. See .goreleaser.yaml for details.

License

MIT License. See LICENSE.

Documentation

Index

Constants

View Source
const (
	// Version is the current version of the calc package.
	Version = "0.1.0"
)

Variables

This section is empty.

Functions

func Factorial

func Factorial(n int) int

func GCD

func GCD(a, b int) int

func LCM

func LCM(a, b int) int

Types

type BinaryOpNode

type BinaryOpNode struct {
	Op    TokenType
	Left  Node
	Right Node
}

func (*BinaryOpNode) Eval

func (n *BinaryOpNode) Eval() float64

type ConstNode

type ConstNode struct {
	Name string
}

func (*ConstNode) Eval

func (n *ConstNode) Eval() float64

type FuncCallNode

type FuncCallNode struct {
	Name string
	Args []Node
}

func (*FuncCallNode) Eval

func (n *FuncCallNode) Eval() float64

type Lexer

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

func NewLexer

func NewLexer(input string) *Lexer

func (*Lexer) NextToken

func (l *Lexer) NextToken() Token

type Node

type Node interface {
	Eval() float64
}

type NumberNode

type NumberNode struct {
	Value float64
}

func (*NumberNode) Eval

func (n *NumberNode) Eval() float64

type Parser

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

func NewParser

func NewParser(input string) *Parser

func (*Parser) Parse

func (p *Parser) Parse() Node

type Token

type Token struct {
	Type  TokenType
	Value string
}

func (Token) String

func (t Token) String() string

type TokenType

type TokenType int
const (
	TokenEOF TokenType = iota
	TokenNumber
	TokenPlus
	TokenMinus
	TokenMul
	TokenDiv
	TokenPow
	TokenLParen
	TokenRParen
	TokenLBracket
	TokenRBracket
	TokenLBrace
	TokenRBrace
	TokenIdent
	TokenComma
)

type UnaryOpNode

type UnaryOpNode struct {
	Op    TokenType
	Child Node
}

func (*UnaryOpNode) Eval

func (n *UnaryOpNode) Eval() float64

Directories

Path Synopsis
cmd
calc command

Jump to

Keyboard shortcuts

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