From e1a691b2b04b383f052d16931f5c34d6bc9d9e22 Mon Sep 17 00:00:00 2001
From: David Phillips <david@sighup.nz>
Date: Thu, 28 Dec 2017 13:51:51 +1300
Subject: Rough-work expr/op parser stubs in

---
 common.h |  6 ++++++
 parser.c | 37 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 42 insertions(+), 1 deletion(-)
 create mode 100644 common.h

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
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
-- 
cgit v1.1