From 9b3f3d155579ca9e15e7059723091c59a5c4b7c7 Mon Sep 17 00:00:00 2001 From: William Bell Date: Thu, 22 Jun 2023 23:31:37 +0100 Subject: [PATCH] fix not equal --- src/built-ins.go | 30 ++++++++++++++++++++++++++++++ src/dowraps.go | 6 ++---- src/error.go | 27 +++++++++++++++++++++++++++ src/jumpStatements.go | 19 ++++++++++++------- src/map.go | 2 +- src/operations.go | 2 +- src/socket.go | 38 ++++++++++++++++++++++++++++++++++++++ src/string.go | 3 ++- 8 files changed, 113 insertions(+), 14 deletions(-) diff --git a/src/built-ins.go b/src/built-ins.go index 525c2d3..7a94bf5 100644 --- a/src/built-ins.go +++ b/src/built-ins.go @@ -48,6 +48,22 @@ func makeGlobal() ArObject { } 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) { if len(a) == 0 { 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} }} + 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) } diff --git a/src/dowraps.go b/src/dowraps.go index a0c4d3e..be8f996 100644 --- a/src/dowraps.go +++ b/src/dowraps.go @@ -35,7 +35,7 @@ func parseDoWrap(code UNPARSEcode, index int, codelines []UNPARSEcode) (any, boo break } 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) @@ -56,9 +56,7 @@ func runDoWrap(d dowrap, stack stack, stacklevel int) (any, ArErr) { return nil, err } switch x := val.(type) { - case Return: - return x, ArErr{} - case Break: + case Return, Break, Continue: return x, ArErr{} } } diff --git a/src/error.go b/src/error.go index ff41ed3..3f2d401 100644 --- a/src/error.go +++ b/src/error.go @@ -13,6 +13,33 @@ type ArErr struct { 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) { if err.code != "" && err.line != 0 && err.path != "" { fmt.Println(" File:", err.path+":"+fmt.Sprint(err.line)) diff --git a/src/jumpStatements.go b/src/jumpStatements.go index 0d3cc25..f151655 100644 --- a/src/jumpStatements.go +++ b/src/jumpStatements.go @@ -4,7 +4,7 @@ import ( "strings" ) -var returnCompile = makeRegex(`( *)return( +)(.|\n)+`) +var returnCompile = makeRegex(`( *)return(( +)(.|\n)+)?`) var breakCompile = makeRegex(`( *)break( *)`) 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) { - resp, worked, err, i := translateVal(UNPARSEcode{ - code: strings.TrimSpace(code.code)[6:], - realcode: code.realcode, - line: code.line, - path: code.path, - }, index, codeline, 1) + val := strings.TrimSpace(code.code)[6:] + var resp any + var worked, err, i = true, ArErr{}, 1 + if val != "" { + resp, worked, err, i = translateVal(UNPARSEcode{ + code: val, + realcode: code.realcode, + line: code.line, + path: code.path, + }, index, codeline, 1) + } return CallReturn{ value: resp, line: code.line, diff --git a/src/map.go b/src/map.go index b458d81..7e12f33 100644 --- a/src/map.go +++ b/src/map.go @@ -44,7 +44,7 @@ func runCreateMap(m createMap, stack stack, stacklevel int) (any, ArErr) { true, } } - newmap[key] = valVal + newmap[keyVal] = valVal } return newmap, ArErr{} } diff --git a/src/operations.go b/src/operations.go index 1c47b20..fca3477 100644 --- a/src/operations.go +++ b/src/operations.go @@ -763,7 +763,7 @@ func notequals(a any, b any, o operationType, stack stack, stacklevel int) (bool o.path, }, stack, stacklevel+1) if !err.EXISTS { - return !anyToBool(val), ArErr{} + return anyToBool(val), ArErr{} } } } diff --git a/src/socket.go b/src/socket.go index cfbe6c8..51c3aec 100644 --- a/src/socket.go +++ b/src/socket.go @@ -83,6 +83,32 @@ func ArSocket(args ...any) (any, 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", func(args ...any) (any, ArErr) { @@ -118,6 +144,18 @@ func ArSocket(args ...any) (any, 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{} }, }, diff --git a/src/string.go b/src/string.go index b129b37..e8c1691 100644 --- a/src/string.go +++ b/src/string.go @@ -254,7 +254,7 @@ func ArString(str string) ArObject { if typeof(a[0]) != "string" { 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{} splitted := (strings.Split(str, splitby)) 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} } a[0] = ArValidToAny(a[0]) + fmt.Println(str, a[0]) return str != a[0], ArErr{} }} obj.obj["__Add__"] = builtinFunc{