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
|
/*
* Copyright (c) 2003-2010 Alexandre Ratchov <alex@caoua.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* this module implements the execution environment of scripts:
* variables lists, procedure lists and various primitives
* to handle them.
*
* a variable is a simple (name, value) pair. The name is stored
* in a name structure (see name.h) and the value is stored in
* a data structure (see data.h).
*
* a procedure is a (name, args, code) triplet. The name is the
* (unique) name that identifies the procedure, 'args' is the
* list of argument names and 'code' is the tree containing the
* instructions of the procedure
*/
#include "utils.h"
#include "exec.h"
#include "data.h"
#include "node.h"
#include "cons.h" /* for cons_errxxx */
/* ----------------------------------------------- variable lists --- */
/*
* create a new variable with the given name and value
* and insert it on the given variable list
*/
struct var *
var_new(struct name **list, char *name, struct data *data)
{
struct var *o;
o = (struct var *)xmalloc(sizeof(struct var), "var");
o->data = data;
name_init(&o->name, name);
name_insert(list, (struct name *)o);
return o;
}
/*
* delete the given variable from the given list
*/
void
var_delete(struct name **list, struct var *o)
{
name_remove(list, (struct name *)o);
if (o->data != NULL) {
data_delete(o->data);
}
name_done(&o->name);
xfree(o);
}
/*
* print "name = value" on stderr
*/
void
var_log(struct var *o)
{
str_log(o->name.str);
log_puts(" = ");
if (o->data != NULL)
data_log(o->data);
else
log_puts("<null>");
}
/*
* delete all variables and clear the given list
*/
void
var_empty(struct name **list)
{
while (*list != NULL) {
var_delete(list, (struct var *)*list);
}
}
/*
* allocate a new procedure with the given name
*/
struct proc *
proc_new(char *name)
{
struct proc *o;
o = (struct proc *)xmalloc(sizeof(struct proc), "proc");
name_init(&o->name, name);
o->args = NULL;
o->code = NULL;
return o;
}
/*
* delete the given procedure: free name, arguments and code
*/
void
proc_delete(struct proc *o)
{
node_delete(o->code);
name_empty(&o->args);
name_done(&o->name);
xfree(o);
}
/*
* free all procedures and clear the given list
*/
void
proc_empty(struct name **first)
{
struct name *i;
while (*first) {
i = *first;
name_remove(first, i);
proc_delete((struct proc *)i);
}
*first = NULL;
}
/*
* dump a procedure on stderr in the following format:
* name (arg1, arg2, ...)
* code_tree
*/
void
proc_log(struct proc *o)
{
struct name *i;
str_log(o->name.str);
log_puts("(");
if (o->args) {
i = o->args;
for (;;) {
log_puts(i->str);
i = i->next;
if (!i) {
break;
}
log_puts(", ");
}
}
log_puts(")\n");
node_log(o->code, 0);
}
/*
* create a new empty execution environment
*/
struct exec *
exec_new(void)
{
struct exec *o;
o = (struct exec *)xmalloc(sizeof(struct exec), "exec");
o->procs = NULL;
o->globals = NULL;
o->locals = &o->globals;
o->procname = "top-level";
o->depth = 0;
o->result = RESULT_OK;
return o;
}
/*
* delete the given execution environment. Must not be
* called inside a procedure
*/
void
exec_delete(struct exec *o)
{
if (o->depth != 0) {
log_puts("exec_done: depth != 0\n");
panic();
}
var_empty(&o->globals);
proc_empty(&o->procs);
xfree(o);
}
/*
* find the variable with the given name in the
* execution environment. If there a matching variable
* in the local list we return it, else we search in the
* global list.
*/
struct var *
exec_varlookup(struct exec *o, char *name)
{
struct name *var;
var = name_lookup(o->locals, name);
if (var != NULL) {
return (struct var *)var;
}
if (o->locals != &o->globals) {
var = name_lookup(&o->globals, name);
if (var != NULL) {
return (struct var *)var;
}
}
return NULL;
}
/*
* find the procedure with the given name
*/
struct proc *
exec_proclookup(struct exec *o, char *name)
{
return (struct proc *)name_lookup(&o->procs, name);
}
/*
* add a new built-in procedure in the exec environment.
*/
void
exec_newbuiltin(struct exec *o, char *name,
unsigned func(struct exec *, struct data **), struct name *args) {
struct proc *newp;
newp = proc_new(name);
newp->args = args;
newp->code = node_new(&node_vmt_builtin, data_newuser((void *)func));
name_add(&o->procs, (struct name *)newp);
}
/*
* add a new global variable in the exec environment
*/
void
exec_newvar(struct exec *o, char *name, struct data *val)
{
var_new(&o->globals, name, val);
}
/*
* dump all procs in the following format:
* proc1(arg1, arg2, ...)
* proc2(arg1, arg2, ...)
* ...
*/
void
exec_dumpprocs(struct exec *o)
{
struct proc *p;
struct name *n;
PROC_FOREACH(p, o->procs) {
log_puts(p->name.str);
log_puts("(");
for (n = p->args; n != NULL; n = n->next) {
log_puts(n->str);
if (n->next) {
log_puts(", ");
}
}
log_puts(")\n");
}
}
/*
* dump all global variables in the following format:
* var1 = value
* var2 = value
* ...
*/
void
exec_dumpvars(struct exec *o)
{
struct var *v;
VAR_FOREACH(v, o->globals) {
log_puts(v->name.str);
log_puts(" = ");
data_log(v->data);
log_puts("\n");
}
}
/*
* find a variable with the given name with value of type DATA_REF
*/
unsigned
exec_lookupname(struct exec *o, char *name, char **val)
{
struct var *var;
var = exec_varlookup(o, name);
if (var == NULL || var->data->type != DATA_REF) {
cons_errss(o->procname, name, "must be set to a reference");
return 0;
}
*val = var->data->val.ref;
return 1;
}
/*
* find a variable with the given name with value of type DATA_STRING
*/
unsigned
exec_lookupstring(struct exec *o, char *name, char **val)
{
struct var *var;
var = exec_varlookup(o, name);
if (var == NULL || var->data->type != DATA_STRING) {
cons_errss(o->procname, name, "must be set to a string");
return 0;
}
*val = var->data->val.str;
return 1;
}
/*
* find a variable with the given name with value of type DATA_LONG
*/
unsigned
exec_lookuplong(struct exec *o, char *name, long *val)
{
struct var *var;
var = exec_varlookup(o, name);
if (var == NULL || var->data->type != DATA_LONG) {
cons_errss(o->procname, name, "must be set to a number");
return 0;
}
*val = var->data->val.num;
return 1;
}
/*
* find a variable with the given name with value of type DATA_LIST
*/
unsigned
exec_lookuplist(struct exec *o, char *name, struct data **val)
{
struct var *var;
var = exec_varlookup(o, name);
if (var == NULL || var->data->type != DATA_LIST) {
cons_errss(o->procname, name, "must be set to a list");
return 0;
}
*val = var->data->val.list;
return 1;
}
/*
* find a variable with the given name convert its value to
* a boolean and return it
*/
unsigned
exec_lookupbool(struct exec *o, char *name, long *val)
{
struct var *var;
unsigned res;
var = exec_varlookup(o, name);
if (var == NULL) {
cons_errss(o->procname, name, "must be set to a bool");
return 0;
}
res = data_eval(var->data);
*val = res;
return 1;
}
|