Logo
~Apps~
~Projects~
~Contact~

a.y

Go to the documentation of this file.
00001 %{
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <ctype.h>
00005 #include <string.h>
00006 #include <math.h>
00007 #include <sys/types.h>
00008 #include <sys/stat.h>
00009 #include <unistd.h>
00010 
00011 #include "a.h"
00012 #include "Parser.h"
00013 
00014 
00015 %}
00016 
00017 %pure_parser
00018 
00019 %union {
00020   double d;
00021   char s[1024];
00022   VARIABLE *v;
00023   VARLIST vl;
00024   EXPRLIST el;
00025   INTLIST il;
00026 }
00027 
00028 %token <d> NUM
00029 %token <s> STRING
00030 %token <s> NUMVAR
00031 %token <s> STRINGVAR
00032 %token <s> IDENTIFIER
00033 %token <s> UNDEF
00034 
00035 %token LTEQ GTEQ NEQ
00036 
00037 %token End For Next
00038 %token <s> Data
00039 %token Input Del Dim Read
00040 
00041 %token Gr Text Pr In Call Plot Hlin Vlin
00042 %token Hgr2 Hgr Hcolor Hplot Draw Xdraw Htab Home
00043 %token Rot Scale Shload Trace Notrace Normal Inverse Flash
00044 %token Color Pop Vtab Himem Lomem Onerr Resume Recall
00045 %token Store Speed Let Goto Run If Restore Amper
00046 
00047 %token Gosub Return
00048 %token <s> Rem
00049 %token Stop On Wait Load Save
00050 
00051 %token Def Poke Print Cont List Clear Get New
00052 
00053 %token <s> Tab
00054 %token To Fn Spc Then At Not Step
00055 
00056 %token And Or
00057 %token Sgn Int Abs Usr Fre Scrn
00058 %token Pdl Pos Sqr Rnd Log Exp Cos Sin
00059 %token Tan Atn Peek Len Str Val Asc Chr
00060 %token Left Right Mid
00061 
00062 %left Or And 
00063 %right Not
00064 %left '<' '>' LTEQ GTEQ NEQ '='
00065 %left '+' '-'
00066 %left '*' '/' '^'
00067 %left UNARYMINUS
00068 
00069 %type <d> expr
00070 %type <s> string_expr
00071 %type <s> print_exprs
00072 %type <v> variable
00073 %type <v> string_variable
00074 %type <vl> var_list
00075 %type <el> expr_list
00076 %type <il> int_list
00077 
00078 %%
00079 
00080 line:
00081         statement
00082 ;
00083 
00084 statement:
00085         UNDEF
00086         {
00087                 fprintf(stderr,"LEX undefined char '%c' (0x%02x)\n",
00088                         isprint(*$1) ? *$1 : '.',*$1);
00089         }
00090 |
00091         End
00092         {
00093                 THIS -> m_next_line = NULL;
00094         }
00095 |
00096         For variable '=' expr To expr
00097         {
00098                 NEXT *nx;
00099 
00100                 for (nx = THIS -> m_nextbase; nx; nx = nx -> next)
00101                         if (nx -> ln == THIS -> m_current_line)
00102                         {
00103                                 THIS -> removelist(&THIS -> m_nextbase,nx);
00104                                 break;
00105                         }
00106                 if (!nx)
00107                         nx = new NEXT; //(NEXT *)malloc(sizeof(NEXT));
00108                 nx -> ln = THIS -> m_current_line;
00109                 nx -> var = $2;
00110                 $2 -> value = $4;
00111                 nx -> tolimit = $6;
00112                 nx -> step = 1;
00113                 nx -> reline = THIS -> m_next_line;
00114                 nx -> next = THIS -> m_nextbase;
00115                 THIS -> m_nextbase = nx;
00116 #ifdef DEBUG
00117 fprintf(stderr,"%s\n",THIS -> m_current_line -> the_str);
00118 #endif
00119         }
00120 |
00121         For variable '=' expr To expr Step expr
00122         {
00123                 NEXT *nx;
00124 
00125                 for (nx = THIS -> m_nextbase; nx; nx = nx -> next)
00126                         if (nx -> ln == THIS -> m_current_line)
00127                         {
00128                                 THIS -> removelist(&THIS -> m_nextbase,nx);
00129                                 break;
00130                         }
00131                 if (!nx)
00132                         nx = new NEXT; //(NEXT *)malloc(sizeof(NEXT));
00133                 nx -> ln = THIS -> m_current_line;
00134                 nx -> var = $2;
00135                 $2 -> value = $4;
00136                 nx -> tolimit = $6;
00137                 nx -> step = $8;
00138                 nx -> reline = THIS -> m_next_line;
00139                 nx -> next = THIS -> m_nextbase;
00140                 THIS -> m_nextbase = nx;
00141         }
00142 |
00143         Next
00144         {
00145                 NEXT *nx;
00146 
00147                 nx = THIS -> m_nextbase;
00148                 if (nx)
00149                 {
00150                 nx -> var -> value += nx -> step;
00151                 if (nx -> step > 0)
00152                 {
00153                         if (nx -> var -> value > nx -> tolimit + nx -> step / 2)
00154                         {
00155                                 THIS -> m_nextbase = nx -> next;
00156                                 delete nx; //free(nx);
00157                         }
00158                         else
00159                         {
00160                                 THIS -> m_next_line = nx -> reline;
00161                         }
00162                 }
00163                 else
00164                 {
00165                         if (nx -> var -> value < nx -> tolimit - nx -> step / 2)
00166                         {
00167                                 THIS -> m_nextbase = nx -> next;
00168                                 delete nx; //free(nx);
00169                         }
00170                         else
00171                         {
00172                                 THIS -> m_next_line = nx -> reline;
00173                         }
00174                 }
00175                 } // if (nx)
00176         }
00177 |
00178         Next var_list
00179         {
00180                 NEXT *nx;
00181                 int i;
00182 
00183                 nx = THIS -> m_nextbase;
00184                 i = 0; /* index in var_list */
00185                 while (nx && i < $2.qty)
00186                 {
00187                         nx -> var -> value += nx -> step;
00188                         if (nx -> step > 0)
00189                         {
00190                                 if (nx -> var -> value > nx -> tolimit)
00191                                 {
00192                                         THIS -> m_nextbase = nx -> next;
00193                                         delete nx; //free(nx);
00194                                         nx = THIS -> m_nextbase;
00195                                 }
00196                                 else
00197                                 {
00198                                         THIS -> m_next_line = nx -> reline;
00199                                         nx = NULL;
00200                                 }
00201                         }
00202                         else
00203                         {
00204                                 if (nx -> var -> value < nx -> tolimit)
00205                                 {
00206                                         THIS -> m_nextbase = nx -> next;
00207                                         delete nx; //free(nx);
00208                                         nx = THIS -> m_nextbase;
00209                                 }
00210                                 else
00211                                 {
00212                                         THIS -> m_next_line = nx -> reline;
00213                                         nx = NULL;
00214                                 }
00215                         }
00216                         i++; /* index in var_list */
00217                 }
00218         }
00219 |
00220         Data 
00221         {
00222                 DATA *d;
00223 
00224                 if (!THIS -> m_running)
00225                 {
00226 #ifdef DEBUGDATA
00227 fprintf(stderr,"Data '%s'\n",$1);
00228 #endif
00229                         d = new DATA; //(DATA *)malloc(sizeof(DATA));
00230                         d -> line = THIS -> m_current_line -> line;
00231                         d -> data = new char[strlen($1) + 1]; //(char *)malloc(strlen($1) + 1);
00232                         strcpy(d -> data,$1);
00233                         THIS -> addlist(&THIS -> m_database,d);
00234                 }
00235         }
00236 |
00237         Input var_list
00238         {
00239                 int i;
00240 
00241 /* check for file ops */
00242                 if (THIS -> m_running)
00243                 for (i = 0; i < $2.qty; i++)
00244                 {
00245 #ifdef DEBUG
00246 fprintf(stderr," * Input %d\n",i);
00247 #endif
00248                         if (THIS -> m_fil && THIS -> m_file_read)
00249                         {
00250                                 THIS -> file_input($2.var[i] -> svalue,1024);
00251                         }
00252                         else
00253                         {
00254                                 THIS -> inputstring($2.var[i] -> svalue,1024);
00255                         }
00256                         $2.var[i] -> value = atof($2.var[i] -> svalue);
00257                 }
00258         }
00259 |
00260         Input STRING ';' var_list
00261         {
00262                 int i;
00263 
00264 /* check for file ops */
00265                 if (THIS -> m_running)
00266                 {
00267 //                      THIS -> text_output($2);
00268                         THIS -> pprintf($2);
00269                         for (i = 0; i < $4.qty; i++)
00270                         {
00271 #ifdef DEBUG
00272 fprintf(stderr," * Input %d\n",i);
00273 #endif
00274                                 if (THIS -> m_fil && THIS -> m_file_read)
00275                                 {
00276                                         THIS -> file_input($4.var[i] -> svalue,1024);
00277                                 }
00278                                 else
00279                                 {
00280                                         THIS -> inputstring($4.var[i] -> svalue,1024);
00281                                 }
00282                                 $4.var[i] -> value = atof($4.var[i] -> svalue);
00283                         }
00284                 }
00285         }
00286 |
00287         Del
00288 |
00289         Dim var_list
00290         {
00291         }
00292 |
00293         Read var_list
00294         {
00295                 if (THIS -> m_running)
00296                 {
00297                         THIS -> read_data(&$2);
00298                 }
00299         }
00300 |
00301         Gr
00302 |
00303         Text
00304 |
00305         Call expr
00306 |
00307         Htab expr
00308 |
00309         Home
00310 |
00311         Normal
00312 |
00313         Inverse
00314 |
00315         Flash
00316 |
00317         Pop
00318         {
00319                 SUB *sub;
00320 
00321                 if (!THIS -> m_subbase)
00322                 {
00323 //                      fprintf(stderr," *** pop without gosub\n");
00324                         THIS -> m_next_line = THIS -> m_onerror;
00325                 }
00326                 else
00327                 {
00328                         sub = THIS -> m_subbase;
00329                         THIS -> m_subbase = sub -> next;
00330                         delete sub; //free(sub);
00331                 }
00332         }
00333 |
00334         Vtab expr
00335 |
00336         Onerr Goto NUM
00337         {
00338                 LINE *ln;
00339 
00340                 THIS -> m_onerror = NULL;
00341                 for (ln = THIS -> m_linebase; ln && !THIS -> m_onerror; ln = ln -> next)
00342                         if (ln -> line == $3)
00343                                 THIS -> m_onerror = ln;
00344         }
00345 |
00346         Speed expr
00347 |
00348         Let variable '=' expr
00349         {
00350                 $2 -> value = $4;
00351         }
00352 |
00353         Let string_variable '=' string_expr
00354         {
00355                 strcpy($2 -> svalue,$4);
00356         }
00357 |
00358         variable '=' expr
00359         {
00360                 $1 -> value = $3;
00361         }
00362 |
00363         string_variable '=' string_expr
00364         {
00365                 strcpy($1 -> svalue,$3);
00366         }
00367 |
00368         Goto NUM
00369         {
00370                 LINE *ln;
00371 
00372                 for (ln = THIS -> m_linebase; ln; ln = ln -> next)
00373                         if (ln -> line == (int)($2 + .01))
00374                                 break;
00375                 if (!ln)
00376                         fprintf(stderr,"GOTO undefined line number: %d\n",(int)($2 + .01));
00377                 THIS -> m_next_line = ln;
00378         }
00379 |
00380         If expr
00381         {
00382                 if (!$2 && THIS -> m_running)
00383                 {
00384                         while (THIS -> m_next_line -> line == -1 && THIS -> m_next_line -> next)
00385                                 THIS -> m_next_line = THIS -> m_next_line -> next;
00386                 }
00387         }
00388 |
00389         Then statement
00390 |
00391         Then NUM
00392         {
00393                 LINE *ln;
00394 
00395                 for (ln = THIS -> m_linebase; ln; ln = ln -> next)
00396                         if (ln -> line == (int)($2 + .01))
00397                                 break;
00398                 if (!ln)
00399                         fprintf(stderr,"GOTO undefined line number: %d\n",(int)$2);
00400                 THIS -> m_next_line = ln;
00401         }
00402 |
00403         Restore
00404         {
00405                 THIS -> m_current_data = THIS -> m_database;
00406                 THIS -> m_dataptr = 0;
00407         }
00408 |
00409         Restore NUM
00410         {
00411                 DATA *d;
00412 
00413                 for (d = THIS -> m_database; d; d = d -> next)
00414                         if (d -> line == (int)($2 + 0.01))
00415                                 break;
00416                 if (!d)
00417                         fprintf(stderr,"RESTORE undefined line number: %d\n",(int)$2);
00418                 THIS -> m_current_data = d ? d : THIS -> m_database;
00419                 THIS -> m_dataptr = 0;
00420         }
00421 |
00422         Gosub NUM
00423         {
00424                 LINE *ln;
00425                 SUB *sub;
00426 
00427                 for (ln = THIS -> m_linebase; ln; ln = ln -> next)
00428                         if (ln -> line == (int)$2)
00429                                 break;
00430                 if (!ln)
00431                         fprintf(stderr,"GOSUB undefined line number: %d\n",(int)$2);
00432                 sub = new SUB; //(SUB *)malloc(sizeof(SUB));
00433                 sub -> returnline = THIS -> m_next_line;
00434                 sub -> next = THIS -> m_subbase;
00435                 THIS -> m_subbase = sub;
00436                 THIS -> m_next_line = ln;
00437         }
00438 |
00439         Return
00440         {
00441                 SUB *sub;
00442 
00443                 if (!THIS -> m_subbase)
00444                 {
00445 //                      fprintf(stderr," *** return without gosub\n");
00446                         THIS -> m_next_line = THIS -> m_onerror;
00447                 }
00448                 else
00449                 {
00450                         sub = THIS -> m_subbase;
00451                         THIS -> m_subbase = sub -> next;
00452                         THIS -> m_next_line = sub -> returnline;
00453                         delete sub; //free(sub);
00454                 }
00455         }
00456 |
00457         Rem
00458         {
00459 //              fprintf(stderr,"Remark '%s'\n",$1);
00460         }
00461 |
00462         Stop
00463 |
00464         On expr Goto int_list
00465         {
00466                 LINE *ln;
00467 
00468 #ifdef DEBUG
00469 fprintf(stderr," ON %d GOTO ...\n",(int)$2);
00470 #endif
00471                 if ( (int)$2 > 0 && (int)$2 <= $4.qty)
00472                 {
00473                         for (ln = THIS -> m_linebase; ln; ln = ln -> next)
00474                                 if (ln -> line == $4.values[(int)$2 - 1])
00475                                         break;
00476                         if (!ln && THIS -> m_running)
00477                                 fprintf(stderr,"ON ... GOTO undefined line number: %d\n",(int)$2);
00478                         THIS -> m_next_line = ln;
00479                 }
00480                 else
00481                 {
00482                         if (THIS -> m_running)
00483                                 fprintf(stderr,"ON ... GOTO outside bounds\n");
00484                         THIS -> m_next_line = NULL;
00485                 }
00486         }
00487 |
00488         On expr Gosub int_list
00489         {
00490                 LINE *ln;
00491                 SUB *sub;
00492 
00493 #ifdef DEBUG
00494 fprintf(stderr," ON %d GOSUB ...\n",(int)$2);
00495 #endif
00496                 if ( (int)$2 > 0 && (int)$2 <= $4.qty)
00497                 {
00498                         for (ln = THIS -> m_linebase; ln; ln = ln -> next)
00499                                 if (ln -> line == $4.values[(int)$2 - 1])
00500                                         break;
00501                         if (!ln && THIS -> m_running)
00502                                 fprintf(stderr,"ON ... GOTO undefined line number: %d\n",(int)$2);
00503                         sub = new SUB; //(SUB *)malloc(sizeof(SUB));
00504                         sub -> returnline = THIS -> m_next_line;
00505                         sub -> next = THIS -> m_subbase;
00506                         THIS -> m_subbase = sub;
00507                         THIS -> m_next_line = ln;
00508                 }
00509                 else
00510                 {
00511                         if (THIS -> m_running)
00512                                 fprintf(stderr,"ON ... GOSUB outside bounds\n");
00513                         THIS -> m_next_line = NULL;
00514                 }
00515         }
00516 |
00517         Def Fn IDENTIFIER '(' IDENTIFIER ')' '=' expr
00518         {
00519                 FUNC *fn;
00520                 char temp[1024];
00521 
00522                 for (fn = THIS -> m_funcbase; fn; fn = fn -> next)
00523                         if (!strcmp(fn -> name,$3))
00524                                 break;
00525                 if (!fn)
00526                 {
00527                         fn = new FUNC; //(FUNC *)malloc(sizeof(FUNC));
00528                         strcpy(fn -> name,$3);
00529                         THIS -> addlist(&THIS -> m_funcbase,fn);
00530                 }
00531                 sprintf(temp,"%s",$5);
00532                 fn -> placeholder = THIS -> reg_variable(temp,NULL);
00533                 fn -> fnline = THIS -> m_current_line;
00534         }
00535 |
00536         Poke expr ',' expr
00537         {
00538                 THIS -> poke($2,$4);
00539         }
00540 |
00541         Print
00542         {
00543                 if (THIS -> m_running)
00544                 {
00545                         THIS -> pprintf("\n");
00546                         THIS -> m_tab_pos = 0;
00547                 }
00548         }
00549 |
00550         Print print_exprs
00551         {
00552                 if (THIS -> m_running)
00553                 {
00554                 char temp[1024];
00555                 int i = 0;
00556 
00557                 while (strlen($2 + i) > 40)
00558                 {
00559                         strncpy(temp,$2 + i,40);
00560                         temp[40] = 0;
00561                         THIS -> pprintf("%s\n",temp);
00562                         i += 40;
00563                 }
00564                 THIS -> pprintf("%s\n",$2 + i);
00565                 THIS -> m_tab_pos = 0;
00566                 }
00567         }
00568 |
00569         Print print_exprs ';'
00570         {
00571                 if (THIS -> m_running)
00572                 {
00573                 char temp[1024];
00574                 int i = 0;
00575 
00576                 while (strlen($2 + i) > 40)
00577                 {
00578                         strncpy(temp,$2 + i,40);
00579                         temp[40] = 0;
00580                         THIS -> pprintf("%s\n",temp);
00581                         i += 40;
00582                 }
00583                 THIS -> pprintf($2 + i);
00584                 THIS -> m_tab_pos = strlen($2 + i);
00585                 }
00586         }
00587 |
00588         List int_list
00589 |
00590         Get string_variable
00591         {
00592                 if (THIS -> m_running)
00593                 {
00594                         if (THIS -> m_fil && THIS -> m_file_read)
00595                         {
00596                                 fread($2 -> svalue,1,1,THIS -> m_fil);
00597                         }
00598                         else
00599                         {
00600                                 THIS -> inputstring($2 -> svalue,1);
00601                         }
00602                         $2 -> svalue[1] = 0;
00603                 }
00604         }
00605 
00606 
00607 
00608 expr:
00609         NUM
00610         {
00611 #ifdef DEBUGEXPR
00612 fprintf(stderr," *-* expr: NUM(%.1f)\n",$1);
00613 #endif
00614                 $$ = $1;
00615         }
00616 |
00617         variable
00618         {
00619 #ifdef DEBUGEXPR
00620 fprintf(stderr," *-* expr: variable(%s)\n",$1 -> name);
00621 #endif
00622                 $$ = $1 -> value;
00623         }
00624 |
00625         '-' expr %prec UNARYMINUS
00626         {
00627 #ifdef DEBUGEXPR
00628 fprintf(stderr," *-* expr: - expr\n");
00629 #endif
00630                 $$ = -$2;
00631         }
00632 |
00633         expr '+' expr
00634         {
00635 #ifdef DEBUGEXPR
00636 fprintf(stderr," *-* expr: expr + expr\n");
00637 #endif
00638                 $$ = $1 + $3;
00639         }
00640 |
00641         expr '-' expr
00642         {
00643 #ifdef DEBUGEXPR
00644 fprintf(stderr," *-* expr: expr - expr\n");
00645 #endif
00646                 $$ = $1 - $3;
00647         }
00648 |
00649         expr '*' expr
00650         {
00651 #ifdef DEBUGEXPR
00652 fprintf(stderr," *-* expr: expr * expr\n");
00653 #endif
00654                 $$ = $1 * $3;
00655         }
00656 |
00657         expr '/' expr
00658         {
00659 #ifdef DEBUGEXPR
00660 fprintf(stderr," *-* expr: expr / expr\n");
00661 #endif
00662                 $$ = $1 / $3;
00663         }
00664 |
00665         expr '<' expr
00666         {
00667 #ifdef DEBUGEXPR
00668 fprintf(stderr," *-* expr: expr < expr\n");
00669 #endif
00670                 $$ = $1 < $3;
00671         }
00672 |
00673         expr '>' expr
00674         {
00675 #ifdef DEBUGEXPR
00676 fprintf(stderr," *-* expr: expr > expr\n");
00677 #endif
00678                 $$ = $1 > $3;
00679         }
00680 |
00681         expr LTEQ expr
00682         {
00683 #ifdef DEBUGEXPR
00684 fprintf(stderr," *-* expr: expr <= expr\n");
00685 #endif
00686                 $$ = $1 <= $3;
00687         }
00688 |
00689         expr GTEQ expr
00690         {
00691 #ifdef DEBUGEXPR
00692 fprintf(stderr," *-* expr: expr >= expr\n");
00693 #endif
00694                 $$ = $1 >= $3;
00695         }
00696 |
00697         expr NEQ expr
00698         {
00699 #ifdef DEBUGEXPR
00700 fprintf(stderr," *-* expr: expr != expr\n");
00701 #endif
00702                 $$ = $1 != $3;
00703         }
00704 |
00705         expr '=' expr
00706         {
00707 #ifdef DEBUGEXPR
00708 fprintf(stderr," *-* expr: expr == expr\n");
00709 #endif
00710                 $$ = $1 == $3;
00711         }
00712 |
00713         string_expr '<' string_expr
00714         {
00715 #ifdef DEBUGEXPR
00716 fprintf(stderr," *-* expr: string_expr < string_expr\n");
00717 #endif
00718                 $$ = (strcmp($1,$3) < 0) ? 1 : 0;
00719         }
00720 |
00721         string_expr '>' string_expr
00722         {
00723 #ifdef DEBUGEXPR
00724 fprintf(stderr," *-* expr: string_expr > string_expr\n");
00725 #endif
00726                 $$ = (strcmp($1,$3) > 0) ? 1 : 0;
00727         }
00728 |
00729         string_expr LTEQ string_expr
00730         {
00731 #ifdef DEBUGEXPR
00732 fprintf(stderr," *-* expr: string_expr <= string_expr\n");
00733 #endif
00734                 $$ = (strcmp($1,$3) <= 0) ? 1 : 0;
00735         }
00736 |
00737         string_expr GTEQ string_expr
00738         {
00739 #ifdef DEBUGEXPR
00740 fprintf(stderr," *-* expr: string_expr >= string_expr\n");
00741 #endif
00742                 $$ = (strcmp($1,$3) >= 0) ? 1 : 0;
00743         }
00744 |
00745         string_expr NEQ string_expr
00746         {
00747 #ifdef DEBUGEXPR
00748 fprintf(stderr," *-* expr: string_expr != string_expr\n");
00749 #endif
00750                 $$ = strcmp($1,$3) ? 1 : 0;
00751         }
00752 |
00753         string_expr '=' string_expr
00754         {
00755 #ifdef DEBUGEXPR
00756 fprintf(stderr," *-* expr: string_expr == string_expr\n");
00757 #endif
00758                 $$ = !strcmp($1,$3);
00759         }
00760 |
00761         expr Or expr
00762         {
00763 #ifdef DEBUGEXPR
00764 fprintf(stderr," *-* expr: expr || expr\n");
00765 #endif
00766                 $$ = $1 || $3;
00767         }
00768 |
00769         expr And expr
00770         {
00771 #ifdef DEBUGEXPR
00772 fprintf(stderr," *-* expr: expr && expr\n");
00773 #endif
00774                 $$ = $1 && $3;
00775         }
00776 |
00777         Abs '(' expr ')'
00778         {
00779 #ifdef DEBUGEXPR
00780 fprintf(stderr," *-* expr: ABS(expr)\n");
00781 #endif
00782                 if ($3 < 0)
00783                         $$ = -$3;
00784                 else
00785                         $$ = $3;
00786         }
00787 |
00788         Asc '(' string_expr ')'
00789         {
00790 #ifdef DEBUGEXPR
00791 fprintf(stderr," *-* expr: ASC(string_expr)\n");
00792 #endif
00793                 $$ = *$3;
00794         }
00795 |
00796         Atn '(' expr ')'
00797         {
00798 #ifdef DEBUGEXPR
00799 fprintf(stderr," *-* expr: ATN(expr)\n");
00800 #endif
00801                 $$ = atan($3);
00802         }
00803 |
00804         Int '(' expr ')'
00805         {
00806 #ifdef DEBUGEXPR
00807 fprintf(stderr," *-* expr: INT(expr)\n");
00808 #endif
00809                 $$ = (long)$3;
00810         }
00811 |
00812         Val '(' string_expr ')'
00813         {
00814 #ifdef DEBUGEXPR
00815 fprintf(stderr," *-* expr: VAL(string_expr)\n");
00816 #endif
00817                 $$ = atof($3);
00818         }
00819 |
00820         Rnd '(' expr ')'
00821         {
00822 #ifdef DEBUGEXPR
00823 fprintf(stderr," *-* expr: RND(expr)\n");
00824 #endif
00825                 $$ = (double)rand();
00826                 $$ /= RAND_MAX;
00827         }
00828 |
00829         '(' expr ')'
00830         {
00831 #ifdef DEBUGEXPR
00832 fprintf(stderr," *-* expr: (expr)\n");
00833 #endif
00834                 $$ = $2;
00835         }
00836 |
00837         Not expr
00838         {
00839 #ifdef DEBUGEXPR
00840 fprintf(stderr," *-* expr: NOT expr\n");
00841 #endif
00842                 $$ = !$2;
00843         }
00844 |
00845         Fn IDENTIFIER '(' expr ')'
00846         {
00847                 FUNC *fn;
00848                 int i;
00849                 char temp[1024];
00850 
00851 #ifdef DEBUGEXPR
00852 fprintf(stderr," *-* expr: FN ... ( expr )\n");
00853 #endif
00854 fprintf(stderr,"LINE: '%s'\n",THIS -> m_the_str);
00855 fprintf(stderr,"REST: '%s'\n",THIS -> m_the_str + THIS -> m_the_ptr);
00856                 for (fn = THIS -> m_funcbase; fn; fn = fn -> next)
00857                         if (!strcmp(fn -> name,$2))
00858                                 break;
00859                 if (fn && fn -> fnline)
00860                 {
00861 fprintf(stderr,"Fn found\n");
00862 fflush(stderr);
00863                         fn -> placeholder -> fnvalue = fn -> placeholder -> value;
00864 fprintf(stderr," ... 1 function line '%s'\n",fn -> fnline -> the_str);
00865 fflush(stderr);
00866                         fn -> placeholder -> value = $4;
00867 fprintf(stderr," ... 2\n");
00868 fflush(stderr);
00869                         THIS -> m_placeholder = fn -> placeholder;
00870 fprintf(stderr," ... 3\n");
00871 fflush(stderr);
00872                         i = 0;
00873 #define FC fn -> fnline -> the_str[i]
00874                         while (FC && FC != '=')
00875                                 i++;
00876 fprintf(stderr," ... 4\n");
00877 fflush(stderr);
00878                         if (FC == '=')
00879                         {
00880 fprintf(stderr,"BEFORE: '%s'\n",THIS -> m_the_str);
00881                                 sprintf(temp,"*%s%s",fn -> fnline -> the_str + i + 1,THIS -> m_the_str + THIS -> m_the_ptr);
00882 fprintf(stderr," ... 5\n");
00883 fflush(stderr);
00884                                 strcpy(THIS -> m_the_str + THIS -> m_the_ptr,temp);
00885 fprintf(stderr," ... 6\n");
00886 fflush(stderr);
00887 fprintf(stderr," AFTER: '%s'\n",THIS -> m_the_str);
00888                         }
00889 fprintf(stderr," ... 7\n");
00890 fflush(stderr);
00891                         $$ = 1;
00892                 }
00893                 else
00894                 {
00895 fprintf(stderr,"Fn not found\n");
00896 fflush(stderr);
00897                         fprintf(stderr,"Using Undefined Function '%s'\n",$2);
00898                         THIS -> m_next_line = THIS -> m_onerror;
00899                         $$ = 0;
00900                 }
00901 //fprintf(stderr,"REPL: '%s'\n",fn -> fnline -> the_str);
00902         }
00903 |
00904         Len '(' string_expr ')'
00905         {
00906 #ifdef DEBUGEXPR
00907 fprintf(stderr," *-* expr: LEN(string_expr)\n");
00908 #endif
00909                 $$ = strlen($3);
00910         }
00911 |
00912         Peek '(' expr ')'
00913         {
00914 #ifdef DEBUGEXPR
00915 fprintf(stderr," *-* expr: PEEK(expr)\n");
00916 #endif
00917                 $$ = THIS -> peek($3);
00918 #ifdef DEBUGMEM
00919 fprintf(stderr,"peek(%d) returns %d\n",(int)$3,(int)$$);
00920 #endif
00921         }
00922 |
00923         Fre '(' expr ')'
00924         {
00925                 $$ = 32000;
00926         }
00927 
00928 string_expr:
00929         STRING
00930 |
00931         string_variable
00932         {
00933                 strcpy($$,$1 -> svalue);
00934         }
00935 |
00936         Mid '(' string_expr ',' expr ')'
00937         {
00938                 if ($5 <= strlen($3))
00939                 {
00940                         strcpy($$,$3 + (int)$5 - 1);
00941                 }
00942                 else
00943                         *$$ = 0;
00944         }
00945 |
00946         Mid '(' string_expr ',' expr ',' expr ')'
00947         {
00948                 if ($5 <= strlen($3))
00949                 {
00950                         strcpy($$,$3 + (int)$5 - 1);
00951                         $$[ (int)$7] = 0;
00952                 }
00953                 else
00954                         *$$ = 0;
00955         }
00956 |
00957         Left '(' string_expr ',' expr ')'
00958         {
00959                 if ($5 > 0)
00960                 {
00961                         strncpy($$,$3, (int)$5);
00962                         $$[(int)$5] = 0;
00963                 }
00964         }
00965 |
00966         Right '(' string_expr ',' expr ')'
00967         {
00968                 if (strlen($3) > $5)
00969                         strcpy($$,$3 + strlen($3) - (int)($5 + 0.01));
00970                 else
00971                         strcpy($$,$3);
00972         }
00973 |
00974         Chr '(' expr ')'
00975         {
00976                 *$$ = (char)$3;
00977                 $$[1] = 0;
00978         }
00979 |
00980         string_expr '+' string_expr
00981         {
00982                 sprintf($$,"%s%s",$1,$3);
00983         }
00984 |
00985         Str '(' expr ')'
00986         {
00987                 sprintf($$,"%f",$3);
00988                 while ($$[strlen($$) - 1] == '0')
00989                         $$[strlen($$) - 1] = 0;
00990                 if ($$[strlen($$) - 1] == '.')
00991                         $$[strlen($$) - 1] = 0;
00992         }
00993 |
00994         Tab '(' expr ')'
00995         {
00996                 *$$ = 0;
00997                 while (THIS -> m_tab_pos < $3)
00998                 {
00999                         strcat($$," ");
01000                         THIS -> m_tab_pos++;
01001                 }
01002         }
01003 |
01004         Spc '(' expr ')'
01005         {
01006                 int i;
01007 
01008                 *$$ = 0;
01009                 for (i = 0; i < $3; i++)
01010                         strcat($$," ");
01011         }
01012 |
01013         '(' string_expr ')'
01014         {
01015                 strcpy($$, $2);
01016         }
01017 
01018 int_list:
01019         NUM
01020         {
01021                 $$.qty = 0;
01022                 $$.values[$$.qty++] = (long)$1;
01023         }
01024 |
01025         int_list ',' NUM
01026         {
01027                 $$ = $1;
01028                 $$.values[$$.qty++] = (long)$3;
01029         }
01030 
01031 var_list:
01032         variable
01033         {
01034                 $$.qty = 0;
01035                 $$.var[$$.qty++] = $1;
01036         }
01037 |
01038         string_variable
01039         {
01040                 $$.qty = 0;
01041                 $$.var[$$.qty++] = $1;
01042         }
01043 |
01044         var_list ',' variable
01045         {
01046                 $$ = $1;
01047                 $$.var[$$.qty++] = $3;
01048         }
01049 |
01050         var_list ',' string_variable
01051         {
01052                 $$ = $1;
01053                 $$.var[$$.qty++] = $3;
01054         }
01055 
01056 variable:
01057         NUMVAR
01058         {
01059                 $$ = THIS -> reg_variable($1,NULL);
01060         }
01061 |
01062         IDENTIFIER
01063         {
01064                 $$ = THIS -> reg_variable($1,NULL);
01065         }
01066 |
01067         NUMVAR '(' expr_list ')'
01068         {
01069                 $$ = THIS -> reg_variable($1,&$3);
01070         }
01071 |
01072         IDENTIFIER '(' expr_list ')'
01073         {
01074                 $$ = THIS -> reg_variable($1,&$3);
01075         }
01076 
01077 string_variable:
01078         STRINGVAR 
01079         {
01080                 $$ = THIS -> reg_variable($1,NULL);
01081         }
01082 |
01083         STRINGVAR '(' expr_list ')'
01084         {
01085                 $$ = THIS -> reg_variable($1,&$3);
01086         }
01087 
01088 expr_list:
01089         expr
01090         {
01091                 $$.qty = 0;
01092                 $$.values[$$.qty++] = $1;
01093         }
01094 |
01095         expr_list ',' expr
01096         {
01097                 $$ = $1;
01098                 $$.values[$$.qty++] = $3;
01099         }
01100 
01101 print_exprs:
01102         expr
01103         {
01104                 char slask[256];
01105 
01106                 sprintf(slask,"%f",$1);
01107                 while (slask[strlen(slask) - 1] == '0')
01108                         slask[strlen(slask) - 1] = 0;
01109                 if (slask[strlen(slask) - 1] == '.')
01110                         slask[strlen(slask) - 1] = 0;
01111                 strcpy($$,slask);
01112                 THIS -> m_tab_pos = strlen($$);
01113         }
01114 |
01115         string_expr
01116         {
01117                 strcpy($$,$1);
01118                 THIS -> m_tab_pos = strlen($$);
01119         }
01120 |
01121         print_exprs print_separator expr
01122         {
01123                 char slask[256];
01124 
01125                 sprintf(slask,"%f",$3);
01126                 while (slask[strlen(slask) - 1] == '0')
01127                         slask[strlen(slask) - 1] = 0;
01128                 if (slask[strlen(slask) - 1] == '.')
01129                         slask[strlen(slask) - 1] = 0;
01130                 sprintf($$,"%s%s",$1,slask);
01131                 THIS -> m_tab_pos = strlen($$);
01132         }
01133 |
01134         print_exprs print_separator string_expr
01135         {
01136                 sprintf($$,"%s%s",$1,$3);
01137                 THIS -> m_tab_pos = strlen($$);
01138         }
01139 
01140 print_separator:
01141 |
01142         ';'
01143 |
01144         ','
01145 
01146 %%
01147 
01148 void yyerror(void *parser,char *s)
01149 {
01150         int i;
01151 
01152         if (THIS -> m_pclParserIO)
01153         {
01154                 THIS -> pprintf("******** error in parser, program file '");
01155                 THIS -> pprintf("%s'\n", THIS -> m_programname );
01156                 THIS -> pprintf("%s\n", THIS -> m_the_str);
01157                 for (i = 0; i < THIS -> m_the_ptr; i++)
01158                         THIS -> pprintf(" ");
01159                 THIS -> pprintf("^ %s\n", s);
01160                 THIS -> pprintf("******** program aborted ********\n");
01161         }
01162         fprintf(stderr,"Error in '%s':\n",THIS -> m_programname);
01163         fprintf(stderr,"%s\n",THIS -> m_the_str);
01164         for (i = 0; i < THIS -> m_the_ptr; i++)
01165                 fprintf(stderr," ");
01166         fprintf(stderr,"^ %s\n",s);
01167         fflush(stderr);
01168         if (!THIS -> m_pclParserIO)
01169         {
01170 //              exit(-1);
01171         }
01172 }
01173 
Page, code, and content Copyright (C) 2005 by Anders Hedström
Generated on Mon Aug 29 20:21:47 2005 for C++ Sockets by  doxygen 1.4.4