From 1ce0d45242097a07b7a4ee539a074ec812851a58 Mon Sep 17 00:00:00 2001 From: Mistivia Date: Wed, 5 Mar 2025 18:31:55 +0800 Subject: finish parser --- src/as_op.c | 28 ++++++++++++++++++++++++++++ src/as_op.h | 14 ++++++++++++++ src/as_parser.c | 52 ++++++++++++++++++++++++++++++++++++++++++---------- src/as_parser.h | 24 +++++++----------------- src/as_tokenizer.c | 6 +++--- src/as_tokenizer.h | 7 ++++++- 6 files changed, 100 insertions(+), 31 deletions(-) create mode 100644 src/as_op.c create mode 100644 src/as_op.h diff --git a/src/as_op.c b/src/as_op.c new file mode 100644 index 0000000..847214d --- /dev/null +++ b/src/as_op.c @@ -0,0 +1,28 @@ +#include "as_op.h" + +#include + +struct opTableEntry{ + Op op; + const char* name; +}; +typedef struct opTableEntry OpTableEntry; + +OpTableEntry opTable[] = { + {ADD, "add"}, + {SUB, "sub"}, + {MUL, "mul"}, + {DIV, "div"}, + {MOD, "mod"}, + {EQ, "eq"}, + {OPEND, NULL} +}; + +Op str2op(const char* str) { + for (int i = 0; opTable[i].name != NULL; i++) { + if (strcmp(opTable[i].name, str) == 0) { + return opTable[i].op; + } + } + return OPEND; +} diff --git a/src/as_op.h b/src/as_op.h new file mode 100644 index 0000000..ec88670 --- /dev/null +++ b/src/as_op.h @@ -0,0 +1,14 @@ +#ifndef FVM_AS_OP_H_ +#define FVM_AS_OP_H_ + +enum op { + ADD, SUB, MUL, DIV, MOD, EQ, + // place holder for the end of the table + OPEND +}; +typedef enum op Op; + +Op str2op(const char *str); + +#endif + diff --git a/src/as_parser.c b/src/as_parser.c index d80cdd9..b419be5 100644 --- a/src/as_parser.c +++ b/src/as_parser.c @@ -7,9 +7,9 @@ // // ::= // ::= | -// ::= | | -// ::= | |