comparison mc-parse.c @ 28:c6994794f084

functional macro (imcomplete)
author kono
date Sat, 08 Feb 2003 00:58:04 +0900
parents b7a94557bf92
children 160e20394f80
comparison
equal deleted inserted replaced
27:af0c69eaa433 28:c6994794f084
83 static int typeid(int s); 83 static int typeid(int s);
84 static int typename(void); 84 static int typename(void);
85 static int typespec(void); 85 static int typespec(void);
86 static int cexpr(int e); 86 static int cexpr(int e);
87 static void code_decl(NMTBL *n); 87 static void code_decl(NMTBL *n);
88 static int macro_args();
89 static void fmacro();
90 static void local_define();
91 static void local_undef();
92 static void replace_macro();
88 93
89 extern void display_ntable(NMTBL *n, char *s); 94 extern void display_ntable(NMTBL *n, char *s);
90 extern void closing(void); 95 extern void closing(void);
91 extern void opening(char *filename); 96 extern void opening(char *filename);
92 extern void gen_gdecl(char *n, int gpc); 97 extern void gen_gdecl(char *n, int gpc);
1786 getsym(); 1791 getsym();
1787 break; 1792 break;
1788 case REGISTER: 1793 case REGISTER:
1789 e1=list2(REGISTER,nptr->dsp); 1794 e1=list2(REGISTER,nptr->dsp);
1790 type=nptr->ty; 1795 type=nptr->ty;
1796 getsym();
1797 break;
1798 case FMACRO:
1799 fmacro();
1800 ch = *chptr;
1791 getsym(); 1801 getsym();
1792 break; 1802 break;
1793 case FLABEL: case BLABEL: 1803 case FLABEL: case BLABEL:
1794 case FUNCTION: case CODE: 1804 case FUNCTION: case CODE:
1795 e1=list2(FNAME,(int)nptr); 1805 e1=list2(FNAME,(int)nptr);
2535 void 2545 void
2536 macro_processing() 2546 macro_processing()
2537 { 2547 {
2538 int i; 2548 int i;
2539 int c; 2549 int c;
2550 int args;
2540 int mode_save; 2551 int mode_save;
2541 2552
2542 ++chptr; 2553 ++chptr;
2543 if (macroeq("ifdef") || macroeq("ifndef")) { 2554 if (macroeq("ifdef") || macroeq("ifndef")) {
2544 c = (chptr[-3]=='n'); 2555 c = (chptr[-4]=='n');
2545 macro_if_current++; 2556 macro_if_current++;
2546 if (!macro_if_skip) { 2557 if (!macro_if_skip) {
2547 mode_save = mode; mode = IFDEF; 2558 mode_save = mode; mode = IFDEF;
2548 ch= *chptr; 2559 ch= *chptr;
2549 i = getsym(); 2560 i = getsym();
2589 if (macroeq("define")) { 2600 if (macroeq("define")) {
2590 i=mode; 2601 i=mode;
2591 mode=GDECL; 2602 mode=GDECL;
2592 ch= *chptr; 2603 ch= *chptr;
2593 if (getsym() == IDENT) { 2604 if (getsym() == IDENT) {
2594 if (nptr->sc == EMPTY) { 2605 if (nptr->sc != EMPTY) { error(MCERR);
2606 } else if (ch=='(') {
2607 nptr->sc = FMACRO;
2608 args = macro_args();
2609 nptr->dsp = glist2((int)cheapp,args);
2610 while ((*cheapp++ = c = *chptr++)
2611 && c != '\n');
2612 *cheapp++ = '\0';
2613 #if 0
2614 fprintf(stderr,"macro function: %s\n",nptr->nm);
2615 i = 0;
2616 while(args) {
2617 fprintf(stderr,"macro args %d: %s\n",i++,(char *) car(args));
2618 args = cadr(args);
2619 }
2620 fprintf(stderr,"macro body: %s\n",(char *)car(nptr->dsp));
2621 #endif
2622 } else {
2595 nptr->sc = MACRO; 2623 nptr->sc = MACRO;
2596 nptr->dsp = (int)cheapp; 2624 nptr->dsp = (int)cheapp;
2597 while ((*cheapp++ = c = *chptr++) 2625 while ((*cheapp++ = c = *chptr++)
2598 && c != '\n'); 2626 && c != '\n');
2599 *cheapp++ = '\0'; 2627 *cheapp++ = '\0';
2600 if (cheapp >= cheap+CHEAPSIZE) 2628 }
2601 error(STRERR); 2629 if (cheapp >= cheap+CHEAPSIZE) /* too late? */
2602 /* if (!c) error(EOFERR); ??? #define hoge only case */ 2630 error(STRERR);
2603 } else error(MCERR);
2604 } else error(MCERR); 2631 } else error(MCERR);
2605 mode=i; 2632 mode=i;
2606 *(chptr = linebuf) = '\0'; 2633 *(chptr = linebuf) = '\0';
2607 } else if (macroeq("undef")) { 2634 } else if (macroeq("undef")) {
2608 i=mode; 2635 i=mode;
2609 mode=GDECL; 2636 mode=LDECL;
2610 ch= *chptr; 2637 ch= *chptr;
2611 if (getsym() == IDENT) { 2638 if (getsym() == IDENT) {
2612 if (nptr->sc == MACRO) { 2639 if (nptr->sc == MACRO) {
2640 nptr->sc = EMPTY;
2641 } else if (nptr->sc == LMACRO) {
2613 nptr->sc = EMPTY; 2642 nptr->sc = EMPTY;
2614 } else error(MCERR); 2643 } else error(MCERR);
2615 } 2644 }
2616 mode=i; 2645 mode=i;
2617 } else if (macroeq("include")) { 2646 } else if (macroeq("include")) {
2645 for (p = chptr; *s;) if (*s++ != *p++) return 0; 2674 for (p = chptr; *s;) if (*s++ != *p++) return 0;
2646 chptr = p; 2675 chptr = p;
2647 return 1; 2676 return 1;
2648 } 2677 }
2649 2678
2679 int
2680 macro_args()
2681 {
2682 int c;
2683 int in_quote = 0;
2684 int in_wquote = 0;
2685 int plevel = 0;
2686 int args = glist2((int)cheapp,0);
2687 for(;;) {
2688 *cheapp++ = c = *chptr++;
2689 if (in_quote) {
2690 if (c=='\\') {
2691 if (*chptr != '\n') {
2692 *cheapp++ = *chptr++;
2693 } else {
2694 getline();
2695 }
2696 } else if (c=='\'') {
2697 in_quote = 0;
2698 }
2699 } else if (in_wquote) {
2700 if (c=='\\') {
2701 if (*chptr !='\n') {
2702 *cheapp++ = *chptr++;
2703 } else {
2704 *cheapp = '\n';
2705 getline();
2706 }
2707 } else if (c=='"') {
2708 in_wquote = 0;
2709 }
2710 } else if (c=='"') {
2711 in_wquote = 1;
2712 } else if (c=='\'') {
2713 in_quote = 1;
2714 } if (plevel==0) {
2715 if (c==',') {
2716 cheapp[-1] = 0;
2717 args = glist2((int)cheapp,args);
2718 } else if (c==')') {
2719 cheapp[-1] = 0;
2720 break;
2721 } else if (c=='(') {
2722 plevel++;
2723 } else if (c=='\\') {
2724 if (*chptr=='\n') {
2725 cheapp--;
2726 getline();
2727 }
2728 } else if (c==' '||c=='\t') {
2729 cheapp--;
2730 } else if (c=='\n') {
2731 cheapp--;
2732 getline();
2733 }
2734 } else if (c==')') {
2735 plevel--;
2736 } else if (c=='\n') {
2737 cheapp--;
2738 getline();
2739 }
2740 }
2741 return reverse0(args);
2742 }
2743
2744 void
2745 fmacro()
2746 {
2747 int args,sargs,values;
2748 #if 0
2749 int i;
2750 fprintf(stderr,"linebuf: %s\n",linebuf);
2751 fprintf(stderr,"fmacro: %s\n",nptr->nm);
2752 i = 0;
2753 args = cadr(nptr->dsp);
2754 while(args) {
2755 fprintf(stderr,"fmacro args %d: %s\n",i++,(char *) car(args));
2756 args = cadr(args);
2757 }
2758 fprintf(stderr,"fmacro body: %s\n",(char *)car(nptr->dsp));
2759 args = macro_args();
2760 i = 0;
2761 while(args) {
2762 fprintf(stderr,"fmacro args value %d: %s\n",i++,(char *) car(args));
2763 args = cadr(args);
2764 }
2765 #endif
2766 args = sargs = cadr(nptr->dsp);
2767 values = macro_args();
2768 while(args) {
2769 local_define(car(args),car(values));
2770 args = cadr(args);
2771 values = cadr(values);
2772 }
2773 mflag++;
2774 replace_macro();
2775 chsave = ch;
2776 chptrsave = chptr;
2777 chptr = cheapp;
2778 while(sargs) {
2779 local_undef(car(sargs));
2780 sargs = cadr(sargs);
2781 }
2782 getch();
2783 }
2784
2785 void
2786 replace_macro()
2787 {
2788 }
2789
2790 void
2791 local_define(char *macro,char *value)
2792 {
2793 NMTBL *nptr0;
2794 nptr0 = lsearch(macro);
2795 nptr0->sc=LMACRO;
2796 nptr0->ty=list3(nptr0->sc,nptr0->dsp,nptr0->ty);
2797 nptr0->dsp=(int)value;
2798 }
2799
2800 void
2801 local_undef(char *macro)
2802 {
2803 NMTBL *nptr0;
2804 int save;
2805 nptr0 = lsearch(macro);
2806 save = nptr0->ty;
2807 nptr0->sc=car(save);
2808 nptr0->dsp=cadr(save);
2809 nptr0->ty=caddr(save);
2810 }
2811
2650 int 2812 int
2651 car(int e) 2813 car(int e)
2652 { 2814 {
2653 return heap[e]; 2815 return heap[e];
2654 } 2816 }