diff --git a/go.mod b/go.mod index ef9fe89..06e880e 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module wbell.dev/m/v2 go 1.19 -require github.com/wadey/go-rounding v1.1.0 // indirect +require github.com/wadey/go-rounding v1.1.0 \ No newline at end of file diff --git a/src/built-ins.go b/src/built-ins.go index d06cef3..b78aafe 100644 --- a/src/built-ins.go +++ b/src/built-ins.go @@ -1,5 +1,7 @@ package main +import "github.com/wadey/go-rounding" + var vars = scope{} func init() { @@ -65,11 +67,68 @@ func init() { } 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["PI"] = PI vars["π"] = PI vars["e"] = e + vars["ln"] = builtinFunc{"ln", ArgonLn} + vars["log"] = builtinFunc{"log", ArgonLog} + vars["logN"] = builtinFunc{"logN", ArgonLogN} 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} } diff --git a/src/logarithms.go b/src/logarithms.go index 0442f6d..ef028da 100644 --- a/src/logarithms.go +++ b/src/logarithms.go @@ -24,7 +24,7 @@ func Ln(x number) number { } 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)), EXISTS: true} } diff --git a/src/maths.go b/src/maths.go deleted file mode 100644 index f4c8d3e..0000000 --- a/src/maths.go +++ /dev/null @@ -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}, -} diff --git a/src/number.go b/src/number.go index bff08c1..3845b64 100644 --- a/src/number.go +++ b/src/number.go @@ -6,12 +6,6 @@ import ( "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 binaryCompile = makeRegex("( *)(-)?(0b[10]+(.\\[10]+)?(e((\\-|\\+)?([0-9]+(\\.[0-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) } -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 func parseNumber(code UNPARSEcode) (number, bool, ArErr, int) { output, _ := newNumber().SetString(strings.TrimSpace(code.code)) diff --git a/src/reduce.go b/src/reduce.go deleted file mode 100644 index 29d0366..0000000 --- a/src/reduce.go +++ /dev/null @@ -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 -} diff --git a/src/string.go b/src/string.go index 982b44b..16804d0 100644 --- a/src/string.go +++ b/src/string.go @@ -11,15 +11,6 @@ func isString(code UNPARSEcode) bool { return stringCompile.MatchString(code.code) } -func toStringClass(str string) ArClass { - return ArClass{ - value: str, - MAP: ArMap{ - "length": len(str), - }, - } -} - func unquoted( str string, ) (string, error) { diff --git a/src/variable.go b/src/variable.go index 99d2c63..c257b30 100644 --- a/src/variable.go +++ b/src/variable.go @@ -132,13 +132,13 @@ func parseSetVariable(code UNPARSEcode, index int, lines []UNPARSEcode, isLine i if err.EXISTS { return setVariable{}, success, err, namei } - switch toset.(type) { + switch x := toset.(type) { case accessVariable: break case setFunction: function = true - params = toset.(setFunction).params - toset = toset.(setFunction).toset + params = x.params + toset = x.toset 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 } @@ -165,15 +165,15 @@ func parseAutoAsignVariable(code UNPARSEcode, index int, lines []UNPARSEcode, is if err.EXISTS { return setVariable{}, success, err, namei } - switch toset.(type) { + switch x := toset.(type) { case accessVariable: break case ArMapGet: break case setFunction: function = true - params = toset.(setFunction).params - toset = toset.(setFunction).toset + params = x.params + toset = x.toset default: return setVariable{}, false, ArErr{"Type Error", "can't set for non variable, did you mean '=='?", code.line, code.path, code.realcode, true}, 1 }