removed some OS/2 remnants
[k8jam.git] / src / compile.c
blob79d1e54018f2ee5a05dd932145ea09ec86772e17
1 /*
2 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
4 * This file is part of Jam - see jam.c for Copyright information.
5 */
6 /*
7 * compile.c - compile parsed jam statements
9 * External routines:
11 * compile_append() - append list results of two statements
12 * compile_break() - compile 'break/continue/return' rule
13 * compile_eval() - evaluate if to determine which leg to compile
14 * compile_foreach() - compile the "for x in y" statement
15 * compile_if() - compile 'if' rule
16 * compile_include() - support for 'include' - call include() on file
17 * compile_list() - expand and return a list
18 * compile_local() - declare (and set) local variables
19 * compile_null() - do nothing -- a stub for parsing
20 * compile_on() - run rule under influence of on-target variables
21 * compile_rule() - compile a single user defined rule
22 * compile_rules() - compile a chain of rules
23 * compile_set() - compile the "set variable" statement
24 * compile_setcomp() - support for `rule` - save parse tree
25 * compile_setexec() - support for `actions` - save execution string
26 * compile_settings() - compile the "on =" (set variable on exec) statement
27 * compile_switch() - compile 'switch' rule
29 * Internal routines:
31 * debug_compile() - printf with indent to show rule expansion.
32 * evaluate_rule() - execute a rule invocation
34 * 02/03/94 (seiwald) - Changed trace output to read "setting" instead of
35 * the awkward sounding "settings".
36 * 04/12/94 (seiwald) - Combined build_depends() with build_includes().
37 * 04/12/94 (seiwald) - actionlist() now just appends a single action.
38 * 04/13/94 (seiwald) - added shorthand L0 for null list pointer
39 * 05/13/94 (seiwald) - include files are now bound as targets, and thus
40 * can make use of $(SEARCH)
41 * 06/01/94 (seiwald) - new 'actions existing' does existing sources
42 * 08/23/94 (seiwald) - Support for '+=' (append to variable)
43 * 12/20/94 (seiwald) - NOTIME renamed NOTFILE.
44 * 01/22/95 (seiwald) - Exit rule.
45 * 02/02/95 (seiwald) - Always rule; LEAVES rule.
46 * 02/14/95 (seiwald) - NoUpdate rule.
47 * 01/20/00 (seiwald) - Upgraded from K&R to ANSI C
48 * 09/07/00 (seiwald) - stop crashing when a rule redefines itself
49 * 09/11/00 (seiwald) - new evaluate_rule() for headers().
50 * 09/11/00 (seiwald) - rules now return values, accessed via [ rule arg ... ]
51 * 09/12/00 (seiwald) - don't complain about rules invoked without targets
52 * 01/13/01 (seiwald) - fix case where rule is defined within another
53 * 01/10/01 (seiwald) - built-ins split out to builtin.c.
54 * 01/11/01 (seiwald) - optimize compile_rules() for tail recursion
55 * 01/21/01 (seiwald) - replace evaluate_if() with compile_eval()
56 * 01/24/01 (seiwald) - 'while' statement
57 * 03/23/01 (seiwald) - "[ on target rule ]" support
58 * 02/28/02 (seiwald) - merge EXEC_xxx flags in with RULE_xxx
59 * 03/02/02 (seiwald) - rules can be invoked via variable names
60 * 03/12/02 (seiwald) - &&,&,||,|,in now short-circuit again
61 * 03/25/02 (seiwald) - if ( "" a b ) one again returns true
62 * 06/21/02 (seiwald) - support for named parameters
63 * 10/22/02 (seiwald) - list_new() now does its own newstr()/copystr()
64 * 10/22/02 (seiwald) - working return/break/continue statements
65 * 11/04/02 (seiwald) - const-ing for string literals
66 * 11/18/02 (seiwald) - remove bogus search() in 'on' statement.
67 * 12/17/02 (seiwald) - new copysettings() to protect target-specific vars
69 #include "jam.h"
71 #include "lists.h"
72 #include "parse.h"
73 #include "compile.h"
74 #include "variable.h"
75 #include "expand.h"
76 #include "rules.h"
77 #include "newstr.h"
78 #include "search.h"
79 #include "matchglob.h"
82 static const char *set_names[] = { "=", "+=", "-=", "?=" };
83 static void debug_compile (int which, const char *s);
87 * compile_append() - append list results of two statements
89 * parse->left more compile_append() by left-recursion
90 * parse->right single rule
92 LIST *compile_append (PARSE *parse, LOL *args, int *jmp) {
93 /* Append right to left. */
94 return list_append(
95 (*parse->left->func)(parse->left, args, jmp),
96 (*parse->right->func)(parse->right, args, jmp)
102 * compile_break() - compile 'break/continue/return' rule
104 * parse->left results
105 * parse->num JMP_BREAK/CONTINUE/RETURN
107 LIST *compile_break (PARSE *parse, LOL *args, int *jmp) {
108 LIST *lv = (*parse->left->func)(parse->left, args, jmp);
109 *jmp = parse->num;
110 return lv;
115 * compile_eval() - evaluate if to determine which leg to compile
117 * Returns:
118 * list if expression true - compile 'then' clause
119 * L0 if expression false - compile 'else' clause
121 static JAMFA_PURE int lcmp (LIST *t, LIST *s) {
122 int status = 0;
123 while (!status && (t || s)) {
124 const char *st = (t ? t->string : "");
125 const char *ss = (s ? s->string : "");
126 status = strcmp(st, ss);
127 t = (t ? list_next(t) : t);
128 s = (s ? list_next(s) : s);
130 return status;
134 LIST *compile_eval (PARSE *parse, LOL *args, int *jmp) {
135 LIST *ll, *lr, *s, *t;
136 int status = 0;
137 /* short circuit lr eval for &&, ||, 'in' and 'any-in' */
138 ll = (*parse->left->func)(parse->left, args, jmp);
139 lr = 0;
140 switch (parse->num) {
141 case EXPR_AND:
142 case EXPR_IN:
143 case EXPR_ANYIN:
144 if (ll) goto eval;
145 break;
146 case EXPR_OR:
147 if (!ll) goto eval;
148 break;
149 default:
150 eval: lr = (*parse->right->func)(parse->right, args, jmp);
151 break;
153 /* now eval */
154 switch (parse->num) {
155 case EXPR_NOT: if (!ll) status = 1; break;
156 case EXPR_AND: if (ll && lr) status = 1; break;
157 case EXPR_OR: if (ll || lr) status = 1; break;
158 case EXPR_IN:
159 /* "a in b": make sure each of ll is equal to something in lr */
160 for (t = ll; t; t = list_next(t)) {
161 for (s = lr; s; s = list_next(s)) if (strcmp(t->string, s->string) == 0) break;
162 if (!s) break;
164 /* No more ll? success */
165 if (!t) status = 1;
166 break;
167 case EXPR_ANYIN:
168 /* "a any-in b": does b contains at least one item from a? */
169 for (t = ll; t && !status; t = list_next(t)) {
170 for (s = lr; s; s = list_next(s)) if (strcmp(t->string, s->string) == 0) { status = 1; break; } /* success */
172 break;
173 case EXPR_EXISTS: if (lcmp(ll, L0) != 0) status = 1; break;
174 case EXPR_EQUALS: if (lcmp(ll, lr) == 0) status = 1; break;
175 case EXPR_NOTEQ: if (lcmp(ll, lr) != 0) status = 1; break;
176 case EXPR_LESS: if (lcmp(ll, lr) < 0) status = 1; break;
177 case EXPR_LESSEQ: if (lcmp(ll, lr) <= 0) status = 1; break;
178 case EXPR_MORE: if (lcmp(ll, lr) > 0) status = 1; break;
179 case EXPR_MOREEQ: if (lcmp(ll, lr) >= 0) status = 1; break;
181 if (DEBUG_IF) {
182 debug_compile(0, "if");
183 list_print(ll);
184 printf("(%d) ", status);
185 list_print(lr);
186 printf("\n");
188 /* find something to return */
189 /* in odd circumstances (like "" = "") we'll have to return a new string */
190 if (!status) t = NULL;
191 else if (ll) { t = ll; ll = NULL; }
192 else if (lr) { t = lr; lr = NULL; }
193 else t = list_new(L0, "1", 0);
194 if (ll) list_free(ll);
195 if (lr) list_free(lr);
196 return t;
201 * compile_foreach() - compile the "for x in y" statement
203 * Compile_foreach() resets the given variable name to each specified
204 * value, executing the commands enclosed in braces for each iteration.
206 * parse->string index variable
207 * parse->left variable values
208 * parse->right rule to compile
210 LIST *compile_foreach (PARSE *p, LOL *args, int *jmp) {
211 LIST *nv = (*p->left->func)(p->left, args, jmp);
212 LIST *result = NULL;
213 /* for each value for var */
214 for (LIST *l = nv; l && *jmp == JMP_NONE; l = list_next(l)) {
215 /* reset $(p->string) for each val */
216 var_set(p->string, list_new(L0, l->string, 1), VAR_SET);
217 /* keep only last result */
218 list_free(result);
219 result = (*p->right->func)(p->right, args, jmp);
220 /* continue loop? */
221 if (*jmp == JMP_CONTINUE) *jmp = JMP_NONE;
223 /* here by break/continue? */
224 if (*jmp == JMP_BREAK || *jmp == JMP_CONTINUE) *jmp = JMP_NONE;
225 list_free(nv);
226 /* returns result of last loop */
227 return result;
232 * compile_if() - compile 'if' rule
234 * parse->left condition tree
235 * parse->right then tree
236 * parse->third else tree
238 LIST *compile_if (PARSE *p, LOL *args, int *jmp) {
239 LIST *l = (*p->left->func)(p->left, args, jmp);
240 p = (l ? p->right : p->third);
241 list_free(l);
242 return (*p->func)(p, args, jmp);
247 * compile_include() - support for 'include' - call include() on file
249 * parse->left list of files to include (can only do 1)
251 static LIST *compile_include_internal (PARSE *parse, LOL *args, int *jmp, int assoft) {
252 LIST *nt = (*parse->left->func)(parse->left, args, jmp);
253 if (DEBUG_COMPILE) {
254 debug_compile(0, (assoft?"softinclude":"include"));
255 list_print(nt);
256 printf("\n");
258 if (nt) {
259 TARGET *t = bindtarget(nt->string);
260 /* bind the include file under the influence of "on-target" variables */
261 /* though they are targets, include files are not built with make() */
262 /* needn't copysettings(), as search sets no vars */
263 pushsettings(t->settings);
264 t->boundname = search(t->name, &t->time);
265 popsettings(t->settings);
266 /* don't parse missing file if NOCARE set */
267 if (t->time || !(t->flags&T_FLAG_NOCARE)) {
268 int doit = 1;
269 if (access(t->boundname, R_OK) != 0) {
270 if (!assoft) {
271 printf("Failed to include file '%s'\n", t->boundname);
272 exit(EXITBAD);
274 doit = 0;
276 if (doit) parse_file(t->boundname);
279 list_free(nt);
280 return L0;
285 * compile_include() - support for 'include' - call include() on file
287 * parse->left list of files to include (can only do 1)
289 LIST *compile_include (PARSE *parse, LOL *args, int *jmp) {
290 return compile_include_internal(parse, args, jmp, 0);
295 * compile_softinclude() - support for 'softinclude' - call include() on file if file exists
297 * parse->left list of files to include (can only do 1)
299 LIST *compile_softinclude (PARSE *parse, LOL *args, int *jmp) {
300 return compile_include_internal(parse, args, jmp, 1);
305 * compile_list() - expand and return a list
307 * parse->string - character string to expand
309 LIST *compile_list (PARSE *parse, LOL *args, int *jmp) {
310 /* voodoo 1 means: s is a copyable string */
311 const char *s = parse->string;
312 return var_expand(L0, s, s+strlen(s), args, 1);
317 * compile_local() - declare (and set) local variables
319 * parse->left list of variables
320 * parse->right list of values
321 * parse->third rules to execute
323 LIST *compile_local (PARSE *parse, LOL *args, int *jmp) {
324 LIST *l;
325 SETTINGS *s = NULL;
326 LIST *nt = (*parse->left->func)(parse->left, args, jmp);
327 LIST *ns = (*parse->right->func)(parse->right, args, jmp);
328 LIST *result;
329 if (DEBUG_COMPILE) {
330 debug_compile(0, "local");
331 list_print(nt);
332 printf(" = ");
333 list_print(ns);
334 printf("\n");
336 /* initial value is ns */
337 for (l = nt; l; l = list_next(l)) s = addsettings(s, 0, l->string, list_copy((LIST *)0, ns));
338 list_free(ns);
339 list_free(nt);
340 /* note that callees of the current context get this "local" */
341 /* variable, making it not so much local as layered */
342 pushsettings(s);
343 result = (*parse->third->func)(parse->third, args, jmp);
344 popsettings(s);
345 freesettings(s);
346 return result;
351 * compile_null() - do nothing -- a stub for parsing
353 LIST *compile_null (PARSE *parse, LOL *args, int *jmp) {
354 return L0;
359 * compile_on() - run rule under influence of on-target variables
361 * parse->left target list; only first used
362 * parse->right rule to run
364 LIST *compile_on (PARSE *parse, LOL *args, int *jmp) {
365 LIST *nt = (*parse->left->func)(parse->left, args, jmp);
366 LIST *result = NULL;
367 if (DEBUG_COMPILE) {
368 debug_compile(0, "on");
369 list_print(nt);
370 printf("\n");
372 /* copy settings, so that 'on target var on target = val' doesn't set var globally */
373 if (nt) {
374 TARGET *t = bindtarget(nt->string);
375 SETTINGS *s = copysettings(t->settings);
376 pushsettings(s);
377 result = (*parse->right->func)(parse->right, args, jmp);
378 popsettings(s);
379 freesettings(s);
381 list_free(nt);
382 return result;
387 * compile_rule() - compile a single user defined rule
389 * parse->left list of rules to run
390 * parse->right parameters (list of lists) to rule, recursing left
392 * Wrapped around evaluate_rule() so that headers() can share it.
394 LIST *compile_rule (PARSE *parse, LOL *args, int *jmp) {
395 LOL nargs[1];
396 LIST *result = NULL;
397 LIST *ll, *l;
398 PARSE *p;
399 /* list of rules to run -- normally 1! */
400 ll = (*parse->left->func)(parse->left, args, jmp);
401 /* build up the list of arg lists */
402 lol_init(nargs);
403 for (p = parse->right; p; p = p->left) lol_add(nargs, (*p->right->func)(p->right, args, jmp));
404 /* run rules, appending results from each */
405 for (l = ll; l; l = list_next(l)) result = evaluate_rule(l->string, nargs, result);
406 list_free(ll);
407 lol_free(nargs);
408 return result;
413 * evaluate_rule() - execute a rule invocation
415 LIST *evaluate_rule (const char *rulename, LOL *args, LIST *result) {
416 //#infdef OPT_EXPAND_RULE_NAMES_EXT
417 /*RULE *rule = bindrule(rulename);*/
418 //#else
419 RULE *rule;
420 char *expanded, *c;
421 dstring_t buf;
422 dstr_init(&buf);
423 if (var_string(rulename, &buf, args, ' ') < 0) {
424 dstr_done(&buf);
425 printf("Failed to expand rule %s -- expansion too long\n", rulename);
426 exit(EXITBAD);
428 expanded = dstr_cstr(&buf);
429 while (expanded[0] == ' ') ++expanded;
430 while ((c = strrchr(expanded, ' '))) *c = '\0';
431 if (DEBUG_COMPILE) {
432 debug_compile(1, rulename);
433 if (strcmp(rulename, expanded)) printf("-> %s ", expanded);
434 lol_print(args);
435 printf("\n");
437 rule = bindrule(expanded);
438 dstr_done(&buf);
439 //#endif
440 /* check traditional targets $(<) and sources $(>) */
441 if (!rule->actions && !rule->procedure) printf("warning: unknown rule %s\n", rule->name);
442 /* if this rule will be executed for updating the targets then construct the action for make() */
443 if (rule->actions) {
444 TARGETS *t;
445 ACTION *action;
446 /* the action is associated with this instance of this rule */
447 action = (ACTION *)malloc(sizeof(ACTION));
448 memset((char *)action, '\0', sizeof(*action));
449 action->rule = rule;
450 action->targets = targetlist((TARGETS *)0, lol_get(args, 0));
451 action->sources = targetlist((TARGETS *)0, lol_get(args, 1));
452 /* append this action to the actions of each target */
453 for (t = action->targets; t; t = t->next) t->target->actions = actionlist(t->target->actions, action);
455 /* now recursively compile any parse tree associated with this rule */
456 if (rule->procedure) {
457 PARSE *parse = rule->procedure;
458 SETTINGS *s = 0;
459 int jmp = JMP_NONE;
460 LIST *l;
461 int i;
462 /* build parameters as local vars */
463 for (l = rule->params, i = 0; l; l = l->next, i++) s = addsettings(s, 0, l->string, list_copy(L0, lol_get(args, i)));
464 /* run rule */
465 /* bring in local params */
466 /* refer/free to ensure rule not freed during use */
467 parse_refer(parse);
468 pushsettings(s);
469 result = list_append(result, (*parse->func)(parse, args, &jmp));
470 popsettings(s);
471 freesettings(s);
472 parse_free(parse);
474 if (DEBUG_COMPILE) debug_compile(-1, 0);
475 return result;
480 * compile_rules() - compile a chain of rules
482 * parse->left single rule
483 * parse->right more compile_rules() by right-recursion
485 LIST *compile_rules (PARSE *parse, LOL *args, int *jmp) {
486 /* ignore result from first statement; return the 2nd */
487 /* optimize recursion on the right by looping */
488 LIST *result = NULL;
489 while (*jmp == JMP_NONE && parse->func == compile_rules) {
490 list_free(result);
491 result = (*parse->left->func)(parse->left, args, jmp);
492 parse = parse->right;
494 if (*jmp == JMP_NONE) {
495 list_free(result);
496 result = (*parse->func)(parse, args, jmp);
498 return result;
503 * compile_set() - compile the "set variable" statement
505 * parse->left variable names
506 * parse->right variable values
507 * parse->num VAR_SET/APPEND/DEFAULT
509 LIST *compile_set (PARSE *parse, LOL *args, int *jmp) {
510 LIST *nt = (*parse->left->func)(parse->left, args, jmp);
511 LIST *ns = (*parse->right->func)(parse->right, args, jmp);
512 LIST *l;
513 if (DEBUG_COMPILE) {
514 debug_compile(0, "set");
515 list_print(nt);
516 printf(" %s ", set_names[parse->num]);
517 list_print(ns);
518 printf("\n");
520 /* call var_set to set variable */
521 /* var_set keeps ns, so need to copy it */
522 for (l = nt; l; l = list_next(l)) var_set(l->string, list_copy(L0, ns), parse->num);
523 list_free(nt);
524 return ns;
529 * compile_setcomp() - support for `rule` - save parse tree
531 * parse->string rule name
532 * parse->left list of argument names
533 * parse->right rules for rule
535 LIST *compile_setcomp (PARSE *parse, LOL *args, int *jmp) {
536 RULE *rule = bindrule(parse->string);
537 LIST *params = 0;
538 PARSE *p;
539 /* build param list */
540 for (p = parse->left; p; p = p->left) params = list_new(params, p->string, 1);
541 if (DEBUG_COMPILE) {
542 debug_compile(0, "rule");
543 printf("%s ", parse->string);
544 list_print(params);
545 printf("\n");
547 /* free old one, if present */
548 if (rule->procedure) parse_free(rule->procedure);
549 if (rule->params) list_free(rule->params);
550 rule->procedure = parse->right;
551 rule->params = params;
552 /* we now own this parse tree */
553 /* don't let parse_free() release it */
554 parse_refer(parse->right);
555 return L0;
560 * compile_setexec() - support for `actions` - save execution string
562 * parse->string rule name
563 * parse->string1 OS command string
564 * parse->num flags
565 * parse->left `bind` variables
567 * Note that the parse flags (as defined in compile.h) are transfered
568 * directly to the rule flags (as defined in rules.h).
570 LIST *compile_setexec (PARSE *parse, LOL *args, int *jmp) {
571 RULE *rule = bindrule(parse->string);
572 LIST *bindlist = (*parse->left->func)(parse->left, args, jmp);
573 /* free old one, if present */
574 if (rule->actions) {
575 freestr(rule->actions);
576 list_free(rule->bindlist);
578 rule->actions = copystr(parse->string1);
579 rule->bindlist = bindlist;
580 rule->flags = parse->num;
581 return L0;
586 * compile_settings() - compile the "on =" (set variable on exec) statement
588 * parse->left variable names
589 * parse->right target name
590 * parse->third variable value
591 * parse->num VAR_SET/APPEND/DEFAULT
593 LIST *compile_settings (PARSE *parse, LOL *args, int *jmp) {
594 LIST *nt = (*parse->left->func)(parse->left, args, jmp);
595 LIST *ns = (*parse->third->func)(parse->third, args, jmp);
596 LIST *targets = (*parse->right->func)(parse->right, args, jmp);
597 LIST *ts;
598 if (DEBUG_COMPILE) {
599 debug_compile(0, "set");
600 list_print(nt);
601 printf("on ");
602 list_print(targets);
603 printf(" %s ", set_names[parse->num]);
604 list_print(ns);
605 printf("\n");
607 /* call addsettings to save variable setting addsettings keeps ns, so need to copy it */
608 /* pass append flag to addsettings() */
609 for (ts = targets; ts; ts = list_next(ts)) {
610 TARGET *t = bindtarget(ts->string);
611 for (LIST *l = nt; l; l = list_next(l)) {
612 t->settings = addsettings(t->settings, parse->num, l->string, list_copy((LIST *)0, ns));
615 list_free(nt);
616 list_free(targets);
617 return ns;
622 * compile_switch() - compile 'switch' rule
624 * parse->left switch value (only 1st used)
625 * parse->right cases
627 * cases->left 1st case
628 * cases->right next cases
630 * case->string argument to match
631 * case->left parse tree to execute
633 LIST *compile_switch (PARSE *parse, LOL *args, int *jmp) {
634 LIST *nt = (*parse->left->func)(parse->left, args, jmp);
635 LIST *result = NULL;
636 if (DEBUG_COMPILE) {
637 debug_compile(0, "switch");
638 list_print(nt);
639 printf("\n");
641 /* step through cases */
642 for (parse = parse->right; parse; parse = parse->right) {
643 if (!matchglob(parse->left->string, nt?nt->string:"")) {
644 /* get and exec parse tree for this case */
645 parse = parse->left->left;
646 result = (*parse->func)(parse, args, jmp);
647 break;
650 list_free(nt);
651 return result;
656 * compile_while() - compile 'while' rule
658 * parse->left condition tree
659 * parse->right execution tree
661 LIST *compile_while (PARSE *p, LOL *args, int *jmp) {
662 LIST *result = NULL;
663 LIST *l;
664 /* returns the value from the last execution of the block */
665 while ((*jmp == JMP_NONE ) && (l = (*p->left->func)(p->left, args, jmp))) {
666 /* always toss while's expression */
667 list_free(l);
668 /* keep only last result */
669 list_free(result);
670 result = (*p->right->func)(p->right, args, jmp);
671 /* continue loop? */
672 if (*jmp == JMP_CONTINUE) *jmp = JMP_NONE;
674 /* here by break/continue? */
675 if (*jmp == JMP_BREAK || *jmp == JMP_CONTINUE) *jmp = JMP_NONE;
676 /* returns result of last loop */
677 return result;
682 * debug_compile() - printf with indent to show rule expansion.
684 static void debug_compile (int which, const char *s) {
685 static int level = 0;
686 static const char indent[36] = ">>>>|>>>>|>>>>|>>>>|>>>>|>>>>|>>>>|";
687 int i = ((1+level)*2)%35;
688 if (which >= 0) printf("%*.*s ", i, i, indent);
689 if (s) printf("%s ", s);
690 level += which;