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:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user