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

BIN
.DS_Store vendored

Binary file not shown.

2
build
View File

@@ -1 +1 @@
go build -ldflags "-s -w" -o bin/argon ./src go build -trimpath -ldflags="-s -w" -o bin/argon ./src

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -70,6 +70,17 @@ func runImport(importOBJ ArImport, stack stack, stacklevel int) (any, ArErr) {
} }
return nil, err 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) { switch x := importOBJ.values.(type) {
case []string: case []string:
for _, v := range x { for _, v := range x {
@@ -77,10 +88,10 @@ func runImport(importOBJ ArImport, stack stack, stacklevel int) (any, ArErr) {
if !ok { 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} 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: case string:
stack[len(stack)-1].obj[x] = stackMap builtinCall(setindex, []any{x, stackMap})
} }
return nil, ArErr{} return nil, ArErr{}
} }

View File

@@ -104,12 +104,6 @@ func translateVal(code UNPARSEcode, index int, codelines []UNPARSEcode, isLine i
return resp, worked, err, 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) operation, worked, err, step := parseOperations(code, index, codelines)
if worked { if worked {
@@ -118,6 +112,12 @@ func translateVal(code UNPARSEcode, index int, codelines []UNPARSEcode, isLine i
return nil, worked, err, step 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) { if isNegative(code) {
return parseNegative(code, index, codelines) return parseNegative(code, index, codelines)
} else if isMapGet(code) { } 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) { 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 { if err.EXISTS {
vars := anymap{} vars := anymap{}
vars[t.errorName] = Map(anymap{ vars[t.errorName] = Map(anymap{
@@ -67,7 +67,7 @@ func runTryCatch(t TryCatch, stack stack, stacklevel int) (any, ArErr) {
"path": err.path, "path": err.path,
"code": err.code, "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 { if err.EXISTS {
return nil, err return nil, err
} }

View File

@@ -1,3 +1,5 @@
import "csv" as csv import "csv" as csv
term.log(csv.read("tests/test.csv")) let table = (csv.read("tests/test.csv"))
term.log(number(table[::-1][0][::-1][0]))

19
tests/diff.ar Normal file
View File

@@ -0,0 +1,19 @@
let zero = 1/infinity
let diff(f) = (x) = (f(x + zero) - f(x)) / zero
let count = 0
let f(x) = do
count = count + 1
term.log(count)
return x^10+x^9+x^8+x^7+x^6+x^5+x^4+x^3+x^2+x+1
x = 100
d = 0
forever do
n = f(x)
term.log("f"+("'"*d)+"("+x+") = "+n)
if (n == 0) break
f = diff(f)
d = d + 1
count = 0

View File

@@ -1,6 +1,6 @@
f() = do f() = do
a = [] a = []
for (i from 0 to 10000) a.append(i) for (i from 0 to 100000) a.append(i)
term.log("start") term.log("start")
f() f()

View File

@@ -1,4 +1,4 @@
x = 10 let x = 10
do do
let x = 20 let x = 20