diff --git a/interpreter/eval.go b/interpreter/eval.go index f872676..df22c29 100644 --- a/interpreter/eval.go +++ b/interpreter/eval.go @@ -512,7 +512,12 @@ func Evaluate(expression ast.Expression, env *environment.Environment) (interfac return nil, err } - defaultError := exception.NewRuntimeError("RT028", types.SafeParseUmbraType(value), types.SafeParseUmbraType(expr.Type.Type)) + expectedType, err := types.ParseTokenType(expr.Type.Type) + if err != nil { + return nil, err + } + + defaultError := exception.NewRuntimeError("RT028", types.SafeParseUmbraType(value), expectedType) switch expr.Type.Type { case tokens.STR_TYPE: @@ -523,6 +528,16 @@ func Evaluate(expression ast.Expression, env *environment.Environment) (interfac return strconv.FormatFloat(v, 'f', -1, 64), nil case bool: return strconv.FormatBool(v), nil + case []interface{}: + var strElements []string + for _, elem := range v { + strElem, ok := elem.(string) + if !ok { + return nil, exception.NewRuntimeError("RT028", types.SafeParseUmbraType(elem), "") + } + strElements = append(strElements, strElem) + } + return strings.Join(strElements, ""), nil } return nil, defaultError case tokens.CHAR_TYPE: diff --git a/lib/strings.u b/lib/strings.u index fd16859..50db018 100644 --- a/lib/strings.u +++ b/lib/strings.u @@ -1,42 +1,63 @@ import "io" -def substring(string str, start num, end num) { - mut result str = "" - if (start < 0 or end > ~string or start > end) { - return result +def join(array arr, separator str) str { + if (~array == 0) { + return "" } - for mut i num = 0, ~range string - 1 { - if (i < start) { - continue + mut totalLength num = 0 + + for mut i num = 0, ~array - 1 { + totalLength = totalLength + ~array[i] + } + + totalLength = totalLength + (~separator * (~array - 1)) + + mut buffer arr = [] + + mut index num = 0 + for mut i num = 0, ~array - 1 { + for mut j num = 0, ~array[i] - 1 { + buffer[index] = (range array[i])[j] + index = index + 1 } - if (i >= end) { - break + if (i < ~array - 1) { + for mut k num = 0, ~separator - 1 { + buffer[index] = (range separator)[k] + index = index + 1 + } } - - result = result + (range string)[i] } - return result + return str(buffer) +} + +def substring(string str, start num, end num) { + mut result arr = [] + if (start < 0 or end > ~string or start > end) { + return "" + } + + for mut i num = start, end - 1 { + result[~result] = (range string)[i] + } + + return join(result, "") } def split(string str, separator char) { mut result arr = [] mut start num = 0 - mut end num = 0 for mut i num = 0, ~range string - 1 { if (char((range string)[i]) == separator) { - result[~result] = substring(string, start, end) + result[~result] = substring(string, start, i) start = i + 1 - end = i + 1 - } else { - end = i + 1 } } - result[~result] = substring(string, start, end) + result[~result] = substring(string, start, ~range string) return result } @@ -71,4 +92,5 @@ pub { split toUpper toLower + join }