summaryrefslogtreecommitdiff
path: root/parser.c
diff options
context:
space:
mode:
authorDavid Phillips <david@sighup.nz>2017-12-28 13:51:51 +1300
committerDavid Phillips <david@sighup.nz>2017-12-28 13:51:51 +1300
commite1a691b2b04b383f052d16931f5c34d6bc9d9e22 (patch)
treef3a3d4a5c7274e0b62f5bca4f30541010d01d839 /parser.c
parentf23abfdde683aee4b808f2795af1a1418125156c (diff)
downloadhence-e1a691b2b04b383f052d16931f5c34d6bc9d9e22.tar.xz
Rough-work expr/op parser stubs in
Diffstat (limited to 'parser.c')
-rw-r--r--parser.c37
1 files changed, 36 insertions, 1 deletions
diff --git a/parser.c b/parser.c
index b2218b1..2f08f46 100644
--- a/parser.c
+++ b/parser.c
@@ -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