diff --git a/stdlib/arith.go b/stdlib/arith.go new file mode 100644 index 0000000..36aeefc --- /dev/null +++ b/stdlib/arith.go @@ -0,0 +1,182 @@ +/* SHS: Syntactically Homogeneous Shell + * Copyright (C) 2019 Aidan Hahn + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package stdlib + +import ( + "fmt" + "strconv" + "git.callpipe.com/aidan/shs/ast" + "git.callpipe.com/aidan/shs/log" +) + +// PKG WIDE TODO: BIGNUM SYSTEM +// then write actually optimal routines once it is in place +// perhaps we simply write out arithmetic routines that operate on the strings +// then we need not worry about storage length. + +func add(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token { + var res float64 + + for i := in; i != nil; i = i.Next { + if i.Tag != ast.NUMBER { + log.Log(log.ERR, "Non-number given to ADD", "add") + return nil + } + + token := i.Inner.(string) + isFloat := false + for _, char := range token { + if char == '.' { + isFloat = true + n, err := strconv.ParseFloat(token, 64) + if err != nil { + log.Log(log.ERR, "Err parsing number: " + err.Error(), "add") + return nil + } + + res += n + } + } + + if !isFloat { + n, err := strconv.ParseInt(token, 10, 64) + if err != nil { + log.Log(log.ERR, "Err parsing number: " + err.Error(), "add") + return nil + } + + res += float64(n) + } + } + + return &ast.Token{Tag: ast.NUMBER, Inner: fmt.Sprintf("%f", res)} +} + +func sub(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token { + var res float64 + + for i := in; i != nil; i = i.Next { + if i.Tag != ast.NUMBER { + log.Log(log.ERR, "Non-number given to SUB", "sub") + return nil + } + + token := i.Inner.(string) + isFloat := false + for _, char := range token { + if char == '.' { + isFloat = true + n, err := strconv.ParseFloat(token, 64) + if err != nil { + log.Log(log.ERR, "Err parsing number: " + err.Error(), "sub") + return nil + } + + res -= n + } + } + + if !isFloat { + n, err := strconv.ParseInt(token, 10, 64) + if err != nil { + log.Log(log.ERR, "Err parsing number: " + err.Error(), "sub") + return nil + } + + res -= float64(n) + } + } + + return &ast.Token{Tag: ast.NUMBER, Inner: fmt.Sprintf("%f", res)} +} + +func mult(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token { + var res float64 + + for i := in; i != nil; i = i.Next { + if i.Tag != ast.NUMBER { + log.Log(log.ERR, "Non-number given to MULT", "mult") + return nil + } + + token := i.Inner.(string) + isFloat := false + for _, char := range token { + if char == '.' { + isFloat = true + n, err := strconv.ParseFloat(token, 64) + if err != nil { + log.Log(log.ERR, "Err parsing number: " + err.Error(), "mult") + return nil + } + + res *= n + } + } + + if !isFloat { + n, err := strconv.ParseInt(token, 10, 64) + if err != nil { + log.Log(log.ERR, "Err parsing number: " + err.Error(), "mult") + return nil + } + + res *= float64(n) + } + } + + return &ast.Token{Tag: ast.NUMBER, Inner: fmt.Sprintf("%f", res)} +} + +func div(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token { + var res float64 + + for i := in; i != nil; i = i.Next { + if i.Tag != ast.NUMBER { + log.Log(log.ERR, "Non-number given to DIV", "div") + return nil + } + + token := i.Inner.(string) + isFloat := false + for _, char := range token { + if char == '.' { + isFloat = true + n, err := strconv.ParseFloat(token, 64) + if err != nil { + log.Log(log.ERR, "Err parsing number: " + err.Error(), "div") + return nil + } + + res /= n + } + } + + if !isFloat { + n, err := strconv.ParseInt(token, 10, 64) + if err != nil { + log.Log(log.ERR, "Err parsing number: " + err.Error(), "div") + return nil + } + + res /= float64(n) + } + } + + return &ast.Token{Tag: ast.NUMBER, Inner: fmt.Sprintf("%f", res)} +} diff --git a/stdlib/stdlib.go b/stdlib/stdlib.go index 43d24fa..7c4dab6 100644 --- a/stdlib/stdlib.go +++ b/stdlib/stdlib.go @@ -37,6 +37,34 @@ func GenFuncTable() ast.FuncTable { TimesCalled: 0, Args: -1, }, + + "+": &ast.Function{ + Function: add, + Name: "add", + TimesCalled: 0, + Args: -1, + }, + + "-": &ast.Function{ + Function: sub, + Name: "sub", + TimesCalled: 0, + Args: -1, + }, + + "*": &ast.Function{ + Function: mult, + Name: "mult", + TimesCalled: 0, + Args: -1, + }, + + "div": &ast.Function{ + Function: div, + Name: "div", + TimesCalled: 0, + Args: -1, + }, } return stdlib