Skip to content
This repository was archived by the owner on Mar 19, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
## calc

[![GoDoc](https://godoc.org/github.com/alfredxing/calc?status.svg)](https://godoc.org/github.com/alfredxing/calc) [![Build Status](https://travis-ci.org/alfredxing/calc.svg?branch=master)](https://travis-ci.org/alfredxing/calc)
[![GoDoc](https://godoc.org/github.com/denysvitali/calc?status.svg)](https://godoc.org/github.com/denysvitali/calc) [![Build Status](https://travis-ci.org/alfredxing/calc.svg?branch=master)](https://travis-ci.org/alfredxing/calc)

A simple, fast, and intuitive command-line calculator written in Go.

### Install
Install calc as you would any other Go program:
```
go get github.com/alfredxing/calc
go get github.com/denysvitali/calc
```

### Usage
Expand Down
31 changes: 28 additions & 3 deletions compute/compute.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package compute

import (
"encoding/binary"
"encoding/hex"
"errors"
"go/scanner"
"go/token"
Expand All @@ -9,9 +11,9 @@ import (
)

import (
"github.com/alfredxing/calc/constants"
"github.com/alfredxing/calc/operators"
"github.com/alfredxing/calc/operators/functions"
"github.com/denysvitali/calc/constants"
"github.com/denysvitali/calc/operators"
"github.com/denysvitali/calc/operators/functions"
)

var resHistory = []float64{}
Expand Down Expand Up @@ -182,6 +184,29 @@ func isNegation(tok token.Token, prev token.Token) bool {
}

func parseFloat(lit string) (float64, error) {
if len(lit) > 2 {
if lit[0:2] == "0x" {
// Hex
hexBytes, err := hex.DecodeString(lit[2:])
if err != nil {
return 0, errors.New("Cannot parse hex: " + lit)
}

var number []byte

diff := 8 - len(hexBytes)
if diff < 0 {
return 0, errors.New("hex exceeds uint64")
}

for i:=0; i<diff; i++ {
number = append(number, 0x00)
}

number = append(number, hexBytes...)
return float64(binary.BigEndian.Uint64(number)), nil
}
}
f, err := strconv.ParseFloat(lit, 64)
if err != nil {
return 0, errors.New("Cannot parse recognized float: " + lit)
Expand Down
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/denysvitali/calc

go 1.20

require golang.org/x/crypto v0.8.0

require (
golang.org/x/sys v0.8.0 // indirect
golang.org/x/term v0.8.0 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ=
golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
22 changes: 20 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"fmt"
"io"
"os"
"regexp"
"strconv"
"strings"
)

import (
"github.com/alfredxing/calc/compute"
"github.com/denysvitali/calc/compute"
)

import (
Expand Down Expand Up @@ -39,6 +40,10 @@ func main() {

term := terminal.NewTerminal(os.Stdin, "> ")
term.AutoCompleteCallback = handleKey

format := "G"
formatRE := regexp.MustCompile("^format\\((.)\\)$")

for {
text, err := term.ReadLine()
if err != nil {
Expand All @@ -54,12 +59,25 @@ func main() {
break
}

if formatRE.MatchString(text) {
matches := formatRE.FindAllStringSubmatch(text, -1)
format = matches[0][1]
continue
}

res, err := compute.Evaluate(text)
if err != nil {
term.Write([]byte(fmt.Sprintln("Error: " + err.Error())))
continue
}
term.Write([]byte(fmt.Sprintln(strconv.FormatFloat(res, 'G', -1, 64))))

switch format {
case "H":
term.Write([]byte(fmt.Sprintf("0x%02X\n", int(res))))
default:
term.Write([]byte(fmt.Sprintln(strconv.FormatFloat(res, format[0], -1, 64))))
}

}
}

Expand Down
2 changes: 1 addition & 1 deletion operators/functions/abs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

import (
"github.com/alfredxing/calc/operators"
"github.com/denysvitali/calc/operators"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion operators/functions/functions.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package functions

import (
"github.com/alfredxing/calc/operators"
"github.com/denysvitali/calc/operators"
)

var Names = map[string]bool{}
Expand Down
2 changes: 1 addition & 1 deletion operators/functions/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

import (
"github.com/alfredxing/calc/operators"
"github.com/denysvitali/calc/operators"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion operators/functions/sqrt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

import (
"github.com/alfredxing/calc/operators"
"github.com/denysvitali/calc/operators"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion operators/functions/trig.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

import (
"github.com/alfredxing/calc/operators"
"github.com/denysvitali/calc/operators"
)

var (
Expand Down