From a2a18a1d0532bab1dbe8a110c630257568f58a91 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Thu, 2 Feb 2023 16:12:59 -0800 Subject: [PATCH] Gperf. --- implementations/C/KEYWORDS.txt | 27 ++++++ implementations/C/Makefile | 13 +-- implementations/C/keywords.c | 152 +++++++++++++++++++++++++++++++++ 3 files changed, 187 insertions(+), 5 deletions(-) create mode 100644 implementations/C/KEYWORDS.txt create mode 100644 implementations/C/keywords.c diff --git a/implementations/C/KEYWORDS.txt b/implementations/C/KEYWORDS.txt new file mode 100644 index 0000000..1c7c7fc --- /dev/null +++ b/implementations/C/KEYWORDS.txt @@ -0,0 +1,27 @@ +!= +"%" +* ++ +- +/ +< +<= +<> += +> +>= +bool +branch +clear +concat +cons +dip +dup +first +i +loop +pop +rest +stack +swaack +swap diff --git a/implementations/C/Makefile b/implementations/C/Makefile index 02f0fec..cce0acb 100644 --- a/implementations/C/Makefile +++ b/implementations/C/Makefile @@ -1,11 +1,14 @@ +OBJS=joy.o keywords.o STATIC_GCLIB=/usr/local/lib/libgc.a /usr/local/lib/libgmp.a -CFLAGS = -ansi -Wall -Wextra -Werror -pedantic -Wstrict-overflow -fno-strict-aliasing -pedantic-errors -I/usr/local/include $(STATIC_GCLIB) +CFLAGS = -ansi -Wall -Wextra -Werror -pedantic -Wstrict-overflow -fno-strict-aliasing -pedantic-errors -I/usr/local/include #COPTS=-I/usr/local/include +# $(STATIC_GCLIB) + +GPERF_OPTS = --readonly-tables --enum --includes --global-table +joy: ${OBJS} $(STATIC_GCLIB) -all: joy - -#joy: joy.c -# cc $(COPTS) joy.c $(STATIC_GCLIB) -o joy +keywords.c: KEYWORDS.txt + gperf --output-file=$@ --hash-function-name=keyword_hash $(GPERF_OPTS) KEYWORDS.txt diff --git a/implementations/C/keywords.c b/implementations/C/keywords.c new file mode 100644 index 0000000..614aba1 --- /dev/null +++ b/implementations/C/keywords.c @@ -0,0 +1,152 @@ +/* ANSI-C code produced by gperf version 3.1 */ +/* Command-line: gperf --output-file=keywords.c --hash-function-name=keyword_hash --readonly-tables --enum --includes --global-table KEYWORDS.txt */ +/* Computed positions: -k'1-2' */ + +#if !((' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \ + && ('%' == 37) && ('&' == 38) && ('\'' == 39) && ('(' == 40) \ + && (')' == 41) && ('*' == 42) && ('+' == 43) && (',' == 44) \ + && ('-' == 45) && ('.' == 46) && ('/' == 47) && ('0' == 48) \ + && ('1' == 49) && ('2' == 50) && ('3' == 51) && ('4' == 52) \ + && ('5' == 53) && ('6' == 54) && ('7' == 55) && ('8' == 56) \ + && ('9' == 57) && (':' == 58) && (';' == 59) && ('<' == 60) \ + && ('=' == 61) && ('>' == 62) && ('?' == 63) && ('A' == 65) \ + && ('B' == 66) && ('C' == 67) && ('D' == 68) && ('E' == 69) \ + && ('F' == 70) && ('G' == 71) && ('H' == 72) && ('I' == 73) \ + && ('J' == 74) && ('K' == 75) && ('L' == 76) && ('M' == 77) \ + && ('N' == 78) && ('O' == 79) && ('P' == 80) && ('Q' == 81) \ + && ('R' == 82) && ('S' == 83) && ('T' == 84) && ('U' == 85) \ + && ('V' == 86) && ('W' == 87) && ('X' == 88) && ('Y' == 89) \ + && ('Z' == 90) && ('[' == 91) && ('\\' == 92) && (']' == 93) \ + && ('^' == 94) && ('_' == 95) && ('a' == 97) && ('b' == 98) \ + && ('c' == 99) && ('d' == 100) && ('e' == 101) && ('f' == 102) \ + && ('g' == 103) && ('h' == 104) && ('i' == 105) && ('j' == 106) \ + && ('k' == 107) && ('l' == 108) && ('m' == 109) && ('n' == 110) \ + && ('o' == 111) && ('p' == 112) && ('q' == 113) && ('r' == 114) \ + && ('s' == 115) && ('t' == 116) && ('u' == 117) && ('v' == 118) \ + && ('w' == 119) && ('x' == 120) && ('y' == 121) && ('z' == 122) \ + && ('{' == 123) && ('|' == 124) && ('}' == 125) && ('~' == 126)) +/* The character set is not based on ISO-646. */ +#error "gperf generated tables don't work with this execution character set. Please report a bug to ." +#endif + +#include +enum + { + TOTAL_KEYWORDS = 27, + MIN_WORD_LENGTH = 1, + MAX_WORD_LENGTH = 6, + MIN_HASH_VALUE = 1, + MAX_HASH_VALUE = 37 + }; + +/* maximum key range = 37, duplicates = 0 */ + +#ifdef __GNUC__ +__inline +#else +#ifdef __cplusplus +inline +#endif +#endif +static unsigned int +keyword_hash (register const char *str, register size_t len) +{ + static const unsigned char asso_values[] = + { + 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, + 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, + 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, + 38, 38, 38, 30, 38, 38, 38, 31, 38, 38, + 38, 38, 26, 21, 38, 1, 38, 3, 38, 38, + 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, + 10, 5, 0, 38, 38, 38, 38, 38, 38, 38, + 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, + 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, + 38, 38, 38, 38, 38, 38, 38, 38, 15, 5, + 0, 15, 0, 38, 38, 25, 38, 38, 0, 38, + 38, 5, 0, 38, 10, 10, 0, 0, 38, 5, + 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, + 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, + 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, + 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, + 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, + 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, + 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, + 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, + 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, + 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, + 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, + 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, + 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, + 38, 38, 38, 38, 38, 38 + }; + register unsigned int hval = len; + + switch (hval) + { + default: + hval += asso_values[(unsigned char)str[1]]; + /*FALLTHROUGH*/ + case 1: + hval += asso_values[(unsigned char)str[0]]; + break; + } + return hval; +} + +static const char * const wordlist[] = + { + "", + ">", + "-", + "dup", + "/", + "", + "=", + ">=", + "pop", + "loop", + "clear", + "<", + "<>", + "", + "cons", + "stack", + "concat", + "<=", + "", + "swap", + "", + "swaack", + "+", + "", + "bool", + "", + "i", + "*", + "dip", + "rest", + "first", + "branch", + "%", + "", "", "", "", + "!=" + }; + +const char * +in_word_set (register const char *str, register size_t len) +{ + if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH) + { + register unsigned int key = keyword_hash (str, len); + + if (key <= MAX_HASH_VALUE) + { + register const char *s = wordlist[key]; + + if (*str == *s && !strcmp (str + 1, s + 1)) + return s; + } + } + return 0; +}