retrofitted arithmetic functions
This commit is contained in:
parent
f3e39e156c
commit
7c630d5a38
4 changed files with 264 additions and 1 deletions
|
|
@ -89,6 +89,11 @@ func (in *Token) Eval(funcs FuncTable, vars VarTable) *Token {
|
||||||
inner = &Token{inner: inner}
|
inner = &Token{inner: inner}
|
||||||
}
|
}
|
||||||
res = funct.CallFunction(inner.Next, vars, funcs).Eval(funcs, vars)
|
res = funct.CallFunction(inner.Next, vars, funcs).Eval(funcs, vars)
|
||||||
|
if res == nil {
|
||||||
|
// function failed. logging is its responsibility
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
res.Append(in.Next)
|
res.Append(in.Next)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
214
stdlib/arith.go
Normal file
214
stdlib/arith.go
Normal file
|
|
@ -0,0 +1,214 @@
|
||||||
|
/* 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"
|
||||||
|
"gitlab.com/whom/shs/ast"
|
||||||
|
"gitlab.com/whom/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
|
||||||
|
|
||||||
|
in = in.Eval(f, a)
|
||||||
|
|
||||||
|
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.Value()
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t := &ast.Token{Tag: ast.NUMBER}
|
||||||
|
t.Set(fmt.Sprintf("%f", res))
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
func sub(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token {
|
||||||
|
var res float64
|
||||||
|
var sub float64
|
||||||
|
|
||||||
|
in = in.Eval(f, a)
|
||||||
|
|
||||||
|
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.Value()
|
||||||
|
isFloat := false
|
||||||
|
var inner float64
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
inner = 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
|
||||||
|
}
|
||||||
|
|
||||||
|
inner = float64(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
if i.Next != nil {
|
||||||
|
sub += inner
|
||||||
|
} else {
|
||||||
|
res = inner
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t := &ast.Token{Tag: ast.NUMBER}
|
||||||
|
t.Set(fmt.Sprintf("%f", res - sub))
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
func mult(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token {
|
||||||
|
res := 1.0
|
||||||
|
|
||||||
|
in = in.Eval(f, a)
|
||||||
|
|
||||||
|
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.Value()
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t := &ast.Token{Tag: ast.NUMBER}
|
||||||
|
t.Set(fmt.Sprintf("%f", res))
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
func div(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token {
|
||||||
|
var res float64
|
||||||
|
|
||||||
|
in = in.Eval(f, a)
|
||||||
|
|
||||||
|
for i := in; i != nil; i = i.Next {
|
||||||
|
inner := 0.0
|
||||||
|
|
||||||
|
if i.Tag != ast.NUMBER {
|
||||||
|
log.Log(log.ERR, "Non-number given to DIV", "div")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
token := i.Value()
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
inner = 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
|
||||||
|
}
|
||||||
|
|
||||||
|
inner = float64(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
if i == in {
|
||||||
|
res = inner
|
||||||
|
} else {
|
||||||
|
res = res / inner
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t := &ast.Token{Tag: ast.NUMBER}
|
||||||
|
t.Set(fmt.Sprintf("%f", res))
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
@ -24,6 +24,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func not(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
func not(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||||
|
in = in.Eval(ft, vt)
|
||||||
|
|
||||||
if in.Tag != ast.BOOL {
|
if in.Tag != ast.BOOL {
|
||||||
log.Log(log.ERR, "non-bool argument to 'not'", "not")
|
log.Log(log.ERR, "non-bool argument to 'not'", "not")
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -96,10 +98,24 @@ func eq(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||||
out = "F"
|
out = "F"
|
||||||
}
|
}
|
||||||
|
|
||||||
case ast.NUMBER, ast.STRING, ast.BOOL:
|
case ast.STRING, ast.BOOL:
|
||||||
if in.Value() != second.Value() {
|
if in.Value() != second.Value() {
|
||||||
out = "F"
|
out = "F"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case ast.NUMBER:
|
||||||
|
l_val, parse_err := strconv.ParseFloat(in.Value(), 64)
|
||||||
|
r_val, parse_err := strconv.ParseFloat(second.Value(), 64)
|
||||||
|
if parse_err != nil {
|
||||||
|
log.Log(log.ERR,
|
||||||
|
"error parsing number: "+parse_err.Error(),
|
||||||
|
"eq")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if l_val != r_val {
|
||||||
|
out = "F"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -95,6 +95,34 @@ func GenFuncTable() ast.FuncTable {
|
||||||
Args: 1,
|
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,
|
||||||
|
},
|
||||||
|
|
||||||
|
"/": &ast.Function{
|
||||||
|
Function: div,
|
||||||
|
Name: "div",
|
||||||
|
TimesCalled: 0,
|
||||||
|
Args: -1,
|
||||||
|
},
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return stdlib
|
return stdlib
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue