impliement more methods to number, hopefully finishing the requirements from within the number object mostly

This commit is contained in:
2024-07-11 03:17:27 +01:00
parent b4a02be086
commit 70f2c47e4f
22 changed files with 970 additions and 197 deletions

File diff suppressed because one or more lines are too long

5
go.mod
View File

@@ -7,7 +7,10 @@ require (
github.com/wadey/go-rounding v1.1.0
)
require github.com/joho/godotenv v1.5.1 // indirect
require (
github.com/chzyer/readline v1.5.1 // indirect
github.com/joho/godotenv v1.5.1 // indirect
)
require (
github.com/gabriel-vasile/mimetype v1.4.2

5
go.sum
View File

@@ -1,3 +1,7 @@
github.com/chzyer/logex v1.2.1/go.mod h1:JLbx6lG2kDbNRFnfkgvh4eRJRPX1QCoOIWomwysCBrQ=
github.com/chzyer/readline v1.5.1 h1:upd/6fQk4src78LMRzh5vItIt361/o4uq553V8B5sGI=
github.com/chzyer/readline v1.5.1/go.mod h1:Eh+b79XXUwfKfcPLepksvw2tcLE/Ct21YObkaSkeBlk=
github.com/chzyer/test v1.0.0/go.mod h1:2JlltgoNkt4TW/z9V/IzDdFaMTM2JPIi26O1pF38GC8=
github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w=
github.com/fatih/color v1.14.1/go.mod h1:2oHN61fhTpgcxD3TSWCgKDiH1+x4OiDVVGH8WlgGZGg=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
@@ -19,6 +23,7 @@ golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210220050731-9a76102bfb43/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

View File

@@ -1,5 +1,7 @@
package main
import "math/big"
func AnyToArValid(arr any) any {
switch arr := arr.(type) {
case []any:
@@ -12,6 +14,8 @@ func AnyToArValid(arr any) any {
return ArBuffer(arr)
case byte:
return ArByte(arr)
case int, int64, float64, float32, *big.Rat, *big.Int:
return Number(arr)
default:
return arr
}
@@ -26,20 +30,3 @@ func ArValidToAny(a any) any {
}
return a
}
func ArValidToHash(a any) (any, ArErr) {
switch a := a.(type) {
case ArObject:
if callable, ok := a.obj["__hash__"]; ok {
value, err := runCall(call{
Callable: callable,
Args: []any{},
}, stack{}, 0)
if err.EXISTS {
return nil, err
}
return value, ArErr{}
}
}
return a, ArErr{}
}

View File

@@ -2,6 +2,7 @@ package main
import (
"bytes"
"encoding/hex"
"fmt"
)
@@ -17,13 +18,13 @@ func ArByte(Byte byte) ArObject {
obj.obj["__string__"] = builtinFunc{
"__string__",
func(a ...any) (any, ArErr) {
return "<byte>", ArErr{}
return ArString("0x" + hex.EncodeToString([]byte{Byte})), ArErr{}
},
}
obj.obj["__repr__"] = builtinFunc{
"__repr__",
func(a ...any) (any, ArErr) {
return "<byte>", ArErr{}
return ArString("<byte 0x" + hex.EncodeToString([]byte{Byte}) + ">"), ArErr{}
},
}
obj.obj["number"] = builtinFunc{
@@ -44,23 +45,15 @@ func ArByte(Byte byte) ArObject {
}
a[0] = ArValidToAny(a[0])
switch x := a[0].(type) {
case number:
if x.Denom().Cmp(one.Denom()) != 0 {
return nil, ArErr{
TYPE: "Type Error",
message: "expected integer, got " + fmt.Sprint(x),
EXISTS: true,
}
}
n := x.Num().Int64()
if n > 255 || n < 0 {
case int64:
if x > 255 || x < 0 {
return nil, ArErr{
TYPE: "ValueError",
message: "expected number between 0 and 255, got " + fmt.Sprint(floor(x).Num().Int64()),
message: "expected number between 0 and 255, got " + fmt.Sprint(x),
EXISTS: true,
}
}
Byte = byte(n)
Byte = byte(x)
case string:
if len(x) != 1 {
return nil, ArErr{
@@ -93,13 +86,13 @@ func ArBuffer(buf []byte) ArObject {
obj.obj["__string__"] = builtinFunc{
"__string__",
func(a ...any) (any, ArErr) {
return "<buffer>", ArErr{}
return ArString("<buffer length=" + fmt.Sprint(len(buf)) + ">"), ArErr{}
},
}
obj.obj["__repr__"] = builtinFunc{
"__repr__",
func(a ...any) (any, ArErr) {
return "<buffer>", ArErr{}
return ArString("<buffer length=" + fmt.Sprint(len(buf)) + ">"), ArErr{}
},
}
obj.obj["from"] = builtinFunc{
@@ -325,7 +318,7 @@ func ArBuffer(buf []byte) ArObject {
case "array":
output := []any{}
for _, v := range buf {
output = append(output, newNumber().SetInt64(int64(v)))
output = append(output, Number(int64(v)))
}
return ArArray(output), ArErr{}
default:
@@ -486,7 +479,7 @@ func ArBuffer(buf []byte) ArObject {
}
}
}
poss := ArValidToAny(a[0])
poss := a[0]
if typeof(poss) != "number" {
return nil, ArErr{
TYPE: "Type Error",
@@ -494,15 +487,14 @@ func ArBuffer(buf []byte) ArObject {
EXISTS: true,
}
}
pos := poss.(number)
if pos.Denom().Cmp(one.Denom()) != 0 {
posNum, err := numberToInt64(poss.(ArObject))
if err != nil {
return nil, ArErr{
TYPE: "Type Error",
message: "position must be an integer",
message: err.Error(),
EXISTS: true,
}
}
posNum := pos.Num().Int64()
if posNum < 0 || posNum >= int64(len(buf)) {
return nil, ArErr{
TYPE: "Index Error",

View File

@@ -227,7 +227,7 @@ func makeGlobal() ArObject {
case ArObject:
newarray := []any{}
for key := range x.obj {
newarray = append(newarray, key)
newarray = append(newarray, AnyToArValid(key))
}
return ArArray(newarray), ArErr{}
}
@@ -250,9 +250,10 @@ func makeGlobal() ArObject {
if len(a) != 1 {
return nil, ArErr{TYPE: "chr", message: "chr takes 1 argument, got " + fmt.Sprint(len(a)), EXISTS: true}
}
a[0] = ArValidToAny(a[0])
switch x := a[0].(type) {
case number:
return string([]rune{rune(floor(x).Num().Int64())}), ArErr{}
case int64:
return string([]rune{rune(x)}), ArErr{}
}
return nil, ArErr{TYPE: "Type Error", message: "Cannot convert '" + typeof(a[0]) + "' to string", EXISTS: true}
}}

View File

@@ -106,7 +106,7 @@ func runForLoop(loop forLoop, stack stack, stacklevel int) (any, ArErr) {
step_, _ := numberToInt64(step)
layer := anymap{}
stacks := append(stack, Map(layer))
for i <= to_ {
for i < to_ {
layer[loop.variable] = Number(i)
resp, err := runVal(loop.body, stacks, stacklevel+1)
if err.EXISTS {

View File

@@ -22,39 +22,6 @@ type ArMapGet struct {
Path string
}
func isObject(val any) bool {
if _, ok := val.(ArObject); ok {
return true
}
return false
}
func hashableObject(obj ArObject) (string, ArErr) {
if callable, ok := obj.obj["__hash__"]; ok {
resp, err := runCall(call{
Callable: callable,
Args: []any{},
}, stack{}, 0)
if err.EXISTS {
return "", err
}
resp = ArValidToAny(resp)
if str, ok := resp.(string); ok {
return str, ArErr{}
}
return "", ArErr{
TYPE: "TypeError",
EXISTS: true,
message: "expected string from __hash__ method, got " + typeof(resp),
}
}
return "", ArErr{
TYPE: "TypeError",
EXISTS: true,
message: "cannot hash object",
}
}
func mapGet(r ArMapGet, stack stack, stacklevel int) (any, ArErr) {
resp, err := runVal(r.VAL, stack, stacklevel+1)
if err.EXISTS {
@@ -156,19 +123,10 @@ func indexGetParse(code UNPARSEcode, index int, codelines []UNPARSEcode) (ArMapG
}, 1
}
var hashabletypes = []string{
"number",
"string",
"bool",
"null",
}
func isUnhashable(val any) bool {
keytype := typeof(val)
for _, v := range hashabletypes {
if v == keytype {
switch val.(type) {
case int64, float64, string, bool, nil:
return false
}
}
return true
}

View File

@@ -1,23 +1,36 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"github.com/chzyer/readline"
"golang.org/x/term"
)
func input(args ...any) string {
var tempFilePath = os.TempDir() + "/argon_input_history.tmp"
func input(args ...any) (string, error) {
output := []any{}
for i := 0; i < len(args); i++ {
output = append(output, anyToArgon(args[i], false, true, 3, 0, true, 0))
}
fmt.Print(output...)
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
input := scanner.Text()
return input
message := fmt.Sprint(output...)
rl, err := readline.NewEx(&readline.Config{
Prompt: message,
HistoryFile: tempFilePath,
HistorySearchFold: true,
})
if err != nil {
log.Fatalf("Failed to create readline instance: %v", err)
}
defer rl.Close()
line, err := rl.Readline()
if err != nil { // io.EOF or other error
return "", err
}
return line, nil
}
func getPassword(args ...any) (string, error) {
@@ -25,38 +38,21 @@ func getPassword(args ...any) (string, error) {
for i := 0; i < len(args); i++ {
output = append(output, anyToArgon(args[i], false, true, 3, 0, true, 0))
}
fmt.Print(output...)
password := []byte{}
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
message := fmt.Sprint(output...)
rl, err := readline.NewEx(&readline.Config{
Prompt: message,
MaskRune: '*',
EnableMask: true,
})
if err != nil {
panic(err)
log.Fatalf("Failed to create readline instance: %v", err)
}
defer term.Restore(int(os.Stdin.Fd()), oldState)
for {
char := make([]byte, 1)
_, err := os.Stdin.Read(char)
if err != nil {
defer rl.Close()
line, err := rl.Readline()
if err != nil { // io.EOF or other error
return "", err
}
if char[0] == 3 || char[0] == 4 {
return "", fmt.Errorf("keyboard interupt")
} else if char[0] == '\r' || char[0] == '\n' {
fmt.Println()
break
} else if char[0] == '\b' || char[0] == 127 {
if len(password) > 0 {
password = password[:len(password)-1]
fmt.Print("\b \b")
}
} else {
password = append(password, char[0])
fmt.Print("*")
}
}
fmt.Print("\r")
return string(password), nil
return line, nil
}
func pause() {

View File

@@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"errors"
"fmt"
"strconv"
)
@@ -71,7 +70,6 @@ func jsonstringify(obj any, level int64) (string, error) {
case nil:
return "null", nil
}
fmt.Println(level)
err := errors.New("Cannot stringify '" + typeof(obj) + "'")
return "", err
}

View File

@@ -10,8 +10,8 @@ var Args = os.Args[1:]
type stack = []ArObject
const VERSION = "3.0.8"
const VERSION_NUM = 6
const VERSION = "3.1.0 oop numbers beta 1"
const VERSION_NUM = 7
func newscope() ArObject {
return Map(anymap{})
@@ -45,7 +45,6 @@ func main() {
}
}()
}
initRandom()
garbageCollect()
global := makeGlobal()
if len(Args) == 0 {

View File

@@ -245,6 +245,7 @@ func Map(m anymap) ArObject {
EXISTS: true,
}
}
args[0] = ArValidToAny(args[0])
if isUnhashable(args[0]) {
return nil, ArErr{
TYPE: "Runtime Error",

View File

@@ -2,6 +2,7 @@ package main
import (
"fmt"
"math"
"math/big"
"strings"
)
@@ -23,6 +24,19 @@ func isNumber(code UNPARSEcode) bool {
return numberCompile.MatchString(code.code) || binaryCompile.MatchString(code.code) || hexCompile.MatchString(code.code) || octalCompile.MatchString(code.code)
}
func exponentBySquaring(base *big.Rat, exp *big.Int) *big.Rat {
if exp.Cmp(_zero) == 0 {
return _one_Rat
}
if exp.Cmp(_one) == 0 {
return base
}
if exp.Bit(0) == 0 {
return exponentBySquaring(new(big.Rat).Mul(base, base), new(big.Int).Div(exp, _two))
}
return new(big.Rat).Mul(base, exponentBySquaring(new(big.Rat).Mul(base, base), new(big.Int).Div(new(big.Int).Sub(exp, _one), _two)))
}
// converts a number type to a string
func numberToString(num *big.Rat, simplify bool) string {
if simplify {
@@ -40,9 +54,9 @@ func numberToString(num *big.Rat, simplify bool) string {
}
}
x, _ := num.Float64()
float := new(big.Float).SetRat(num)
return fmt.Sprint(x)
return float.String()
}
var int64_max = new(big.Int).SetInt64(9223372036854775807)
@@ -92,7 +106,10 @@ func numberToInt64(num ArObject) (int64, error) {
value := num.obj["__value__"]
switch x := value.(type) {
case *big.Rat:
return floor(x).Num().Int64(), nil
if x.IsInt() {
return x.Num().Int64(), nil
}
return 0, fmt.Errorf("ration number cannot be converted to int64")
case *big.Int:
return x.Int64(), nil
case int64:
@@ -154,6 +171,7 @@ func AddObjects(A ArObject, B ArObject) (ArObject, ArErr) {
return ArObject{}, ArErr{"Type Error", "cannot add " + typeof(A) + " and " + typeof(B), 0, "", "", true}
}
var _two = big.NewInt(2)
var _one = big.NewInt(1)
var _one_Rat = big.NewRat(1, 1)
var _one_Number ArObject
@@ -176,8 +194,14 @@ func Number(value any) ArObject {
case *big.Rat:
if x.IsInt() {
value = x.Num()
if value.(*big.Int).Cmp(int64_max) <= 0 && value.(*big.Int).Cmp(int64_min) >= 0 {
value = value.(*big.Int).Int64()
}
}
case *big.Int:
if x.Cmp(int64_max) <= 0 && x.Cmp(int64_min) >= 0 {
value = x.Int64()
}
case int:
value = int64(x)
case int64:
@@ -196,12 +220,67 @@ func Number(value any) ArObject {
val.obj["__value__"] = value
val.obj["__LessThan__"] = builtinFunc{
"__LessThan__",
func(a ...any) (any, ArErr) {
resp, err := val.obj["__Compare__"].(builtinFunc).FUNC(a...)
if err.EXISTS {
return nil, err
}
resp, _ = numberToInt64(resp.(ArObject))
return resp.(int64) == -1, ArErr{}
},
}
val.obj["__LessThanEqual__"] = builtinFunc{
"__LessThanEqual__",
func(a ...any) (any, ArErr) {
resp, err := val.obj["__Compare__"].(builtinFunc).FUNC(a...)
if err.EXISTS {
return nil, err
}
resp, _ = numberToInt64(resp.(ArObject))
return resp.(int64) != 1, ArErr{}
},
}
val.obj["__GreaterThan__"] = builtinFunc{
"__GreaterThan__",
func(a ...any) (any, ArErr) {
resp, err := val.obj["__Compare__"].(builtinFunc).FUNC(a...)
if err.EXISTS {
return nil, err
}
resp, _ = numberToInt64(resp.(ArObject))
return resp.(int64) == 1, ArErr{}
},
}
val.obj["__GreaterThanEqual__"] = builtinFunc{
"__GreaterThanEqual__",
func(a ...any) (any, ArErr) {
resp, err := val.obj["__Compare__"].(builtinFunc).FUNC(a...)
if err.EXISTS {
return nil, err
}
resp, _ = numberToInt64(resp.(ArObject))
return resp.(int64) != -1, ArErr{}
},
}
val.obj["__NotEqual__"] = builtinFunc{
"__NotEqual__",
func(a ...any) (any, ArErr) {
resp, err := val.obj["__Equal__"].(builtinFunc).FUNC(a...)
if err.EXISTS {
return nil, err
}
return !resp.(bool), ArErr{}
},
}
switch CurrentNumber := value.(type) {
case *big.Int:
_BigInt_logic(val, CurrentNumber)
case *big.Rat:
_BigRat_logic(val, CurrentNumber)
case int64:
debugPrintln("int64", CurrentNumber)
_int64_logic(val, CurrentNumber)
}
@@ -227,7 +306,7 @@ func _BigInt_logic(val ArObject, CurrentNumber *big.Int) {
coloured := a[0].(bool)
output := []string{}
if coloured {
output = append(output, "\x1b[34;5;240m")
output = append(output, "\x1b[34;5;25m")
}
output = append(output, fmt.Sprint(CurrentNumber))
if coloured {
@@ -278,6 +357,24 @@ func _BigInt_logic(val ArObject, CurrentNumber *big.Int) {
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
},
}
val.obj["__Equal__"] = builtinFunc{
"__Equal__",
func(a ...any) (any, ArErr) {
if len(a) != 1 {
return nil, ArErr{"Type Error", "expected 1 argument, got " + fmt.Sprint(len(a)), 0, "", "", true}
}
a[0] = ArValidToAny(a[0])
switch ReceivingNumber := a[0].(type) {
case *big.Int:
return CurrentNumber.Cmp(ReceivingNumber) == 0, ArErr{}
case int64:
return CurrentNumber.Cmp(big.NewInt(ReceivingNumber)) == 0, ArErr{}
case *big.Rat:
return new(big.Rat).SetInt(CurrentNumber).Cmp(ReceivingNumber) == 0, ArErr{}
}
return false, ArErr{}
},
}
val.obj["__json__"] = builtinFunc{
"__json__",
val.obj["__string__"].(builtinFunc).FUNC,
@@ -455,6 +552,209 @@ func _BigInt_logic(val ArObject, CurrentNumber *big.Int) {
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
},
}
val.obj["__Modulo__"] = builtinFunc{
"__Modulo__",
func(a ...any) (any, ArErr) {
if len(a) != 1 {
return nil, ArErr{"Type Error", "expected 1 argument, got " + fmt.Sprint(len(a)), 0, "", "", true}
}
if typeof(a[0]) != "number" {
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
}
a[0] = ArValidToAny(a[0])
switch ReceivingNumber := a[0].(type) {
case *big.Int:
if ReceivingNumber.Cmp(_zero) == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
return Number(new(big.Int).Mod(CurrentNumber, ReceivingNumber)), ArErr{}
case int64:
if ReceivingNumber == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
return Number(new(big.Int).Mod(CurrentNumber, big.NewInt(ReceivingNumber))), ArErr{}
case *big.Rat:
if ReceivingNumber.Cmp(_zero_Rat) == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
x := newNumber().Set(ReceivingNumber)
output := new(big.Rat).SetInt(CurrentNumber)
x.Quo(output, x)
x = floor(x)
x.Mul(x, ReceivingNumber)
output.Sub(output, x)
return Number(output), ArErr{}
}
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
},
}
val.obj["__PostModulo__"] = builtinFunc{
"__PostModulo__",
func(a ...any) (any, ArErr) {
if len(a) != 1 {
return nil, ArErr{"Type Error", "expected 1 argument, got " + fmt.Sprint(len(a)), 0, "", "", true}
}
if typeof(a[0]) != "number" {
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
}
a[0] = ArValidToAny(a[0])
switch ReceivingNumber := a[0].(type) {
case *big.Int:
if CurrentNumber.Cmp(_zero) == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
return Number(new(big.Int).Mod(ReceivingNumber, CurrentNumber)), ArErr{}
case int64:
if CurrentNumber.Cmp(_zero) == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
return Number(new(big.Int).Mod(big.NewInt(ReceivingNumber), CurrentNumber)), ArErr{}
case *big.Rat:
if CurrentNumber.Cmp(_zero) == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
currentNumber_RAT := newNumber().SetInt(CurrentNumber)
x := newNumber().Set(currentNumber_RAT)
output := new(big.Rat).Set(ReceivingNumber)
x.Quo(output, x)
x = floor(x)
x.Mul(x, currentNumber_RAT)
output.Sub(output, x)
return Number(output), ArErr{}
}
return false, ArErr{}
},
}
val.obj["__IntDivide__"] = builtinFunc{
"__IntDivide__",
func(a ...any) (any, ArErr) {
if len(a) != 1 {
return nil, ArErr{"Type Error", "expected 1 argument, got " + fmt.Sprint(len(a)), 0, "", "", true}
}
if typeof(a[0]) != "number" {
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
}
a[0] = ArValidToAny(a[0])
switch ReceivingNumber := a[0].(type) {
case *big.Int:
if ReceivingNumber.Cmp(_zero) == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
return Number(new(big.Int).Div(CurrentNumber, ReceivingNumber)), ArErr{}
case int64:
if ReceivingNumber == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
return Number(new(big.Int).Div(CurrentNumber, big.NewInt(ReceivingNumber))), ArErr{}
case *big.Rat:
if ReceivingNumber.Cmp(_zero_Rat) == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
x := newNumber().Set(ReceivingNumber)
output := new(big.Rat).SetInt(CurrentNumber)
x.Quo(output, x)
x = floor(x)
return Number(x), ArErr{}
}
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
},
}
val.obj["__PostIntDivide__"] = builtinFunc{
"__PostIntDivide__",
func(a ...any) (any, ArErr) {
if len(a) != 1 {
return nil, ArErr{"Type Error", "expected 1 argument, got " + fmt.Sprint(len(a)), 0, "", "", true}
}
if typeof(a[0]) != "number" {
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
}
a[0] = ArValidToAny(a[0])
switch ReceivingNumber := a[0].(type) {
case *big.Int:
if CurrentNumber.Cmp(_zero) == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
return Number(new(big.Int).Div(ReceivingNumber, CurrentNumber)), ArErr{}
case int64:
if CurrentNumber.Cmp(_zero) == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
return Number(new(big.Int).Div(big.NewInt(ReceivingNumber), CurrentNumber)), ArErr{}
case *big.Rat:
if CurrentNumber.Cmp(_zero) == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
currentNumber_RAT := newNumber().SetInt(CurrentNumber)
x := newNumber().Set(currentNumber_RAT)
output := new(big.Rat).Set(ReceivingNumber)
x.Quo(output, x)
x = floor(x)
return Number(x), ArErr{}
}
return false, ArErr{}
},
}
val.obj["__Power__"] = builtinFunc{
"__Power__",
func(a ...any) (any, ArErr) {
if len(a) != 1 {
return nil, ArErr{"Type Error", "expected 1 argument, got " + fmt.Sprint(len(a)), 0, "", "", true}
}
if typeof(a[0]) != "number" {
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
}
a[0] = ArValidToAny(a[0])
switch ReceivingNumber := a[0].(type) {
case *big.Int:
if ReceivingNumber.Cmp(_zero) == 0 {
return Number(1), ArErr{}
}
output := new(big.Int).Set(CurrentNumber)
output.Exp(output, new(big.Int).Abs(ReceivingNumber), nil)
if ReceivingNumber.Cmp(_zero) < 0 {
output = new(big.Int).Quo(_one, output)
}
return Number(output), ArErr{}
case int64:
if ReceivingNumber == 0 {
return Number(1), ArErr{}
}
output := new(big.Int).Set(CurrentNumber)
output.Exp(output, new(big.Int).Abs(big.NewInt(ReceivingNumber)), nil)
if ReceivingNumber < 0 {
output = new(big.Int).Quo(_one, output)
}
return Number(output), ArErr{}
case *big.Rat:
if ReceivingNumber.Cmp(_zero_Rat) == 0 {
return Number(1), ArErr{}
}
exponent_numerator := new(big.Int).Abs(ReceivingNumber.Num())
exponent_denominator := ReceivingNumber.Denom()
output := new(big.Rat).SetInt(CurrentNumber)
output_float, _ := output.Float64()
// error if output_float is infinity
if math.IsInf(output_float, 0) {
return nil, ArErr{"Runtime Error", "number too large to perform rational exponential calculations on it.", 0, "", "", true}
}
exponent_denominator_float, _ := exponent_denominator.Float64()
if math.IsInf(exponent_denominator_float, 0) {
return nil, ArErr{"Runtime Error", "demominator too large to perform rational exponential calculations on it.", 0, "", "", true}
}
if exponent_denominator_float != 1 {
output_float = math.Pow(output_float, 1/exponent_denominator_float)
}
output = new(big.Rat).SetFloat64(output_float)
output = exponentBySquaring(output, exponent_numerator)
if ReceivingNumber.Cmp(_zero_Rat) < 0 {
output = new(big.Rat).Quo(_one_Rat, output)
}
return Number(output), ArErr{}
}
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
},
}
}
func _BigRat_logic(val ArObject, CurrentNumber *big.Rat) {
@@ -476,7 +776,7 @@ func _BigRat_logic(val ArObject, CurrentNumber *big.Rat) {
coloured := a[0].(bool)
output := []string{}
if coloured {
output = append(output, "\x1b[34;5;240m")
output = append(output, "\x1b[34;5;25m")
}
output = append(output, numberToString(CurrentNumber, true))
if coloured {
@@ -541,6 +841,25 @@ func _BigRat_logic(val ArObject, CurrentNumber *big.Rat) {
},
}
val.obj["__Equal__"] = builtinFunc{
"__Equal__",
func(a ...any) (any, ArErr) {
if len(a) != 1 {
return nil, ArErr{"Type Error", "expected 1 argument, got " + fmt.Sprint(len(a)), 0, "", "", true}
}
a[0] = ArValidToAny(a[0])
switch ReceivingNumber := a[0].(type) {
case *big.Int:
return CurrentNumber.Cmp(new(big.Rat).SetInt(ReceivingNumber)) == 0, ArErr{}
case int64:
return CurrentNumber.Cmp(new(big.Rat).SetInt64(ReceivingNumber)) == 0, ArErr{}
case *big.Rat:
return CurrentNumber.Cmp(ReceivingNumber) == 0, ArErr{}
}
return false, ArErr{}
},
}
val.obj["__Add__"] = builtinFunc{
"__Add__",
func(a ...any) (any, ArErr) {
@@ -711,6 +1030,229 @@ func _BigRat_logic(val ArObject, CurrentNumber *big.Rat) {
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
},
}
val.obj["__Modulo__"] = builtinFunc{
"__Modulo__",
func(a ...any) (any, ArErr) {
if len(a) != 1 {
return nil, ArErr{"Type Error", "expected 1 argument, got " + fmt.Sprint(len(a)), 0, "", "", true}
}
if typeof(a[0]) != "number" {
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
}
a[0] = ArValidToAny(a[0])
switch ReceivingNumber := a[0].(type) {
case *big.Int:
if ReceivingNumber.Cmp(_zero) == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
ReceivingNumber_RAT := new(big.Rat).SetInt(ReceivingNumber)
x := newNumber().Set(ReceivingNumber_RAT)
output := new(big.Rat).Set(CurrentNumber)
x.Quo(output, x)
x = floor(x)
x.Mul(x, ReceivingNumber_RAT)
output.Sub(output, x)
return Number(output), ArErr{}
case int64:
if ReceivingNumber == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
ReceivingNumber_RAT := new(big.Rat).SetInt64(ReceivingNumber)
x := newNumber().Set(ReceivingNumber_RAT)
output := new(big.Rat).Set(CurrentNumber)
x.Quo(output, x)
x = floor(x)
x.Mul(x, ReceivingNumber_RAT)
output.Sub(output, x)
return Number(output), ArErr{}
case *big.Rat:
if ReceivingNumber.Cmp(_zero_Rat) == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
x := newNumber().Set(ReceivingNumber)
output := new(big.Rat).Set(CurrentNumber)
x.Quo(output, x)
x = floor(x)
x.Mul(x, ReceivingNumber)
output.Sub(output, x)
return Number(output), ArErr{}
}
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
},
}
val.obj["__PostModulo__"] = builtinFunc{
"__PostModulo__",
func(a ...any) (any, ArErr) {
if len(a) != 1 {
return nil, ArErr{"Type Error", "expected 1 argument, got " + fmt.Sprint(len(a)), 0, "", "", true}
}
if typeof(a[0]) != "number" {
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
}
a[0] = ArValidToAny(a[0])
switch ReceivingNumber := a[0].(type) {
case *big.Int:
if CurrentNumber.Cmp(_zero_Rat) == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
ReceivingNumber_RAT := new(big.Rat).SetInt(ReceivingNumber)
x := newNumber().Set(ReceivingNumber_RAT)
output := new(big.Rat).Set(CurrentNumber)
x.Quo(output, x)
x = floor(x)
x.Mul(x, ReceivingNumber_RAT)
output.Sub(output, x)
return Number(output), ArErr{}
case int64:
if CurrentNumber.Cmp(_zero_Rat) == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
ReceivingNumber_RAT := new(big.Rat).SetInt64(ReceivingNumber)
x := newNumber().Set(ReceivingNumber_RAT)
output := new(big.Rat).Set(CurrentNumber)
x.Quo(output, x)
x = floor(x)
x.Mul(x, ReceivingNumber_RAT)
output.Sub(output, x)
return Number(output), ArErr{}
case *big.Rat:
if CurrentNumber.Cmp(_zero_Rat) == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
x := newNumber().Set(ReceivingNumber)
output := new(big.Rat).Set(CurrentNumber)
x.Quo(output, x)
x = floor(x)
x.Mul(x, ReceivingNumber)
output.Sub(output, x)
return Number(output), ArErr{}
}
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
},
}
val.obj["__IntDivide__"] = builtinFunc{
"__IntDivide__",
func(a ...any) (any, ArErr) {
if len(a) != 1 {
return nil, ArErr{"Type Error", "expected 1 argument, got " + fmt.Sprint(len(a)), 0, "", "", true}
}
if typeof(a[0]) != "number" {
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
}
a[0] = ArValidToAny(a[0])
switch ReceivingNumber := a[0].(type) {
case *big.Int:
if ReceivingNumber.Cmp(_zero) == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
return Number(floor(new(big.Rat).Quo(CurrentNumber, new(big.Rat).SetInt(ReceivingNumber)))), ArErr{}
case int64:
if ReceivingNumber == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
return Number(floor(new(big.Rat).Quo(CurrentNumber, new(big.Rat).SetInt64(ReceivingNumber)))), ArErr{}
case *big.Rat:
if ReceivingNumber.Cmp(_zero_Rat) == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
return Number(floor(new(big.Rat).Quo(CurrentNumber, ReceivingNumber))), ArErr{}
}
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
},
}
val.obj["__PostIntDivide__"] = builtinFunc{
"__PostIntDivide__",
func(a ...any) (any, ArErr) {
if len(a) != 1 {
return nil, ArErr{"Type Error", "expected 1 argument, got " + fmt.Sprint(len(a)), 0, "", "", true}
}
if typeof(a[0]) != "number" {
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
}
a[0] = ArValidToAny(a[0])
switch ReceivingNumber := a[0].(type) {
case *big.Int:
if CurrentNumber.Cmp(_zero_Rat) == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
return Number(floor(new(big.Rat).Quo(new(big.Rat).SetInt(ReceivingNumber), CurrentNumber))), ArErr{}
case int64:
if CurrentNumber.Cmp(_zero_Rat) == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
return Number(floor(new(big.Rat).Quo(new(big.Rat).SetInt64(ReceivingNumber), CurrentNumber))), ArErr{}
case *big.Rat:
if CurrentNumber.Cmp(_zero_Rat) == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
return Number(floor(new(big.Rat).Quo(ReceivingNumber, CurrentNumber))), ArErr{}
}
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
},
}
val.obj["__Power__"] = builtinFunc{
"__Power__",
func(a ...any) (any, ArErr) {
if len(a) != 1 {
return nil, ArErr{"Type Error", "expected 1 argument, got " + fmt.Sprint(len(a)), 0, "", "", true}
}
if typeof(a[0]) != "number" {
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
}
a[0] = ArValidToAny(a[0])
switch ReceivingNumber := a[0].(type) {
case *big.Int:
if ReceivingNumber.Cmp(_zero) == 0 {
return Number(1), ArErr{}
}
output := new(big.Rat).Set(CurrentNumber)
output = exponentBySquaring(output, new(big.Int).Abs(ReceivingNumber))
if ReceivingNumber.Cmp(_zero) < 0 {
output = new(big.Rat).Quo(_one_Rat, output)
}
return Number(output), ArErr{}
case int64:
if ReceivingNumber == 0 {
return Number(1), ArErr{}
}
output := new(big.Rat).Set(CurrentNumber)
output = exponentBySquaring(output, new(big.Int).Abs(big.NewInt(ReceivingNumber)))
if ReceivingNumber < 0 {
output = new(big.Rat).Quo(_one_Rat, output)
}
return Number(output), ArErr{}
case *big.Rat:
if ReceivingNumber.Cmp(_zero_Rat) == 0 {
return Number(1), ArErr{}
}
exponent_numerator := new(big.Int).Abs(ReceivingNumber.Num())
exponent_denominator := ReceivingNumber.Denom()
output := new(big.Rat).Set(CurrentNumber)
output_float, _ := output.Float64()
// error if output_float is infinity
if math.IsInf(output_float, 0) {
return nil, ArErr{"Runtime Error", "number too large to perform rational exponential calculations on it.", 0, "", "", true}
}
exponent_denominator_float, _ := exponent_denominator.Float64()
if math.IsInf(exponent_denominator_float, 0) {
return nil, ArErr{"Runtime Error", "demominator too large to perform rational exponential calculations on it.", 0, "", "", true}
}
if exponent_denominator_float != 1 {
output_float = math.Pow(output_float, 1/exponent_denominator_float)
}
output = new(big.Rat).SetFloat64(output_float)
output = exponentBySquaring(output, exponent_numerator)
if ReceivingNumber.Cmp(_zero_Rat) < 0 {
output = new(big.Rat).Quo(_one_Rat, output)
}
return Number(output), ArErr{}
}
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
},
}
val.obj["__factorial__"] = builtinFunc{
"__factorial__",
@@ -739,7 +1281,7 @@ func _int64_logic(val ArObject, CurrentNumber int64) {
coloured := a[0].(bool)
output := []string{}
if coloured {
output = append(output, "\x1b[34;5;240m")
output = append(output, "\x1b[34;5;25m")
}
output = append(output, fmt.Sprint(CurrentNumber))
if coloured {
@@ -816,6 +1358,25 @@ func _int64_logic(val ArObject, CurrentNumber int64) {
},
}
val.obj["__Equal__"] = builtinFunc{
"__Equal__",
func(a ...any) (any, ArErr) {
if len(a) != 1 {
return nil, ArErr{"Type Error", "expected 1 argument, got " + fmt.Sprint(len(a)), 0, "", "", true}
}
a[0] = ArValidToAny(a[0])
switch ReceivingNumber := a[0].(type) {
case *big.Int:
return big.NewInt(CurrentNumber).Cmp(ReceivingNumber) == 0, ArErr{}
case int64:
return CurrentNumber == ReceivingNumber, ArErr{}
case *big.Rat:
return new(big.Rat).SetInt64(CurrentNumber).Cmp(ReceivingNumber) == 0, ArErr{}
}
return false, ArErr{}
},
}
val.obj["__Add__"] = builtinFunc{
"__Add__",
func(a ...any) (any, ArErr) {
@@ -1008,11 +1569,218 @@ func _int64_logic(val ArObject, CurrentNumber int64) {
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
},
}
val.obj["__Modulo__"] = builtinFunc{
"__Modulo__",
func(a ...any) (any, ArErr) {
if len(a) != 1 {
return nil, ArErr{"Type Error", "expected 1 argument, got " + fmt.Sprint(len(a)), 0, "", "", true}
}
if typeof(a[0]) != "number" {
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
}
a[0] = ArValidToAny(a[0])
switch ReceivingNumber := a[0].(type) {
case *big.Int:
if ReceivingNumber.Cmp(_zero) == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
CurrentNumber_BigInt := big.NewInt(CurrentNumber)
return Number(new(big.Int).Mod(CurrentNumber_BigInt, ReceivingNumber)), ArErr{}
case int64:
if ReceivingNumber == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
return Number(CurrentNumber % ReceivingNumber), ArErr{}
case *big.Rat:
if ReceivingNumber.Cmp(_zero_Rat) == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
CurrentNumber_Rat := new(big.Rat).SetInt64(CurrentNumber)
x := newNumber().Set(ReceivingNumber)
output := new(big.Rat).Set(CurrentNumber_Rat)
x.Quo(output, x)
x = floor(x)
x.Mul(x, ReceivingNumber)
output.Sub(output, x)
return Number(output), ArErr{}
}
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
},
}
val.obj["__PostModulo__"] = builtinFunc{
"__PostModulo__",
func(a ...any) (any, ArErr) {
if len(a) != 1 {
return nil, ArErr{"Type Error", "expected 1 argument, got " + fmt.Sprint(len(a)), 0, "", "", true}
}
if typeof(a[0]) != "number" {
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
}
a[0] = ArValidToAny(a[0])
switch ReceivingNumber := a[0].(type) {
case *big.Int:
if CurrentNumber == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
return Number(new(big.Int).Mod(ReceivingNumber, big.NewInt(CurrentNumber))), ArErr{}
case int64:
if CurrentNumber == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
return Number(ReceivingNumber % CurrentNumber), ArErr{}
case *big.Rat:
if CurrentNumber == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
x := newNumber().Set(ReceivingNumber)
output := new(big.Rat).SetInt64(CurrentNumber)
x.Quo(output, x)
x = floor(x)
x.Mul(x, ReceivingNumber)
output.Sub(output, x)
return Number(output), ArErr{}
}
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
},
}
val.obj["__IntDivide__"] = builtinFunc{
"__IntDivide__",
func(a ...any) (any, ArErr) {
if len(a) != 1 {
return nil, ArErr{"Type Error", "expected 1 argument, got " + fmt.Sprint(len(a)), 0, "", "", true}
}
if typeof(a[0]) != "number" {
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
}
a[0] = ArValidToAny(a[0])
switch ReceivingNumber := a[0].(type) {
case *big.Int:
if ReceivingNumber.Cmp(_zero) == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
return Number(new(big.Int).Div(new(big.Int).SetInt64(CurrentNumber), ReceivingNumber)), ArErr{}
case int64:
if ReceivingNumber == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
return Number(CurrentNumber / ReceivingNumber), ArErr{}
case *big.Rat:
if ReceivingNumber.Cmp(_zero_Rat) == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
return Number(floor(new(big.Rat).Quo(new(big.Rat).SetInt64(CurrentNumber), ReceivingNumber))), ArErr{}
}
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
},
}
val.obj["__PostIntDivide__"] = builtinFunc{
"__PostIntDivide__",
func(a ...any) (any, ArErr) {
if len(a) != 1 {
return nil, ArErr{"Type Error", "expected 1 argument, got " + fmt.Sprint(len(a)), 0, "", "", true}
}
if typeof(a[0]) != "number" {
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
}
a[0] = ArValidToAny(a[0])
switch ReceivingNumber := a[0].(type) {
case *big.Int:
if CurrentNumber == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
return Number(new(big.Int).Div(ReceivingNumber, new(big.Int).SetInt64(CurrentNumber))), ArErr{}
case int64:
if CurrentNumber == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
return Number(CurrentNumber / ReceivingNumber), ArErr{}
case *big.Rat:
if CurrentNumber == 0 {
return nil, ArErr{"Runtime Error", "division by zero", 0, "", "", true}
}
return Number(floor(new(big.Rat).Quo(ReceivingNumber, new(big.Rat).SetInt64(CurrentNumber)))), ArErr{}
}
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
},
}
val.obj["__Power__"] = builtinFunc{
"__Power__",
func(a ...any) (any, ArErr) {
if len(a) != 1 {
return nil, ArErr{"Type Error", "expected 1 argument, got " + fmt.Sprint(len(a)), 0, "", "", true}
}
if typeof(a[0]) != "number" {
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
}
a[0] = ArValidToAny(a[0])
switch ReceivingNumber := a[0].(type) {
case *big.Int:
if ReceivingNumber.Cmp(_zero) == 0 {
return Number(1), ArErr{}
}
output := new(big.Int).SetInt64(CurrentNumber)
output.Exp(output, new(big.Int).Abs(ReceivingNumber), nil)
if ReceivingNumber.Cmp(_zero) < 0 {
output = new(big.Int).Quo(_one, output)
}
return Number(output), ArErr{}
case int64:
if ReceivingNumber == 0 {
return Number(1), ArErr{}
}
output := new(big.Int).SetInt64(CurrentNumber)
output.Exp(output, new(big.Int).Abs(big.NewInt(ReceivingNumber)), nil)
if ReceivingNumber < 0 {
output = new(big.Int).Quo(_one, output)
}
return Number(output), ArErr{}
case *big.Rat:
if ReceivingNumber.Cmp(_zero_Rat) == 0 {
return Number(1), ArErr{}
}
exponent_numerator := new(big.Int).Abs(ReceivingNumber.Num())
exponent_denominator := ReceivingNumber.Denom()
output := new(big.Rat).SetInt64(CurrentNumber)
output_float, _ := output.Float64()
// error if output_float is infinity
if math.IsInf(output_float, 0) {
return nil, ArErr{"Runtime Error", "number too large to perform rational exponential calculations on it.", 0, "", "", true}
}
exponent_denominator_float, _ := exponent_denominator.Float64()
if math.IsInf(exponent_denominator_float, 0) {
return nil, ArErr{"Runtime Error", "demominator too large to perform rational exponential calculations on it.", 0, "", "", true}
}
if exponent_denominator_float != 1 {
output_float = math.Pow(output_float, 1/exponent_denominator_float)
}
output = new(big.Rat).SetFloat64(output_float)
output = exponentBySquaring(output, exponent_numerator)
if ReceivingNumber.Cmp(_zero_Rat) < 0 {
output = new(big.Rat).Quo(_one_Rat, output)
}
return Number(output), ArErr{}
}
return nil, ArErr{"Type Error", "expected number, got " + typeof(a[0]), 0, "", "", true}
},
}
val.obj["__factorial__"] = builtinFunc{
"__factorial__",
func(a ...any) (any, ArErr) {
return nil, ArErr{"Runtime Error", "factorial of a non-integer number", 0, "", "", true}
if CurrentNumber < 0 {
return nil, ArErr{"Runtime Error", "factorial of a negative number", 0, "", "", true}
}
if CurrentNumber == 0 {
return Number(1), ArErr{}
}
result := big.NewInt(1)
for i := int64(1); i <= CurrentNumber; i++ {
result.Mul(result, big.NewInt(i))
}
return Number(result), ArErr{}
},
}
}

View File

@@ -71,7 +71,7 @@ func parseOperations(code UNPARSEcode, index int, codelines []UNPARSEcode) (oper
path: code.path,
}, index, codelines, 0)
if !success || err.EXISTS {
return operationType{}, success, err, 0
continue
}
values = append(values, resp)
totalStep += respindex - 1
@@ -298,7 +298,7 @@ func calcDivide(o operationType, stack stack, stacklevel int) (any, ArErr) {
stack,
stacklevel+1,
)
resp = ArValidToAny(resp)
if err.EXISTS {
return nil, err
}
@@ -468,7 +468,7 @@ func calcAnd(o operationType, stack stack, stacklevel int) (any, ArErr) {
stack,
stacklevel+1,
)
resp = ArValidToAny(resp)
if err.EXISTS {
return nil, err
}
@@ -488,7 +488,7 @@ func calcOr(o operationType, stack stack, stacklevel int) (any, ArErr) {
stack,
stacklevel+1,
)
resp = ArValidToAny(resp)
if err.EXISTS {
return nil, err
}
@@ -524,7 +524,7 @@ func calcNotIn(o operationType, stack stack, stacklevel int) (any, ArErr) {
stack,
stacklevel+1,
)
resp = ArValidToAny(resp)
if err.EXISTS {
return false, err
}
@@ -695,7 +695,6 @@ func calcMod(o operationType, stack stack, stacklevel int) (any, ArErr) {
stack,
stacklevel+1,
)
resp = ArValidToAny(resp)
if err.EXISTS {
return nil, err
}
@@ -744,7 +743,7 @@ func calcIntDiv(o operationType, stack stack, stacklevel int) (any, ArErr) {
stack,
stacklevel+1,
)
resp = ArValidToAny(resp)
if err.EXISTS {
return nil, err
}
@@ -767,7 +766,7 @@ func calcIntDiv(o operationType, stack stack, stacklevel int) (any, ArErr) {
}
return nil, ArErr{
"Runtime Error",
"Cannot divide type '" + typeof(resp) + "'",
"Cannot int divide type '" + typeof(resp) + "'",
o.line,
o.path,
o.code,
@@ -783,7 +782,7 @@ func calcIntDiv(o operationType, stack stack, stacklevel int) (any, ArErr) {
// stack,
// stacklevel+1,
// )
// resp = ArValidToAny(resp)
//
// if err.EXISTS {
// return nil, err
// }
@@ -804,7 +803,7 @@ func calcIntDiv(o operationType, stack stack, stacklevel int) (any, ArErr) {
// stack,
// stacklevel+1,
// )
// resp = ArValidToAny(resp)
//
// if err.EXISTS {
// return nil, err
// }

View File

@@ -6,10 +6,10 @@ import (
"time"
)
func random() number {
return newNumber().SetFloat64(
rand.Float64(),
)
var rand_source = rand.New(rand.NewSource(time.Now().UnixMicro()))
func random() ArObject {
return Number(rand_source.Float64())
}
func randomRange(args ...any) (any, ArErr) {
@@ -33,23 +33,88 @@ func randomRange(args ...any) (any, ArErr) {
EXISTS: true,
}
}
min := args[0].(number)
max := args[1].(number)
if min.Cmp(max) > 0 {
min := args[0].(ArObject)
max := args[1].(ArObject)
compare_num, err := CompareObjects(min, max)
if err.EXISTS {
return nil, err
}
compare, Err := numberToInt64(compare_num)
if Err != nil {
return nil, ArErr{
TYPE: "Runtime Error",
message: "takes a min less than max",
message: Err.Error(),
EXISTS: true,
}
}
difference := newNumber().Sub(max, min)
rand := random()
rand.Mul(rand, difference)
rand.Add(rand, min)
return rand, ArErr{}
if compare == 1 {
return nil, ArErr{
TYPE: "Runtime Error",
message: "range() num 1 must be less than or equal to num 2",
EXISTS: true,
}
}
var ArRandom = Map(anymap{
num_range, err := runOperation(
operationType{
operation: 11,
values: []any{max, min},
},
stack{},
0,
)
if err.EXISTS {
return nil, err
}
if _, ok := num_range.(ArObject); !ok {
return nil, ArErr{
TYPE: "Runtime Error",
message: "could not subtract the two numbers to calculate the range",
EXISTS: true,
}
}
num_range_obj := num_range.(ArObject)
rand := random()
multiplier, err := runOperation(
operationType{
operation: 12,
values: []any{rand, num_range_obj},
},
stack{},
0,
)
if err.EXISTS {
return nil, err
}
if _, ok := multiplier.(ArObject); !ok {
return nil, ArErr{
TYPE: "Runtime Error",
message: "could not multiply the random number by the range",
EXISTS: true,
}
}
return runOperation(
operationType{
operation: 10,
values: []any{multiplier, min},
},
stack{},
0,
)
}
var ArRandom = ArObject{anymap{
"__call__": builtinFunc{"random", func(args ...any) (any, ArErr) {
if len(args) != 0 {
return nil, ArErr{
@@ -83,22 +148,15 @@ var ArRandom = Map(anymap{
EXISTS: true,
}
}
if !a[0].(number).IsInt() {
new_seed, err := numberToInt64(a[0].(ArObject))
if err != nil {
return nil, ArErr{
TYPE: "Runtime Error",
message: "takes an integer not a float",
message: err.Error(),
EXISTS: true,
}
}
rand.Seed(
a[0].(number).Num().Int64(),
)
rand_source.Seed(new_seed)
return nil, ArErr{}
}},
})
func initRandom() {
rand.Seed(
time.Now().UnixMicro(),
)
}
}}

View File

@@ -2,6 +2,7 @@ package main
import (
"fmt"
"math/big"
"reflect"
)
@@ -210,6 +211,8 @@ func runVal(line any, stack stack, stacklevel int) (any, ArErr) {
return runTryCatch(x, stack, stacklevel+1)
case compiledNumber:
return Number(x.value), ArErr{}
case int64, int, float64, float32, *big.Rat, *big.Int:
return Number(x), ArErr{}
case bool, ArObject, nil, Callable, builtinFunc, anymap:
return x, ArErr{}
}

View File

@@ -19,7 +19,7 @@ func shell(global ArObject) {
}
}
}()
fmt.Print("\x1b[32;5;240mWelcome to the Argon v3!\x1b[0m\n\n")
fmt.Print("\x1b[32;5;25mWelcome to the Argon v3!\x1b[25m\n\n")
for {
indent := 0
previous := 0
@@ -27,7 +27,12 @@ func shell(global ArObject) {
textBefore := ">>>"
for i := 1; indent > 0 || (previous != indent && indent >= 0) || i == 1; i++ {
indentStr := strings.Repeat(" ", indent)
code := indentStr + input("\x1b[38;5;240m"+textBefore+indentStr+" \x1b[0m\x1b[1;5;240m")
line, err := input("\x1b[38;5;240m" + textBefore + indentStr + " \x1b[0m\x1b[1;5;25m")
if err != nil {
fmt.Println("\x1b[0m\n\x1b[32;5;25mBye :)\x1b[0m")
os.Exit(0)
}
code := indentStr + line
fmt.Print("\x1b[0m")
totranslate = append(totranslate, UNPARSEcode{code, code, i, "<shell>"})
trimmed := strings.TrimSpace(code)

View File

@@ -151,14 +151,12 @@ func ArString(str string) ArObject {
{
if a[0] == nil {
start = 0
} else if typeof(a[0]) != "number" || !a[0].(number).IsInt() {
return "", ArErr{
TYPE: "Type Error",
message: "slice index must be an integer",
EXISTS: true,
} else if x, ok := a[0].(ArObject); ok {
start64, err := numberToInt64(x)
if err != nil {
return nil, ArErr{"Type Error", err.Error(), 0, "", "", true}
}
} else {
start = int(a[0].(number).Num().Int64())
start = int(start64)
}
}
if len(a) > 1 {

View File

@@ -140,7 +140,11 @@ var ArInput = Map(
return ArString(resp), ArErr{}
}},
"__call__": builtinFunc{"input", func(args ...any) (any, ArErr) {
return input(args...), ArErr{}
resp, err := input(args...)
if err != nil {
return nil, ArErr{TYPE: "Runtime Error", message: err.Error(), EXISTS: true}
}
return ArString(resp), ArErr{}
}},
"pause": builtinFunc{"pause", func(args ...any) (any, ArErr) {
pause()

View File

@@ -30,7 +30,7 @@ func anyToArgon(x any, representive bool, simplify bool, depth int, indent int,
switch x := x.(type) {
case bool:
if colored {
output = append(output, "\x1b[35;5;240m")
output = append(output, "\x1b[35;5;25m")
}
output = append(output, strconv.FormatBool(x))
if colored {
@@ -38,7 +38,7 @@ func anyToArgon(x any, representive bool, simplify bool, depth int, indent int,
}
case nil:
if colored {
output = append(output, "\x1b[31;5;240m")
output = append(output, "\x1b[31;5;25m")
}
output = append(output, "null")
if colored {
@@ -99,7 +99,7 @@ func anyToArgon(x any, representive bool, simplify bool, depth int, indent int,
} else {
outputkeyval := []string{}
if colored {
outputkeyval = append(outputkeyval, "\x1b[36;5;240m")
outputkeyval = append(outputkeyval, "\x1b[36;5;25m")
}
outputkeyval = append(outputkeyval, key.(string))
if colored {
@@ -119,7 +119,7 @@ func anyToArgon(x any, representive bool, simplify bool, depth int, indent int,
output = append(output, anyToArgon(item, true, true, depth-1, indent+1, colored, plain))
}
if colored {
output = append(output, "\x1b[38;5;240m(...)\x1b[0m")
output = append(output, "\x1b[38;5;25m(...)\x1b[0m")
} else {
output = append(output, "(...)")
}
@@ -144,7 +144,7 @@ func anyToArgon(x any, representive bool, simplify bool, depth int, indent int,
return "[" + maybenewline + (strings.Repeat(" ", (indent+1)*plain)) + strings.Join(output, ","+maybenewline+(strings.Repeat(" ", (indent+1)*plain))) + maybenewline + (strings.Repeat(" ", indent*plain)) + "]"
case builtinFunc:
if colored {
output = append(output, "\x1b[38;5;240m")
output = append(output, "\x1b[38;5;25m")
}
output = append(output, "<builtin function "+x.name+">")
if colored {
@@ -152,7 +152,7 @@ func anyToArgon(x any, representive bool, simplify bool, depth int, indent int,
}
case Callable:
if colored {
output = append(output, "\x1b[38;5;240m")
output = append(output, "\x1b[38;5;25m")
}
output = append(output, "<function "+x.name+">")
if colored {

View File

@@ -103,13 +103,6 @@ func translateVal(code UNPARSEcode, index int, codelines []UNPARSEcode, isLine i
}
QuickKnownFailures["squareroot"+code.code] = true
}
if !QuickKnownFailures["factorial"+code.code] && isFactorial(code) {
resp, worked, err, i = parseFactorial(code, index, codelines)
if worked {
return resp, worked, err, i
}
QuickKnownFailures["factorial"+code.code] = true
}
if isVariable(code) {
return parseVariable(code)
}
@@ -139,6 +132,13 @@ func translateVal(code UNPARSEcode, index int, codelines []UNPARSEcode, isLine i
return nil, worked, err, step
}
}
if !QuickKnownFailures["factorial"+code.code] && isFactorial(code) {
resp, worked, err, i = parseFactorial(code, index, codelines)
if worked {
return resp, worked, err, i
}
QuickKnownFailures["factorial"+code.code] = true
}
if !QuickKnownFailures["call"+code.code] && isCall(code) {
resp, worked, err, i = parseCall(code, index, codelines)
if worked {

View File

@@ -1 +0,0 @@
term.log(9!!)