From 530f499b766f077a30feacd0c8182f9eebb6fb6d Mon Sep 17 00:00:00 2001 From: Alfred Xing Date: Mon, 16 Feb 2015 14:23:33 -0800 Subject: [PATCH] Format the code with `go fmt` Suggested in #2 --- compute/compute.go | 278 +++++++++++++++---------------- compute/compute_test.go | 115 ++++++------- compute/stack.go | 98 +++++------ constants/constants.go | 14 +- constants/standard.go | 32 ++-- main.go | 88 +++++----- operators/functions/abs.go | 24 +-- operators/functions/functions.go | 8 +- operators/functions/log.go | 64 +++---- operators/functions/sqrt.go | 24 +-- operators/functions/trig.go | 124 +++++++------- operators/operators.go | 30 ++-- operators/standard.go | 122 +++++++------- 13 files changed, 511 insertions(+), 510 deletions(-) diff --git a/compute/compute.go b/compute/compute.go index 767f2ee..891a882 100644 --- a/compute/compute.go +++ b/compute/compute.go @@ -1,179 +1,179 @@ package compute import ( - "strings" - "strconv" - "errors" - "go/scanner" - "go/token" + "errors" + "go/scanner" + "go/token" + "strconv" + "strings" ) import ( - "github.com/alfredxing/calc/operators" - "github.com/alfredxing/calc/operators/functions" - "github.com/alfredxing/calc/constants" + "github.com/alfredxing/calc/constants" + "github.com/alfredxing/calc/operators" + "github.com/alfredxing/calc/operators/functions" ) func Evaluate(in string) (float64, error) { - floats := NewFloatStack() - ops := NewStringStack() - s := initScanner(in) + floats := NewFloatStack() + ops := NewStringStack() + s := initScanner(in) - var prev token.Token = token.ILLEGAL + var prev token.Token = token.ILLEGAL ScanLoop: - for { - _, tok, lit := s.Scan() - switch { - case tok == token.EOF: - break ScanLoop - case constants.IsConstant(lit): - floats.Push(constants.GetValue(lit)) - if prev == token.RPAREN || isOperand(prev) { - evalUnprecedenced("*", ops, floats) - } - case isOperand(tok): - val, err := parseFloat(lit) - if err != nil { - return 0, err - } - floats.Push(val) - if prev == token.RPAREN || constants.IsConstant(prev.String()) { - evalUnprecedenced("*", ops, floats) - } - case functions.IsFunction(lit): - if isOperand(prev) || prev == token.RPAREN { - evalUnprecedenced("*", ops, floats) - } - ops.Push(lit) - case isOperator(tok.String()): - op := tok.String() - if isNegation(tok, prev) { - op = "neg" - } - evalUnprecedenced(op, ops, floats) - case tok == token.LPAREN: - if isOperand(prev) { - evalUnprecedenced("*", ops, floats) - } - ops.Push(tok.String()) - case tok == token.RPAREN: - for ops.Pos >= 0 && ops.SafeTop() != "(" { - err := evalOp(ops.SafePop(), floats) - if err != nil { - return 0, err - } - } - _, err := ops.Pop() - if err != nil { - return 0, errors.New("Can't find matching parenthesis!") - } - if ops.Pos >= 0 { - if functions.IsFunction(ops.SafeTop()) { - err := evalOp(ops.SafePop(), floats) - if err != nil { - return 0, err - } - } - } - case tok == token.SEMICOLON: - default: - inspect := tok.String() - if strings.TrimSpace(lit) != "" { - inspect += " (`" + lit + "`)" - } - return 0, errors.New("Unrecognized token " + inspect + " in expression") - } - prev = tok - } - - for ops.Pos >= 0 { - op, _ := ops.Pop() - err := evalOp(op, floats) - if err != nil { - return 0, err - } - } - - res, err := floats.Top() - if err != nil { - return 0, errors.New("Expression could not be parsed!") - } - return res, nil + for { + _, tok, lit := s.Scan() + switch { + case tok == token.EOF: + break ScanLoop + case constants.IsConstant(lit): + floats.Push(constants.GetValue(lit)) + if prev == token.RPAREN || isOperand(prev) { + evalUnprecedenced("*", ops, floats) + } + case isOperand(tok): + val, err := parseFloat(lit) + if err != nil { + return 0, err + } + floats.Push(val) + if prev == token.RPAREN || constants.IsConstant(prev.String()) { + evalUnprecedenced("*", ops, floats) + } + case functions.IsFunction(lit): + if isOperand(prev) || prev == token.RPAREN { + evalUnprecedenced("*", ops, floats) + } + ops.Push(lit) + case isOperator(tok.String()): + op := tok.String() + if isNegation(tok, prev) { + op = "neg" + } + evalUnprecedenced(op, ops, floats) + case tok == token.LPAREN: + if isOperand(prev) { + evalUnprecedenced("*", ops, floats) + } + ops.Push(tok.String()) + case tok == token.RPAREN: + for ops.Pos >= 0 && ops.SafeTop() != "(" { + err := evalOp(ops.SafePop(), floats) + if err != nil { + return 0, err + } + } + _, err := ops.Pop() + if err != nil { + return 0, errors.New("Can't find matching parenthesis!") + } + if ops.Pos >= 0 { + if functions.IsFunction(ops.SafeTop()) { + err := evalOp(ops.SafePop(), floats) + if err != nil { + return 0, err + } + } + } + case tok == token.SEMICOLON: + default: + inspect := tok.String() + if strings.TrimSpace(lit) != "" { + inspect += " (`" + lit + "`)" + } + return 0, errors.New("Unrecognized token " + inspect + " in expression") + } + prev = tok + } + + for ops.Pos >= 0 { + op, _ := ops.Pop() + err := evalOp(op, floats) + if err != nil { + return 0, err + } + } + + res, err := floats.Top() + if err != nil { + return 0, errors.New("Expression could not be parsed!") + } + return res, nil } func evalUnprecedenced(op string, ops *StringStack, floats *FloatStack) { - for ops.Pos >= 0 && shouldPopNext(op, ops.SafeTop()) { - evalOp(ops.SafePop(), floats) - } - ops.Push(op) + for ops.Pos >= 0 && shouldPopNext(op, ops.SafeTop()) { + evalOp(ops.SafePop(), floats) + } + ops.Push(op) } func shouldPopNext(n1 string, n2 string) bool { - if !isOperator(n2) { - return false - } - if n1 == "neg" { - return false - } - op1 := parseOperator(n1) - op2 := parseOperator(n2) - if op1.Associativity == operators.L { - return op1.Precedence <= op2.Precedence - } - return op1.Precedence < op2.Precedence + if !isOperator(n2) { + return false + } + if n1 == "neg" { + return false + } + op1 := parseOperator(n1) + op2 := parseOperator(n2) + if op1.Associativity == operators.L { + return op1.Precedence <= op2.Precedence + } + return op1.Precedence < op2.Precedence } func evalOp(opName string, floats *FloatStack) error { - op := operators.FindOperatorFromString(opName) - if op == nil { - return errors.New("Either unmatched paren or unrecognized operator") - } - - var args = make([]float64, op.Args) - for i := op.Args - 1; i >= 0; i-- { - arg, err := floats.Pop() - if err != nil { - return errors.New("Not enough arguments to operator!") - } - args[i] = arg - } - - floats.Push(op.Operation(args)) - - return nil + op := operators.FindOperatorFromString(opName) + if op == nil { + return errors.New("Either unmatched paren or unrecognized operator") + } + + var args = make([]float64, op.Args) + for i := op.Args - 1; i >= 0; i-- { + arg, err := floats.Pop() + if err != nil { + return errors.New("Not enough arguments to operator!") + } + args[i] = arg + } + + floats.Push(op.Operation(args)) + + return nil } func isOperand(tok token.Token) bool { - return tok == token.FLOAT || tok == token.INT + return tok == token.FLOAT || tok == token.INT } func isOperator(lit string) bool { - return operators.IsOperator(lit) + return operators.IsOperator(lit) } func isNegation(tok token.Token, prev token.Token) bool { - return tok == token.SUB && - (prev == token.ILLEGAL || isOperator(prev.String()) || prev == token.LPAREN) + return tok == token.SUB && + (prev == token.ILLEGAL || isOperator(prev.String()) || prev == token.LPAREN) } func parseFloat(lit string) (float64, error) { - f, err := strconv.ParseFloat(lit, 64) - if err != nil { - return 0, errors.New("Cannot parse recognized float: " + lit) - } - return f, nil + f, err := strconv.ParseFloat(lit, 64) + if err != nil { + return 0, errors.New("Cannot parse recognized float: " + lit) + } + return f, nil } func parseOperator(lit string) *operators.Operator { - return operators.FindOperatorFromString(lit) + return operators.FindOperatorFromString(lit) } func initScanner(in string) scanner.Scanner { - var s scanner.Scanner - src := []byte(in) - fset := token.NewFileSet() - file := fset.AddFile("", fset.Base(), len(src)) - s.Init(file, src, nil, 0) - return s + var s scanner.Scanner + src := []byte(in) + fset := token.NewFileSet() + file := fset.AddFile("", fset.Base(), len(src)) + s.Init(file, src, nil, 0) + return s } diff --git a/compute/compute_test.go b/compute/compute_test.go index 236da2d..bed24cd 100644 --- a/compute/compute_test.go +++ b/compute/compute_test.go @@ -1,71 +1,72 @@ package compute import ( - "testing" - "math" - "strconv" + "math" + "strconv" + "testing" ) var exps = map[string]float64{ - "1+1": 2, - "1+2^3^2": 513, - "2^(3+4)": 128, - "2^(3/(1+2))": 2, - "2(1+3)": 8, - "2^2(1+3)": 16, - "-2": -2, - "1+(-1)^2": 2, - "3-5": -2, - "-3*4": -12, - "3*-4": -12, - "3*(3-(5+6)^12)*23^3-5^23": -126476703133661843, - "2^3^2": 512, - "-3^2": -9, - "2(1+1)4": 16, - "3*abs(1-5)": 12, - "ln(3^15)": 16.47918433, - "sqrt(10)": 3.16227766, - "abs(-3/2)": 1.5, - "1+2sin(-1024)tan(acos(1))^2": 1, - "tan(10)cos(20)": 0.2645844, - "2(e^3)": 40.1710738464, - "sin(pi*π)": -0.430301217, - "3π": 9.42477796, + "1+1": 2, + "1+2^3^2": 513, + "2^(3+4)": 128, + "2^(3/(1+2))": 2, + "2(1+3)": 8, + "2^2(1+3)": 16, + "-2": -2, + "1+(-1)^2": 2, + "3-5": -2, + "-3*4": -12, + "3*-4": -12, + "3*(3-(5+6)^12)*23^3-5^23": -126476703133661843, + "2^3^2": 512, + "-3^2": -9, + "2(1+1)4": 16, + "3*abs(1-5)": 12, + "ln(3^15)": 16.47918433, + "sqrt(10)": 3.16227766, + "abs(-3/2)": 1.5, + "1+2sin(-1024)tan(acos(1))^2": 1, + "tan(10)cos(20)": 0.2645844, + "2(e^3)": 40.1710738464, + "sin(pi*π)": -0.430301217, + "3π": 9.42477796, } + const DELTA = 0.000001 func TestEvaluate(t *testing.T) { - for expression, expected := range exps { - res, err := Evaluate(expression) - if err != nil { - t.Error(err) - } else if math.Abs(res - expected) > DELTA { - message := expression + " failed: actual value " + - strconv.FormatFloat(res, 'G', -1, 64) + - " differs from expected value " + - strconv.FormatFloat(expected, 'G', -1, 64) - t.Error(message) - } - } + for expression, expected := range exps { + res, err := Evaluate(expression) + if err != nil { + t.Error(err) + } else if math.Abs(res-expected) > DELTA { + message := expression + " failed: actual value " + + strconv.FormatFloat(res, 'G', -1, 64) + + " differs from expected value " + + strconv.FormatFloat(expected, 'G', -1, 64) + t.Error(message) + } + } } func BenchmarkEvaluate(b *testing.B) { - tests := []string{ - "π", - "1+2^3^2", - "2^(3+4)", - "2^(3/(1+2))", - "2^2(1+3)", - "1+(-1)^2", - "3*(3-(5+6)^12)*23^3-5^23", - "2^3^2", - "ln(3^15)", - "sqrt(10)", - "abs(-3/2)", - "1+2sin(-1024)tan(acos(1))^2", - "tan(10)cos(20)", - } - for i := 0; i < b.N; i++ { - Evaluate(tests[i % len(tests)]) - } + tests := []string{ + "π", + "1+2^3^2", + "2^(3+4)", + "2^(3/(1+2))", + "2^2(1+3)", + "1+(-1)^2", + "3*(3-(5+6)^12)*23^3-5^23", + "2^3^2", + "ln(3^15)", + "sqrt(10)", + "abs(-3/2)", + "1+2sin(-1024)tan(acos(1))^2", + "tan(10)cos(20)", + } + for i := 0; i < b.N; i++ { + Evaluate(tests[i%len(tests)]) + } } diff --git a/compute/stack.go b/compute/stack.go index d08c0e1..75743aa 100644 --- a/compute/stack.go +++ b/compute/stack.go @@ -1,89 +1,89 @@ package compute import ( - "errors" + "errors" ) type StringStack struct { - Slice []string - Pos int + Slice []string + Pos int } type FloatStack struct { - Slice []float64 - Pos int + Slice []float64 + Pos int } func NewStringStack() *StringStack { - return &StringStack{ - Slice: []string{}, - Pos: -1, - } + return &StringStack{ + Slice: []string{}, + Pos: -1, + } } func NewFloatStack() *FloatStack { - return &FloatStack{ - Slice: []float64{}, - Pos: -1, - } + return &FloatStack{ + Slice: []float64{}, + Pos: -1, + } } func (s *StringStack) Push(a string) { - s.Pos++ - if s.Pos < len(s.Slice) { - s.Slice[s.Pos] = a - } else { - s.Slice = append(s.Slice, a) - } + s.Pos++ + if s.Pos < len(s.Slice) { + s.Slice[s.Pos] = a + } else { + s.Slice = append(s.Slice, a) + } } func (s *StringStack) Pop() (string, error) { - ret, err := s.Top() - if err != nil { - return "", errors.New("Can't pop; stack is empty!") - } - s.Pos-- - return ret, nil + ret, err := s.Top() + if err != nil { + return "", errors.New("Can't pop; stack is empty!") + } + s.Pos-- + return ret, nil } func (s *StringStack) SafePop() string { - ret, _ := s.Pop() - return ret + ret, _ := s.Pop() + return ret } func (s *StringStack) Top() (string, error) { - if s.Pos < 0 { - return "", errors.New("No elements in stack!") - } - return s.Slice[s.Pos], nil + if s.Pos < 0 { + return "", errors.New("No elements in stack!") + } + return s.Slice[s.Pos], nil } func (s *StringStack) SafeTop() string { - ret, _ := s.Top() - return ret + ret, _ := s.Top() + return ret } func (s *FloatStack) Push(a float64) { - s.Pos++ - if s.Pos < len(s.Slice) { - s.Slice[s.Pos] = a - } else { - s.Slice = append(s.Slice, a) - } + s.Pos++ + if s.Pos < len(s.Slice) { + s.Slice[s.Pos] = a + } else { + s.Slice = append(s.Slice, a) + } } func (s *FloatStack) Pop() (float64, error) { - ret, err := s.Top() - if err != nil { - return 0, errors.New("Can't pop; stack is empty!") - } - s.Pos-- - return ret, nil + ret, err := s.Top() + if err != nil { + return 0, errors.New("Can't pop; stack is empty!") + } + s.Pos-- + return ret, nil } func (s *FloatStack) Top() (float64, error) { - if s.Pos < 0 { - return 0, errors.New("No elements in stack!") - } - return s.Slice[s.Pos], nil + if s.Pos < 0 { + return 0, errors.New("No elements in stack!") + } + return s.Slice[s.Pos], nil } diff --git a/constants/constants.go b/constants/constants.go index f7a88b4..211edf7 100644 --- a/constants/constants.go +++ b/constants/constants.go @@ -3,20 +3,20 @@ package constants var Constants = map[string]float64{} type Constant struct { - Name string - Value float64 + Name string + Value float64 } func Register(c *Constant) { - Constants[c.Name] = c.Value + Constants[c.Name] = c.Value } func IsConstant(str string) bool { - _, exist := Constants[str] - return exist + _, exist := Constants[str] + return exist } func GetValue(str string) float64 { - val, _ := Constants[str] - return val + val, _ := Constants[str] + return val } diff --git a/constants/standard.go b/constants/standard.go index 57fc293..5210aa5 100644 --- a/constants/standard.go +++ b/constants/standard.go @@ -1,26 +1,26 @@ package constants import ( - "math" + "math" ) var ( - e = &Constant{ - Name: "e", - Value: math.E, - } - pi = &Constant{ - Name: "pi", - Value: math.Pi, - } - piSym = &Constant{ - Name: "π", - Value: math.Pi, - } + e = &Constant{ + Name: "e", + Value: math.E, + } + pi = &Constant{ + Name: "pi", + Value: math.Pi, + } + piSym = &Constant{ + Name: "π", + Value: math.Pi, + } ) func init() { - Register(e) - Register(pi) - Register(piSym) + Register(e) + Register(pi) + Register(piSym) } diff --git a/main.go b/main.go index 1c371c5..592c4b1 100644 --- a/main.go +++ b/main.go @@ -1,64 +1,64 @@ package main import ( - "fmt" - "os" - "strings" - "strconv" + "fmt" + "os" + "strconv" + "strings" ) import ( - "github.com/alfredxing/calc/compute" + "github.com/alfredxing/calc/compute" ) import ( - "golang.org/x/crypto/ssh/terminal" + "golang.org/x/crypto/ssh/terminal" ) func main() { - if len(os.Args) > 1 { - input := strings.Replace(strings.Join(os.Args[1:], ""), " ", "", -1) - res, err := compute.Evaluate(input) - if err != nil { - fmt.Println("Error: " + err.Error()) - return - } - fmt.Printf("%s\n", strconv.FormatFloat(res, 'G', -1, 64)) - return - } + if len(os.Args) > 1 { + input := strings.Replace(strings.Join(os.Args[1:], ""), " ", "", -1) + res, err := compute.Evaluate(input) + if err != nil { + fmt.Println("Error: " + err.Error()) + return + } + fmt.Printf("%s\n", strconv.FormatFloat(res, 'G', -1, 64)) + return + } - oldState, err := terminal.MakeRaw(0) - if err != nil { - panic(err) - } - defer terminal.Restore(0, oldState) + oldState, err := terminal.MakeRaw(0) + if err != nil { + panic(err) + } + defer terminal.Restore(0, oldState) - term := terminal.NewTerminal(os.Stdin, "> ") - term.AutoCompleteCallback = handleKey - for { - text, err := term.ReadLine() - if err != nil { - panic(err) - } + term := terminal.NewTerminal(os.Stdin, "> ") + term.AutoCompleteCallback = handleKey + for { + text, err := term.ReadLine() + if err != nil { + panic(err) + } - text = strings.Replace(text, " ", "", -1) - if text == "exit" || text == "quit" { - break - } + text = strings.Replace(text, " ", "", -1) + if text == "exit" || text == "quit" { + break + } - res, err := compute.Evaluate(text) - if err != nil { - fmt.Println("Error: " + err.Error()) - continue - } - fmt.Printf("%s\n", strconv.FormatFloat(res, 'G', -1, 64)) - } + res, err := compute.Evaluate(text) + if err != nil { + fmt.Println("Error: " + err.Error()) + continue + } + fmt.Printf("%s\n", strconv.FormatFloat(res, 'G', -1, 64)) + } } func handleKey(line string, pos int, key rune) (newLine string, newPos int, ok bool) { - if key == '\x03' { - fmt.Println() - os.Exit(0) - } - return "", 0, false + if key == '\x03' { + fmt.Println() + os.Exit(0) + } + return "", 0, false } diff --git a/operators/functions/abs.go b/operators/functions/abs.go index fe34722..8c63316 100644 --- a/operators/functions/abs.go +++ b/operators/functions/abs.go @@ -1,25 +1,25 @@ package functions import ( - "math" + "math" ) import ( - "github.com/alfredxing/calc/operators" + "github.com/alfredxing/calc/operators" ) var ( - abs = &operators.Operator{ - Name: "abs", - Precedence: 0, - Associativity: operators.L, - Args: 1, - Operation: func (args []float64) float64 { - return math.Abs(args[0]) - }, - } + abs = &operators.Operator{ + Name: "abs", + Precedence: 0, + Associativity: operators.L, + Args: 1, + Operation: func(args []float64) float64 { + return math.Abs(args[0]) + }, + } ) func init() { - Register(abs) + Register(abs) } diff --git a/operators/functions/functions.go b/operators/functions/functions.go index 71b38c3..15a6d79 100644 --- a/operators/functions/functions.go +++ b/operators/functions/functions.go @@ -1,16 +1,16 @@ package functions import ( - "github.com/alfredxing/calc/operators" + "github.com/alfredxing/calc/operators" ) var Names = map[string]bool{} func Register(op *operators.Operator) { - operators.Register(op) - Names[op.Name] = true + operators.Register(op) + Names[op.Name] = true } func IsFunction(str string) bool { - return Names[str] + return Names[str] } diff --git a/operators/functions/log.go b/operators/functions/log.go index 2f076cb..1d1ac8c 100644 --- a/operators/functions/log.go +++ b/operators/functions/log.go @@ -1,45 +1,45 @@ package functions import ( - "math" + "math" ) import ( - "github.com/alfredxing/calc/operators" + "github.com/alfredxing/calc/operators" ) var ( - log = &operators.Operator{ - Name: "log", - Precedence: 0, - Associativity: operators.L, - Args: 1, - Operation: func (args []float64) float64 { - return math.Log10(args[0]) - }, - } - ln = &operators.Operator{ - Name: "ln", - Precedence: 0, - Associativity: operators.L, - Args: 1, - Operation: func (args []float64) float64 { - return math.Log(args[0]) - }, - } - lg = &operators.Operator{ - Name: "lg", - Precedence: 0, - Associativity: operators.L, - Args: 1, - Operation: func (args []float64) float64 { - return math.Log2(args[0]) - }, - } + log = &operators.Operator{ + Name: "log", + Precedence: 0, + Associativity: operators.L, + Args: 1, + Operation: func(args []float64) float64 { + return math.Log10(args[0]) + }, + } + ln = &operators.Operator{ + Name: "ln", + Precedence: 0, + Associativity: operators.L, + Args: 1, + Operation: func(args []float64) float64 { + return math.Log(args[0]) + }, + } + lg = &operators.Operator{ + Name: "lg", + Precedence: 0, + Associativity: operators.L, + Args: 1, + Operation: func(args []float64) float64 { + return math.Log2(args[0]) + }, + } ) func init() { - Register(log) - Register(ln) - Register(lg) + Register(log) + Register(ln) + Register(lg) } diff --git a/operators/functions/sqrt.go b/operators/functions/sqrt.go index 98155b1..0c74fbd 100644 --- a/operators/functions/sqrt.go +++ b/operators/functions/sqrt.go @@ -1,25 +1,25 @@ package functions import ( - "math" + "math" ) import ( - "github.com/alfredxing/calc/operators" + "github.com/alfredxing/calc/operators" ) var ( - sqrt = &operators.Operator{ - Name: "sqrt", - Precedence: 0, - Associativity: operators.L, - Args: 1, - Operation: func (args []float64) float64 { - return math.Sqrt(args[0]) - }, - } + sqrt = &operators.Operator{ + Name: "sqrt", + Precedence: 0, + Associativity: operators.L, + Args: 1, + Operation: func(args []float64) float64 { + return math.Sqrt(args[0]) + }, + } ) func init() { - Register(sqrt) + Register(sqrt) } diff --git a/operators/functions/trig.go b/operators/functions/trig.go index 9509722..26bbf06 100644 --- a/operators/functions/trig.go +++ b/operators/functions/trig.go @@ -1,75 +1,75 @@ package functions import ( - "math" + "math" ) import ( - "github.com/alfredxing/calc/operators" + "github.com/alfredxing/calc/operators" ) var ( - sin = &operators.Operator{ - Name: "sin", - Precedence: 0, - Associativity: operators.L, - Args: 1, - Operation: func (args []float64) float64 { - return math.Sin(args[0]) - }, - } - cos = &operators.Operator{ - Name: "cos", - Precedence: 0, - Associativity: operators.L, - Args: 1, - Operation: func (args []float64) float64 { - return math.Cos(args[0]) - }, - } - tan = &operators.Operator{ - Name: "tan", - Precedence: 0, - Associativity: operators.L, - Args: 1, - Operation: func (args []float64) float64 { - return math.Tan(args[0]) - }, - } - asin = &operators.Operator{ - Name: "asin", - Precedence: 0, - Associativity: operators.L, - Args: 1, - Operation: func (args []float64) float64 { - return math.Asin(args[0]) - }, - } - acos = &operators.Operator{ - Name: "acos", - Precedence: 0, - Associativity: operators.L, - Args: 1, - Operation: func (args []float64) float64 { - return math.Acos(args[0]) - }, - } - atan = &operators.Operator{ - Name: "atan", - Precedence: 0, - Associativity: operators.L, - Args: 1, - Operation: func (args []float64) float64 { - return math.Atan(args[0]) - }, - } + sin = &operators.Operator{ + Name: "sin", + Precedence: 0, + Associativity: operators.L, + Args: 1, + Operation: func(args []float64) float64 { + return math.Sin(args[0]) + }, + } + cos = &operators.Operator{ + Name: "cos", + Precedence: 0, + Associativity: operators.L, + Args: 1, + Operation: func(args []float64) float64 { + return math.Cos(args[0]) + }, + } + tan = &operators.Operator{ + Name: "tan", + Precedence: 0, + Associativity: operators.L, + Args: 1, + Operation: func(args []float64) float64 { + return math.Tan(args[0]) + }, + } + asin = &operators.Operator{ + Name: "asin", + Precedence: 0, + Associativity: operators.L, + Args: 1, + Operation: func(args []float64) float64 { + return math.Asin(args[0]) + }, + } + acos = &operators.Operator{ + Name: "acos", + Precedence: 0, + Associativity: operators.L, + Args: 1, + Operation: func(args []float64) float64 { + return math.Acos(args[0]) + }, + } + atan = &operators.Operator{ + Name: "atan", + Precedence: 0, + Associativity: operators.L, + Args: 1, + Operation: func(args []float64) float64 { + return math.Atan(args[0]) + }, + } ) func init() { - Register(sin) - Register(cos) - Register(tan) - Register(asin) - Register(acos) - Register(atan) + Register(sin) + Register(cos) + Register(tan) + Register(asin) + Register(acos) + Register(atan) } diff --git a/operators/operators.go b/operators/operators.go index 2f31d96..d733bcb 100644 --- a/operators/operators.go +++ b/operators/operators.go @@ -3,31 +3,31 @@ package operators var Ops = map[string]*Operator{} const ( - L = 0 - R = 1 + L = 0 + R = 1 ) type Operator struct { - Name string - Precedence int - Associativity int - Args int - Operation func(args []float64) float64 + Name string + Precedence int + Associativity int + Args int + Operation func(args []float64) float64 } func Register(op *Operator) { - Ops[op.Name] = op + Ops[op.Name] = op } func IsOperator(str string) bool { - _, exist := Ops[str] - return exist + _, exist := Ops[str] + return exist } func FindOperatorFromString(str string) *Operator { - op, exist := Ops[str] - if exist { - return op - } - return nil + op, exist := Ops[str] + if exist { + return op + } + return nil } diff --git a/operators/standard.go b/operators/standard.go index 3f7f71b..f126dbb 100644 --- a/operators/standard.go +++ b/operators/standard.go @@ -1,71 +1,71 @@ package operators import ( - "math" + "math" ) var ( - add = &Operator{ - Name: "+", - Precedence: 1, - Associativity: L, - Args: 2, - Operation: func (args []float64) float64 { - return args[0] + args[1] - }, - } - sub = &Operator{ - Name: "-", - Precedence: 1, - Associativity: L, - Args: 2, - Operation: func (args []float64) float64 { - return args[0] - args[1] - }, - } - neg = &Operator{ - Name: "neg", - Precedence: 2, - Associativity: L, - Args: 1, - Operation: func (args []float64) float64 { - return 0 - args[0] - }, - } - mul = &Operator{ - Name: "*", - Precedence: 2, - Associativity: L, - Args: 2, - Operation: func (args []float64) float64 { - return args[0] * args[1] - }, - } - div = &Operator{ - Name: "/", - Precedence: 2, - Associativity: L, - Args: 2, - Operation: func (args []float64) float64 { - return args[0] / args[1] - }, - } - pow = &Operator{ - Name: "^", - Precedence: 3, - Associativity: R, - Args: 2, - Operation: func (args []float64) float64 { - return math.Pow(args[0], args[1]) - }, - } + add = &Operator{ + Name: "+", + Precedence: 1, + Associativity: L, + Args: 2, + Operation: func(args []float64) float64 { + return args[0] + args[1] + }, + } + sub = &Operator{ + Name: "-", + Precedence: 1, + Associativity: L, + Args: 2, + Operation: func(args []float64) float64 { + return args[0] - args[1] + }, + } + neg = &Operator{ + Name: "neg", + Precedence: 2, + Associativity: L, + Args: 1, + Operation: func(args []float64) float64 { + return 0 - args[0] + }, + } + mul = &Operator{ + Name: "*", + Precedence: 2, + Associativity: L, + Args: 2, + Operation: func(args []float64) float64 { + return args[0] * args[1] + }, + } + div = &Operator{ + Name: "/", + Precedence: 2, + Associativity: L, + Args: 2, + Operation: func(args []float64) float64 { + return args[0] / args[1] + }, + } + pow = &Operator{ + Name: "^", + Precedence: 3, + Associativity: R, + Args: 2, + Operation: func(args []float64) float64 { + return math.Pow(args[0], args[1]) + }, + } ) func init() { - Register(add) - Register(sub) - Register(neg) - Register(pow) - Register(mul) - Register(div) + Register(add) + Register(sub) + Register(neg) + Register(pow) + Register(mul) + Register(div) }