Browse Source

lex: Use hash values to match keywords

master
Jake Wakeling 1 year ago
parent
commit
e69652dcb5
  1. 33
      src/lex.c

33
src/lex.c

@ -30,8 +30,6 @@ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
*/
#include <stdio.h>
#include "lex.h"
#include "misc.h"
#include "token.h"
@ -41,6 +39,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
#include <ctype.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#define C (l->s[l->sp])
@ -73,27 +72,25 @@ skip:;
t.ln = l->ln; t.cl = l->cl; if (l->sp + 1 != l->sl) {
if (ISIDNCA(C)) { /* Keywords, Types, and Identifiers */
s = sdspush(s, C); ++l->sp;
for (; ISIDNCB(C); ++l->sp) { s = sdspush(s, C); }
if (!strcmp(s, "return")) { t.t = RETURN; goto end; }
else if (!strcmp(s, "u8")) { t.t = U8; goto end; }
else if (!strcmp(s, "u16")) { t.t = U16; goto end; }
else if (!strcmp(s, "u32")) { t.t = U32; goto end; }
else if (!strcmp(s, "u64")) { t.t = U64; goto end; }
else if (!strcmp(s, "u128")) { t.t = U128; goto end; }
else if (!strcmp(s, "i8")) { t.t = I8; goto end; }
else if (!strcmp(s, "i16")) { t.t = I16; goto end; }
else if (!strcmp(s, "i32")) { t.t = I32; goto end; }
else if (!strcmp(s, "i64")) { t.t = I64; goto end; }
else if (!strcmp(s, "i128")) { t.t = I128; goto end; }
else { t.t = IDN; goto end; }
switch (crc32(s)) {
case 0xA79E3F0F: { t.t = RETURN; goto end; }
case 0x3B9327D2: { t.t = U8; goto end; }
case 0xA49CE182: { t.t = U16; goto end; }
case 0x91C74719: { t.t = U32; goto end; }
case 0x05D31669: { t.t = U64; goto end; }
case 0x9D7EECFA: { t.t = U128; goto end; }
case 0xDDE47A8F: { t.t = I8; goto end; }
case 0xB1A0BB96: { t.t = I16; goto end; }
case 0x84FB1D0D: { t.t = I32; goto end; }
case 0x10EF4C7D: { t.t = I64; goto end; }
case 0x87B104DD: { t.t = I128; goto end; }
default: { t.t = IDN; goto end; }
}
}
else if (isdigit(C)) { /* Numbers */
s = sdspush(s, C); ++l->sp;
for (; isalnum(C); ++l->sp) { s = sdspush(s, C); }
t.t = NUM; goto end;

Loading…
Cancel
Save