2 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
3 * This file is part of Jam - see jam.c for Copyright information.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License ONLY.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * compile.c - compile parsed jam statements
22 * compile_append() - append list results of two statements
23 * compile_break() - compile 'break/continue/return' rule
24 * compile_eval() - evaluate if to determine which leg to compile
25 * compile_foreach() - compile the "for x in y" statement
26 * compile_if() - compile 'if' rule
27 * compile_include() - support for 'include' - call include() on file
28 * compile_list() - expand and return a list
29 * compile_local() - declare (and set) local variables
30 * compile_null() - do nothing -- a stub for parsing
31 * compile_on() - run rule under influence of on-target variables
32 * compile_rule() - compile a single user defined rule
33 * compile_rules() - compile a chain of rules
34 * compile_set() - compile the "set variable" statement
35 * compile_setcomp() - support for `rule` - save parse tree
36 * compile_setexec() - support for `actions` - save execution string
37 * compile_settings() - compile the "on =" (set variable on exec) statement
38 * compile_switch() - compile 'switch' rule
42 * debug_compile() - printf with indent to show rule expansion.
43 * evaluate_rule() - execute a rule invocation
55 #include "matchglob.h"
59 static const char *set_names
[] = { "=", "+=", "-=", "?=" };
60 static void debug_compile (int which
, const char *s
);
64 * compile_append() - append list results of two statements
66 * parse->left more compile_append() by left-recursion
67 * parse->right single rule
69 LIST
*compile_append (PARSE
*parse
, LOL
*args
, int *jmp
) {
70 /* Append right to left. */
72 (*parse
->left
->func
)(parse
->left
, args
, jmp
),
73 (*parse
->right
->func
)(parse
->right
, args
, jmp
)
79 * compile_break() - compile 'break/continue/return' rule
82 * parse->num JMP_BREAK/CONTINUE/RETURN
84 LIST
*compile_break (PARSE
*parse
, LOL
*args
, int *jmp
) {
85 LIST
*lv
= (*parse
->left
->func
)(parse
->left
, args
, jmp
);
92 * compile_eval() - evaluate if to determine which leg to compile
95 * list if expression true - compile 'then' clause
96 * L0 if expression false - compile 'else' clause
98 static JAMFA_PURE
int lcmp (LIST
*t
, LIST
*s
) {
100 while (!status
&& (t
|| s
)) {
101 const char *st
= (t
? t
->string
: "");
102 const char *ss
= (s
? s
->string
: "");
103 status
= strcmp(st
, ss
);
104 t
= (t
? list_next(t
) : t
);
105 s
= (s
? list_next(s
) : s
);
111 static int recmp (LIST
*s
, LIST
*restr
) {
114 printf("restr: <%s>\n", (restr ? restr->string : "(null)"));
115 printf("s : <%s>\n", (s ? s->string : "(null)"));
117 regexp_t
*re
= regexp_compile((restr
? restr
->string
: ""), 0);
118 res
= regexp_execute(re
, (s
? s
->string
: ""), NULL
, 0);
124 LIST
*compile_eval (PARSE
*parse
, LOL
*args
, int *jmp
) {
125 LIST
*ll
, *lr
, *s
, *t
;
127 /* short circuit lr eval for &&, ||, 'in' and 'any-in' */
128 ll
= (*parse
->left
->func
)(parse
->left
, args
, jmp
);
130 switch (parse
->num
) {
140 eval
: lr
= (*parse
->right
->func
)(parse
->right
, args
, jmp
);
144 switch (parse
->num
) {
145 case EXPR_NOT
: if (!ll
) status
= 1; break;
146 case EXPR_AND
: if (ll
&& lr
) status
= 1; break;
147 case EXPR_OR
: if (ll
|| lr
) status
= 1; break;
149 /* "a in b": make sure each of ll is equal to something in lr */
150 for (t
= ll
; t
; t
= list_next(t
)) {
151 for (s
= lr
; s
; s
= list_next(s
)) if (strcmp(t
->string
, s
->string
) == 0) break;
154 /* No more ll? success */
158 /* "a any-in b": does b contains at least one item from a? */
159 for (t
= ll
; t
&& !status
; t
= list_next(t
)) {
160 for (s
= lr
; s
; s
= list_next(s
)) if (strcmp(t
->string
, s
->string
) == 0) { status
= 1; break; } /* success */
163 case EXPR_EXISTS
: if (lcmp(ll
, L0
) != 0) status
= 1; break;
164 case EXPR_EQUALS
: if (lcmp(ll
, lr
) == 0) status
= 1; break;
165 case EXPR_NOTEQ
: if (lcmp(ll
, lr
) != 0) status
= 1; break;
166 case EXPR_LESS
: if (lcmp(ll
, lr
) < 0) status
= 1; break;
167 case EXPR_LESSEQ
: if (lcmp(ll
, lr
) <= 0) status
= 1; break;
168 case EXPR_MORE
: if (lcmp(ll
, lr
) > 0) status
= 1; break;
169 case EXPR_MOREEQ
: if (lcmp(ll
, lr
) >= 0) status
= 1; break;
170 case EXPR_REXPEQ
: if (recmp(ll
, lr
)) status
= 1; break;
173 debug_compile(0, "if");
175 printf("(%d) ", status
);
176 list_print_ex(stdout
, lr
, LPFLAG_NO_TRSPACE
);
179 /* find something to return */
180 /* in odd circumstances (like "" = "") we'll have to return a new string */
181 if (!status
) t
= NULL
;
182 else if (ll
) { t
= ll
; ll
= NULL
; }
183 else if (lr
) { t
= lr
; lr
= NULL
; }
184 else t
= list_new(L0
, "1", 0);
185 if (ll
) list_free(ll
);
186 if (lr
) list_free(lr
);
192 * compile_foreach() - compile the "for x in y" statement
194 * Compile_foreach() resets the given variable name to each specified
195 * value, executing the commands enclosed in braces for each iteration.
197 * parse->string index variable
198 * parse->left variable values
199 * parse->right rule to compile
201 LIST
*compile_foreach (PARSE
*p
, LOL
*args
, int *jmp
) {
202 LIST
*nv
= (*p
->left
->func
)(p
->left
, args
, jmp
);
207 s
= addsettings(s
, VAR_SET
, p
->string
, L0
);
210 /* for each value for var */
211 for (LIST
*l
= nv
; l
&& *jmp
== JMP_NONE
; l
= list_next(l
)) {
212 /* reset $(p->string) for each val */
213 var_set(p
->string
, list_new(L0
, l
->string
, 1), VAR_SET
);
214 /* keep only last result */
216 result
= (*p
->right
->func
)(p
->right
, args
, jmp
);
218 if (*jmp
== JMP_CONTINUE
) *jmp
= JMP_NONE
;
220 /* here by break/continue? */
221 if (*jmp
== JMP_BREAK
|| *jmp
== JMP_CONTINUE
) *jmp
= JMP_NONE
;
228 /* returns result of last loop */
234 * compile_if() - compile 'if' rule
236 * parse->left condition tree
237 * parse->right then tree
238 * parse->third else tree
240 LIST
*compile_if (PARSE
*p
, LOL
*args
, int *jmp
) {
241 LIST
*l
= (*p
->left
->func
)(p
->left
, args
, jmp
);
242 p
= (l
? p
->right
: p
->third
);
244 return (*p
->func
)(p
, args
, jmp
);
249 * compile_include() - support for 'include' - call include() on file
251 * parse->left list of files to include (can only do 1)
252 * parse->num !0: softinclude
254 LIST
*compile_include (PARSE
*parse
, LOL
*args
, int *jmp
) {
255 int softinc
= parse
->num
;
256 LIST
*nt
= (*parse
->left
->func
)(parse
->left
, args
, jmp
);
258 debug_compile(0, (softinc
? "softinclude" : "include"));
259 list_print_ex(stdout
, nt
, LPFLAG_NO_TRSPACE
);
263 TARGET
*t
= bindtarget(nt
->string
);
264 /* bind the include file under the influence of "on-target" variables */
265 /* though they are targets, include files are not built with make() */
266 /* needn't copysettings(), as search sets no vars */
267 pushsettings(t
->settings
);
268 t
->boundname
= search(t
->name
, &t
->time
);
269 popsettings(t
->settings
);
270 /* don't parse missing file if NOCARE set */
271 if (t
->time
|| !(t
->flags
&T_FLAG_NOCARE
)) {
273 if (file_type(t
->boundname
) != 0 || access(t
->boundname
, R_OK
) != 0) {
275 printf("Failed to include file '%s'\n", t
->boundname
);
280 if (doit
) parse_file(t
->boundname
);
289 * compile_list() - expand and return a list
291 * parse->string - character string to expand
292 * parse->num - "don't expand" flag
294 LIST
*compile_list (PARSE
*parse
, LOL
*args
, int *jmp
) {
295 /* voodoo 1 means: parse->string is a copyable string */
296 return (parse
->num
? list_new(L0
, parse
->string
, 1) : var_expand(parse
->string
, NULL
, args
, 1));
301 * compile_local() - declare (and set) local variables
303 * parse->left list of variables
304 * parse->right list of values
305 * parse->third rules to execute
307 LIST
*compile_local (PARSE
*parse
, LOL
*args
, int *jmp
) {
310 LIST
*nt
= (*parse
->left
->func
)(parse
->left
, args
, jmp
);
311 LIST
*ns
= (*parse
->right
->func
)(parse
->right
, args
, jmp
);
314 debug_compile(0, "local");
317 list_print_ex(stdout
, ns
, LPFLAG_NO_TRSPACE
);
320 /* initial value is ns */
321 for (l
= nt
; l
; l
= list_next(l
)) s
= addsettings(s
, 0, l
->string
, list_copy((LIST
*)0, ns
));
324 /* note that callees of the current context get this "local" */
325 /* variable, making it not so much local as layered */
327 result
= (*parse
->third
->func
)(parse
->third
, args
, jmp
);
335 * compile_null() - do nothing -- a stub for parsing
337 JAMFA_CONST LIST
*compile_null (PARSE
*parse
, LOL
*args
, int *jmp
) {
343 * compile_on() - run rule under influence of on-target variables
345 * parse->left target list; only first used
346 * parse->right rule to run
348 LIST
*compile_on (PARSE
*parse
, LOL
*args
, int *jmp
) {
349 LIST
*nt
= (*parse
->left
->func
)(parse
->left
, args
, jmp
);
352 debug_compile(0, "on");
353 list_print_ex(stdout
, nt
, LPFLAG_NO_TRSPACE
);
356 /* copy settings, so that 'on target var on target = val' doesn't set var globally */
358 TARGET
*t
= bindtarget(nt
->string
);
359 SETTINGS
*s
= copysettings(t
->settings
);
361 result
= (*parse
->right
->func
)(parse
->right
, args
, jmp
);
371 * compile_rule() - compile a single user defined rule
373 * parse->left list of rules to run
374 * parse->right parameters (list of lists) to rule, recursing left
376 * Wrapped around evaluate_rule() so that headers() can share it.
378 LIST
*compile_rule (PARSE
*parse
, LOL
*args
, int *jmp
) {
383 /* list of rules to run -- normally 1! */
384 ll
= (*parse
->left
->func
)(parse
->left
, args
, jmp
);
385 /* build up the list of arg lists */
387 for (p
= parse
->right
; p
; p
= p
->left
) lol_add(nargs
, (*p
->right
->func
)(p
->right
, args
, jmp
));
388 /* run rules, appending results from each */
389 for (l
= ll
; l
; l
= list_next(l
)) result
= evaluate_rule(l
->string
, nargs
, result
);
397 * evaluate_rule() - execute a rule invocation
399 LIST
*evaluate_rule (const char *rulename
, LOL
*args
, LIST
*result
) {
400 //#infdef OPT_EXPAND_RULE_NAMES_EXT
401 /*RULE *rule = bindrule(rulename);*/
407 if (var_string(rulename
, &buf
, args
, ' ') < 0) {
409 printf("Failed to expand rule %s -- expansion too long\n", rulename
);
412 expanded
= dstr_cstr(&buf
);
413 while (expanded
[0] == ' ') ++expanded
;
414 while ((c
= strrchr(expanded
, ' '))) *c
= '\0';
416 debug_compile(1, rulename
);
417 if (strcmp(rulename
, expanded
)) printf("-> %s ", expanded
);
421 rule
= bindrule(expanded
);
424 /* check traditional targets $(<) and sources $(>) */
425 if (!rule
->actions
&& !rule
->procedure
) printf("warning: unknown rule %s\n", rule
->name
);
426 /* if this rule will be executed for updating the targets then construct the action for make() */
430 /* the action is associated with this instance of this rule */
431 action
= (ACTION
*)malloc(sizeof(ACTION
));
432 memset((char *)action
, '\0', sizeof(*action
));
434 action
->targets
= targetlist((TARGETS
*)0, lol_get(args
, 0));
435 action
->sources
= targetlist((TARGETS
*)0, lol_get(args
, 1));
436 /* append this action to the actions of each target */
437 for (t
= action
->targets
; t
; t
= t
->next
) t
->target
->actions
= actionlist(t
->target
->actions
, action
);
439 /* now recursively compile any parse tree associated with this rule */
440 if (rule
->procedure
) {
441 PARSE
*parse
= rule
->procedure
;
446 /* build parameters as local vars */
447 for (l
= rule
->params
, i
= 0; l
; l
= l
->next
, i
++) s
= addsettings(s
, 0, l
->string
, list_copy(L0
, lol_get(args
, i
)));
449 /* bring in local params */
450 /* refer/free to ensure rule not freed during use */
453 result
= list_append(result
, (*parse
->func
)(parse
, args
, &jmp
));
458 if (DEBUG_COMPILE
) debug_compile(-1, 0);
464 * compile_rules() - compile a chain of rules
466 * parse->left single rule
467 * parse->right more compile_rules() by right-recursion
469 LIST
*compile_rules (PARSE
*parse
, LOL
*args
, int *jmp
) {
470 /* ignore result from first statement; return the 2nd */
471 /* optimize recursion on the right by looping */
473 while (*jmp
== JMP_NONE
&& parse
->func
== compile_rules
) {
475 result
= (*parse
->left
->func
)(parse
->left
, args
, jmp
);
476 parse
= parse
->right
;
478 if (*jmp
== JMP_NONE
) {
480 result
= (*parse
->func
)(parse
, args
, jmp
);
487 * compile_set() - compile the "set variable" statement
489 * parse->left variable names
490 * parse->right variable values
491 * parse->num VAR_SET/APPEND/DEFAULT
493 LIST
*compile_set (PARSE
*parse
, LOL
*args
, int *jmp
) {
494 LIST
*nt
= (*parse
->left
->func
)(parse
->left
, args
, jmp
);
495 LIST
*ns
= (*parse
->right
->func
)(parse
->right
, args
, jmp
);
498 debug_compile(0, "set");
500 printf("%s ", set_names
[parse
->num
]);
501 list_print_ex(stdout
, ns
, LPFLAG_NO_TRSPACE
);
504 /* call var_set to set variable */
505 /* var_set keeps ns, so need to copy it */
506 for (l
= nt
; l
; l
= list_next(l
)) var_set(l
->string
, list_copy(L0
, ns
), parse
->num
);
513 * compile_setcomp() - support for `rule` - save parse tree
515 * parse->string rule name
516 * parse->left list of argument names
517 * parse->right rules for rule
519 LIST
*compile_setcomp (PARSE
*parse
, LOL
*args
, int *jmp
) {
520 RULE
*rule
= bindrule(parse
->string
);
523 /* build param list */
524 for (p
= parse
->left
; p
; p
= p
->left
) params
= list_new(params
, p
->string
, 1);
526 debug_compile(0, "rule");
527 printf("%s ", parse
->string
);
528 list_print_ex(stdout
, params
, LPFLAG_NO_TRSPACE
);
531 /* free old one, if present */
532 if (rule
->procedure
) parse_free(rule
->procedure
);
533 if (rule
->params
) list_free(rule
->params
);
534 rule
->procedure
= parse
->right
;
535 rule
->params
= params
;
536 /* we now own this parse tree */
537 /* don't let parse_free() release it */
538 parse_refer(parse
->right
);
544 * compile_setexec() - support for `actions` - save execution string
546 * parse->string rule name
547 * parse->string1 OS command string
549 * parse->left `bind` variables
551 * Note that the parse flags (as defined in compile.h) are transfered
552 * directly to the rule flags (as defined in rules.h).
554 LIST
*compile_setexec (PARSE
*parse
, LOL
*args
, int *jmp
) {
555 RULE
*rule
= bindrule(parse
->string
);
556 LIST
*bindlist
= (*parse
->left
->func
)(parse
->left
, args
, jmp
);
557 /* free old one, if present */
559 freestr(rule
->actions
);
560 list_free(rule
->bindlist
);
562 rule
->actions
= copystr(parse
->string1
);
563 rule
->bindlist
= bindlist
;
564 rule
->flags
= parse
->num
;
570 * compile_settings() - compile the "on =" (set variable on exec) statement
572 * parse->left variable names
573 * parse->right target name
574 * parse->third variable value
575 * parse->num VAR_SET/APPEND/DEFAULT
577 LIST
*compile_settings (PARSE
*parse
, LOL
*args
, int *jmp
) {
578 LIST
*nt
= (*parse
->left
->func
)(parse
->left
, args
, jmp
);
579 LIST
*ns
= (*parse
->third
->func
)(parse
->third
, args
, jmp
);
580 LIST
*targets
= (*parse
->right
->func
)(parse
->right
, args
, jmp
);
583 debug_compile(0, "set");
587 printf("%s ", set_names
[parse
->num
]);
588 list_print_ex(stdout
, ns
, LPFLAG_NO_TRSPACE
);
591 /* call addsettings to save variable setting addsettings keeps ns, so need to copy it */
592 /* pass append flag to addsettings() */
593 for (ts
= targets
; ts
; ts
= list_next(ts
)) {
594 TARGET
*t
= bindtarget(ts
->string
);
595 for (LIST
*l
= nt
; l
; l
= list_next(l
)) {
596 t
->settings
= addsettings(t
->settings
, parse
->num
, l
->string
, list_copy((LIST
*)0, ns
));
606 * compile_switch() - compile 'switch' rule
608 * parse->left switch value (only 1st used)
611 * cases->left 1st case
612 * cases->right next cases
614 * case->string argument to match
615 * case->left parse tree to execute
617 LIST
*compile_switch (PARSE
*parse
, LOL
*args
, int *jmp
) {
618 LIST
*nt
= (*parse
->left
->func
)(parse
->left
, args
, jmp
);
621 debug_compile(0, "switch");
622 list_print_ex(stdout
, nt
, LPFLAG_NO_TRSPACE
);
625 /* step through cases */
626 for (parse
= parse
->right
; parse
; parse
= parse
->right
) {
627 if (!matchglob(parse
->left
->string
, nt
?nt
->string
:"")) {
628 /* get and exec parse tree for this case */
629 parse
= parse
->left
->left
;
630 result
= (*parse
->func
)(parse
, args
, jmp
);
640 * compile_while() - compile 'while' rule
642 * parse->left condition tree
643 * parse->right execution tree
645 LIST
*compile_while (PARSE
*p
, LOL
*args
, int *jmp
) {
648 /* returns the value from the last execution of the block */
649 while ((*jmp
== JMP_NONE
) && (l
= (*p
->left
->func
)(p
->left
, args
, jmp
))) {
650 /* always toss while's expression */
652 /* keep only last result */
654 result
= (*p
->right
->func
)(p
->right
, args
, jmp
);
656 if (*jmp
== JMP_CONTINUE
) *jmp
= JMP_NONE
;
658 /* here by break/continue? */
659 if (*jmp
== JMP_BREAK
|| *jmp
== JMP_CONTINUE
) *jmp
= JMP_NONE
;
660 /* returns result of last loop */
666 * debug_compile() - printf with indent to show rule expansion.
668 static void debug_compile (int which
, const char *s
) {
669 static int level
= 0;
670 static const char indent
[36] = ">>>>|>>>>|>>>>|>>>>|>>>>|>>>>|>>>>|";
671 int i
= ((1+level
)*2)%35;
672 if (which
>= 0) printf("%*.*s ", i
, i
, indent
);
673 if (s
) printf("%s ", s
);