[perl #69838] a2p generates code using $[
Zefram [Thu, 22 Oct 2009 22:27:57 +0000 (00:27 +0200)]
x2p/a2p.h
x2p/a2py.c
x2p/walk.c

index 6b6004c..3a01df5 100644 (file)
--- a/x2p/a2p.h
+++ b/x2p/a2p.h
@@ -433,7 +433,6 @@ EXT STR str_yes;
 
 EXT bool do_split INIT(FALSE);
 EXT bool split_to_array INIT(FALSE);
-EXT bool set_array_base INIT(FALSE);
 EXT bool saw_RS INIT(FALSE);
 EXT bool saw_OFS INIT(FALSE);
 EXT bool saw_ORS INIT(FALSE);
index 84fdc48..286f7cc 100644 (file)
@@ -428,7 +428,7 @@ yylex(void)
        }
        for (d = s; isALPHA(*s) || isDIGIT(*s) || *s == '_'; )
            s++;
-       split_to_array = set_array_base = TRUE;
+       split_to_array = TRUE;
        if (d != s)
        {
            yylval = string(d,s-d);
@@ -464,8 +464,6 @@ yylex(void)
 
     case 'a': case 'A':
        SNARFWORD;
-       if (strEQ(d,"ARGC"))
-           set_array_base = TRUE;
        if (strEQ(d,"ARGV")) {
            yylval=numary(string("ARGV",0));
            XOP(VAR);
@@ -598,7 +596,6 @@ yylex(void)
        if (strEQ(d,"in"))
            XTERM(IN);
        if (strEQ(d,"index")) {
-           set_array_base = TRUE;
            XTERM(INDEX);
        }
        if (strEQ(d,"int")) {
@@ -644,7 +641,6 @@ yylex(void)
     case 'm': case 'M':
        SNARFWORD;
        if (strEQ(d,"match")) {
-           set_array_base = TRUE;
            XTERM(MATCH);
        }
        if (strEQ(d,"m"))
@@ -653,7 +649,7 @@ yylex(void)
     case 'n': case 'N':
        SNARFWORD;
        if (strEQ(d,"NF"))
-           do_chop = do_split = split_to_array = set_array_base = TRUE;
+           do_chop = do_split = split_to_array = TRUE;
        if (strEQ(d,"next")) {
            saw_line_op = TRUE;
            XTERM(NEXT);
@@ -719,11 +715,9 @@ yylex(void)
     case 's': case 'S':
        SNARFWORD;
        if (strEQ(d,"split")) {
-           set_array_base = TRUE;
            XOP(SPLIT);
        }
        if (strEQ(d,"substr")) {
-           set_array_base = TRUE;
            XTERM(SUBSTR);
        }
        if (strEQ(d,"sub"))
@@ -1204,7 +1198,6 @@ numary(int arg)
     str_cat(key,"[]");
     hstore(symtab,key->str_ptr,str_make("1"));
     str_free(key);
-    set_array_base = TRUE;
     return arg;
 }
 
index d3d5f91..2890375 100644 (file)
@@ -84,11 +84,6 @@ walk(int useval, int level, register int node, int *numericptr, int minprec)
        str = walk(0,level,ops[node+1].ival,&numarg,P_MIN);
        if (do_split && need_entire && !absmaxfld)
            split_to_array = TRUE;
-       if (do_split && split_to_array)
-           set_array_base = TRUE;
-       if (set_array_base) {
-           str_cat(str,"$[ = 1;\t\t\t# set array base to 1\n");
-       }
        if (fswitch && !const_FS)
            const_FS = fswitch;
        if (saw_FS > 1 || saw_RS)
@@ -650,9 +645,10 @@ sub Pick {\n\
        str_set(str,"substr(");
        str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_COMMA+1));
        str_free(fstr);
-       str_cat(str,", ");
+       str_cat(str,", (");
        str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,P_COMMA+1));
        str_free(fstr);
+       str_cat(str,")-1");
        if (len == 3) {
            str_cat(str,", ");
            str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg,P_COMMA+1));
@@ -712,13 +708,13 @@ sub Pick {\n\
        break;
     case OINDEX:
        str = str_new(0);
-       str_set(str,"index(");
+       str_set(str,"(1+index(");
        str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_COMMA+1));
        str_free(fstr);
        str_cat(str,", ");
        str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,P_COMMA+1));
        str_free(fstr);
-       str_cat(str,")");
+       str_cat(str,"))");
        numeric = 1;
        break;
     case OMATCH:
@@ -970,12 +966,12 @@ sub Pick {\n\
            }
            else if (strEQ(str->str_ptr,"$NF")) {
                numeric = 1;
-               str_set(str,"$#Fld");
+               str_set(str,"($#Fld+1)");
            }
            else if (strEQ(str->str_ptr,"$0"))
                str_set(str,"$_");
            else if (strEQ(str->str_ptr,"$ARGC"))
-               str_set(str,"($#ARGV+1)");
+               str_set(str,"($#ARGV+2)");
        }
        else {
 #ifdef NOTDEF
@@ -988,7 +984,7 @@ sub Pick {\n\
            str_cat(tmpstr,"[]");
            tmp2str = hfetch(symtab,tmpstr->str_ptr);
            if (tmp2str && atoi(tmp2str->str_ptr))
-               str_cat(str,"[");
+               str_cat(str,"[(");
            else
                str_cat(str,"{");
            str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,P_MIN));
@@ -999,7 +995,7 @@ sub Pick {\n\
            }
            else {
                if (tmp2str && atoi(tmp2str->str_ptr))
-                   strcpy(tokenbuf,"]");
+                   strcpy(tokenbuf,")-1]");
                else
                    strcpy(tokenbuf,"}");
                *tokenbuf += (char)128;
@@ -1012,10 +1008,10 @@ sub Pick {\n\
        str = str_new(0);
        if (split_to_array) {
            str_set(str,"$Fld");
-           str_cat(str,"[");
+           str_cat(str,"[(");
            str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN));
            str_free(fstr);
-           str_cat(str,"]");
+           str_cat(str,")-1]");
        }
        else {
            i = atoi(walk(1,level,ops[node+1].ival,&numarg,P_MIN)->str_ptr);
@@ -1470,7 +1466,7 @@ sub Pick {\n\
        tmp2str = hfetch(symtab,str->str_ptr);
        if (tmp2str && atoi(tmp2str->str_ptr)) {
            sprintf(tokenbuf,
-             "foreach %s ($[ .. $#%s) ",
+             "foreach %s (0 .. $#%s) ",
              s,
              d+1);
        }