fix ln and remove unused code

This commit is contained in:
2023-03-11 12:37:02 +00:00
parent 42de489ca5
commit 3a449dec63
8 changed files with 69 additions and 127 deletions

2
go.mod
View File

@@ -2,4 +2,4 @@ module wbell.dev/m/v2
go 1.19 go 1.19
require github.com/wadey/go-rounding v1.1.0 // indirect require github.com/wadey/go-rounding v1.1.0

View File

@@ -1,5 +1,7 @@
package main package main
import "github.com/wadey/go-rounding"
var vars = scope{} var vars = scope{}
func init() { func init() {
@@ -65,11 +67,68 @@ func init() {
} }
return nil, ArErr{TYPE: "TypeError", message: "Cannot create array from '" + typeof(a[0]) + "'", EXISTS: true} return nil, ArErr{TYPE: "TypeError", message: "Cannot create array from '" + typeof(a[0]) + "'", EXISTS: true}
}} }}
vars["maths"] = maths
vars["math"] = maths
vars["time"] = ArTime vars["time"] = ArTime
vars["PI"] = PI vars["PI"] = PI
vars["π"] = PI vars["π"] = PI
vars["e"] = e vars["e"] = e
vars["ln"] = builtinFunc{"ln", ArgonLn}
vars["log"] = builtinFunc{"log", ArgonLog}
vars["logN"] = builtinFunc{"logN", ArgonLogN}
vars["thread"] = builtinFunc{"thread", ArThread} vars["thread"] = builtinFunc{"thread", ArThread}
vars["round"] = builtinFunc{"round", func(a ...any) (any, ArErr) {
if len(a) == 0 {
return nil, ArErr{TYPE: "round", message: "round takes 1 argument",
EXISTS: true}
}
precision := newNumber()
if len(a) > 1 {
switch x := a[1].(type) {
case number:
if !x.IsInt() {
return nil, ArErr{TYPE: "TypeError", message: "Cannot round to '" + typeof(a[1]) + "'", EXISTS: true}
}
precision = x
default:
return nil, ArErr{TYPE: "TypeError", message: "Cannot round to '" + typeof(a[1]) + "'", EXISTS: true}
}
}
switch x := a[0].(type) {
case number:
return rounding.Round(newNumber().Set(x), int(precision.Num().Int64()), rounding.HalfUp), ArErr{}
}
return nil, ArErr{TYPE: "TypeError", message: "Cannot round '" + typeof(a[0]) + "'", EXISTS: true}
}}
vars["floor"] = builtinFunc{"floor", func(a ...any) (any, ArErr) {
if len(a) == 0 {
return nil, ArErr{TYPE: "floor", message: "floor takes 1 argument",
EXISTS: true}
}
switch x := a[0].(type) {
case number:
n := newNumber().Set(x)
if n.Sign() < 0 {
return rounding.Round(n, 0, rounding.Up), ArErr{}
}
return rounding.Round(n, 0, rounding.Down), ArErr{}
}
return nil, ArErr{TYPE: "TypeError", message: "Cannot floor '" + typeof(a[0]) + "'", EXISTS: true}
}}
vars["ceil"] = builtinFunc{"ceil", func(a ...any) (any, ArErr) {
if len(a) == 0 {
return nil, ArErr{TYPE: "ceil", message: "ceil takes 1 argument",
EXISTS: true}
}
switch x := a[0].(type) {
case number:
n := newNumber().Set(x)
if n.Sign() < 0 {
return rounding.Round(n, 0, rounding.Down), ArErr{}
}
return rounding.Round(n, 0, rounding.Up), ArErr{}
}
return nil, ArErr{TYPE: "TypeError", message: "Cannot ceil '" + typeof(a[0]) + "'", EXISTS: true}
}}
vars["sqrt"] = builtinFunc{"sqrt", ArgonSqrt}
} }

View File

