Allow function calls in expressions and add len() built-in

- Parse function calls in parse_term() so they work inside expressions
  (e.g. z = len(x), y = len(x) + 1)
- Add len() built-in for string length in evaluator
This commit is contained in:
Jose Luis Montañes Ojados
2026-02-16 18:31:39 +01:00
parent a36e52a9c3
commit 21efb0563b
3 changed files with 40 additions and 12 deletions

View File

@@ -168,6 +168,17 @@ size_t eval(ASTNode *node, Environment *env, void *allocator, int debug,
printf("\n");
return 0;
}
if (strcmp(node->data.call.name, "len") == 0) {
if (node->data.call.arg_count == 1) {
size_t val = eval(node->data.call.args[0], env, allocator, debug, gc);
Object *obj = (Object *) JLANG_RESOLVE(allocator, val);
if (obj->type == OBJ_STRING) {
return obj_new_int(allocator, obj->data.string_val.length);
}
}
return 0;
}
printf("ERROR: funcion '%s' no definida\n", node->data.call.name);
exit(1);
}