2010-05-22 21:35:18 +02:00
|
|
|
/***************************************************************************
|
|
|
|
|
|
|
|
gbc_reserved_make.c
|
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
(c) 2000-2012 Benoît Minisini <gambas@users.sourceforge.net>
|
2010-05-22 21:35:18 +02:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2011-06-03 02:51:09 +02:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
2011-12-31 03:39:20 +01:00
|
|
|
MA 02110-1301, USA.
|
2010-05-22 21:35:18 +02:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBC_RESERVED_MAKE_C
|
|
|
|
|
|
|
|
#include <ctype.h>
|
2010-12-14 00:46:01 +01:00
|
|
|
#include <errno.h>
|
2010-05-22 21:35:18 +02:00
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_reserved.h"
|
|
|
|
#include "gb_type_common.h"
|
|
|
|
#include "gb_pcode.h"
|
|
|
|
#include "gb_reserved_keyword.h"
|
|
|
|
|
2010-05-25 13:19:00 +02:00
|
|
|
#define HASH_SIZE 109
|
|
|
|
|
2010-05-27 00:01:28 +02:00
|
|
|
/*uint hash(const char *key, int len)
|
2010-05-22 21:35:18 +02:00
|
|
|
{
|
|
|
|
int i;
|
2010-05-25 13:19:00 +02:00
|
|
|
uint h = 0;
|
2010-05-22 21:35:18 +02:00
|
|
|
for (i = 0; i < len; i++)
|
2010-05-25 13:19:00 +02:00
|
|
|
h = (h << 4) + (h ^ (tolower(key[i])));
|
2010-05-22 21:35:18 +02:00
|
|
|
|
2010-05-25 13:19:00 +02:00
|
|
|
return h % HASH_SIZE;
|
2010-05-27 00:01:28 +02:00
|
|
|
}*/
|
2010-05-22 21:35:18 +02:00
|
|
|
|
2010-12-14 00:46:01 +01:00
|
|
|
char buffer[1024];
|
|
|
|
|
|
|
|
char *read_line(FILE *f)
|
|
|
|
{
|
|
|
|
return fgets(buffer, sizeof(buffer), f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void read_write_until(FILE *in, FILE *out, char *until)
|
|
|
|
{
|
|
|
|
char *line;
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
line = read_line(in);
|
|
|
|
if (!line)
|
|
|
|
break;
|
|
|
|
if (out) fputs(line, out);
|
|
|
|
while (*line && ((unsigned char)*line <= ' '))
|
|
|
|
line++;
|
|
|
|
if (!strncmp(line, until, strlen(until)))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-22 21:35:18 +02:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
COMP_INFO *info;
|
2010-05-25 13:19:00 +02:00
|
|
|
SUBR_INFO *subr;
|
2010-05-22 21:35:18 +02:00
|
|
|
int len;
|
2010-05-27 00:01:28 +02:00
|
|
|
int i, p, n;
|
2010-05-22 21:35:18 +02:00
|
|
|
uint h;
|
|
|
|
char c;
|
2010-05-27 00:01:28 +02:00
|
|
|
char last[16];
|
|
|
|
char next[16];
|
2010-12-14 00:46:01 +01:00
|
|
|
FILE *in, *out;
|
2010-05-22 21:35:18 +02:00
|
|
|
|
2010-12-14 00:46:01 +01:00
|
|
|
in = fopen("../share/gb_reserved_temp.h", "r");
|
|
|
|
if (!in)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "unable to open '../share/gb_reserved_temp.h': %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
out = fopen("../share/gb_reserved_temp.h.tmp", "w");
|
|
|
|
|
|
|
|
read_write_until(in, out, "int RESERVED_find_word(const char *word, int len)");
|
|
|
|
|
|
|
|
read_write_until(in, out, "static void *jump[] = {");
|
|
|
|
|
2010-05-25 13:19:00 +02:00
|
|
|
for (h = 0; h < 12; h++)
|
|
|
|
{
|
2010-12-14 00:46:01 +01:00
|
|
|
if ((h % 8) == 0)
|
|
|
|
fprintf(out, "\t\t");
|
|
|
|
fprintf(out, "&&__%02d, ", h);
|
2010-05-25 13:19:00 +02:00
|
|
|
if ((h % 8) == 7)
|
2010-12-14 00:46:01 +01:00
|
|
|
fprintf(out, "\n");
|
2010-05-25 13:19:00 +02:00
|
|
|
}
|
2010-12-14 00:46:01 +01:00
|
|
|
fprintf(out, "\n\t};\n\n");
|
2010-05-25 13:19:00 +02:00
|
|
|
|
|
|
|
//printf(" goto *jump[h %% %d];\n\n", HASH_SIZE);
|
2010-12-14 00:46:01 +01:00
|
|
|
fprintf(out, "\tgoto *jump[len];\n\n");
|
2010-05-25 13:19:00 +02:00
|
|
|
|
2010-12-14 00:46:01 +01:00
|
|
|
fprintf(out, "__00:\n__01:\n\treturn -1;\n");
|
2010-05-27 00:01:28 +02:00
|
|
|
|
|
|
|
for (h = 2; h < 12; h++)
|
2010-05-25 13:19:00 +02:00
|
|
|
{
|
2010-12-14 00:46:01 +01:00
|
|
|
fprintf(out, "__%02d:\n", h);
|
2010-05-25 13:19:00 +02:00
|
|
|
|
2010-05-27 00:01:28 +02:00
|
|
|
*last = 0;
|
|
|
|
|
|
|
|
for(;;)
|
2010-05-25 13:19:00 +02:00
|
|
|
{
|
2010-05-27 00:01:28 +02:00
|
|
|
n = -1;
|
|
|
|
|
|
|
|
*next = 0;
|
|
|
|
|
2010-05-25 13:19:00 +02:00
|
|
|
for (info = &COMP_res_info[1], i = 1; info->name; info++, i++)
|
|
|
|
{
|
|
|
|
len = strlen(info->name);
|
|
|
|
if (len != h)
|
|
|
|
continue;
|
2010-05-27 00:01:28 +02:00
|
|
|
|
|
|
|
if (strcmp(info->name, last) <= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (*next == 0 || strcmp(info->name, next) <= 0)
|
2010-05-25 13:19:00 +02:00
|
|
|
{
|
2010-05-27 00:01:28 +02:00
|
|
|
strcpy(next, info->name);
|
|
|
|
n = i;
|
2010-05-25 13:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
2010-05-27 00:01:28 +02:00
|
|
|
|
|
|
|
if (n < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
info = &COMP_res_info[n];
|
|
|
|
len = strlen(info->name);
|
|
|
|
|
|
|
|
strcpy(last, info->name);
|
|
|
|
|
2010-12-14 00:46:01 +01:00
|
|
|
fprintf(out, "\tif (");
|
2010-05-27 00:01:28 +02:00
|
|
|
for (p = 0; p < len; p++)
|
|
|
|
{
|
|
|
|
if (p)
|
2010-12-14 00:46:01 +01:00
|
|
|
fprintf(out, " && ");
|
2010-05-27 00:01:28 +02:00
|
|
|
c = info->name[p];
|
|
|
|
if (isalpha(c))
|
2010-12-14 00:46:01 +01:00
|
|
|
fprintf(out, "tolower(word[%d]) == '%c'", p, tolower(info->name[p]));
|
2010-05-27 00:01:28 +02:00
|
|
|
else if (c == '\\')
|
2010-12-14 00:46:01 +01:00
|
|
|
fprintf(out, "word[%d] == '\\\\'", p);
|
2010-05-27 00:01:28 +02:00
|
|
|
else
|
2010-12-14 00:46:01 +01:00
|
|
|
fprintf(out, "word[%d] == '%c'", p, info->name[p]);
|
2010-05-27 00:01:28 +02:00
|
|
|
}
|
|
|
|
/*printf(" if (!strncmp(\"");
|
|
|
|
for (p = 0; p < len; p++)
|
|
|
|
{
|
|
|
|
c = info->name[p];
|
|
|
|
if (c == '\\')
|
|
|
|
printf("\\\\");
|
|
|
|
else
|
|
|
|
putchar(tolower(info->name[p]));
|
|
|
|
}
|
|
|
|
printf("\", word, %d)", len);*/
|
2010-12-14 00:46:01 +01:00
|
|
|
fprintf(out, ") return %d;\n", n);
|
2010-05-25 13:19:00 +02:00
|
|
|
}
|
2010-05-27 00:01:28 +02:00
|
|
|
|
2010-12-14 00:46:01 +01:00
|
|
|
fprintf(out, "\treturn -1;\n");
|
2010-05-25 13:19:00 +02:00
|
|
|
}
|
|
|
|
|
2010-12-14 00:46:01 +01:00
|
|
|
read_write_until(in, NULL, "}");
|
|
|
|
read_write_until(in, NULL, "}");
|
|
|
|
fprintf(out, "}\n");
|
|
|
|
|
|
|
|
read_write_until(in, out, "static void *jump[] = {");
|
|
|
|
|
2010-05-25 13:19:00 +02:00
|
|
|
for (h = 0; h < 12; h++)
|
2010-05-22 21:35:18 +02:00
|
|
|
{
|
2010-12-14 00:46:01 +01:00
|
|
|
if ((h % 8) == 0)
|
|
|
|
fprintf(out, "\t\t");
|
|
|
|
fprintf(out, "&&__%02d, ", h);
|
2010-05-22 21:35:18 +02:00
|
|
|
if ((h % 8) == 7)
|
2010-12-14 00:46:01 +01:00
|
|
|
fprintf(out, "\n");
|
2010-05-22 21:35:18 +02:00
|
|
|
}
|
2010-12-14 00:46:01 +01:00
|
|
|
fprintf(out, "\n\t};\n\n");
|
2010-05-22 21:35:18 +02:00
|
|
|
|
2010-12-14 00:46:01 +01:00
|
|
|
fprintf(out, "\tgoto *jump[len];\n\n");
|
2010-05-22 21:35:18 +02:00
|
|
|
|
2010-12-14 00:46:01 +01:00
|
|
|
fprintf(out, "__00:\n__01:\n\treturn -1;\n");
|
2010-05-27 00:01:28 +02:00
|
|
|
|
|
|
|
for (h = 2; h < 12; h++)
|
2010-05-22 21:35:18 +02:00
|
|
|
{
|
2010-12-14 00:46:01 +01:00
|
|
|
fprintf(out, "__%02d:\n", h);
|
2010-05-22 21:35:18 +02:00
|
|
|
|
2010-05-27 00:01:28 +02:00
|
|
|
*last = 0;
|
|
|
|
|
|
|
|
for(;;)
|
2010-05-22 21:35:18 +02:00
|
|
|
{
|
2010-05-27 00:01:28 +02:00
|
|
|
n = -1;
|
|
|
|
|
|
|
|
*next = 0;
|
|
|
|
|
2010-05-25 13:19:00 +02:00
|
|
|
for (subr = &COMP_subr_info[0], i = 0; subr->name; subr++, i++)
|
2010-05-22 21:35:18 +02:00
|
|
|
{
|
2010-05-25 13:19:00 +02:00
|
|
|
len = strlen(subr->name);
|
|
|
|
if (len != h)
|
|
|
|
continue;
|
2010-05-27 00:01:28 +02:00
|
|
|
|
|
|
|
if (strcmp(subr->name, last) <= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (*next == 0 || strcmp(subr->name, next) <= 0)
|
2010-05-25 13:19:00 +02:00
|
|
|
{
|
2010-05-27 00:01:28 +02:00
|
|
|
strcpy(next, subr->name);
|
|
|
|
n = i;
|
2010-05-25 13:19:00 +02:00
|
|
|
}
|
2010-05-22 21:35:18 +02:00
|
|
|
}
|
2010-05-27 00:01:28 +02:00
|
|
|
|
|
|
|
if (n < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
subr = &COMP_subr_info[n];
|
|
|
|
len = strlen(subr->name);
|
|
|
|
|
|
|
|
strcpy(last, subr->name);
|
|
|
|
|
2010-12-14 00:46:01 +01:00
|
|
|
fprintf(out, "\tif ("); //COMPARE_%d(\"%s\", word)) return %d;\n", len, len, info->name, i);
|
2010-05-27 00:01:28 +02:00
|
|
|
for (p = 0; p < len; p++)
|
|
|
|
{
|
|
|
|
if (p)
|
2010-12-14 00:46:01 +01:00
|
|
|
fprintf(out, " && ");
|
2010-05-27 00:01:28 +02:00
|
|
|
c = subr->name[p];
|
|
|
|
if (isalpha(c))
|
2010-12-14 00:46:01 +01:00
|
|
|
fprintf(out, "tolower(word[%d]) == '%c'", p, tolower(subr->name[p]));
|
2010-05-27 00:01:28 +02:00
|
|
|
else if (c == '\\')
|
2010-12-14 00:46:01 +01:00
|
|
|
fprintf(out, "word[%d] == '\\\\'", p);
|
2010-05-27 00:01:28 +02:00
|
|
|
else
|
2010-12-14 00:46:01 +01:00
|
|
|
fprintf(out, "word[%d] == '%c'", p, subr->name[p]);
|
2010-05-27 00:01:28 +02:00
|
|
|
}
|
2010-12-14 00:46:01 +01:00
|
|
|
fprintf(out, ") return %d;\n", n);
|
2010-05-22 21:35:18 +02:00
|
|
|
}
|
2010-05-27 00:01:28 +02:00
|
|
|
|
2010-12-14 00:46:01 +01:00
|
|
|
fprintf(out, "\treturn -1;\n");
|
2010-05-22 21:35:18 +02:00
|
|
|
}
|
|
|
|
|
2010-12-14 00:46:01 +01:00
|
|
|
fprintf(out, "}\n");
|
|
|
|
|
|
|
|
fclose(in);
|
|
|
|
fclose(out);
|
|
|
|
|
|
|
|
unlink("../share/gb_reserved_temp.h");
|
|
|
|
rename("../share/gb_reserved_temp.h.tmp", "../share/gb_reserved_temp.h");
|
|
|
|
|
2010-05-22 21:35:18 +02:00
|
|
|
return 0;
|
|
|
|
}
|