fix not equal

This commit is contained in:
2023-06-22 23:31:37 +01:00
parent ef951afab5
commit 9b3f3d1555
8 changed files with 113 additions and 14 deletions

View File

@@ -48,6 +48,22 @@ func makeGlobal() ArObject {
} }
return nil, ArErr{TYPE: "TypeError", message: "Cannot create map from '" + typeof(a[0]) + "'", EXISTS: true} return nil, ArErr{TYPE: "TypeError", message: "Cannot create map from '" + typeof(a[0]) + "'", EXISTS: true}
}} }}
vars["hex"] = builtinFunc{"hex", func(a ...any) (any, ArErr) {
if len(a) != 1 {
return nil, ArErr{TYPE: "TypeError", message: "expected 1 argument, got " + fmt.Sprint(len(a)), EXISTS: true}
}
a[0] = ArValidToAny(a[0])
switch x := a[0].(type) {
case number:
if x.Denom().Cmp(one.Denom()) != 0 {
return nil, ArErr{TYPE: "TypeError", message: "Cannot convert non-integer to hex", EXISTS: true}
}
n := x.Num().Int64()
return ArString(fmt.Sprintf("%x", n)), ArErr{}
}
return nil, ArErr{TYPE: "TypeError", message: "Cannot convert '" + typeof(a[0]) + "' to hex", EXISTS: true}
}}
vars["throwError"] = builtinFunc{"throwError", ArThrowError}
vars["array"] = builtinFunc{"array", func(a ...any) (any, ArErr) { vars["array"] = builtinFunc{"array", func(a ...any) (any, ArErr) {
if len(a) == 0 { if len(a) == 0 {
return ArArray([]any{}), ArErr{} return ArArray([]any{}), ArErr{}
@@ -241,5 +257,19 @@ func makeGlobal() ArObject {
} }
return nil, ArErr{TYPE: "TypeError", message: "Cannot convert '" + typeof(a[0]) + "' to string", EXISTS: true} return nil, ArErr{TYPE: "TypeError", message: "Cannot convert '" + typeof(a[0]) + "' to string", EXISTS: true}
}} }}
vars["ord"] = builtinFunc{"ord", func(a ...any) (any, ArErr) {
if len(a) != 1 {
return nil, ArErr{TYPE: "ord", message: "ord takes 1 argument, got " + fmt.Sprint(len(a)), EXISTS: true}
}
a[0] = ArValidToAny(a[0])
switch x := a[0].(type) {
case string:
if len(x) != 1 {
return nil, ArErr{TYPE: "ord", message: "ord takes a string with only one character, got " + fmt.Sprint(len(a)), EXISTS: true}
}
return floor(newNumber().SetInt64(int64([]rune(x)[0]))), ArErr{}
}
return nil, ArErr{TYPE: "TypeError", message: "Cannot convert '" + typeof(a[0]) + "' to string", EXISTS: true}
}}
return Map(vars) return Map(vars)
} }

View File

@@ -35,7 +35,7 @@ func parseDoWrap(code UNPARSEcode, index int, codelines []UNPARSEcode) (any, boo
break break
} }
if indent != setindent { if indent != setindent {
return nil, false, ArErr{"Syntax Error", "invalid indent", code.line, code.path, code.realcode, true}, 1 return nil, false, ArErr{"Syntax Error", "invalid indent", i, code.path, codelines[i].code, true}, 1
} }
val, _, err, step := translateVal(codelines[i], i, codelines, 2) val, _, err, step := translateVal(codelines[i], i, codelines, 2)
@@ -56,9 +56,7 @@ func runDoWrap(d dowrap, stack stack, stacklevel int) (any, ArErr) {
return nil, err return nil, err
} }
switch x := val.(type) { switch x := val.(type) {
case Return: case Return, Break, Continue:
return x, ArErr{}
case Break:
return x, ArErr{} return x, ArErr{}
} }
} }

View File

