From 406eafb8a470509771628b6de516b9f59fd0216d Mon Sep 17 00:00:00 2001 From: Ugric Date: Tue, 14 Mar 2023 00:07:18 +0000 Subject: [PATCH] prevent unhashable panic --- src/built-ins.go | 4 +++ src/getIndex.go | 1 + src/letterseperateseperate.go | 3 ++ src/to-argon.go | 2 +- src/translate.go | 60 ++++++++++++++++++++++------------- src/typeof.go | 2 ++ src/variable.go | 4 +++ 7 files changed, 53 insertions(+), 23 deletions(-) diff --git a/src/built-ins.go b/src/built-ins.go index c7da8d0..7dfcf86 100644 --- a/src/built-ins.go +++ b/src/built-ins.go @@ -42,6 +42,10 @@ func init() { switch y := v.(type) { case ArArray: if len(y) == 2 { + keytype := typeof(y[0]) + if keytype == "array" || keytype == "map" { + return nil, ArErr{TYPE: "TypeError", message: "Cannot use unhashable value as key: " + keytype, EXISTS: true} + } newmap[y[0]] = y[1] continue } diff --git a/src/getIndex.go b/src/getIndex.go index 4d19fc3..b04ae6e 100644 --- a/src/getIndex.go +++ b/src/getIndex.go @@ -433,6 +433,7 @@ func indexGetParse(code UNPARSEcode, index int, codelines []UNPARSEcode) (ArMapG } tival, worked, err, i := translateVal(UNPARSEcode{code: ti, realcode: code.realcode, line: code.line, path: code.path}, index, codelines, 0) if !worked { + fmt.Println(err) if i == len(split)-1 { return ArMapGet{}, false, err, i } diff --git a/src/letterseperateseperate.go b/src/letterseperateseperate.go index 9364364..381539f 100644 --- a/src/letterseperateseperate.go +++ b/src/letterseperateseperate.go @@ -22,6 +22,9 @@ func getValuesFromLetter(str string, splitstr string, index int, codelines []UNP if worked { arguments = append(arguments, resp) temp = []string{} + if i == len(commasplit)-1 { + return arguments, true, ArErr{} + } } else if i == len(commasplit)-1 { return nil, false, ArErr{"Syntax Error", "invalid argument", codelines[index].line, codelines[index].path, codelines[index].realcode, true} } diff --git a/src/to-argon.go b/src/to-argon.go index 4b53e63..e7492eb 100644 --- a/src/to-argon.go +++ b/src/to-argon.go @@ -91,7 +91,7 @@ func anyToArgon(x any, quote bool, simplify bool, depth int, indent int, colored keyval := "" if typeof(key) != "string" || !SpacelessVariableCompiled.MatchString(key.(string)) { - keyval = anyToArgon(key, true, true, depth-1, indent+1, colored, plain) + keyval = anyToArgon(key, true, true, depth, indent+1, colored, plain) } else { outputkeyval := []string{} if colored { diff --git a/src/translate.go b/src/translate.go index e405992..03469ba 100644 --- a/src/translate.go +++ b/src/translate.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "strings" ) @@ -11,15 +12,20 @@ type UNPARSEcode struct { path string } -// returns (number | string | nil), success, error, step func translateVal(code UNPARSEcode, index int, codelines []UNPARSEcode, isLine int) (any, bool, ArErr, int) { + var ( + resp any = nil + worked bool = false + err ArErr = ArErr{"Syntax Error", "invalid syntax", code.line, code.path, code.realcode, true} + i int = 1 + ) if isLine == 2 { if isDeleteVariable(code) { return parseDelete(code, index, codelines) } else if isComment(code) { - resp, worked, err, step := parseComment(code, index, codelines) + resp, worked, err, i = parseComment(code, index, codelines) if worked { - return resp, worked, err, step + return resp, worked, err, i } } else if isReturn(code) { return parseReturn(code, index, codelines) @@ -47,30 +53,32 @@ func translateVal(code UNPARSEcode, index int, codelines []UNPARSEcode, isLine i } if isBrackets(code) { - bracket, worked, err, step := parseBrackets(code, index, codelines) + resp, worked, err, i = parseBrackets(code, index, codelines) if worked { - return bracket, worked, err, step + return resp, worked, err, i } } else if isnot(code) { return parseNot(code, index, codelines, isLine) } if isSetVariable(code) { - setvar, worked, err, step := parseSetVariable(code, index, codelines, isLine) + resp, worked, err, i = parseSetVariable(code, index, codelines, isLine) if worked { - return setvar, worked, err, step + return resp, worked, err, i } } if isAutoAsignVariable(code) { - setvar, worked, err, step := parseAutoAsignVariable(code, index, codelines, isLine) + resp, worked, err, i = parseAutoAsignVariable(code, index, codelines, isLine) if worked { - return setvar, worked, err, step + return resp, worked, err, i } } - operation, worked, err, step := parseOperations(code, index, codelines) - if worked { - return operation, worked, err, step - } else if err.EXISTS { - return nil, worked, err, step + { + operation, worked, err, step := parseOperations(code, index, codelines) + if worked { + return operation, worked, err, step + } else if err.EXISTS { + return nil, worked, err, step + } } if isNumber(code) { return parseNumber(code) @@ -79,9 +87,9 @@ func translateVal(code UNPARSEcode, index int, codelines []UNPARSEcode, isLine i } else if isFactorial(code) { return parseFactorial(code, index, codelines) } else if isCall(code) { - call, worked, err, step := parseCall(code, index, codelines) + resp, worked, err, i = parseCall(code, index, codelines) if worked { - return call, worked, err, step + return resp, worked, err, i } } if isBoolean(code) { @@ -89,20 +97,28 @@ func translateVal(code UNPARSEcode, index int, codelines []UNPARSEcode, isLine i } else if isVariable(code) { return parseVariable(code) } else if isArray(code) { - return parseArray(code, index, codelines) - } else if isMapGet(code) { + resp, worked, err, i = parseArray(code, index, codelines) + fmt.Println(resp, worked, err, i) + if worked { + return resp, worked, err, i + } + } + if isMapGet(code) { return mapGetParse(code, index, codelines) } else if isIndexGet(code) { - return indexGetParse(code, index, codelines) - } else if isString(code) { + resp, worked, err, i = indexGetParse(code, index, codelines) + if worked { + return resp, worked, err, i + } + } + if isString(code) { return parseString(code) } else if issquareroot(code) { return parseSquareroot(code, index, codelines) } - return nil, false, ArErr{"Syntax Error", "invalid syntax", code.line, code.path, code.realcode, true}, 1 + return resp, worked, err, i } -// returns [](number | string), error func translate(codelines []UNPARSEcode) ([]any, ArErr) { translated := []any{} for i := 0; i < len(codelines); { diff --git a/src/typeof.go b/src/typeof.go index 8751ac6..c53ae45 100644 --- a/src/typeof.go +++ b/src/typeof.go @@ -16,6 +16,8 @@ func typeof(val any) string { return "function" case ArMap: return "map" + case ArArray: + return "array" case accessVariable: return "variable" } diff --git a/src/variable.go b/src/variable.go index b7ac7a2..3c2d47f 100644 --- a/src/variable.go +++ b/src/variable.go @@ -244,6 +244,10 @@ func setVariableValue(v setVariable, stack stack, stacklevel int) (any, ArErr) { } switch y := respp.(type) { case ArMap: + keytype := typeof(key) + if keytype == "array" || keytype == "map" { + return nil, ArErr{TYPE: "TypeError", message: "Cannot use unhashable value as key: " + keytype, EXISTS: true} + } varMutex.Lock() y[key] = resp varMutex.Unlock()