Skip to content

Commit 56b595e

Browse files
gh-22: Merge function table and namespace.
1 parent d3abe3d commit 56b595e

3 files changed

Lines changed: 30 additions & 351 deletions

File tree

src/builtins.c

Lines changed: 11 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -2230,10 +2230,17 @@ static Value deser_val(JsonValue* obj, UnserCtx* ctx, Interpreter* interp, const
22302230

22312231
JsonValue* nm = json_obj_get(obj, "name");
22322232
if (nm && nm->type == JSON_STR) {
2233-
Func* existing = func_table_lookup(interp->functions, nm->as.str, NULL);
2234-
if (existing) {
2235-
if (id) unser_func_set(ctx, id, existing);
2236-
return value_func(existing);
2233+
Value existing = value_null();
2234+
DeclType dt = TYPE_UNKNOWN;
2235+
bool initialized = false;
2236+
if (interp && interp->global_env && env_get(interp->global_env, nm->as.str, &existing, &dt, &initialized)) {
2237+
if (initialized && existing.type == VAL_FUNC && existing.as.func) {
2238+
if (id) unser_func_set(ctx, id, existing.as.func);
2239+
Value ret = value_func(existing.as.func);
2240+
value_free(existing);
2241+
return ret;
2242+
}
2243+
value_free(existing);
22372244
}
22382245
}
22392246

@@ -4326,42 +4333,6 @@ static Value builtin_import_path(Interpreter* interp, Value* args, int argc, Exp
43264333

43274334
// Expose module symbols into caller env under alias prefix: alias.name -> value
43284335
size_t alias_len = strlen(alias);
4329-
// Expose functions defined in this module as alias.name by matching closure ancestry
4330-
for (FuncEntry* fe = interp->functions->entries; fe != NULL; fe = fe->next) {
4331-
if (!fe->func || !fe->func->closure) continue;
4332-
Env* c = fe->func->closure;
4333-
bool belongs = false;
4334-
while (c) {
4335-
if (c == mod_env) { belongs = true; break; }
4336-
c = c->parent;
4337-
}
4338-
if (!belongs) continue;
4339-
4340-
const char* fname = fe->name ? fe->name : "";
4341-
const char* unq = strchr(fname, '.');
4342-
if (unq) unq = unq + 1; else unq = fname;
4343-
4344-
size_t qlen = alias_len + 1 + strlen(unq) + 1;
4345-
char* qualified = malloc(qlen);
4346-
if (!qualified) { RUNTIME_ERROR(interp, "Out of memory", line, col); }
4347-
snprintf(qualified, qlen, "%s.%s", alias, unq);
4348-
Value fv = value_func(fe->func);
4349-
4350-
// Try to assign qualified symbol into caller env
4351-
if (!env_assign(env, qualified, fv, TYPE_FUNC, true)) {
4352-
value_free(fv);
4353-
free(qualified);
4354-
RUNTIME_ERROR(interp, "IMPORT failed to assign qualified function name", line, col);
4355-
}
4356-
4357-
// Also register a qualified name in the global function table so
4358-
// calls written as 'alias.name()' can be resolved via func_table_lookup
4359-
// (some call sites may appear as a single IDENT with a dot).
4360-
(void)func_table_add(interp->functions, qualified, fe->func);
4361-
4362-
value_free(fv);
4363-
free(qualified);
4364-
}
43654336
for (size_t i = 0; i < mod_env->count; i++) {
43664337
EnvEntry* e = &mod_env->entries[i];
43674338
if (!e->initialized) continue;
@@ -5004,80 +4975,6 @@ static Value builtin_signature(Interpreter* interp, Value* args, int argc, Expr*
50044975
}
50054976
}
50064977

5007-
// If not in environment or not a function there, check the interpreter function table
5008-
Func* ff = NULL;
5009-
if (interp && interp->functions) ff = func_table_lookup(interp->functions, name, env);
5010-
if (ff != NULL) {
5011-
struct Func* f = ff;
5012-
size_t cap = 256;
5013-
char* buf = malloc(cap);
5014-
if (!buf) RUNTIME_ERROR(interp, "Out of memory", line, col);
5015-
buf[0] = '\0';
5016-
strcat(buf, f->name ? f->name : name);
5017-
strcat(buf, "(");
5018-
for (size_t i = 0; i < f->params.count; i++) {
5019-
Param p = f->params.items[i];
5020-
const char* tname = "UNKNOWN";
5021-
switch (p.type) {
5022-
case TYPE_INT: tname = "INT"; break;
5023-
case TYPE_FLT: tname = "FLT"; break;
5024-
case TYPE_STR: tname = "STR"; break;
5025-
case TYPE_TNS: tname = "TNS"; break;
5026-
case TYPE_FUNC: tname = "FUNC"; break;
5027-
case TYPE_THR: tname = "THR"; break;
5028-
default: tname = "ANY"; break;
5029-
}
5030-
if (i > 0) strcat(buf, ", ");
5031-
strcat(buf, tname);
5032-
strcat(buf, ": ");
5033-
strcat(buf, p.name ? p.name : "");
5034-
if (p.default_value != NULL) {
5035-
Value dv = eval_expr(interp, p.default_value, f->closure);
5036-
strcat(buf, " = ");
5037-
if (dv.type == VAL_STR) {
5038-
size_t need = strlen(buf) + strlen(dv.as.s) + 4;
5039-
if (need > cap) { cap = need * 2; buf = realloc(buf, cap); }
5040-
strcat(buf, "\"");
5041-
strcat(buf, dv.as.s);
5042-
strcat(buf, "\"");
5043-
} else if (dv.type == VAL_INT) {
5044-
char* s = int_to_binary_str(dv.as.i);
5045-
size_t need = strlen(buf) + strlen(s) + 2;
5046-
if (need > cap) { cap = need * 2; buf = realloc(buf, cap); }
5047-
strcat(buf, s);
5048-
free(s);
5049-
} else if (dv.type == VAL_FLT) {
5050-
char* s = flt_to_binary_str(dv.as.f);
5051-
size_t need = strlen(buf) + strlen(s) + 2;
5052-
if (need > cap) { cap = need * 2; buf = realloc(buf, cap); }
5053-
strcat(buf, s);
5054-
free(s);
5055-
} else {
5056-
const char* tn = value_type_name(dv);
5057-
size_t need = strlen(buf) + strlen(tn) + 2;
5058-
if (need > cap) { cap = need * 2; buf = realloc(buf, cap); }
5059-
strcat(buf, tn);
5060-
}
5061-
value_free(dv);
5062-
}
5063-
}
5064-
strcat(buf, "):");
5065-
const char* rname = "ANY";
5066-
switch (f->return_type) {
5067-
case TYPE_INT: rname = "INT"; break;
5068-
case TYPE_FLT: rname = "FLT"; break;
5069-
case TYPE_STR: rname = "STR"; break;
5070-
case TYPE_TNS: rname = "TNS"; break;
5071-
case TYPE_FUNC: rname = "FUNC"; break;
5072-
case TYPE_THR: rname = "THR"; break;
5073-
default: rname = "ANY"; break;
5074-
}
5075-
strcat(buf, rname);
5076-
Value out = value_str(buf);
5077-
free(buf);
5078-
return out;
5079-
}
5080-
50814978
// Non-function: return "TYPE: name" using declared type if available
50824979
if (!entry) {
50834980
RUNTIME_ERROR(interp, "SIGNATURE: identifier not found or uninitialized", line, col);
@@ -6206,37 +6103,6 @@ static Value builtin_import(Interpreter* interp, Value* args, int argc, Expr** a
62066103
// Expose module symbols into caller env under alias prefix: alias.name -> value
62076104
size_t alias_len = strlen(alias);
62086105

6209-
// Expose functions defined in this module as alias.name by matching closure ancestry
6210-
for (FuncEntry* fe = interp->functions->entries; fe != NULL; fe = fe->next) {
6211-
if (!fe->func || !fe->func->closure) continue;
6212-
Env* c = fe->func->closure;
6213-
bool belongs = false;
6214-
while (c) {
6215-
if (c == mod_env) { belongs = true; break; }
6216-
c = c->parent;
6217-
}
6218-
if (!belongs) continue;
6219-
6220-
const char* fname = fe->name ? fe->name : "";
6221-
const char* unq = strchr(fname, '.');
6222-
if (unq) unq = unq + 1; else unq = fname;
6223-
6224-
size_t qlen = alias_len + 1 + strlen(unq) + 1;
6225-
char* qualified = malloc(qlen);
6226-
if (!qualified) { RUNTIME_ERROR(interp, "Out of memory", line, col); }
6227-
snprintf(qualified, qlen, "%s.%s", alias, unq);
6228-
Value fv = value_func(fe->func);
6229-
if (!env_assign(env, qualified, fv, TYPE_FUNC, true)) {
6230-
value_free(fv);
6231-
free(qualified);
6232-
RUNTIME_ERROR(interp, "IMPORT failed to assign qualified function name", line, col);
6233-
}
6234-
6235-
(void)func_table_add(interp->functions, qualified, fe->func);
6236-
value_free(fv);
6237-
free(qualified);
6238-
}
6239-
62406106
for (size_t i = 0; i < mod_env->count; i++) {
62416107
EnvEntry* e = &mod_env->entries[i];
62426108
if (!e->initialized) continue;
@@ -6775,7 +6641,6 @@ static Value builtin_parallel(Interpreter* interp, Value* args, int argc, Expr**
67756641
}
67766642
*thr_interp = (Interpreter){0};
67776643
thr_interp->global_env = interp->global_env;
6778-
thr_interp->functions = interp->functions;
67796644
thr_interp->loop_depth = 0;
67806645
thr_interp->error = NULL;
67816646
thr_interp->error_line = 0;

0 commit comments

Comments
 (0)