Back to index

plt-scheme  4.2.1
printbody.c
Go to the documentation of this file.
00001 /*   wbuild
00002      Copyright (C) 1996  Joel N. Weber II <nemo@koa.iolani.honolulu.hi.us>
00003      
00004      This program is free software; you can redistribute it and/or
00005      modify it under the terms of the GNU General Public License
00006      as published by the Free Software Foundation; either version 2
00007      of the License, or (at your option) any later version.
00008      
00009      This program is distributed in the hope that it will be useful,
00010      but WITHOUT ANY WARRANTY; without even the implied warranty of
00011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012      GNU General Public License for more details.
00013      
00014      You should have received a copy of the GNU General Public License
00015      along with this program; if not, write to the Free Software
00016      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 #include <config.h>
00020 
00021 #include <stdio.h>
00022 #include <wbuild.h>
00023 #include <wsym.h>
00024 #include <ctype.h>
00025 
00026 #include <libintl.h>
00027 #define _(String) gettext(String)
00028 
00029 #include <libit/malloc.h>
00030 
00031 /* The following cases are possible:
00032  *
00033  * \item{1.} |$|: replaced by |self|.
00034  *
00035  * \item{2.} |$$|: replaced by a single \$.
00036  *
00037  * \item{3.} |$id|: replaced by |self->part.id| plus some typecasts.
00038  *
00039  * \item{4.} |$id1$id2|: replaced by |id1->part.id2| plus some
00040  * typecasts.
00041  *
00042  * \item{5.} |$(id1)id2|: replaced by |self->part.id2| plus typecasts,
00043  * where |part| is the class |id2| or a superclass.
00044  *
00045  * \item{6.} |$id1$(id2)id3|: replaced by |id1->part.id3| plus typecasts,
00046  * where |part| is the class |id2| or a superclass.
00047  */
00048 
00049 
00050 inline static char *expand_hash_word_p(FILE *f, char *p, Class class,
00051        int lineno, STRING procname)
00052 {
00053        STRING word;
00054        Class part;
00055 
00056        if (*(p+1) == '#') {
00057               p++;
00058               putc('#', f);
00059               return p;
00060        }
00061        if (!class->super) {
00062               fprintf(stderr, _("%s:%d: (in %s) no superclass (#) defined\n"),
00063                      get(class->filename), lineno, get(procname));
00064               nerrors++;
00065               return p;
00066        }
00067        word = get_word(p + 1);
00068        if (!word) {
00069               fputs("XtSuperclass(self)", f);
00070               return p;
00071        }
00072        p += Strlen(word);
00073        if ((part = find_method_class(class->super, word)) != NULL
00074               || (part = find_classvar_class(class->super, word)) != NULL) {
00075               fprintf(f, "%sClassRec.%s_class.%s",
00076                      get(get_instname(class->super->name)),
00077                      get(get_instname(part->name)), get(word));
00078        } else {
00079               fprintf(stderr,
00080                      _("%s:%d: (in %s) identifier `#%s' is unknown\n"),
00081                      get(class->filename), lineno, get(procname),
00082                      get(word));
00083               nerrors++;
00084        }
00085        delete(word);
00086        return p;
00087 }
00088 
00089 static void expand_word_part1(FILE *f, Class class, Class part1,
00090        STRING word, STRING word1, int lineno, STRING procname)
00091 {
00092        Class part;
00093 
00094        if ((part = find_instvar_class(part1, word)) != NULL)
00095               fprintf(f, "((%sWidget)%s)->%s.%s", get(class->name),
00096                      get(word1), get(get_instname(part->name)), get(word));
00097        else if ((part = find_method_class(part1, word)) != NULL
00098               || (part = find_classvar_class(part1, word)) != NULL)
00099               fprintf(f,
00100                      "((%sWidgetClass)%s->core.widget_class)->%s_class.%s",
00101                      get(class->name), get(word1),
00102                      get(get_instname(part->name)), get(word));
00103        else if ((part = find_constr_class(part1, word)) != NULL)
00104               fprintf(f,
00105                      "((%sConstraintRec*)((%sWidget)%s)->"
00106                      "core.constraints)->%s.%s",
00107                      get(class->name), get(class->name), get(word1),
00108                      get(get_instname(part->name)), get(word));
00109        else {
00110               fprintf(stderr,
00111                      _("%s:%d: (in %s) identifier `$%s' is unknown\n"),
00112                      get(class->filename), lineno, get(procname), get(word));
00113               nerrors++;
00114        }
00115 }
00116 
00117 
00118 inline static char *expand_word_p(FILE *f, char *p, Class class, int lineno,
00119        STRING procname)
00120 {
00121        STRING word1, word;
00122        Class part1;
00123 
00124        if (*(p+1) == '$') {
00125               /* case |$$| */
00126               p++;
00127        } else if (*(p+1) == '(') {
00128               /* case |$(id1)id2| */
00129               word1 = SELF;
00130               word = get_word(p + 2);
00131               p += Strlen(word) + 1;
00132               if (*(p+1) != ')') {
00133                      fprintf(stderr,
00134                             _("%s:%d: (in %s) missing `)' after "
00135                             "`$%s(%s'\n"),
00136                             get(class->filename), lineno, get(procname),
00137                             get(word1), get(word));
00138                      nerrors++;
00139                      return p;
00140               }
00141               part1 = find_superclass(class, word);
00142               if (!part1) {
00143                      fprintf(stderr,
00144                             _("%s:%d: (in %s) class `%s' is not a "
00145                             "superclass\n"),
00146                             get(class->filename), lineno, get(procname),
00147                             get(word));
00148                      nerrors++;
00149                      return p;
00150               }
00151               word = get_word(p + 2);
00152               if (!word) {
00153                      fprintf(stderr,
00154                             _("%s:%d: (in %s) missing identifier after "
00155                             "`$(%s)'\n"),
00156                             get(class->filename), lineno, get(procname),
00157                             get(word));
00158                      nerrors++;
00159                      return p;
00160               }
00161               p += Strlen(word) + 1;
00162               expand_word_part1(f, class, part1, word, word1, lineno,
00163                      procname);
00164        } else if (!(word = get_word(p + 1))) {
00165               /* case |$| */
00166               fputs("self", f);
00167        } else if (p += Strlen(word), *(p+1) != '$') {
00168               /* case |$id| */
00169               word1 = SELF;
00170               part1 = class;
00171               expand_word_part1(f, class, part1, word, word1, lineno,
00172                      procname);
00173        } else if (*(p+2) != '(') {
00174               /* case |$id1$id2| */
00175               word1 = word;
00176               word = get_word(p + 2);
00177               p += Strlen(word) + 1;
00178               part1 = class;
00179               expand_word_part1(f, class, part1, word, word1, lineno,
00180                      procname);
00181        } else {
00182               /* case |$id1$(id2)id3| */
00183               word1 = word;
00184               word = get_word(p + 3);
00185               p += Strlen(word) + 2;
00186               if (*(p+1) != ')') {
00187                      fprintf(stderr, 
00188                             _("%s:%d: (in %s) missing `)' after `$%s(%s'\n"),
00189                             get(class->filename), lineno, get(procname),
00190                             get(word1), get(word));
00191                      nerrors++;
00192                      return p;
00193               }
00194               part1 = find_superclass(class, word);
00195               if (!part1) {
00196                      fprintf(stderr,
00197                             _("%s:%d: (in %s) class `%s' is not a "
00198                             "superclass\n"),
00199                             get(class->filename), lineno, get(procname),
00200                             get(word));
00201                      nerrors++;
00202                      return p;
00203               }
00204               word = get_word(p + 2);
00205               if (!word) {
00206                      fprintf(stderr,
00207                             _("%s:%d: (in %s) missing identifier after "
00208                             "`$%s()'\n"),
00209                             get(class->filename), lineno, get(procname),
00210                             get(word1));
00211                      nerrors++;
00212                      return p;
00213               }
00214               p += Strlen(word) + 1;
00215               expand_word_part1(f, class, part1, word, word1, lineno,
00216                      procname);
00217        }
00218        return p;
00219 }
00220 
00221 void print_body(FILE *f, STRING body, Class class, STRING procname,
00222        int lineno, int ismacro)
00223 {
00224        char *p, prev = '\0';
00225        int quoted = 0, squoted = 0, comment = 0;
00226 
00227        for (p = get(body); *p; prev = prev == '\\' ? '\0' : *p, p++)
00228               switch (*p) {
00229                      case '"':
00230                             putc('"', f);
00231                             if (! squoted && ! comment && prev != '\\')
00232                                    quoted = ! quoted;
00233                             break;
00234                      case '\'':
00235                             putc('\'', f);
00236                             if (! quoted && ! comment && prev != '\\')
00237                                    squoted = ! squoted;
00238                             break;
00239                      case '*':
00240                             putc('*', f);
00241                             if (! quoted && ! squoted && ! comment &&
00242                                    (prev == '/'))
00243                                           comment = 1;
00244                             break;
00245                      case '/':
00246                             putc('/', f);
00247                             if (comment && prev == '*')
00248                                    comment = 0;
00249                             break;
00250                      case '$':
00251                             if (quoted || squoted || comment ||
00252                                    (prev == '\\'))
00253                                           putc('$', f);
00254                             else {
00255                                    p = expand_word_p(f, p, class, lineno,
00256                                           procname);
00257                             }
00258                             break;
00259                      case '#':
00260                             if (quoted || squoted || comment ||
00261                                    (prev == '\\'))
00262                                           putc('#', f);
00263                             else if (prev == '\n'
00264                                    && ((strneq(p, "#ifdef", 6) && !isalnum(p[6]))
00265                                    || (strneq(p, "#ifndef", 7) && !isalnum(p[7]))
00266                                    || (strneq(p, "#if", 3) && !isalnum(p[3]))
00267                                    || (strneq(p, "#define", 7) && !isalnum(p[7]))
00268                                    || (strneq(p, "#include", 8) && !isalnum(p[8]))
00269                                    || (strneq(p, "#undef", 6) && !isalnum(p[6]))
00270                                    || (strneq(p, "#else", 5) && !isalnum(p[5]))
00271                                    || (strneq(p, "#endif", 6) && !isalnum(p[6]))))
00272                                           putc('#', f);
00273                             else {
00274                                    p = expand_hash_word_p(f, p, class,
00275                                           lineno, procname);
00276                             }
00277                             break;
00278                      case '\n':
00279                             if (ismacro)
00280                                    putc('\\', f);
00281                             putc('\n', f);
00282                             break;
00283                      default:
00284                             putc(*p, f);
00285        }
00286        putc('\n', f);
00287 }