1 /* $RCSfile: walk.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:31 $
3 * Copyright (c) 1991-2001, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
16 bool realexit = FALSE;
17 bool saw_getline = FALSE;
18 bool subretnum = FALSE;
20 bool saw_argv0 = FALSE;
27 STR *curargs = Nullstr;
29 static void addsemi ( STR *str );
30 static void emit_split ( STR *str, int level );
31 static void fixtab ( STR *str, int lvl );
32 static void numericize ( int node );
33 static void tab ( STR *str, int lvl );
35 int prewalk ( int numit, int level, int node, int *numericptr );
36 STR * walk ( int useval, int level, int node, int *numericptr, int minprec );
40 walk(int useval, int level, register int node, int *numericptr, int minprec)
45 /* minimum precedence without parens */
59 int prec = P_MAX; /* assume no parens needed */
65 type = ops[node].ival;
72 while (isALPHA(*namelist)) {
73 for (d = tokenbuf,s=namelist;
74 isALPHA(*s) || isDIGIT(*s) || *s == '_';
77 while (*s && !isALPHA(*s)) s++;
79 nameary[++arymax] = savestr(tokenbuf);
86 str = walk(0,level,ops[node+1].ival,&numarg,P_MIN);
87 if (do_split && need_entire && !absmaxfld)
88 split_to_array = TRUE;
89 if (do_split && split_to_array)
90 set_array_base = TRUE;
92 str_cat(str,"$[ = 1;\t\t\t# set array base to 1\n");
94 if (fswitch && !const_FS)
96 if (saw_FS > 1 || saw_RS)
98 if (saw_ORS && need_entire)
101 str_cat(str,"$FS = '");
102 if (strchr("*+?.[]()|^$\\",fswitch))
104 sprintf(tokenbuf,"%c",fswitch);
105 str_cat(str,tokenbuf);
106 str_cat(str,"';\t\t# field separator from -F switch\n");
108 else if (saw_FS && !const_FS) {
109 str_cat(str,"$FS = ' ';\t\t# set field separator\n");
112 str_cat(str,"$, = ' ';\t\t# set output field separator\n");
115 str_cat(str,"$\\ = \"\\n\";\t\t# set output record separator\n");
118 str_cat(str,"$ARGV0 = $0;\t\t# remember what we ran as\n");
120 if (str->str_cur > 20)
122 if (ops[node+2].ival) {
123 str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg,P_MIN));
127 fstr = walk(0,level+1,ops[node+3].ival,&numarg,P_MIN);
128 if (*fstr->str_ptr) {
130 str_cat(str,"line: ");
131 str_cat(str,"while (<>) {\n");
133 if (saw_FS && !const_FS)
136 str_cat(str,"chomp;\t# strip record separator\n");
140 emit_split(str,level);
146 str_cat(str,"continue {\n $FNRbase = $. if eof;\n}\n");
149 str_cat(str,"while (<>) { } # (no line actions)\n");
150 if (ops[node+4].ival) {
154 str_scat(str,fstr=walk(0,level,ops[node+4].ival,&numarg,P_MIN));
159 str_cat(str,"exit $ExitValue;\n");
165 for (len = 0; len < 4; len++) {
166 if (saw_getline & (1 << len)) {
167 sprintf(tokenbuf,"\nsub Getline%d {\n",len);
168 str_cat(str, tokenbuf);
171 str_cat(str," &Pick('',@_);\n");
173 str_cat(str," ($fh) = @_;\n");
177 str_cat(str," $FNRbase = $. if eof;\n");
180 str_cat(str," local($_);\n");
183 " if ($getline_ok = (($_ = <$fh>) ne ''))");
186 " if ($getline_ok = (($_ = <>) ne ''))");
187 str_cat(str, " {\n");
193 str_cat(str,"chomp;\t# strip record separator\n");
196 if (do_split && !(len & 1)) {
198 emit_split(str,level);
203 str_cat(str,"}\n $_;\n}\n");
208 if (do_fancy_opens) {
211 local($mode,$name,$pipe) = @_;\n\
213 open($name,$mode.$name.$pipe) unless $opened{$name}++;\n\
219 str = walk(0,level,ops[node+1].ival,&numarg,P_MIN);
220 str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg,P_MIN));
223 str_scat(str,fstr=walk(0,level,ops[node+3].ival,&numarg,P_MIN));
231 str = walk(1,level,ops[node+1].ival,&numarg,prec+1);
233 str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,prec+1));
241 tmpstr=walk(0,level,ops[node+1].ival,&numarg,P_MIN);
242 /* translate \nnn to [\nnn] */
243 for (s = tmpstr->str_ptr, d = tokenbuf; *s; s++, d++) {
244 if (*s == '\\' && isDIGIT(s[1]) && isDIGIT(s[2]) && isDIGIT(s[3])){
256 for (d=tokenbuf; *d; d++)
258 str_cat(str,tokenbuf);
265 str = walk(0,level,oper1(OPRINT,0),&numarg,P_MIN);
267 str_scat(str,fstr=walk(0,level,ops[node+1].ival,&numarg,P_MIN));
272 tmpstr = walk(0,level,ops[node+1].ival,&numarg,P_MIN);
273 if (*tmpstr->str_ptr) {
276 str_scat(str,tmpstr);
277 str_cat(str,") {\n");
279 str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg,P_MIN));
286 str = walk(0,level,ops[node+2].ival,&numarg,P_MIN);
293 str_scat(str,fstr=walk(useval != 0,level,ops[node+1].ival,&numarg,P_MIN));
299 str = walk(1,level,ops[node+1].ival,&numarg,prec);
301 str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,prec+1));
303 str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg,prec+1));
308 str = walk(1,level,ops[node+1].ival,&numarg,prec);
310 str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,prec+1));
312 str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg,prec+1));
319 str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,prec));
324 str = walk(1,level,ops[node+1].ival,&numarg,prec);
326 str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,prec+1));
329 str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg,prec+1));
335 str_scat(str,fstr=walk(useval != 0,level,ops[node+1].ival,&numarg,P_MIN));
342 str = walk(1,level,ops[node+1].ival,&numarg,prec);
345 str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,prec+1));
347 str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg,prec+1));
352 str = walk(1,level,ops[node+1].ival,&numarg,prec);
355 str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,prec+1));
357 str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg,prec+1));
364 str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,prec));
370 str = walk(1,level,ops[node+2].ival,&numarg,prec+1);
372 tmpstr = walk(0,level,ops[node+1].ival,&numarg,P_MIN);
373 tmp2str = walk(1,level,ops[node+3].ival,&numarg,prec+1);
376 (!numarg && (*tmp2str->str_ptr == '"' || *tmp2str->str_ptr == '\''))) {
379 str_set(tmpstr,"eq");
380 else if (strEQ(t,"!="))
381 str_set(tmpstr,"ne");
382 else if (strEQ(t,"<"))
383 str_set(tmpstr,"lt");
384 else if (strEQ(t,"<="))
385 str_set(tmpstr,"le");
386 else if (strEQ(t,">"))
387 str_set(tmpstr,"gt");
388 else if (strEQ(t,">="))
389 str_set(tmpstr,"ge");
390 if (!strchr(tmpstr->str_ptr,'\'') && !strchr(tmpstr->str_ptr,'"') &&
391 !strchr(tmp2str->str_ptr,'\'') && !strchr(tmp2str->str_ptr,'"') )
395 if (numeric & 1) /* numeric is very good guess */
403 str_scat(str,tmpstr);
406 str_scat(str,tmp2str);
413 str_scat(str,fstr=walk(useval != 0,level,ops[node+1].ival,&numarg,P_MIN));
420 str = walk(1,level,ops[node+2].ival,&numarg,prec+1);
422 tmpstr = walk(0,level,ops[node+1].ival,&numarg,P_MIN);
423 if (strEQ(tmpstr->str_ptr,"~"))
426 str_scat(str,tmpstr);
430 str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg,prec+1));
438 fstr=walk(useval != 0,level,ops[node+1].ival,&numarg,P_MIN));
445 type = ops[ops[node+1].ival].ival & 255;
446 str = walk(1,level,ops[node+1].ival,&numarg,prec+(type != OCONCAT));
448 type = ops[ops[node+2].ival].ival & 255;
450 fstr=walk(1,level,ops[node+2].ival,&numarg,prec+(type != OCONCAT)));
455 str = walk(0,level,ops[node+2].ival,&numarg,prec+1);
457 tmpstr = walk(0,level,ops[node+1].ival,&numarg,P_MIN);
458 str_scat(str,tmpstr);
459 if (str_len(tmpstr) > 1)
463 str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg,prec));
466 if (strEQ(str->str_ptr,"$/ = ''"))
467 str_set(str, "$/ = \"\\n\\n\"");
471 str = walk(1,level,ops[node+1].ival,&numarg,prec);
473 str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,prec+1));
479 str = walk(1,level,ops[node+1].ival,&numarg,prec);
481 str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,prec+1));
487 str = walk(1,level,ops[node+1].ival,&numarg,prec);
489 str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,prec+1));
495 str = walk(1,level,ops[node+1].ival,&numarg,prec);
497 str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,prec+1));
503 str = walk(1,level,ops[node+1].ival,&numarg,prec+1);
505 str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,prec));
511 str = walk(1,level,ops[node+1].ival,&numarg,prec);
513 str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,prec+1));
519 str = walk(1,level,ops[node+1].ival,&numarg,prec+1);
525 str = walk(1,level,ops[node+1].ival,&numarg,prec+1);
533 str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,prec+1));
541 str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,prec+1));
549 str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,prec));
560 fstr=walk(useval != 0,level,ops[node+1].ival,&numarg,P_MIN));
570 str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN));
571 if (!*fstr->str_ptr) {
573 len = 2; /* a legal fiction */
580 tmpstr=walk(1,level,ops[node+3].ival,&numarg,P_MIN);
581 fstr=walk(1,level,ops[node+2].ival,&numarg,P_MIN);
582 if (!do_fancy_opens) {
584 if (*t == '"' || *t == '\'')
585 t = cpytill(tokenbuf,t+1,*t);
587 fatal("Internal error: OGETLINE %s", t);
589 s = savestr(tokenbuf);
590 for (t = tokenbuf; *t; t++) {
594 if (!isALPHA(*t) && !isDIGIT(*t))
597 if (!strchr(tokenbuf,'_'))
599 tmp3str = hfetch(symtab,tokenbuf);
602 str_cat(opens,"open(");
603 str_cat(opens,tokenbuf);
607 str_cat(opens,tmpstr->str_ptr+1);
609 if (*fstr->str_ptr == '|')
612 if (*fstr->str_ptr == '|')
613 str_cat(opens,") || die 'Cannot pipe from \"");
615 str_cat(opens,") || die 'Cannot open file \"");
617 str_cat(opens,"'.\"");
620 str_cat(opens,"\".'");
621 str_cat(opens,"\".';\n");
622 hstore(symtab,tokenbuf,str_make("x"));
627 str_cat(tmpstr,tokenbuf);
630 if (*fstr->str_ptr == '|')
631 str_cat(tmpstr,", '|'");
635 tmpstr = str_make("");
636 sprintf(tokenbuf," = &Getline%d(%s)",len,tmpstr->str_ptr);
637 str_cat(str,tokenbuf);
640 str_cat(str,",$getline_ok)");
641 saw_getline |= 1 << len;
645 str_set(str,"sprintf(");
646 str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN));
652 str_set(str,"substr(");
653 str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_COMMA+1));
656 str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,P_COMMA+1));
660 str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg,P_COMMA+1));
664 str_cat(str,"999999");
669 str_set(str,ops[node+1].cval);
675 tmpstr = walk(1,level,ops[node+2].ival,&numarg,P_MIN);
680 str_scat(str,tmpstr);
681 str_cat(str," = split(");
683 fstr = walk(1,level,ops[node+3].ival,&numarg,P_COMMA+1);
684 if (str_len(fstr) == 3 && *fstr->str_ptr == '\'') {
685 i = fstr->str_ptr[1] & 127;
686 if (strchr("*+?.[]()|^$\\",i))
687 sprintf(tokenbuf,"/\\%c/",i);
689 sprintf(tokenbuf,"' '");
691 sprintf(tokenbuf,"/%c/",i);
692 str_cat(str,tokenbuf);
699 sprintf(tokenbuf,"/[%c\\n]/",const_FS);
700 str_cat(str,tokenbuf);
709 str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_COMMA+1));
719 str_set(str,"index(");
720 str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_COMMA+1));
723 str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,P_COMMA+1));
731 str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_MATCH+1));
734 str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,P_MATCH+1));
736 str_cat(str," && ($RLENGTH = length($&), $RSTART = length($`)+1)");
742 fstr=walk(1,level-1,ops[node+2].ival,&numarg,P_MIN);
743 curargs = str_new(0);
744 str_sset(curargs,fstr);
745 str_cat(curargs,",");
746 tmp2str=walk(1,level,ops[node+5].ival,&numarg,P_MIN);
752 t = tmp2str->str_ptr;
753 while ((t = instr(t,"return ")))
757 for (t = s+7; *t; t++) {
758 if (*t == ';' || *t == '}')
763 tmp2str->str_cur -= 7;
769 str_scat(str,tmpstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN));
773 str_cat(str,"local(");
775 str_cat(str,") = @_;");
778 str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg,P_MIN));
781 str_scat(str,fstr=walk(1,level,ops[node+4].ival,&numarg,P_MIN));
784 str_scat(str,tmp2str);
792 tmp2str = str_new(0);
794 str_set(tmp2str,"1");
795 hstore(symtab,tmpstr->str_ptr,tmp2str);
802 str_cat(str,"return ");
803 str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_UNI+1));
809 str_cat(str,"return");
814 str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN));
817 tmpstr = hfetch(symtab,str->str_ptr+3);
818 if (tmpstr && tmpstr->str_ptr)
819 numeric |= atoi(tmpstr->str_ptr);
820 str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,P_MIN));
834 tmpstr = walk(1,level,ops[node+3].ival,&numarg,P_MATCH+1);
835 if (strNE(tmpstr->str_ptr,"$_")) {
836 str_cat(tmpstr, " =~ s");
840 str_set(tmpstr, "s");
843 str_set(tmpstr, "s");
844 type = ops[ops[node+2].ival].ival;
847 tmp3str = str_new(0);
849 tmp2str=walk(1,level,ops[ops[node+2].ival+1].ival,&numarg,P_MIN);
850 for (t = tmp2str->str_ptr, d=tokenbuf; *t; d++,t++) {
858 str_set(tmp2str,tokenbuf);
861 tmp2str=walk(1,level,ops[node+2].ival,&numarg,P_MIN);
862 str_set(tmp3str,"($s_ = '\"'.(");
863 str_scat(tmp3str,tmp2str);
864 str_cat(tmp3str,").'\"') =~ s/&/\\$&/g, ");
865 str_set(tmp2str,"eval $s_");
866 s = (char*)(*s == 'g' ? "ge" : "e");
869 type = ops[ops[node+1].ival].ival;
872 fstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN);
873 if (type == OREGEX) {
876 str_scat(str,tmp3str);
877 str_scat(str,tmpstr);
879 str_scat(str,tmp2str);
883 else if ((type == OFLD && !split_to_array) || (type == OVAR && len == 1)) {
886 str_scat(str,tmp3str);
887 str_scat(str,tmpstr);
891 str_scat(str,tmp2str);
899 str_cat(str,"$s = ");
902 str_scat(str,tmp3str);
903 str_scat(str,tmpstr);
905 str_scat(str,tmp2str);
918 str = walk(1,level,ops[node+1].ival,&numarg,P_MIN);
922 tmpstr = walk(1,level,ops[node+1].ival,&numarg,P_MIN);
924 for (t = tmpstr->str_ptr, d=tokenbuf; *t; d++,t++) {
927 else if (*t == '\\') {
931 case '\\': case '"': case 'n': case 't': case '$':
933 default: /* hide this from perl */
942 str_cat(str,tokenbuf);
949 str_set(str,"defined $");
953 str_set(str,"delete $");
963 str_scat(str,tmpstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN));
965 tmp2str = hfetch(symtab,tmpstr->str_ptr);
966 if (tmp2str && atoi(tmp2str->str_ptr))
968 if (strEQ(str->str_ptr,"$FNR")) {
971 str_set(str,"($.-$FNRbase)");
973 else if (strEQ(str->str_ptr,"$NR")) {
977 else if (strEQ(str->str_ptr,"$NF")) {
979 str_set(str,"$#Fld");
981 else if (strEQ(str->str_ptr,"$0"))
983 else if (strEQ(str->str_ptr,"$ARGC"))
984 str_set(str,"($#ARGV+1)");
989 sprintf(tokenbuf,"$%s,",tmpstr->str_ptr);
990 ??? if (instr(curargs->str_ptr,tokenbuf))
991 str_cat(str,"\377"); /* can't translate yet */
994 str_cat(tmpstr,"[]");
995 tmp2str = hfetch(symtab,tmpstr->str_ptr);
996 if (tmp2str && atoi(tmp2str->str_ptr))
1000 str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,P_MIN));
1002 if (strEQ(str->str_ptr,"$ARGV[0")) {
1003 str_set(str,"$ARGV0");
1007 if (tmp2str && atoi(tmp2str->str_ptr))
1008 strcpy(tokenbuf,"]");
1010 strcpy(tokenbuf,"}");
1012 str_cat(str,tokenbuf);
1019 if (split_to_array) {
1020 str_set(str,"$Fld");
1022 str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN));
1027 i = atoi(walk(1,level,ops[node+1].ival,&numarg,P_MIN)->str_ptr);
1029 sprintf(tokenbuf,"$%s",nameary[i]);
1031 sprintf(tokenbuf,"$Fld%d",i);
1032 str_set(str,tokenbuf);
1037 str_set(str,"$Fld[");
1038 i = ops[node+1].ival;
1039 if ((ops[i].ival & 255) == OPAREN)
1041 tmpstr=walk(1,level,i,&numarg,P_MIN);
1042 str_scat(str,tmpstr);
1061 tmpstr = walk(0,level,ops[node+1].ival,&numarg,P_MIN);
1062 for (s = tmpstr->str_ptr; *s && *s != '\n'; s++)
1064 str_scat(str,tmpstr);
1070 tmpstr = walk(0,level,ops[node+1].ival,&numarg,P_MIN);
1071 for (s = tmpstr->str_ptr; *s && *s != '\n'; s++)
1073 str_scat(str,tmpstr);
1079 str = walk(1,level,ops[node+1].ival,&numarg,prec);
1081 str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,P_MIN));
1083 str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg,prec+1));
1092 str = walk(0,level,ops[node+1].ival,&numarg,P_MIN);
1093 str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg,P_MIN));
1099 str_scat(str,fstr=walk(0,level,ops[node+1].ival,&numarg,P_MIN));
1102 tmpstr = walk(0,level,ops[node+2].ival,&numarg,P_MIN);
1103 if (*tmpstr->str_ptr == ';') {
1105 str_cat(str,tmpstr->str_ptr+1);
1112 str = str_make("close(");
1113 tmpstr = walk(1,level,ops[node+1].ival,&numarg,P_MIN);
1114 if (!do_fancy_opens) {
1115 t = tmpstr->str_ptr;
1116 if (*t == '"' || *t == '\'')
1117 t = cpytill(tokenbuf,t+1,*t);
1119 fatal("Internal error: OCLOSE %s",t);
1120 s = savestr(tokenbuf);
1121 for (t = tokenbuf; *t; t++) {
1125 if (!isALPHA(*t) && !isDIGIT(*t))
1128 if (!strchr(tokenbuf,'_'))
1132 str_set(str,"close ");
1133 str_cat(str,tokenbuf);
1136 sprintf(tokenbuf,"delete $opened{%s} && close(%s)",
1137 tmpstr->str_ptr, tmpstr->str_ptr);
1139 str_set(str,tokenbuf);
1144 lparen = ""; /* set to parens if necessary */
1147 if (len == 3) { /* output redirection */
1148 tmpstr = walk(1,level,ops[node+3].ival,&numarg,P_MIN);
1149 tmp2str = walk(1,level,ops[node+2].ival,&numarg,P_MIN);
1150 if (!do_fancy_opens) {
1151 t = tmpstr->str_ptr;
1152 if (*t == '"' || *t == '\'')
1153 t = cpytill(tokenbuf,t+1,*t);
1155 fatal("Internal error: OPRINT");
1157 s = savestr(tokenbuf);
1158 for (t = tokenbuf; *t; t++) {
1162 if (!isALPHA(*t) && !isDIGIT(*t))
1165 if (!strchr(tokenbuf,'_'))
1167 tmp3str = hfetch(symtab,tokenbuf);
1169 str_cat(opens,"open(");
1170 str_cat(opens,tokenbuf);
1171 str_cat(opens,", ");
1174 str_scat(opens,tmp2str);
1175 str_cat(opens,tmpstr->str_ptr+1);
1176 if (*tmp2str->str_ptr == '|')
1177 str_cat(opens,") || die 'Cannot pipe to \"");
1179 str_cat(opens,") || die 'Cannot create file \"");
1181 str_cat(opens,"'.\"");
1184 str_cat(opens,"\".'");
1185 str_cat(opens,"\".';\n");
1186 hstore(symtab,tokenbuf,str_make("x"));
1194 sprintf(tokenbuf,"&Pick('%s', %s) &&\n",
1195 tmp2str->str_ptr, tmpstr->str_ptr);
1196 str_cat(str,tokenbuf);
1198 strcpy(tokenbuf,"$fh");
1206 strcpy(tokenbuf,"");
1207 str_cat(str,lparen); /* may be null */
1208 if (type == OPRINTF)
1209 str_cat(str,"printf");
1211 str_cat(str,"print");
1213 if (len == 3 || do_fancy_opens) {
1218 str_cat(str,tokenbuf);
1220 tmpstr = walk(1+(type==OPRINT),level,ops[node+1].ival,&numarg,P_MIN);
1221 if (!*tmpstr->str_ptr && lval_field) {
1222 t = (char*)(saw_OFS ? "$," : "' '");
1223 if (split_to_array) {
1224 sprintf(tokenbuf,"join(%s,@Fld)",t);
1225 str_cat(tmpstr,tokenbuf);
1228 for (i = 1; i < maxfld; i++) {
1230 sprintf(tokenbuf,"$%s, ",nameary[i]);
1232 sprintf(tokenbuf,"$Fld%d, ",i);
1233 str_cat(tmpstr,tokenbuf);
1235 if (maxfld <= arymax)
1236 sprintf(tokenbuf,"$%s",nameary[maxfld]);
1238 sprintf(tokenbuf,"$Fld%d",maxfld);
1239 str_cat(tmpstr,tokenbuf);
1242 if (*tmpstr->str_ptr) {
1244 if (!saw_fh && *tmpstr->str_ptr == '(') {
1246 str_scat(str,tmpstr);
1250 str_scat(str,tmpstr);
1255 str_cat(str,rparen); /* may be null */
1259 str = str_make("rand(1)");
1262 str = str_make("srand(");
1265 str = str_make("atan2(");
1268 str = str_make("sin(");
1271 str = str_make("cos(");
1274 str = str_make("system(");
1277 str = str_make("length(");
1280 str = str_make("log(");
1283 str = str_make("exp(");
1286 str = str_make("sqrt(");
1289 str = str_make("int(");
1293 tmpstr = walk(1,level,ops[node+1].ival,&numarg,P_MIN);
1295 tmpstr = str_new(0);
1296 if (!tmpstr->str_ptr || !*tmpstr->str_ptr) {
1298 t = (char*)(saw_OFS ? "$," : "' '");
1299 if (split_to_array) {
1300 sprintf(tokenbuf,"join(%s,@Fld)",t);
1301 str_cat(tmpstr,tokenbuf);
1304 sprintf(tokenbuf,"join(%s, ",t);
1305 str_cat(tmpstr,tokenbuf);
1306 for (i = 1; i < maxfld; i++) {
1308 sprintf(tokenbuf,"$%s,",nameary[i]);
1310 sprintf(tokenbuf,"$Fld%d,",i);
1311 str_cat(tmpstr,tokenbuf);
1313 if (maxfld <= arymax)
1314 sprintf(tokenbuf,"$%s)",nameary[maxfld]);
1316 sprintf(tokenbuf,"$Fld%d)",maxfld);
1317 str_cat(tmpstr,tokenbuf);
1321 str_cat(tmpstr,"$_");
1323 if (strEQ(tmpstr->str_ptr,"$_")) {
1324 if (type == OLENGTH && !do_chop) {
1325 str = str_make("(length(");
1326 str_cat(tmpstr,") - 1");
1329 str_scat(str,tmpstr);
1335 str_set(str,"last");
1339 str_set(str,"next line");
1345 str_set(str,"exit");
1350 fstr=walk(1,level,ops[node+1].ival,&numarg,prec+1));
1356 str_set(str,"$ExitValue = ");
1359 fstr=walk(1,level,ops[node+1].ival,&numarg,P_ASSIGN));
1363 str_cat(str,"last line");
1368 str_set(str,"next");
1374 str_set(str,"if (");
1375 str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN));
1378 str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg,P_MIN));
1381 i = ops[node+3].ival;
1383 if ((ops[i].ival & 255) == OBLOCK) {
1386 if ((ops[i].ival & 255) != OIF)
1395 str_scat(str,fstr=walk(0,level,i,&numarg,P_MIN));
1399 str_cat(str,"else ");
1400 str_scat(str,fstr=walk(0,level,ops[node+3].ival,&numarg,P_MIN));
1407 str_set(str,"while (");
1408 str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN));
1411 str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg,P_MIN));
1417 str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN));
1419 if (str->str_ptr[str->str_cur - 1] == '\n')
1421 str_cat(str," while (");
1422 str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg,P_MIN));
1428 str_set(str,"for (");
1429 str_scat(str,tmpstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN));
1432 t = s = tmpstr->str_ptr;
1433 while (isALPHA(*t) || isDIGIT(*t) || *t == '$' || *t == '_')
1440 fstr=walk(1,level,ops[node+2].ival,&numarg,P_MIN);
1441 if (i && (t = strchr(fstr->str_ptr,0377))) {
1442 if (strnEQ(fstr->str_ptr,s,i))
1449 str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg,P_MIN));
1452 str_scat(str,fstr=walk(0,level,ops[node+4].ival,&numarg,P_MIN));
1456 tmpstr = walk(0,level,ops[node+1].ival,&numarg,P_MIN);
1457 d = strchr(tmpstr->str_ptr,'$');
1459 fatal("Illegal for loop: %s",tmpstr->str_ptr);
1464 fatal("Illegal for loop: %s",d);
1466 for (t = s; (i = *t); t++) {
1468 if (i == '}' || i == ']')
1476 tmp2str = hfetch(symtab,str->str_ptr);
1477 if (tmp2str && atoi(tmp2str->str_ptr)) {
1479 "foreach %s ($[ .. $#%s) ",
1485 "foreach %s (keys %%%s) ",
1489 str_set(str,tokenbuf);
1490 str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg,P_MIN));
1497 if (len >= 2 && ops[node+2].ival) {
1498 str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg,P_MIN));
1501 fixtab(str,++level);
1502 str_scat(str,fstr=walk(0,level,ops[node+1].ival,&numarg,P_MIN));
1505 fixtab(str,--level);
1509 str_scat(str,fstr=walk(0,level,ops[node+3].ival,&numarg,P_MIN));
1517 fatal("Garbage length in walk");
1518 str = walk(0,level,ops[node+1].ival,&numarg,P_MIN);
1519 for (i = 2; i<= len; i++) {
1520 str_scat(str,fstr=walk(0,level,ops[node+i].ival,&numarg,P_MIN));
1532 if (useval && prec < minprec) { /* need parens? */
1533 fstr = str_new(str->str_cur+2);
1534 str_nset(fstr,"(",1);
1536 str_ncat(fstr,")",1);
1541 *numericptr = numeric;
1544 printf("%3d %5d %15s %d %4d ",level,node,opname[type],len,str->str_cur);
1545 for (t = str->str_ptr; *t && t - str->str_ptr < 40; t++)
1548 else if (*t == '\t')
1559 tab(register STR *str, register int lvl)
1570 fixtab(register STR *str, register int lvl)
1574 /* strip trailing white space */
1576 s = str->str_ptr+str->str_cur - 1;
1577 while (s >= str->str_ptr && (*s == ' ' || *s == '\t' || *s == '\n'))
1580 str->str_cur = s + 1 - str->str_ptr;
1581 if (s >= str->str_ptr && *s != '\n')
1588 addsemi(register STR *str)
1592 s = str->str_ptr+str->str_cur - 1;
1593 while (s >= str->str_ptr && (*s == ' ' || *s == '\t' || *s == '\n'))
1595 if (s >= str->str_ptr && *s != ';' && *s != '}')
1600 emit_split(register STR *str, int level)
1605 str_cat(str,"@Fld");
1608 for (i = 1; i < maxfld; i++) {
1610 sprintf(tokenbuf,"$%s,",nameary[i]);
1612 sprintf(tokenbuf,"$Fld%d,",i);
1613 str_cat(str,tokenbuf);
1615 if (maxfld <= arymax)
1616 sprintf(tokenbuf,"$%s)",nameary[maxfld]);
1618 sprintf(tokenbuf,"$Fld%d)",maxfld);
1619 str_cat(str,tokenbuf);
1622 sprintf(tokenbuf," = split(/[%c\\n]/, $_, 9999);\n",const_FS);
1623 str_cat(str,tokenbuf);
1626 str_cat(str," = split($FS, $_, 9999);\n");
1628 str_cat(str," = split(' ', $_, 9999);\n");
1633 prewalk(int numit, int level, register int node, int *numericptr)
1639 int numeric = FALSE;
1647 type = ops[node].ival;
1652 prewalk(0,level,ops[node+1].ival,&numarg);
1653 if (ops[node+2].ival) {
1654 prewalk(0,level,ops[node+2].ival,&numarg);
1657 prewalk(0,level,ops[node+3].ival,&numarg);
1659 if (ops[node+3].ival) {
1660 prewalk(0,level,ops[node+4].ival,&numarg);
1664 prewalk(0,level,ops[node+1].ival,&numarg);
1665 prewalk(0,level,ops[node+2].ival,&numarg);
1667 prewalk(0,level,ops[node+3].ival,&numarg);
1671 prewalk(1,level,ops[node+1].ival,&numarg);
1672 prewalk(1,level,ops[node+2].ival,&numarg);
1677 prewalk(0,level,ops[node+1].ival,&numarg);
1681 prewalk(0,level,ops[node+1].ival,&numarg);
1684 i = prewalk(0,level,ops[node+1].ival,&numarg);
1687 prewalk(0,level,ops[node+2].ival,&numarg);
1691 prewalk(0,level,ops[node+2].ival,&numarg);
1696 prewalk(0,level,ops[node+1].ival,&numarg);
1699 prewalk(0,level,ops[node+1].ival,&numarg);
1700 prewalk(0,level,ops[node+2].ival,&numarg);
1703 prewalk(0,level,ops[node+1].ival,&numarg);
1704 prewalk(0,level,ops[node+2].ival,&numarg);
1707 prewalk(0,level,ops[node+1].ival,&numarg);
1710 prewalk(0,level,ops[node+1].ival,&numarg);
1714 prewalk(0,level,ops[node+1].ival,&numarg);
1716 prewalk(0,level,ops[node+2].ival,&numarg);
1719 prewalk(0,level,ops[node+1].ival,&numarg);
1721 prewalk(0,level,ops[node+2].ival,&numarg);
1724 prewalk(0,level,ops[node+1].ival,&numarg);
1728 prewalk(0,level,ops[node+2].ival,&numarg);
1730 prewalk(0,level,ops[node+1].ival,&numarg);
1731 prewalk(0,level,ops[node+3].ival,&numarg);
1736 prewalk(0,level,ops[node+1].ival,&numarg);
1740 prewalk(0,level,ops[node+2].ival,&numarg);
1741 prewalk(0,level,ops[node+1].ival,&numarg);
1742 prewalk(0,level,ops[node+3].ival,&numarg);
1746 prewalk(0,level,ops[node+1].ival,&numarg);
1750 prewalk(0,level,ops[node+1].ival,&numarg);
1751 prewalk(0,level,ops[node+2].ival,&numarg);
1754 prewalk(0,level,ops[node+2].ival,&numarg);
1755 prewalk(0,level,ops[node+1].ival,&numarg);
1756 prewalk(0,level,ops[node+3].ival,&numarg);
1757 if (numarg || strlen(ops[ops[node+1].ival+1].cval) > (Size_t)1) {
1758 numericize(ops[node+2].ival);
1760 numericize(ops[node+3].ival);
1765 prewalk(1,level,ops[node+1].ival,&numarg);
1766 prewalk(1,level,ops[node+2].ival,&numarg);
1770 prewalk(1,level,ops[node+1].ival,&numarg);
1771 prewalk(1,level,ops[node+2].ival,&numarg);
1775 prewalk(1,level,ops[node+1].ival,&numarg);
1776 prewalk(1,level,ops[node+2].ival,&numarg);
1780 prewalk(1,level,ops[node+1].ival,&numarg);
1781 prewalk(1,level,ops[node+2].ival,&numarg);
1785 prewalk(1,level,ops[node+1].ival,&numarg);
1786 prewalk(1,level,ops[node+2].ival,&numarg);
1790 prewalk(1,level,ops[node+1].ival,&numarg);
1791 prewalk(1,level,ops[node+2].ival,&numarg);
1795 prewalk(1,level,ops[node+1].ival,&numarg);
1799 prewalk(1,level,ops[node+1].ival,&numarg);
1803 prewalk(1,level,ops[node+1].ival,&numarg);
1807 prewalk(1,level,ops[node+1].ival,&numarg);
1811 prewalk(1,level,ops[node+1].ival,&numarg);
1815 prewalk(1,level,ops[node+1].ival,&numarg);
1819 prewalk(0,level,ops[node+1].ival,&numarg);
1825 prewalk(0,level,ops[node+1].ival,&numarg);
1828 prewalk(0,level,ops[node+1].ival,&numarg);
1829 prewalk(1,level,ops[node+2].ival,&numarg);
1831 prewalk(1,level,ops[node+3].ival,&numarg);
1838 prewalk(0,level,ops[node+2].ival,&numarg);
1840 prewalk(0,level,ops[node+3].ival,&numarg);
1841 prewalk(0,level,ops[node+1].ival,&numarg);
1844 prewalk(0,level,ops[node+1].ival,&numarg);
1845 prewalk(0,level,ops[node+2].ival,&numarg);
1849 prewalk(0,level,ops[node+1].ival,&numarg);
1850 prewalk(0,level,ops[node+2].ival,&numarg);
1856 tmpstr = walk(0,level,ops[node+1].ival,&numarg,P_MIN);
1858 prewalk(0,level,ops[node+2].ival,&numarg);
1859 prewalk(0,level,ops[node+4].ival,&numarg);
1860 prewalk(0,level,ops[node+5].ival,&numarg);
1862 str_cat(tmpstr,"(");
1863 tmp2str = str_new(0);
1864 if (subretnum || numarg)
1865 str_set(tmp2str,"1");
1866 hstore(symtab,tmpstr->str_ptr,tmp2str);
1872 prewalk(0,level,ops[node+1].ival,&numarg);
1878 tmp2str = str_new(0);
1879 str_scat(tmp2str,tmpstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN));
1880 fixrargs(tmpstr->str_ptr,ops[node+2].ival,0);
1882 str_cat(tmp2str,"(");
1883 tmpstr = hfetch(symtab,tmp2str->str_ptr);
1884 if (tmpstr && tmpstr->str_ptr)
1885 numeric |= atoi(tmpstr->str_ptr);
1886 prewalk(0,level,ops[node+2].ival,&numarg);
1892 prewalk(0,level,ops[node+3].ival,&numarg);
1893 prewalk(0,level,ops[ops[node+2].ival+1].ival,&numarg);
1894 prewalk(0,level,ops[node+1].ival,&numarg);
1898 prewalk(0,level,ops[node+1].ival,&numarg);
1902 prewalk(0,level,ops[node+1].ival,&numarg);
1908 prewalk(0,level,ops[node+1].ival,&numarg);
1914 prewalk(0,level,ops[node+2].ival,&numarg);
1918 prewalk(0,level,ops[node+1].ival,&numarg);
1921 i = ops[node+1].ival;
1922 prewalk(0,level,i,&numarg);
1935 prewalk(0,level,ops[node+1].ival,&numarg);
1936 prewalk(0,level,ops[node+2].ival,&numarg);
1937 prewalk(0,level,ops[node+3].ival,&numarg);
1942 prewalk(0,level,ops[node+1].ival,&numarg);
1943 prewalk(0,level,ops[node+2].ival,&numarg);
1947 prewalk(0,level,ops[node+1].ival,&numarg);
1949 prewalk(0,level,ops[node+2].ival,&numarg);
1954 prewalk(0,level,ops[node+1].ival,&numarg);
1958 if (len == 3) { /* output redirection */
1959 prewalk(0,level,ops[node+3].ival,&numarg);
1960 prewalk(0,level,ops[node+2].ival,&numarg);
1962 prewalk(0+(type==OPRINT),level,ops[node+1].ival,&numarg);
1988 prewalk(type != OLENGTH && type != OSYSTEM,
1989 level,ops[node+1].ival,&numarg);
1997 prewalk(1,level,ops[node+1].ival,&numarg);
2005 prewalk(0,level,ops[node+1].ival,&numarg);
2006 prewalk(0,level,ops[node+2].ival,&numarg);
2008 prewalk(0,level,ops[node+3].ival,&numarg);
2012 prewalk(0,level,ops[node+1].ival,&numarg);
2013 prewalk(0,level,ops[node+2].ival,&numarg);
2016 prewalk(0,level,ops[node+1].ival,&numarg);
2017 prewalk(0,level,ops[node+2].ival,&numarg);
2018 prewalk(0,level,ops[node+3].ival,&numarg);
2019 prewalk(0,level,ops[node+4].ival,&numarg);
2022 prewalk(0,level,ops[node+2].ival,&numarg);
2023 prewalk(0,level,ops[node+1].ival,&numarg);
2027 prewalk(0,level,ops[node+2].ival,&numarg);
2030 prewalk(0,level,ops[node+1].ival,&numarg);
2037 fatal("Garbage length in prewalk");
2038 prewalk(0,level,ops[node+1].ival,&numarg);
2039 for (i = 2; i<= len; i++) {
2040 prewalk(0,level,ops[node+i].ival,&numarg);
2045 *numericptr = numeric;
2050 numericize(register int node)
2058 type = ops[node].ival;
2061 if (type == OVAR && len == 1) {
2062 tmpstr=walk(0,0,ops[node+1].ival,&numarg,P_MIN);
2063 tmp2str = str_make("1");
2064 hstore(symtab,tmpstr->str_ptr,tmp2str);