From c0cb24584cb692cf472ced68d40700ba949cdeee Mon Sep 17 00:00:00 2001 From: lquint <83752971+lquint@users.noreply.github.com> Date: Mon, 28 Jun 2021 15:24:15 +0200 Subject: [PATCH] Add files via upload --- lexer.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 71 insertions(+), 10 deletions(-) diff --git a/lexer.c b/lexer.c index d96b605..74f1dd1 100644 --- a/lexer.c +++ b/lexer.c @@ -59,12 +59,15 @@ char *currentText(Exploration *exploration) return exploration->text + exploration->position; } +struct String { enum opcode type; char *stringValue; int len;}; +struct Class { enum opcode type; char *stringValue; int len;}; struct Node { enum opcode type; union { - char *stringValue; + struct String STRING; + struct Class CLASS; Node *children[2]; }; }; @@ -81,7 +84,8 @@ Node *mkNode(enum opcode type) Node *mkString(char *value) { Node *node= mkNode(STRING); - node->stringValue= value; + node->STRING.stringValue= value; + node->STRING.len=strlen(value); return node; } @@ -119,7 +123,8 @@ Node *mkOr(Node *node1, Node *node2) Node *mkClass(char* str) { Node *node= mkNode(CLASS); - node->stringValue= str; + node->STRING.stringValue= str; + node->STRING.len=strlen(str); return node; } @@ -143,6 +148,63 @@ Node *mkDot() return node; } +Node *_checktype(Node *object, enum opcode type) +{ + if (object->type == type) return object; + fprintf(stderr, "\naccesing type %i as if it were a %i\n", object->type, type); + exit(1); + return 0; +} + +#define get(object, type, member) (_checktype(object, type)->type.member) + +const char* getTypeName(enum opcode type) +{ + switch (type) + { + case STAR: return "STAR"; + case EXCLAMATION_MARK: return "NOT"; + case DOT: return "DOT"; + case QUESTION_MARK: return "QMARK"; + case PLUS : return "PLUS"; + case CLASS : return "CLASS"; + default : printf("unexpected use");return 0; + abort(); + } +} + +void println(Node *node, int indent) +{ + for(int i=0;itype) { + case STRING: printf("%.*s", get(node, STRING, len), get(node, STRING, stringValue)); return; + case AND: { + printf("AND\n"); + println(node->children[0],indent);printf("\n"); + println(node->children[1],indent);printf("\n");return; + } + case OR: { + printf("OR\n"); + println(node->children[0],indent);printf("\n"); + println(node->children[1],indent);printf("\n");return; + } + case DOT:{ + printf("DOT\n");return; + } + case CLASS:{ + printf("CLASS : Char in [%.*s]\n",get(node, CLASS, len),get(node, CLASS, stringValue));return; + } + default : { + printf("%s\n",getTypeName(node->type)); + println(node->children[0],indent);printf("\n");return; + } + } + abort(); +} @@ -150,11 +212,10 @@ int execute(Node *node, Exploration *in) { switch (node->type) { case STRING: { - int length= strlen(node->stringValue); - if (strncmp(currentText(in), node->stringValue, length)) { + if (strncmp(currentText(in), node->STRING.stringValue, node->STRING.len)) { return 0; } - advance(in, length); + advance(in, node->STRING.len); return 1; } case AND: { @@ -190,7 +251,7 @@ int execute(Node *node, Exploration *in) if (!currentChar(in)) { return 0; } - if (strchr(node->stringValue, currentChar(in))) { + if (strchr(node->STRING.stringValue, currentChar(in))) { advance(in, 1); return 1; } @@ -243,7 +304,7 @@ int main(int argc, char **argv) Node *program= mkStar(mkClass("abc")); - + Node *program3=mkAnd(mkAnd(mkPlus(mkString("hello world")),mkExclamationMark(mkString("how are u world !?"))),mkStar(mkClass("abcde"))); if (!execute(program, in) || !atEnd(in)) { @@ -252,8 +313,8 @@ int main(int argc, char **argv) { printf("match, current position : %i\n", getPosition(in)); }// 0 => no match, 1 => match - + println(program3,0); return 0; -} +} \ No newline at end of file