fixed hashgen

This commit is contained in:
Edward Emelianov 2022-12-15 09:19:41 +03:00
parent 8331a4d348
commit 76517ca91f
3 changed files with 47 additions and 41 deletions

View File

@ -21,7 +21,6 @@
#include <ctype.h> #include <ctype.h>
#define ALLOCSZ (5000) #define ALLOCSZ (5000)
#define DJB2
typedef struct{ typedef struct{
char *dict; char *dict;
@ -117,20 +116,25 @@ static uint32_t (*hash[HASHFNO])(const char *str) = {djb2, sdbm, jenkins};
static const char *hashnames[HASHFNO] = {"DJB2", "SDBM", "Jenkins"}; static const char *hashnames[HASHFNO] = {"DJB2", "SDBM", "Jenkins"};
typedef struct{ typedef struct{
char str[32]; char str[32]; // string command
uint32_t hash; char fname[32]; // function namee
uint32_t hash; // command hash
} strhash; } strhash;
static int sorthashesH(const void *a, const void *b){ static int sorthashesH(const void *a, const void *b){ // sort by hash
register uint32_t h1 = ((strhash*)a)->hash, h2 = ((strhash*)b)->hash; register uint32_t h1 = ((strhash*)a)->hash, h2 = ((strhash*)b)->hash;
if(h1 > h2) return 1; if(h1 > h2) return 1;
else if(h1 < h2) return -1; else if(h1 < h2) return -1;
return 0; return 0;
} }
static int sorthashesS(const void *a, const void *b){ static int sorthashesS(const void *a, const void *b){ // sort by string
char *s1 = ((strhash*)a)->str, *s2 = ((strhash*)b)->str; char *s1 = ((strhash*)a)->str, *s2 = ((strhash*)b)->str;
return strcmp(s1, s2); return strcmp(s1, s2);
} }
static int sorthashesF(const void *a, const void *b){ // sort by fname
char *s1 = ((strhash*)a)->fname, *s2 = ((strhash*)b)->fname;
return strcmp(s1, s2);
}
static FILE *openoutp(const char *name){ static FILE *openoutp(const char *name){
FILE *f = fopen(name, "w"); FILE *f = fopen(name, "w");
@ -165,22 +169,8 @@ static char *fnname(const char *cmd){
} }
static const char *fhdr = static const char *fhdr =
"int parsecmd(char *cmdwargs){\n\ "int parsecmd(char *cmd, char *args){\n\
if(!cmdwargs || !*cmdwargs) return 0;\n\ if(!cmd || !args) return 0;\n\
char cmd[32];\n\
int i = 0;\n\
char *args = cmdwargs;\n\
while(*args && *args < 33) ++args;\n\
if(!args || !*args) return 0;\n\
while(*args > 33 && i < 31){\n\
cmd[i++] = *args++;\n\
}\n\
cmd[i] = 0;\n\
if(i == 31) args = NULL;\n\
if(args){\n\
while(*args && *args < 33) ++args;\n\
if(!*args) args = NULL;\n\
}\n\
uint32_t h = hashf(cmd);\n\ uint32_t h = hashf(cmd);\n\
switch(h){\n" switch(h){\n"
; ;
@ -191,11 +181,9 @@ static const char *ffooter =
}\n\n" }\n\n"
; ;
static const char *fns = static const char *fns =
"TRUE_INLINE int fn_%s(_U_ uint32_t hash, _U_ char *args){ // %s (%u)\n\ "int fn_%s(_U_ uint32_t hash, _U_ char *args) WAL; // \"%s\" (%u)\n\n"
return 1;\n\
}\n\n"
; ;
static const char *fproto = "int parsecmd(char *cmdwargs);\n\n"; static const char *fproto = "int parsecmd(char *cmdwargs, char *args);\n\n";
static const char *sw = static const char *sw =
" case CMD_%s:\n\ " case CMD_%s:\n\
return fn_%s(h, args);\n\ return fn_%s(h, args);\n\
@ -207,9 +195,9 @@ static const char *srchdr =
#ifndef _U_\n\ #ifndef _U_\n\
#define _U_ __attribute__((__unused__))\n\ #define _U_ __attribute__((__unused__))\n\
#endif\n\n\ #endif\n\n\
#ifndef TRUE_INLINE\n\ #ifndef WAL\n\
#define TRUE_INLINE __attribute__((always_inline)) static inline\n\ #define WAL __attribute__ ((weak, alias (\"__f1\")))\n\
#endif\n\n" #endif\n\nint __f1(_U_ uint32_t h, _U_ char *a){return 1;}\n\n"
; ;
static void build(strhash *H, int hno, int hlen){ static void build(strhash *H, int hno, int hlen){
@ -229,8 +217,7 @@ static void build(strhash *H, int hno, int hlen){
fprintf(source, srchdr, G.headerfile); fprintf(source, srchdr, G.headerfile);
if(G.genfunc){ if(G.genfunc){
for(int i = 0; i < hlen; ++i){ for(int i = 0; i < hlen; ++i){
//fprintf(source, fns, "popo", "lolo", 12); fprintf(source, fns, H[i].fname, H[i].str, H[i].hash);
fprintf(source, fns, fnname(H[i].str), H[i].str, H[i].hash);
} }
} }
fprintf(header, "%s", fproto); fprintf(header, "%s", fproto);
@ -238,7 +225,7 @@ static void build(strhash *H, int hno, int hlen){
fprintf(source, "%s", fhdr); fprintf(source, "%s", fhdr);
for(int i = 0; i < hlen; ++i){ for(int i = 0; i < hlen; ++i){
char *m = macroname(H[i].str); char *m = macroname(H[i].str);
fprintf(source, sw, m, fnname(H[i].str)); fprintf(source, sw, m, H[i].fname);
fprintf(header, "#define CMD_%-*s (%u)\n", lmax, m, H[i].hash); fprintf(header, "#define CMD_%-*s (%u)\n", lmax, m, H[i].hash);
} }
fprintf(source, "%s", ffooter); fprintf(source, "%s", ffooter);
@ -274,18 +261,30 @@ int main(int argc, char **argv){
}else{ }else{
snprintf(H[idx].str, 31, "%s", word); snprintf(H[idx].str, 31, "%s", word);
} }
snprintf(H[idx].fname, 31, "%s", fnname(H[idx].str));
++idx; ++idx;
if(!nxt) break; if(!nxt) break;
word = nxt + 1; word = nxt + 1;
} }
// test fname matches
qsort(H, idx, sizeof(strhash), sorthashesF);
int mflag = 0;
int imax1 = idx - 1, hno = 0; int imax1 = idx - 1, hno = 0;
for(int i = 0; i < imax1; ++i){ // test hash matches
if(0 == strcmp(H[i].fname, H[i+1].fname)){
mflag = 1;
WARNX("Have two similar function names for '%s' and '%s': 'fn_%s'",
H[i].str, H[i+1].str, H[i].fname);
}
}
if(mflag) ERRX("Can't generate code when names of some functions matches");
for(; hno < HASHFNO; ++hno){ for(; hno < HASHFNO; ++hno){
for(int i = 0; i < idx; ++i) for(int i = 0; i < idx; ++i)
H[i].hash = hash[hno](H[i].str); H[i].hash = hash[hno](H[i].str);
qsort(H, idx, sizeof(strhash), sorthashesH); qsort(H, idx, sizeof(strhash), sorthashesH);
strhash *p = H; strhash *p = H;
int nmatches = 0; int nmatches = 0;
for(int i = 0; i < imax1; ++i, ++p){ for(int i = 0; i < imax1; ++i, ++p){ // test hash matches
if(p->hash == p[1].hash) ++nmatches; if(p->hash == p[1].hash) ++nmatches;
} }
if(nmatches == 0){ if(nmatches == 0){

View File

@ -4,15 +4,20 @@
#include "hash.h" #include "hash.h"
/* int fn_hello(uint32_t hash, char *args){
static int f(uint32_t h, const char *args){ printf("HELLO! Hash=%u, param=%s\n", hash, args);
printf("%u -> '%s'\n", h, args);
return 1; return 1;
}*/ }
int fn_world(uint32_t hash, char *args){
printf("WORLD: %u - %s\n", hash, args);
return 1;
}
int main(int argc, char **argv){ int main(int argc, char **argv){
if(argc != 2) return 1; if(argc < 2) return 1;
if(!parsecmd(argv[1])) printf("%s not found\n", argv[1]); char *args = "";
if(argc > 2) args = argv[2];
if(!parsecmd(argv[1], args)) printf("%s not found\n", argv[1]);
else printf("All OK\n"); else printf("All OK\n");
return 0; return 0;
} }

View File

@ -2,10 +2,12 @@ hello
world world
what what
put put
change sim key
change ip
change param
set set
clear clear
reset reset
get get
out out
in in