2 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
4 * This file is part of Jam - see jam.c for Copyright information.
7 * compile.c - compile parsed jam statements
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
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
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. */
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
);
115 * compile_eval() - evaluate if to determine which leg to compile
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
) {
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
);
134 LIST
*compile_eval (PARSE
*parse
, LOL
*args
, int *jmp
) {
135 LIST
*ll
, *lr
, *s
, *t
;
137 /* short circuit lr eval for &&, ||, 'in' and 'any-in' */
138 ll
= (*parse
->left
->func
)(parse
->left
, args
, jmp
);
140 switch (parse
->num
) {
150 eval
: lr
= (*parse
->right
->func
)(parse
->right
, args
, jmp
);
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;
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;
164 /* No more ll? success */
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 */
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;
182 debug_compile(0, "if");
184 printf("(%d) ", status
);
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
);
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
);
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 */
219 result
= (*p
->right
->func
)(p
->right
, args
, jmp
);
221 if (*jmp
== JMP_CONTINUE
) *jmp
= JMP_NONE
;
223 /* here by break/continue? */
224 if (*jmp
== JMP_BREAK
|| *jmp
== JMP_CONTINUE
) *jmp
= JMP_NONE
;
226 /* returns result of last loop */
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
);
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
);
254 debug_compile(0, (assoft
?"softinclude":"include"));
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
)) {
269 if (access(t
->boundname
, R_OK
) != 0) {
271 printf("Failed to include file '%s'\n", t
->boundname
);
276 if (doit
) parse_file(t
->boundname
);
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
) {
326 LIST
*nt
= (*parse
->left
->func
)(parse
->left
, args
, jmp
);
327 LIST
*ns
= (*parse
->right
->func
)(parse
->right
, args
, jmp
);
330 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
);
351 * compile_null() - do nothing -- a stub for parsing
353 LIST
*compile_null (PARSE
*parse
, LOL
*args
, int *jmp
) {
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
);
368 debug_compile(0, "on");
372 /* copy settings, so that 'on target var on target = val' doesn't set var globally */
374 TARGET
*t
= bindtarget(nt
->string
);
375 SETTINGS
*s
= copysettings(t
->settings
);
377 result
= (*parse
->right
->func
)(parse
->right
, args
, jmp
);
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
) {
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 */
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
);
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);*/
423 if (var_string(rulename
, &buf
, args
, ' ') < 0) {
425 printf("Failed to expand rule %s -- expansion too long\n", rulename
);
428 expanded
= dstr_cstr(&buf
);
429 while (expanded
[0] == ' ') ++expanded
;
430 while ((c
= strrchr(expanded
, ' '))) *c
= '\0';
432 debug_compile(1, rulename
);
433 if (strcmp(rulename
, expanded
)) printf("-> %s ", expanded
);
437 rule
= bindrule(expanded
);
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() */
446 /* the action is associated with this instance of this rule */
447 action
= (ACTION
*)malloc(sizeof(ACTION
));
448 memset((char *)action
, '\0', sizeof(*action
));
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
;
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
)));
465 /* bring in local params */
466 /* refer/free to ensure rule not freed during use */
469 result
= list_append(result
, (*parse
->func
)(parse
, args
, &jmp
));
474 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 */
489 while (*jmp
== JMP_NONE
&& parse
->func
== compile_rules
) {
491 result
= (*parse
->left
->func
)(parse
->left
, args
, jmp
);
492 parse
= parse
->right
;
494 if (*jmp
== JMP_NONE
) {
496 result
= (*parse
->func
)(parse
, args
, jmp
);
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
);
514 debug_compile(0, "set");
516 printf(" %s ", set_names
[parse
->num
]);
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
);
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
);
539 /* build param list */
540 for (p
= parse
->left
; p
; p
= p
->left
) params
= list_new(params
, p
->string
, 1);
542 debug_compile(0, "rule");
543 printf("%s ", parse
->string
);
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
);
560 * compile_setexec() - support for `actions` - save execution string
562 * parse->string rule name
563 * parse->string1 OS command string
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 */
575 freestr(rule
->actions
);
576 list_free(rule
->bindlist
);
578 rule
->actions
= copystr(parse
->string1
);
579 rule
->bindlist
= bindlist
;
580 rule
->flags
= parse
->num
;
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
);
599 debug_compile(0, "set");
603 printf(" %s ", set_names
[parse
->num
]);
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
));
622 * compile_switch() - compile 'switch' rule
624 * parse->left switch value (only 1st used)
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
);
637 debug_compile(0, "switch");
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
);
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
) {
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 */
668 /* keep only last result */
670 result
= (*p
->right
->func
)(p
->right
, args
, jmp
);
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 */
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
);