MVP grade arithmetic routines

This commit is contained in:
Aidan 2020-06-26 20:17:45 -07:00
parent 6a66c00ae5
commit 9301c77573
No known key found for this signature in database
GPG key ID: 327711E983899316
2 changed files with 210 additions and 0 deletions

182
stdlib/arith.go Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
*/
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)}
}

View file

@ -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