1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
/*
ToolBus -- The ToolBus Application Architecture
Copyright (C) 1998-2000 Stichting Mathematisch Centrum, Amsterdam,
The Netherlands.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "toolbus.h"
#include "terms.h"
#include "symbol.h"
#include "procs.h"
#include "tools.h"
#include "utils.h"
/* We use a hash-table to map names to id's. Every symbol also has
a module number, to make it easy to find the module in which a
function is implemented, and optionally a function that implements
the symbol when it is an expression or action.
*/
/* Take a prime number as hash-table size */
#define NAME_TABLE_SIZE 5009
#define MAXNAME NAME_TABLE_SIZE*2
/* static sym_idx n_name = 0; */
/*static char *name_table[MAXNAME];*/
/* Create a hash table initialized to 0. */
static name_bucket *hash_table[NAME_TABLE_SIZE] = { NULL };
static name_bucket *idx_table[MAXNAME];
static sym_idx ID = 0;
/* Calculate the hash index */
static unsigned hash_it(char *n)
{
unsigned i, nr = n[0];
for(i=1; n[i]; i++) {
/* For now, we just try a simple xor hash function. */
nr ^= n[i];
}
return nr % NAME_TABLE_SIZE;
}
/* Retrieve the bucket address of a symbol, updating it's module
when necessary. */
static name_bucket *lookup_bucket(char *name, sym_idx module)
{
name_bucket *cur;
unsigned nr = hash_it(name);
cur = hash_table[nr];
while(cur && !streq(cur->name, name)) {
cur = cur->nb_next;
}
if(cur) {
/* Newer definitions override older ones */
/* except when module is ALL or INTERNAL. */
if(module != IDM_ALL && module != IDM_INTERNAL)
cur->module = module;
return cur;
}
/* if module == IDM_ALL, we are looking for an existing bucket! */
if(module == IDM_ALL)
return NULL;
if(ID == MAXNAME)
/* We might want to just 'realloc'ate more space for names */
err_fatal("lookup -- too many names");
cur = malloc(sizeof(name_bucket));
cur->nb_next = hash_table[nr];
hash_table[nr] = cur;
cur->name = strdup(name);
idx_table[ID] = cur;
cur->id = ID++;
/* Module type defaults to system_module */
if(module == IDM_ALL)
cur->module = system_module;
else
cur->module = module;
cur->u.sig = NULL;
return cur;
}
/* Lookup the index of symbol ``name'', if undefined add it to name_table */
/* and allocate a fresh copy of name */
sym_idx TBlookup(register char *name)
{
name_bucket *b = lookup_bucket(name, IDM_INTERNAL);
assert(b);
return b->id;
}
sym_idx lookupn(register char *bgn, register char *end)
{ char name[256];
register char *s = name;
if(end - bgn > 256)
err_fatal("lookupn -- name too long");
while(bgn < end)
*s++ = *bgn++;
*s = '\0';
return TBlookup(name);
}
/* Retrieve the textual representation of a symbol */
char *get_txt(sym_idx idx)
{
if(idx >= 0 && idx < ID) {
assert(idx_table[idx]);
return idx_table[idx]->name;
}
err_fatal("get_txt -- illegal index");
return NULL;
}
/* Retrieve the signature of an expression function */
struct expr_sign *get_expr_sign(sym_idx sym)
{
name_bucket *bucket;
assert(sym >= 0 && sym < ID);
bucket = idx_table[sym];
assert(bucket);
if(bucket->type == IDT_EXPR_FUN)
return bucket->u.sig;
return NULL;
}
/* Register a new module. */
sym_idx register_module(char *name)
{
name_bucket *bucket = lookup_bucket(name, system_module);
bucket->type = IDT_MODULE;
return bucket->id;
}
/* Frontend for register_func function. */
sym_idx register_function(char *name, sym_idx module, expr_sign *sig)
{
name_bucket *bucket = lookup_bucket(name, module);
bucket->type = IDT_EXPR_FUN;
sig->name = bucket->id;
bucket->u.sig = sig;
return bucket->id;
}
/* Register a function so the ToolBus can typecheck and evaluate it. */
sym_idx register_func(char *name, sym_idx module, type *restype, TBbool eval,
TBbool chkargs, int nargs, type *args[],
term *(*func)(int, term **, term *), char *descr)
{
int i;
expr_sign *sig = malloc(sizeof(expr_sign));
sig->restype = restype;
sig->evargs = eval;
sig->chkargs = chkargs;
sig->nargs = nargs;
if(nargs)
sig->argtype = malloc(sizeof(type *)*nargs);
else
sig->argtype = NULL;
for(i=0; i<nargs; i++)
sig->argtype[i] = args[i];
sig->func = func;
sig->descr = strdup(descr);
return register_function(name, module, sig);
}
/* Register a function with 0 arguments. */
sym_idx register_func0(char *name, sym_idx module, type *restype,
term *(*fn)(int, term **, term *), char *descr)
{
return register_func(name, module, restype, TBfalse, TBtrue, 0, NULL, fn, descr);
}
/* Register a function with 1 argument. */
sym_idx register_func1(char *name, sym_idx module, type *restype, TBbool eval,
type *arg, term *(*fn)(int, term **, term *),
char *descr)
{
return register_func(name, module, restype, eval, TBtrue, 1, &arg, fn, descr);
}
/* Register a function with 2 arguments. */
sym_idx register_func2(char *name, sym_idx module, type *restype, TBbool eval,
type *a1, type *a2, term *(*fn)(int, term **, term *),
char *descr)
{
type *arg[2];
arg[0] = a1;
arg[1] = a2;
return register_func(name, module, restype, eval, TBtrue, 2, arg, fn, descr);
}
/* Register a function with 3 arguments. */
sym_idx register_func3(char *name, sym_idx module, type *restype, TBbool eval,
type *a1, type *a2, type *a3,
term *(*fn)(int, term **, term *), char *descr)
{
type *arg[2];
arg[0] = a1;
arg[1] = a2;
arg[2] = a3;
return register_func(name, module, restype, eval, TBtrue, 3, arg, fn, descr);
}
/* Register a function with variable arguments. */
sym_idx register_func_var(char *name, sym_idx module, term *restype,
TBbool eval, term *(*fn)(int, term **, term *), char *descr)
{
return register_func(name, module, restype, eval, TBfalse, 0, NULL, fn, descr);
}
TBbool is_expr_fun(term *T)
{
name_bucket *bucket = idx_table[fun_sym(T)];
if(bucket->type == IDT_EXPR_FUN)
return TBtrue;
return TBfalse;
}
term *make_symbol_list(int type)
{
int i;
term_list *list = NULL;
for(i=0; i<ID; i++)
if(type == IDT_ALL || idx_table[i]->type == type)
list = mk_list(TBmake("%s", idx_table[i]->name), list);
return list;
}
/*******************************************************\
* functions in module 'system' *
\*******************************************************/
term *system_functions(int nargs, term **args, term *proc_inst)
{
return make_symbol_list(IDT_EXPR_FUN);
}
term *system_modules(int nargs, term **args, term *proc_inst)
{
return make_symbol_list(IDT_MODULE);
}
static expr_sign *retrieve_signature(term *func)
{
char *name;
name_bucket *bucket;
assert(is_str(func));
name = str_val(func);
bucket = lookup_bucket(name, IDM_ALL);
if(bucket)
return bucket->u.sig;
return NULL;
}
term *system_function_module(int nargs, term **args, term *proc_inst)
{
expr_sign *sig;
char *name;
name_bucket *bucket;
assert(nargs == 1);
assert(is_str(*args));
sig = retrieve_signature(*args);
name = str_val(*args);
bucket = lookup_bucket(name, IDM_ALL);
if(!bucket)
return mk_str("*unknown*");
if(bucket->module > IDM_INTERNAL)
return mk_str(get_txt(bucket->module));
else
return mk_str("*intern*");
}
term *system_function_result(int nargs, term **args, term *proc_inst)
{
expr_sign *sig;
assert(nargs == 1);
sig = retrieve_signature(*args);
if(!sig)
return mk_str("*unknown*");
return sig->restype;
}
term *system_function_args(int nargs, term **args, term *proc_inst)
{
expr_sign *sig;
term_list *result = NULL;
int i;
assert(nargs == 1);
sig = retrieve_signature(*args);
if(!sig)
return mk_list1(mk_str("*unknown*"));
if(!sig->chkargs)
return mk_list1(mk_str("..."));
for(i=sig->nargs-1; i>=0; i--)
result = mk_list(sig->argtype[i], result);
return result;
}
term *system_function_description(int nargs, term **args, term *proc_inst)
{
expr_sign *sig;
assert(nargs == 1);
sig = retrieve_signature(args[0]);
if(!sig)
return mk_str("*unknown*");
return mk_str(sig->descr);
}
void system_init_module()
{
sym_idx mod = register_module("system");
register_func0("functions", mod, Term, system_functions,
"list of known functions");
register_func0("modules", mod, Term, system_modules,
"list of known modules");
register_func1("function-module", mod, Str, TBtrue, Str,
system_function_module, "retrieve the module a function " \
"is defined in.");
register_func1("function-result", mod, Term, TBtrue, Str,
system_function_result, "retrieve the result sort of a " \
"function.");
register_func1("function-arguments", mod, List, TBtrue, Str,
system_function_args, "retrieve the list of argument types " \
"of a function. A list with the single string element " \
"\"...\" means it is a function with a variable number of "\
"arguments.");
register_func1("function-description", mod, Str, TBtrue, Str,
system_function_description, "Retreive a short description " \
"of the function (like the one you are reading now).");
}
|