2 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
4 * This file is part of Jam - see jam.c for Copyright information.
8 * compile.c - compile parsed jam statements
12 * compile_append() - append list results of two statements
13 * compile_break() - compile 'break/continue/return' rule
14 * compile_eval() - evaluate if to determine which leg to compile
15 * compile_foreach() - compile the "for x in y" statement
16 * compile_if() - compile 'if' rule
17 * compile_include() - support for 'include' - call include() on file
18 * compile_list() - expand and return a list
19 * compile_local() - declare (and set) local variables
20 * compile_null() - do nothing -- a stub for parsing
21 * compile_on() - run rule under influence of on-target variables
22 * compile_rule() - compile a single user defined rule
23 * compile_rules() - compile a chain of rules
24 * compile_set() - compile the "set variable" statement
25 * compile_setcomp() - support for `rule` - save parse tree
26 * compile_setexec() - support for `actions` - save execution string
27 * compile_settings() - compile the "on =" (set variable on exec) statement
28 * compile_switch() - compile 'switch' rule
32 * debug_compile() - printf with indent to show rule expansion.
33 * evaluate_rule() - execute a rule invocation
35 * 02/03/94 (seiwald) - Changed trace output to read "setting" instead of
36 * the awkward sounding "settings".
37 * 04/12/94 (seiwald) - Combined build_depends() with build_includes().
38 * 04/12/94 (seiwald) - actionlist() now just appends a single action.
39 * 04/13/94 (seiwald) - added shorthand L0 for null list pointer
40 * 05/13/94 (seiwald) - include files are now bound as targets, and thus
41 * can make use of $(SEARCH)
42 * 06/01/94 (seiwald) - new 'actions existing' does existing sources
43 * 08/23/94 (seiwald) - Support for '+=' (append to variable)
44 * 12/20/94 (seiwald) - NOTIME renamed NOTFILE.
45 * 01/22/95 (seiwald) - Exit rule.
46 * 02/02/95 (seiwald) - Always rule; LEAVES rule.
47 * 02/14/95 (seiwald) - NoUpdate rule.
48 * 01/20/00 (seiwald) - Upgraded from K&R to ANSI C
49 * 09/07/00 (seiwald) - stop crashing when a rule redefines itself
50 * 09/11/00 (seiwald) - new evaluate_rule() for headers().
51 * 09/11/00 (seiwald) - rules now return values, accessed via [ rule arg ... ]
52 * 09/12/00 (seiwald) - don't complain about rules invoked without targets
53 * 01/13/01 (seiwald) - fix case where rule is defined within another
54 * 01/10/01 (seiwald) - built-ins split out to builtin.c.
55 * 01/11/01 (seiwald) - optimize compile_rules() for tail recursion
56 * 01/21/01 (seiwald) - replace evaluate_if() with compile_eval()
57 * 01/24/01 (seiwald) - 'while' statement
58 * 03/23/01 (seiwald) - "[ on target rule ]" support
59 * 02/28/02 (seiwald) - merge EXEC_xxx flags in with RULE_xxx
60 * 03/02/02 (seiwald) - rules can be invoked via variable names
61 * 03/12/02 (seiwald) - &&,&,||,|,in now short-circuit again
62 * 03/25/02 (seiwald) - if ( "" a b ) one again returns true
63 * 06/21/02 (seiwald) - support for named parameters
64 * 10/22/02 (seiwald) - list_new() now does its own newstr()/copystr()
65 * 10/22/02 (seiwald) - working return/break/continue statements
66 * 11/04/02 (seiwald) - const-ing for string literals
67 * 11/18/02 (seiwald) - remove bogus search() in 'on' statement.
68 * 12/17/02 (seiwald) - new copysettings() to protect target-specific vars
83 static const char *set_names
[] = { "=", "+=", "?=" };
84 static void debug_compile (int which
, const char *s
);
85 int glob (const char *s
, const char *c
);
89 * compile_append() - append list results of two statements
91 * parse->left more compile_append() by left-recursion
92 * parse->right single rule
94 LIST
*compile_append (PARSE
*parse
, LOL
*args
, int *jmp
) {
95 /* Append right to left. */
97 (*parse
->left
->func
)(parse
->left
, args
, jmp
),
98 (*parse
->right
->func
)(parse
->right
, args
, jmp
));
103 * compile_break() - compile 'break/continue/return' rule
105 * parse->left results
106 * parse->num JMP_BREAK/CONTINUE/RETURN
108 LIST
*compile_break (PARSE
*parse
, LOL
*args
, int *jmp
) {
109 LIST
*lv
= (*parse
->left
->func
)(parse
->left
, args
, jmp
);
116 * compile_eval() - evaluate if to determine which leg to compile
119 * list if expression true - compile 'then' clause
120 * L0 if expression false - compile 'else' clause
122 static int lcmp (LIST
*t
, LIST
*s
) {
125 while (!status
&& (t
|| s
)) {
126 const char *st
= t
?t
->string
:"";
127 const char *ss
= s
?s
->string
:"";
129 status
= strcmp(st
, ss
);
130 t
= t
?list_next(t
):t
;
131 s
= s
?list_next(s
):s
;
138 LIST
*compile_eval (PARSE
*parse
, LOL
*args
, int *jmp
) {
139 LIST
*ll
, *lr
, *s
, *t
;
142 /* Short circuit lr eval for &&, ||, and 'in' */
143 ll
= (*parse
->left
->func
)(parse
->left
, args
, jmp
);
146 switch (parse
->num
) {
147 case EXPR_AND
: case EXPR_IN
:
155 lr
= (*parse
->right
->func
)(parse
->right
, args
, jmp
);
159 switch ( parse
->num
) {
164 if (ll
&& lr
) status
= 1;
167 if (ll
|| lr
) status
= 1;
170 /* "a in b": make sure each of */
171 /* ll is equal to something in lr. */
172 for (t
= ll
; t
; t
= list_next(t
)) {
173 for (s
= lr
; s
; s
= list_next(s
)) if (!strcmp(t
->string
, s
->string
)) break;
176 /* No more ll? Success */
180 if (lcmp(ll
, L0
) != 0) status
= 1;
183 if (lcmp(ll
, lr
) == 0) status
= 1;
186 if (lcmp(ll
, lr
) != 0) status
= 1;
189 if (lcmp(ll
, lr
) < 0) status
= 1;
192 if (lcmp(ll
, lr
) <= 0) status
= 1;
195 if (lcmp(ll
, lr
) > 0) status
= 1;
198 if (lcmp(ll
, lr
) >= 0) status
= 1;
203 debug_compile(0, "if");
205 printf("(%d) ", status
);
210 /* Find something to return. */
211 /* In odd circumstances (like "" = "") */
212 /* we'll have to return a new string. */
214 else if (ll
) t
= ll
, ll
= 0;
215 else if (lr
) t
= lr
, lr
= 0;
216 else t
= list_new(L0
, "1", 0);
218 if (ll
) list_free(ll
);
219 if (lr
) list_free(lr
);
226 * compile_foreach() - compile the "for x in y" statement
228 * Compile_foreach() resets the given variable name to each specified
229 * value, executing the commands enclosed in braces for each iteration.
231 * parse->string index variable
232 * parse->left variable values
233 * parse->right rule to compile
235 LIST
*compile_foreach (PARSE
*p
, LOL
*args
, int *jmp
) {
236 LIST
*nv
= (*p
->left
->func
)(p
->left
, args
, jmp
);
240 /* for each value for var */
241 for (l
= nv
; l
&& *jmp
== JMP_NONE
; l
= list_next(l
)) {
242 /* Reset $(p->string) for each val. */
243 var_set(p
->string
, list_new(L0
, l
->string
, 1), VAR_SET
);
244 /* Keep only last result. */
246 result
= (*p
->right
->func
)(p
->right
, args
, jmp
);
248 if (*jmp
== JMP_CONTINUE
) *jmp
= JMP_NONE
;
250 /* Here by break/continue? */
251 if (*jmp
== JMP_BREAK
|| *jmp
== JMP_CONTINUE
) *jmp
= JMP_NONE
;
253 /* Returns result of last loop */
259 * compile_if() - compile 'if' rule
261 * parse->left condition tree
262 * parse->right then tree
263 * parse->third else tree
265 LIST
*compile_if (PARSE
*p
, LOL
*args
, int *jmp
) {
266 LIST
*l
= (*p
->left
->func
)(p
->left
, args
, jmp
);
267 p
= l
?p
->right
:p
->third
;
269 return (*p
->func
)(p
, args
, jmp
);
274 * compile_include() - support for 'include' - call include() on file
276 * parse->left list of files to include (can only do 1)
278 LIST
*compile_include (PARSE
*parse
, LOL
*args
, int *jmp
) {
279 LIST
*nt
= (*parse
->left
->func
)(parse
->left
, args
, jmp
);
281 debug_compile(0, "include");
286 TARGET
*t
= bindtarget(nt
->string
);
287 /* Bind the include file under the influence of */
288 /* "on-target" variables. Though they are targets, */
289 /* include files are not built with make(). */
290 /* Needn't copysettings(), as search sets no vars. */
291 pushsettings(t
->settings
);
292 t
->boundname
= search(t
->name
, &t
->time
);
293 popsettings(t
->settings
);
294 /* Don't parse missing file if NOCARE set */
295 if (t
->time
|| !(t
->flags
& T_FLAG_NOCARE
)) parse_file(t
->boundname
);
303 * compile_list() - expand and return a list
305 * parse->string - character string to expand
307 LIST
*compile_list (PARSE
*parse
, LOL
*args
, int *jmp
) {
308 /* voodoo 1 means: s is a copyable string */
309 const char *s
= parse
->string
;
310 return var_expand(L0
, s
, s
+strlen(s
), args
, 1);
315 * compile_local() - declare (and set) local variables
317 * parse->left list of variables
318 * parse->right list of values
319 * parse->third rules to execute
321 LIST
*compile_local (PARSE
*parse
, LOL
*args
, int *jmp
) {
324 LIST
*nt
= (*parse
->left
->func
)(parse
->left
, args
, jmp
);
325 LIST
*ns
= (*parse
->right
->func
)(parse
->right
, args
, jmp
);
329 debug_compile(0, "local");
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
));
340 /* Note that callees of the current context get this "local" */
341 /* variable, making it not so much local as layered. */
343 result
= (*parse
->third
->func
)(parse
->third
, args
, jmp
);
352 * compile_null() - do nothing -- a stub for parsing
354 LIST
*compile_null (PARSE
*parse
, LOL
*args
, int *jmp
) {
360 * compile_on() - run rule under influence of on-target variables
362 * parse->left target list; only first used
363 * parse->right rule to run
365 LIST
*compile_on (PARSE
*parse
, LOL
*args
, int *jmp
) {
366 LIST
*nt
= (*parse
->left
->func
)(parse
->left
, args
, jmp
);
370 debug_compile(0, "on");
376 * Copy settings, so that 'on target var on target = val'
377 * doesn't set var globally.
380 TARGET
*t
= bindtarget(nt
->string
);
381 SETTINGS
*s
= copysettings(t
->settings
);
384 result
= (*parse
->right
->func
)(parse
->right
, args
, jmp
);
395 * compile_rule() - compile a single user defined rule
397 * parse->left list of rules to run
398 * parse->right parameters (list of lists) to rule, recursing left
400 * Wrapped around evaluate_rule() so that headers() can share it.
402 LIST
*compile_rule (PARSE
*parse
, LOL
*args
, int *jmp
) {
408 /* list of rules to run -- normally 1! */
409 ll
= (*parse
->left
->func
)(parse
->left
, args
, jmp
);
410 /* Build up the list of arg lists */
412 for (p
= parse
->right
; p
; p
= p
->left
) lol_add(nargs
, (*p
->right
->func
)(p
->right
, args
, jmp
));
413 /* Run rules, appending results from each */
414 for (l
= ll
; l
; l
= list_next(l
)) result
= evaluate_rule(l
->string
, nargs
, result
);
423 * evaluate_rule() - execute a rule invocation
425 LIST
*evaluate_rule (const char *rulename
, LOL
*args
, LIST
*result
) {
426 RULE
*rule
= bindrule(rulename
);
429 debug_compile(1, rulename
);
434 /* Check traditional targets $(<) and sources $(>) */
435 if (!rule
->actions
&& !rule
->procedure
) printf("warning: unknown rule %s\n", rule
->name
);
436 /* If this rule will be executed for updating the targets */
437 /* then construct the action for make(). */
442 /* The action is associated with this instance of this rule */
443 action
= (ACTION
*)malloc(sizeof(ACTION
));
444 memset((char *)action
, '\0', sizeof(*action
));
446 action
->targets
= targetlist((TARGETS
*)0, lol_get(args
, 0));
447 action
->sources
= targetlist((TARGETS
*)0, lol_get(args
, 1));
449 /* Append this action to the actions of each target */
450 for (t
= action
->targets
; t
; t
= t
->next
) t
->target
->actions
= actionlist(t
->target
->actions
, action
);
452 /* Now recursively compile any parse tree associated with this rule */
453 if (rule
->procedure
) {
454 PARSE
*parse
= rule
->procedure
;
460 /* build parameters as local vars */
461 for (l
= rule
->params
, i
= 0; l
; l
= l
->next
, i
++) s
= addsettings(s
, 0, l
->string
, list_copy(L0
, lol_get(args
, i
)));
463 /* Bring in local params. */
464 /* refer/free to ensure rule not freed during use. */
467 result
= list_append(result
, (*parse
->func
)(parse
, args
, &jmp
));
473 if (DEBUG_COMPILE
) debug_compile(-1, 0);
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. */
490 while (*jmp
== JMP_NONE
&& parse
->func
== compile_rules
) {
492 result
= (*parse
->left
->func
)(parse
->left
, args
, jmp
);
493 parse
= parse
->right
;
495 if (*jmp
== JMP_NONE
) {
497 result
= (*parse
->func
)(parse
, args
, jmp
);
505 * compile_set() - compile the "set variable" statement
507 * parse->left variable names
508 * parse->right variable values
509 * parse->num VAR_SET/APPEND/DEFAULT
511 LIST
*compile_set (PARSE
*parse
, LOL
*args
, int *jmp
) {
512 LIST
*nt
= (*parse
->left
->func
)(parse
->left
, args
, jmp
);
513 LIST
*ns
= (*parse
->right
->func
)(parse
->right
, args
, jmp
);
517 debug_compile(0, "set");
519 printf(" %s ", set_names
[parse
->num
]);
523 /* Call var_set to set variable */
524 /* var_set keeps ns, so need to copy it */
525 for (l
= nt
; l
; l
= list_next(l
)) var_set(l
->string
, list_copy(L0
, ns
), parse
->num
);
533 * compile_setcomp() - support for `rule` - save parse tree
535 * parse->string rule name
536 * parse->left list of argument names
537 * parse->right rules for rule
539 LIST
*compile_setcomp (PARSE
*parse
, LOL
*args
, int *jmp
) {
540 RULE
*rule
= bindrule(parse
->string
);
544 /* Build param list */
545 for (p
= parse
->left
; p
; p
= p
->left
) params
= list_new(params
, p
->string
, 1);
548 debug_compile(0, "rule");
549 printf("%s ", parse
->string
);
554 /* Free old one, if present */
555 if (rule
->procedure
) parse_free(rule
->procedure
);
556 if (rule
->params
) list_free(rule
->params
);
558 rule
->procedure
= parse
->right
;
559 rule
->params
= params
;
561 /* we now own this parse tree */
562 /* don't let parse_free() release it */
563 parse_refer(parse
->right
);
570 * compile_setexec() - support for `actions` - save execution string
572 * parse->string rule name
573 * parse->string1 OS command string
575 * parse->left `bind` variables
577 * Note that the parse flags (as defined in compile.h) are transfered
578 * directly to the rule flags (as defined in rules.h).
580 LIST
*compile_setexec (PARSE
*parse
, LOL
*args
, int *jmp
) {
581 RULE
*rule
= bindrule(parse
->string
);
582 LIST
*bindlist
= (*parse
->left
->func
)(parse
->left
, args
, jmp
);
584 /* Free old one, if present */
586 freestr(rule
->actions
);
587 list_free(rule
->bindlist
);
590 rule
->actions
= copystr(parse
->string1
);
591 rule
->bindlist
= bindlist
;
592 rule
->flags
= parse
->num
;
599 * compile_settings() - compile the "on =" (set variable on exec) statement
601 * parse->left variable names
602 * parse->right target name
603 * parse->third variable value
604 * parse->num VAR_SET/APPEND/DEFAULT
606 LIST
*compile_settings (PARSE
*parse
, LOL
*args
, int *jmp
) {
607 LIST
*nt
= (*parse
->left
->func
)(parse
->left
, args
, jmp
);
608 LIST
*ns
= (*parse
->third
->func
)(parse
->third
, args
, jmp
);
609 LIST
*targets
= (*parse
->right
->func
)(parse
->right
, args
, jmp
);
613 debug_compile(0, "set");
617 printf(" %s ", set_names
[parse
->num
]);
622 /* Call addsettings to save variable setting */
623 /* addsettings keeps ns, so need to copy it */
624 /* Pass append flag to addsettings() */
625 for (ts
= targets
; ts
; ts
= list_next(ts
)) {
626 TARGET
*t
= bindtarget(ts
->string
);
629 for (l
= nt
; l
; l
= list_next(l
))
630 t
->settings
= addsettings(t
->settings
, parse
->num
, l
->string
, list_copy((LIST
*)0, ns
));
641 * compile_switch() - compile 'switch' rule
643 * parse->left switch value (only 1st used)
646 * cases->left 1st case
647 * cases->right next cases
649 * case->string argument to match
650 * case->left parse tree to execute
652 LIST
*compile_switch (PARSE
*parse
, LOL
*args
, int *jmp
) {
653 LIST
*nt
= (*parse
->left
->func
)(parse
->left
, args
, jmp
);
657 debug_compile(0, "switch");
661 /* Step through cases */
662 for (parse
= parse
->right
; parse
; parse
= parse
->right
) {
663 if (!glob( parse
->left
->string
, nt
?nt
->string
:"")) {
664 /* Get & exec parse tree for this case */
665 parse
= parse
->left
->left
;
666 result
= (*parse
->func
)(parse
, args
, jmp
);
677 * compile_while() - compile 'while' rule
679 * parse->left condition tree
680 * parse->right execution tree
682 LIST
*compile_while (PARSE
*p
, LOL
*args
, int *jmp
) {
686 /* Returns the value from the last execution of the block */
687 while ((*jmp
== JMP_NONE
) && (l
= (*p
->left
->func
)(p
->left
, args
, jmp
))) {
688 /* Always toss while's expression */
690 /* Keep only last result. */
692 result
= (*p
->right
->func
)(p
->right
, args
, jmp
);
694 if (*jmp
== JMP_CONTINUE
) *jmp
= JMP_NONE
;
696 /* Here by break/continue? */
697 if (*jmp
== JMP_BREAK
|| *jmp
== JMP_CONTINUE
) *jmp
= JMP_NONE
;
698 /* Returns result of last loop */
705 * debug_compile() - printf with indent to show rule expansion.
708 static void debug_compile (int which
, const char *s
) {
709 static int level
= 0;
710 static char indent
[36] = ">>>>|>>>>|>>>>|>>>>|>>>>|>>>>|>>>>|";
711 int i
= ((1+level
)*2)%35;
713 if (which
>= 0) printf("%*.*s ", i
, i
, indent
);
714 if (s
) printf("%s ", s
);