@@ -24,7 +24,7 @@ func Ln(x number) number {
} }
func ArgonLn(a ...any) (any, ArErr) { func ArgonLn(a ...any) (any, ArErr) {
if len(a) != 0 { if len(a) != 1 {
return nil, ArErr{TYPE: "Runtime Error", message: "ln takes 1 argument, got " + fmt.Sprint(len(a)), return nil, ArErr{TYPE: "Runtime Error", message: "ln takes 1 argument, got " + fmt.Sprint(len(a)),
EXISTS: true} EXISTS: true}
} }

View File

@@ -1,67 +0,0 @@
package main
import (
"github.com/wadey/go-rounding"
)
var maths = ArMap{
"round": builtinFunc{"round", func(a ...any) (any, ArErr) {
if len(a) == 0 {
return nil, ArErr{TYPE: "round", message: "round takes 1 argument",
EXISTS: true}
}
precision := newNumber()
if len(a) > 1 {
switch x := a[1].(type) {
case number:
if !x.IsInt() {
return nil, ArErr{TYPE: "TypeError", message: "Cannot round to '" + typeof(a[1]) + "'", EXISTS: true}
}
precision = x
default:
return nil, ArErr{TYPE: "TypeError", message: "Cannot round to '" + typeof(a[1]) + "'", EXISTS: true}
}
}
switch x := a[0].(type) {
case number:
return rounding.Round(newNumber().Set(x), int(precision.Num().Int64()), rounding.HalfUp), ArErr{}
}
return nil, ArErr{TYPE: "TypeError", message: "Cannot round '" + typeof(a[0]) + "'", EXISTS: true}
}},
"floor": builtinFunc{"floor", func(a ...any) (any, ArErr) {
if len(a) == 0 {
return nil, ArErr{TYPE: "floor", message: "floor takes 1 argument",
EXISTS: true}
}
switch x := a[0].(type) {
case number:
n := newNumber().Set(x)
if n.Sign() < 0 {
return rounding.Round(n, 0, rounding.Up), ArErr{}
}
return rounding.Round(n, 0, rounding.Down), ArErr{}
}
return nil, ArErr{TYPE: "TypeError", message: "Cannot floor '" + typeof(a[0]) + "'", EXISTS: true}
}},
"ceil": builtinFunc{"ceil", func(a ...any) (any, ArErr) {
if len(a) == 0 {
return nil, ArErr{TYPE: "ceil", message: "ceil takes 1 argument",
EXISTS: true}
}
switch x := a[0].(type) {
case number:
n := newNumber().Set(x)
if n.Sign() < 0 {
return rounding.Round(n, 0, rounding.Down), ArErr{}
}
return rounding.Round(n, 0, rounding.Up), ArErr{}
}
return nil, ArErr{TYPE: "TypeError", message: "Cannot ceil '" + typeof(a[0]) + "'", EXISTS: true}
}},
"sqrt": builtinFunc{"sqrt", ArgonSqrt},
"ln": builtinFunc{"ln", ArgonLn},
"log": builtinFunc{"log", ArgonLog},
"logN": builtinFunc{"logN", ArgonLogN},
}

View File

@@ -6,12 +6,6 @@ import (
"strings" "strings"
) )
type translateNumber struct {
number number
code string
line int
}
var numberCompile = makeRegex("( *)(-)?((([0-9]+(\\.[0-9]+)?)|(\\.[0-9]+))(e((\\-|\\+)?([0-9]+(\\.[0-9]+)?)))?)( *)") var numberCompile = makeRegex("( *)(-)?((([0-9]+(\\.[0-9]+)?)|(\\.[0-9]+))(e((\\-|\\+)?([0-9]+(\\.[0-9]+)?)))?)( *)")
var binaryCompile = makeRegex("( *)(-)?(0b[10]+(.\\[10]+)?(e((\\-|\\+)?([0-9]+(\\.[0-9]+)?)))?)( *)") var binaryCompile = makeRegex("( *)(-)?(0b[10]+(.\\[10]+)?(e((\\-|\\+)?([0-9]+(\\.[0-9]+)?)))?)( *)")
var hexCompile = makeRegex("( *)(-)?(0x[a-fA-F0-9]+(\\.[a-fA-F0-9]+)?)( *)") var hexCompile = makeRegex("( *)(-)?(0x[a-fA-F0-9]+(\\.[a-fA-F0-9]+)?)( *)")
@@ -55,32 +49,6 @@ func numberToString(num number, simplify bool) string {
return fmt.Sprint(x) return fmt.Sprint(x)
} }
var superscript = map[byte]string{
'0': "⁰",
'1': "¹",
'2': "²",
'3': "³",
'4': "⁴",
'5': "⁵",
'6': "⁶",
'7': "⁷",
'8': "⁸",
'9': "⁹",
}
var subscript = map[byte]string{
'0': "₀",
'1': "₁",
'2': "₂",
'3': "₃",
'4': "₄",
'5': "₅",
'6': "₆",
'7': "₇",
'8': "₈",
'9': "₉",
}
// returns translateNumber, success, error // returns translateNumber, success, error
func parseNumber(code UNPARSEcode) (number, bool, ArErr, int) { func parseNumber(code UNPARSEcode) (number, bool, ArErr, int) {
output, _ := newNumber().SetString(strings.TrimSpace(code.code)) output, _ := newNumber().SetString(strings.TrimSpace(code.code))

View File

@@ -1,9 +0,0 @@
package main
func reduce[T any](reducer func(x T, y T) T, arr []T) T {
result := arr[0]
for i := 1; i < len(arr); i++ {
result = reducer(arr[i], result)
}
return result
}

View File

@@ -11,15 +11,6 @@ func isString(code UNPARSEcode) bool {
return stringCompile.MatchString(code.code) return stringCompile.MatchString(code.code)
} }
func toStringClass(str string) ArClass {
return ArClass{
value: str,
MAP: ArMap{
"length": len(str),
},
}
}
func unquoted( func unquoted(
str string, str string,
) (string, error) { ) (string, error) {

View File

@@ -132,13 +132,13 @@ func parseSetVariable(code UNPARSEcode, index int, lines []UNPARSEcode, isLine i
if err.EXISTS { if err.EXISTS {
return setVariable{}, success, err, namei return setVariable{}, success, err, namei
} }
switch toset.(type) { switch x := toset.(type) {
case accessVariable: case accessVariable:
break break
case setFunction: case setFunction:
function = true function = true
params = toset.(setFunction).params params = x.params
toset = toset.(setFunction).toset toset = x.toset
if toset == nil { if toset == nil {
return setVariable{}, false, ArErr{"Type Error", "can't set for non variable, did you mean to put 'let' before?", code.line, code.path, code.realcode, true}, 1 return setVariable{}, false, ArErr{"Type Error", "can't set for non variable, did you mean to put 'let' before?", code.line, code.path, code.realcode, true}, 1
} }
@@ -165,15 +165,15 @@ func parseAutoAsignVariable(code UNPARSEcode, index int, lines []UNPARSEcode, is
if err.EXISTS { if err.EXISTS {
return setVariable{}, success, err, namei return setVariable{}, success, err, namei
} }
switch toset.(type) { switch x := toset.(type) {
case accessVariable: case accessVariable:
break break
case ArMapGet: case ArMapGet:
break break
case setFunction: case setFunction:
function = true function = true
params = toset.(setFunction).params params = x.params
toset = toset.(setFunction).toset toset = x.toset
default: default:
return setVariable{}, false, ArErr{"Type Error", "can't set for non variable, did you mean '=='?", code.line, code.path, code.realcode, true}, 1 return setVariable{}, false, ArErr{"Type Error", "can't set for non variable, did you mean '=='?", code.line, code.path, code.realcode, true}, 1
} }