update how powers work

This commit is contained in:
2023-06-17 00:57:17 +01:00
parent 651830ec31
commit 63d7616053
15 changed files with 107 additions and 33 deletions

View File

@@ -84,7 +84,7 @@ func runCall(c call, stack stack, stacklevel int) (any, ArErr) {
c.line,
c.code,
c.path,
}, stack, stacklevel)
}, stack, stacklevel+1)
if !err.EXISTS {
callable = callable_
}

View File

@@ -7,7 +7,7 @@ import (
var debug = os.Getenv("__ARGON_DEBUG__") == "true"
func debugPrintln(a ...interface{}) {
func debugPrintln(a ...any) {
if debug {
go func() {
defer func() {

View File

@@ -26,6 +26,14 @@ func mapGet(r ArMapGet, stack stack, stacklevel int) (any, ArErr) {
if err.EXISTS {
return nil, err
}
switch m := resp.(type) {
case ArObject:
if obj, ok := m.obj[r.args[0]]; ok {
return obj, ArErr{}
}
}
switch m := resp.(type) {
case ArObject:
if callable, ok := m.obj["__getindex__"]; ok {
@@ -47,13 +55,6 @@ func mapGet(r ArMapGet, stack stack, stacklevel int) (any, ArErr) {
}
}
switch m := resp.(type) {
case ArObject:
if obj, ok := m.obj[r.args[0]]; ok {
return obj, ArErr{}
}
}
key, err := runVal(r.args[0], stack, stacklevel+1)
if err.EXISTS {
return nil, err

View File

@@ -46,8 +46,8 @@ func jsonstringify(obj any, level int) (string, error) {
output := []string{}
obj = ArValidToAny(obj)
switch x := obj.(type) {
case ArObject:
for key, value := range x.obj {
case anymap:
for key, value := range x {
str, err := jsonstringify(value, level+1)
if err != nil {
return "", err
@@ -89,6 +89,7 @@ var ArJSON = Map(anymap{
if typeof(args[0]) != "string" {
return nil, ArErr{TYPE: "Runtime Error", message: "parse takes a string not a '" + typeof(args[0]) + "'", EXISTS: true}
}
args[0] = ArValidToAny(args[0])
return jsonparse(args[0].(string)), ArErr{}
}},
"stringify": builtinFunc{"stringify", func(args ...any) (any, ArErr) {
@@ -99,6 +100,6 @@ var ArJSON = Map(anymap{
if err != nil {
return nil, ArErr{TYPE: "Runtime Error", message: err.Error(), EXISTS: true}
}
return str, ArErr{}
return ArString(str), ArErr{}
}},
})

View File

@@ -16,6 +16,7 @@ func newscope() ArObject {
func main() {
debugPrintln("In debug mode...")
if !debug {
defer func() {
if r := recover(); r != nil {

View File

@@ -46,6 +46,8 @@ var operations = [][]string{
"**",
}}
var one = newNumber().SetInt64(1)
type operationType struct {
operation int
values []any
@@ -242,9 +244,9 @@ func compareValues(o operationType, stack stack, stacklevel int) (bool, ArErr) {
true,
}
case 8:
return notequals(resp, resp2, o, stack, stacklevel)
return notequals(resp, resp2, o, stack, stacklevel+1)
case 9:
return equals(resp, resp2, o, stack, stacklevel)
return equals(resp, resp2, o, stack, stacklevel+1)
default:
return false, ArErr{
"Runtime Error",
@@ -818,12 +820,49 @@ func calcPower(o operationType, stack stack, stacklevel int) (number, ArErr) {
return nil, err
}
if typeof(resp) == "number" {
n1, _ := output.Float64()
n2, _ := resp.(number).Float64()
output = newNumber().SetFloat64(math.Pow(n1, n2))
if output == nil {
output = infinity
n := newNumber().Set(resp.(number))
if n.Cmp(newNumber().SetInt64(10)) <= 0 {
toOut := newNumber().SetInt64(1)
clone := newNumber().Set(output)
nAbs := (abs(newNumber().Set(n)))
j := newNumber()
for ; j.Cmp(nAbs) < 0; j.Add(j, one) {
toOut.Mul(toOut, clone)
}
nAbs.Sub(nAbs, j)
if nAbs.Cmp(newNumber()) < 0 {
j.Sub(j, one)
n1, _ := toOut.Float64()
n2, _ := nAbs.Float64()
calculated := newNumber().SetFloat64(math.Pow(n1, n2))
if calculated == nil {
calculated = infinity
}
toOut.Mul(toOut, calculated)
}
if n.Cmp(newNumber()) < 0 {
toOut.Quo(newNumber().SetInt64(1), toOut)
}
output.Set(toOut)
} else if n.Cmp(newNumber().SetInt64(1)) != 0 {
n1, _ := output.Float64()
n2, _ := n.Float64()
calculated := newNumber().SetFloat64(math.Pow(n1, n2))
if calculated == nil {
calculated = infinity
}
output.Mul(output, calculated)
}
/*
n1, _ := output.Float64()
n2, _ := resp.(number).Float64()
output = newNumber().SetFloat64(math.Pow(n1, n2))
if output == nil {
output = infinity
}
*/
} else {
return nil, ArErr{
"Runtime Error",

View File

@@ -70,6 +70,17 @@ func runImport(importOBJ ArImport, stack stack, stacklevel int) (any, ArErr) {
}
return nil, err
}
setindex, ok := stack[len(stack)-1].obj["__setindex__"]
if !ok {
return nil, ArErr{
"Import Error",
"could not find __setindex__ in module scope",
importOBJ.line,
importOBJ.path,
importOBJ.code,
true,
}
}
switch x := importOBJ.values.(type) {
case []string:
for _, v := range x {
@@ -77,10 +88,10 @@ func runImport(importOBJ ArImport, stack stack, stacklevel int) (any, ArErr) {
if !ok {
return nil, ArErr{"Import Error", "could not find value " + anyToArgon(v, true, false, 3, 0, false, 0) + " in module " + anyToArgon(path, true, false, 3, 0, false, 0), importOBJ.line, importOBJ.path, importOBJ.code, true}
}
stack[len(stack)-1].obj[v] = val
builtinCall(setindex, []any{v, val})
}
case string:
stack[len(stack)-1].obj[x] = stackMap
builtinCall(setindex, []any{x, stackMap})
}
return nil, ArErr{}
}

View File

@@ -104,12 +104,6 @@ func translateVal(code UNPARSEcode, index int, codelines []UNPARSEcode, isLine i
return resp, worked, err, i
}
}
if isCall(code) {
resp, worked, err, i = parseCall(code, index, codelines)
if worked {
return resp, worked, err, i
}
}
{
operation, worked, err, step := parseOperations(code, index, codelines)
if worked {
@@ -118,6 +112,12 @@ func translateVal(code UNPARSEcode, index int, codelines []UNPARSEcode, isLine i
return nil, worked, err, step
}
}
if isCall(code) {
resp, worked, err, i = parseCall(code, index, codelines)
if worked {
return resp, worked, err, i
}
}
if isNegative(code) {
return parseNegative(code, index, codelines)
} else if isMapGet(code) {

View File

@@ -57,7 +57,7 @@ func parseTryCatch(code UNPARSEcode, index int, codelines []UNPARSEcode) (TryCat
}
func runTryCatch(t TryCatch, stack stack, stacklevel int) (any, ArErr) {
val, err := runVal(t.Try, stack, stacklevel)
val, err := runVal(t.Try, stack, stacklevel+1)
if err.EXISTS {
vars := anymap{}
vars[t.errorName] = Map(anymap{
@@ -67,7 +67,7 @@ func runTryCatch(t TryCatch, stack stack, stacklevel int) (any, ArErr) {
"path": err.path,
"code": err.code,
})
val, err = runVal(t.Catch, append(stack, Map(vars)), stacklevel)
val, err = runVal(t.Catch, append(stack, Map(vars)), stacklevel+1)
if err.EXISTS {
return nil, err
}