diff options
Diffstat (limited to 'parser.c')
-rw-r--r-- | parser.c | 85 |
1 files changed, 28 insertions, 57 deletions
@@ -7,45 +7,21 @@ #include "logic.h" #include "gate.h" -enum STATEMENT_TYPE { - DECL_EXPR, - DECL_INPUT, - DECL_OUTPUT, - MODULE_NAME -}; - -enum UNARY_OPERATOR { - UOP_NOT -}; - -enum BINARY_OPERATOR { - BOP_AND, - BOR_OR, - BOP_NAND, - BOP_NOR, - BOP_XOR -}; - struct tok_lookup { char *str; int (*handler)(char*); }; -struct bop_lookup { +struct op_lookup { char *str; enum BINARY (*handler)(enum BINARY, enum BINARY); }; -struct uop_lookup { - char *str; - enum BINARY (*handler)(enum BINARY); -}; - -static struct uop_lookup uop_handlers[] = { - {.str = "not", .handler = logic_not}, +static struct op_lookup uop_handlers[] = { + {.str = "not", .handler = logic_nand}, }; -static struct bop_lookup bop_handlers[] = { +static struct op_lookup bop_handlers[] = { {.str = "and", .handler = logic_and}, {.str = "or", .handler = logic_or}, {.str = "nand", .handler = logic_nand}, @@ -67,24 +43,32 @@ expect(const char *expect, char *actual) { } -int parse_uop(char *str, enum BINARY (*handler)(enum BINARY)) { - /**/ +int parse_uop(char *str, enum BINARY (*handler)(enum BINARY, enum BINARY)) { (void)str; (void)handler; emit_error("uop unimplemented\n"); - return 0; + /* FIXME add gate with specified handler, tie both inputs together */ + return 1; } -int parse_bop(char *str, enum BINARY (*handler)(enum BINARY, enum BINARY)) { - /**/ - (void)str; - (void)handler; - emit_error("bop unimplemented\n"); +int parse_bop(char *str, char *name, enum BINARY (*handler)(enum BINARY, enum BINARY)) { + char *gate_name1 = str; + char *gate_name2 = NULL; + + strtok(gate_name1, " "); + gate_name2 = gate_name1 + strlen(gate_name1) + 1; + strtok(gate_name2, " "); + + /* FIXME allow input from other gates, not just inputs */ + struct gate *in1 = gate_get_input_by_name(gate_name1); + struct gate *in2 = gate_get_input_by_name(gate_name2); + + gate_add(name, handler, in1, in2); return 0; } int -parse_op(char *str) { +parse_op(char *str, char *name) { int match = 0; size_t i = 0; @@ -93,7 +77,7 @@ parse_op(char *str) { for (i = 0; i < sizeof(bop_handlers)/sizeof(bop_handlers[0]); i++) { if (strcmp(bop_handlers[i].str, str) == 0) { match = 1; - if (parse_bop("FIXME", bop_handlers[i].handler)) { + if (parse_bop(str+strlen(str)+1, name, bop_handlers[i].handler)) { return 1; } } @@ -120,8 +104,6 @@ parse_expr(char *str) { char *ident = NULL; char *op = NULL; - printf("expression %s\n", str); - /* FIXME magic constant string */ ident_len = strspn(str, "abcdefghijklmnopqrstuvwxyz0123456789"); @@ -133,26 +115,13 @@ parse_expr(char *str) { ident = str; op = str + ident_len + strlen(expr_sep); - /* FIXME do something with the identifier */ - - return parse_op(op); + return parse_op(op, ident); } int -parse_node(char *str) { - enum NODE_TYPE type = NODE_OUTPUT; - +parse_input(char *str) { strtok(str, " "); - if (strcmp(str, "output") == 0) { - type = NODE_OUTPUT; - } else if (strcmp(str, "input") == 0) { - type = NODE_INPUT; - } else { - emit_error("'%s' is not a valid node type\n", str); - return 1; - } - - printf("FIXME need to add node of type %s\n", type == NODE_OUTPUT ? "oot" : "innnn"); + gate_input_add(str); return 0; } @@ -166,7 +135,7 @@ parse_module(char *str) { static struct tok_lookup tok_handlers[] = { - {.str = "node", .handler = parse_node}, + {.str = "input", .handler = parse_input}, {.str = "module", .handler = parse_module}, {.str = "expr", .handler = parse_expr} }; @@ -216,6 +185,8 @@ int main(void) { return 1; } } + gate_dump(); + return 0; } |