@@ -13,6 +13,33 @@ type ArErr struct {
EXISTS bool EXISTS bool
} }
func ArThrowError(a ...any) (any, ArErr) {
if len(a) != 2 {
return nil, ArErr{
TYPE: "TypeError",
message: "throwError takes 2 arguments, " + fmt.Sprint(len(a)) + " given",
EXISTS: true,
}
} else if typeof(a[0]) != "string" {
return nil, ArErr{
TYPE: "TypeError",
message: "throwError type must be a string",
EXISTS: true,
}
} else if typeof(a[1]) != "string" {
return nil, ArErr{
TYPE: "TypeError",
message: "throwError message must be a string",
EXISTS: true,
}
}
return nil, ArErr{
TYPE: ArValidToAny(a[0]).(string),
message: ArValidToAny(a[1]).(string),
EXISTS: true,
}
}
func panicErr(err ArErr) { func panicErr(err ArErr) {
if err.code != "" && err.line != 0 && err.path != "" { if err.code != "" && err.line != 0 && err.path != "" {
fmt.Println(" File:", err.path+":"+fmt.Sprint(err.line)) fmt.Println(" File:", err.path+":"+fmt.Sprint(err.line))

View File

@@ -4,7 +4,7 @@ import (
"strings" "strings"
) )
var returnCompile = makeRegex(`( *)return( +)(.|\n)+`) var returnCompile = makeRegex(`( *)return(( +)(.|\n)+)?`)
var breakCompile = makeRegex(`( *)break( *)`) var breakCompile = makeRegex(`( *)break( *)`)
var continueCompile = makeRegex(`( *)continue( *)`) var continueCompile = makeRegex(`( *)continue( *)`)
@@ -45,12 +45,17 @@ func isContinue(code UNPARSEcode) bool {
} }
func parseReturn(code UNPARSEcode, index int, codeline []UNPARSEcode) (CallReturn, bool, ArErr, int) { func parseReturn(code UNPARSEcode, index int, codeline []UNPARSEcode) (CallReturn, bool, ArErr, int) {
resp, worked, err, i := translateVal(UNPARSEcode{ val := strings.TrimSpace(code.code)[6:]
code: strings.TrimSpace(code.code)[6:], var resp any
realcode: code.realcode, var worked, err, i = true, ArErr{}, 1
line: code.line, if val != "" {
path: code.path, resp, worked, err, i = translateVal(UNPARSEcode{
}, index, codeline, 1) code: val,
realcode: code.realcode,
line: code.line,
path: code.path,
}, index, codeline, 1)
}
return CallReturn{ return CallReturn{
value: resp, value: resp,
line: code.line, line: code.line,

View File

@@ -44,7 +44,7 @@ func runCreateMap(m createMap, stack stack, stacklevel int) (any, ArErr) {
true, true,
} }
} }
newmap[key] = valVal newmap[keyVal] = valVal
} }
return newmap, ArErr{} return newmap, ArErr{}
} }

View File

@@ -763,7 +763,7 @@ func notequals(a any, b any, o operationType, stack stack, stacklevel int) (bool
o.path, o.path,
}, stack, stacklevel+1) }, stack, stacklevel+1)
if !err.EXISTS { if !err.EXISTS {
return !anyToBool(val), ArErr{} return anyToBool(val), ArErr{}
} }
} }
} }

View File

@@ -83,6 +83,32 @@ func ArSocket(args ...any) (any, ArErr) {
return ArString(string(buf[:n])), ArErr{} return ArString(string(buf[:n])), ArErr{}
}, },
}, },
"writeData": builtinFunc{
"writeData",
func(args ...any) (any, ArErr) {
if len(args) != 1 {
return ArObject{}, ArErr{
TYPE: "SocketError",
message: "Socket.writeData() takes exactly 1 argument",
EXISTS: true,
}
}
data := ArValidToAny(args[0])
switch x := data.(type) {
case []any:
bytes := []byte{}
for _, v := range x {
bytes = append(bytes, byte(v.(number).Num().Int64()))
}
conn.Write(bytes)
return nil, ArErr{}
}
return nil, ArErr{
TYPE: "SocketError",
message: "Socket.writeData() argument must be a array of numbers",
}
},
},
"write": builtinFunc{ "write": builtinFunc{
"write", "write",
func(args ...any) (any, ArErr) { func(args ...any) (any, ArErr) {
@@ -118,6 +144,18 @@ func ArSocket(args ...any) (any, ArErr) {
return conn == nil, ArErr{} return conn == nil, ArErr{}
}, },
}, },
"RemoteAddr": builtinFunc{
"RemoteAddr",
func(args ...any) (any, ArErr) {
return ArString(conn.RemoteAddr().String()), ArErr{}
},
},
"LocalAddr": builtinFunc{
"LocalAddr",
func(args ...any) (any, ArErr) {
return ArString(conn.LocalAddr().String()), ArErr{}
},
},
}), ArErr{} }), ArErr{}
}, },
}, },

View File

@@ -254,7 +254,7 @@ func ArString(str string) ArObject {
if typeof(a[0]) != "string" { if typeof(a[0]) != "string" {
return nil, ArErr{"TypeError", "expected string, got " + typeof(a[0]), 0, "", "", true} return nil, ArErr{"TypeError", "expected string, got " + typeof(a[0]), 0, "", "", true}
} }
splitby := a[0].(ArObject).obj["__value__"].(string) splitby := ArValidToAny(a[0]).(string)
output := []any{} output := []any{}
splitted := (strings.Split(str, splitby)) splitted := (strings.Split(str, splitby))
for _, v := range splitted { for _, v := range splitted {
@@ -553,6 +553,7 @@ func ArString(str string) ArObject {
return nil, ArErr{"TypeError", "expected 1 argument, got " + fmt.Sprint(len(a)), 0, "", "", true} return nil, ArErr{"TypeError", "expected 1 argument, got " + fmt.Sprint(len(a)), 0, "", "", true}
} }
a[0] = ArValidToAny(a[0]) a[0] = ArValidToAny(a[0])
fmt.Println(str, a[0])
return str != a[0], ArErr{} return str != a[0], ArErr{}
}} }}
obj.obj["__Add__"] = builtinFunc{ obj.obj["__Add__"] = builtinFunc{