Skip to content

Commit 9c00c26

Browse files
gh-123: Fix LEN.
1 parent 076174f commit 9c00c26

1 file changed

Lines changed: 15 additions & 15 deletions

File tree

src/builtins.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7201,24 +7201,24 @@ static Value builtin_ilen(Interpreter* interp, Value* args, int argc, Expr** arg
72017201
return value_int(len);
72027202
}
72037203

7204-
// LEN (length - for now just strings)
7204+
// LEN: per specification, returns the number of supplied INT or STR arguments.
7205+
// Passing TNS or any other unsupported type is a runtime error.
72057206
static Value builtin_len(Interpreter* interp, Value* args, int argc, Expr** arg_nodes, Env* env, int line, int col) {
72067207
(void)arg_nodes; (void)env;
7207-
7208-
if (argc == 0) {
7209-
RUNTIME_ERROR(interp, "LEN requires at least one argument", line, col);
7210-
}
7211-
7212-
if (args[0].type == VAL_STR) {
7213-
return value_int((int64_t)strlen(args[0].as.s));
7214-
}
7215-
if (args[0].type == VAL_TNS) {
7216-
Tensor* t = args[0].as.tns;
7217-
if (!t) return value_int(0);
7218-
if (t->ndim == 0) return value_int(0);
7219-
return value_int((int64_t)t->shape[0]);
7208+
7209+
int out_base = 10; // default to decimal when no INT args present
7210+
for (int i = 0; i < argc; ++i) {
7211+
if (args[i].type == VAL_INT) {
7212+
int bi = numeric_base_of(args[i]);
7213+
if (bi > out_base) out_base = bi;
7214+
} else if (args[i].type == VAL_STR) {
7215+
/* allowed */
7216+
} else {
7217+
RUNTIME_ERROR(interp, "LEN expects INT or STR arguments", line, col);
7218+
}
72207219
}
7221-
RUNTIME_ERROR(interp, "LEN expects STR or TNS", line, col);
7220+
7221+
return value_int_base((int64_t)argc, out_base);
72227222
}
72237223

72247224
// Main, OS

0 commit comments

Comments
 (0)