diff --git a/README.md b/README.md index 9f736a9..9df32af 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/compute/compute.go b/compute/compute.go index 337fd62..be47a22 100644 --- a/compute/compute.go +++ b/compute/compute.go @@ -1,6 +1,8 @@ package compute import ( + "encoding/binary" + "encoding/hex" "errors" "go/scanner" "go/token" @@ -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{} @@ -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 ") term.AutoCompleteCallback = handleKey + + format := "G" + formatRE := regexp.MustCompile("^format\\((.)\\)$") + for { text, err := term.ReadLine() if err != nil { @@ -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)))) + } + } } diff --git a/operators/functions/abs.go b/operators/functions/abs.go index 8c63316..ac5334a 100644 --- a/operators/functions/abs.go +++ b/operators/functions/abs.go @@ -5,7 +5,7 @@ import ( ) import ( - "github.com/alfredxing/calc/operators" + "github.com/denysvitali/calc/operators" ) var ( diff --git a/operators/functions/functions.go b/operators/functions/functions.go index 15a6d79..1db0ae0 100644 --- a/operators/functions/functions.go +++ b/operators/functions/functions.go @@ -1,7 +1,7 @@ package functions import ( - "github.com/alfredxing/calc/operators" + "github.com/denysvitali/calc/operators" ) var Names = map[string]bool{} diff --git a/operators/functions/log.go b/operators/functions/log.go index 1d1ac8c..481a01c 100644 --- a/operators/functions/log.go +++ b/operators/functions/log.go @@ -5,7 +5,7 @@ import ( ) import ( - "github.com/alfredxing/calc/operators" + "github.com/denysvitali/calc/operators" ) var ( diff --git a/operators/functions/sqrt.go b/operators/functions/sqrt.go index 0c74fbd..6f369e7 100644 --- a/operators/functions/sqrt.go +++ b/operators/functions/sqrt.go @@ -5,7 +5,7 @@ import ( ) import ( - "github.com/alfredxing/calc/operators" + "github.com/denysvitali/calc/operators" ) var ( diff --git a/operators/functions/trig.go b/operators/functions/trig.go index e8daa54..897bd3d 100644 --- a/operators/functions/trig.go +++ b/operators/functions/trig.go @@ -5,7 +5,7 @@ import ( ) import ( - "github.com/alfredxing/calc/operators" + "github.com/denysvitali/calc/operators" ) var (