diff options
author | David Phillips <david@sighup.nz> | 2017-12-28 13:51:51 +1300 |
---|---|---|
committer | David Phillips <david@sighup.nz> | 2017-12-28 13:51:51 +1300 |
commit | e1a691b2b04b383f052d16931f5c34d6bc9d9e22 (patch) | |
tree | f3a3d4a5c7274e0b62f5bca4f30541010d01d839 | |
parent | f23abfdde683aee4b808f2795af1a1418125156c (diff) | |
download | hence-e1a691b2b04b383f052d16931f5c34d6bc9d9e22.tar.xz |
Rough-work expr/op parser stubs in
-rw-r--r-- | common.h | 6 | ||||
-rw-r--r-- | parser.c | 37 |
2 files changed, 42 insertions, 1 deletions
diff --git a/common.h b/common.h new file mode 100644 index 0000000..e5c0ab4 --- /dev/null +++ b/common.h @@ -0,0 +1,6 @@ +#ifndef COMMON_H +#define COMMON_H + +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) + +#endif @@ -2,6 +2,7 @@ #include <stdio.h> #include <string.h> +#include "common.h" #include "error.h" #include "logic.h" #include "wire.h" @@ -52,6 +53,20 @@ static struct bop_lookup bop_handlers[] = { {.str = "xor", .handler = logic_xor} }; +int +expect(const char *expect, char *actual) { + int min_len = 0; + + min_len = MIN(strlen(expect), strlen(actual)); + + if (strncmp(expect, actual, min_len) != 0) { + emit_error("Expected '%s' at start of '%s'\n", expect, actual); + return 1; + } + return 0; +} + + int parse_uop(char *str, enum BINARY (*handler)(enum BINARY)) { /**/ (void)str; @@ -73,6 +88,7 @@ parse_op(char *str) { int match = 0; size_t i = 0; + strtok(str, " "); match = 0; for (i = 0; i < sizeof(bop_handlers)/sizeof(bop_handlers[0]); i++) { if (strcmp(bop_handlers[i].str, str) == 0) { @@ -99,8 +115,27 @@ parse_op(char *str) { int parse_expr(char *str) { + const char *expr_sep = ": "; + size_t ident_len = 0; + char *ident = NULL; + char *op = NULL; + printf("expression %s\n", str); - return 0; + + /* FIXME magic constant string */ + ident_len = strspn(str, "abcdefghijklmnopqrstuvwxyz0123456789"); + + if (expect(expr_sep, str + ident_len)) { + return -1; + } + + str[ident_len] = '\0'; + ident = str; + op = str + ident_len + strlen(expr_sep); + + /* FIXME do something with the identifier */ + + return parse_op(op); } int |