summaryrefslogtreecommitdiff
path: root/src/lexer.c
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2023-06-13 14:50:00 -0700
committerKai Stevenson <kai@kaistevenson.com>2023-06-13 14:50:00 -0700
commit1d3bbec4660dfe4e2117826ceef9b32758b40345 (patch)
tree7c33cdf17774a122af89f3ae7743771f047942a7 /src/lexer.c
parentcaa9d17ea794a0b4823170551545498d458e1fc2 (diff)
restructured project
Diffstat (limited to 'src/lexer.c')
-rw-r--r--src/lexer.c198
1 files changed, 198 insertions, 0 deletions
diff --git a/src/lexer.c b/src/lexer.c
new file mode 100644
index 0000000..c6c4ffe
--- /dev/null
+++ b/src/lexer.c
@@ -0,0 +1,198 @@
+#include <stdlib.h>
+#include <string.h>
+#include "lexer.h"
+
+token_t init_token(tokenType_t type, void* value) {
+ token_t t;
+ t.type = type;
+ t.value = value;
+ return t;
+}
+tokenSet_t init_tokenset(token_t* tokens, int count) {
+ tokenSet_t tokenSet;
+ tokenSet.tokens = tokens;
+ tokenSet.count = count;
+ return tokenSet;
+}
+char* tttos(tokenType_t tokenType) {
+ switch (tokenType) {
+ case ERR:
+ return "ERR";
+ case EOL:
+ return "EOL";
+ case INDENT:
+ return "INDENT";
+ case STRING:
+ return "STRING";
+ case INT:
+ return "INT";
+ case OPEN_PAREN:
+ return "OPEN_PAREN";
+ case CLOSE_PAREN:
+ return "CLOSE_PAREN";
+ case START_COMMENT:
+ return "START_COMMENT";
+ case JUSTIFICATION:
+ return "JUSTIFICATION";
+ case PREMISE:
+ return "PREMISE";
+ case NEGATION:
+ return "NEGATION";
+ case ASSUMPTION:
+ return "ASSUMPTION";
+ case CONJUNCTION:
+ return "CONJUNCTION";
+ case DISJUNCTION:
+ return "DISJUNCTION";
+ case IMPLICATION:
+ return "IMPLICATION";
+ case BI_IMPLICATION:
+ return "BI_IMPLICATION";
+ default:
+ return "???";
+ }
+}
+int advance_whitespace(char** linePointer) {
+ int64_t a = (int64_t)linePointer;
+ while (**linePointer == ' ') {
+ (*linePointer)++;
+ }
+ return (int)((int64_t)linePointer - a);
+}
+tokenSet_t lex_line(char* line) {
+ char** p = malloc(sizeof(char*));
+ *p = line;
+ int m = 50;
+ token_t* tokens = malloc(sizeof(token_t) * m);
+ int i = 0;
+ while (1) {
+ token_t t = lex_first_token(p);
+ if (t.type == EOL) {
+ break;
+ }
+ if (t.type == ERR) {
+ exit(1);
+ }
+ tokens[i] = t;
+ i++;
+ }
+ free(p);
+ return init_tokenset(tokens, i);
+}
+token_t lex_first_token(char** content) {
+ int m = 5;
+ char* s = malloc(sizeof(char) * m);
+ int c = 0;
+ advance_whitespace(content);
+ while (**content != '\n') {
+ s[c] = **content;
+ s[c+1] = '\0';
+ (*content)++;
+ /* check if we need to parse a string or an int */
+ if (s[c] >= 65 && s[c] <= 90) {
+ free(s);
+ (*content)--;
+ return lex_first_string(content);
+ }
+ else if (s[c] >= 48 && s[c] <= 57) {
+ free(s);
+ (*content)--;
+ return lex_first_int(content);
+ }
+ /* otherwise, check to see if a symbol has been matched */
+ if (strcmp(s, "(") == 0) {
+ free(s);
+ return init_token(OPEN_PAREN, NULL);
+ }
+ else if (strcmp(s, ")") == 0) {
+ free(s);
+ return init_token(CLOSE_PAREN, NULL);
+ }
+ else if (strcmp(s, "#") == 0) {
+ free(s);
+ return init_token(START_COMMENT, NULL);
+ }
+ else if (strcmp(s, " ") == 0) {
+ free(s);
+ return init_token(INDENT, NULL);
+ }
+ else if (strcmp(s, "|") == 0) {
+ free(s);
+ return init_token(JUSTIFICATION, NULL);
+ }
+ else if (strcmp(s, "pr") == 0) {
+ free(s);
+ return init_token(PREMISE, NULL);
+ }
+ else if (strcmp(s, "as") == 0) {
+ free(s);
+ return init_token(ASSUMPTION, NULL);
+ }
+ else if (strcmp(s, "i") == 0) {
+ free(s);
+ return init_token(INTRODUCTION, NULL);
+ }
+ else if (strcmp(s, "e") == 0) {
+ free(s);
+ return init_token(ELIMINATION, NULL);
+ }
+ else if (strcmp(s, "~") == 0) {
+ free(s);
+ return init_token(NEGATION, NULL);
+ }
+ else if (strcmp(s, "^") == 0) {
+ free(s);
+ return init_token(CONJUNCTION, NULL);
+ }
+ else if (strcmp(s, "v") == 0) {
+ free(s);
+ return init_token(DISJUNCTION, NULL);
+ }
+ else if (strcmp(s, "->") == 0) {
+ free(s);
+ return init_token(IMPLICATION, NULL);
+ }
+ else if (strcmp(s, "<-") == 0) {
+ free(s);
+ return init_token(BI_IMPLICATION, NULL);
+ }
+ else if (c <= m) {
+ c++;
+ continue;
+ }
+ else {
+ free(s);
+ return init_token(ERR, NULL);
+ }
+ }
+ return init_token(EOL, NULL);
+}
+token_t lex_first_string(char** content) {
+ int m = 50;
+ char* s = malloc(sizeof(char) * m);
+ int c = 0;
+ advance_whitespace(content);
+ while (**content >= 65 && **content <= 90) {
+ s[c] = **content;
+ (*content)++;
+ c++;
+ }
+ s[c] = '\0';
+ return init_token(STRING, s);
+}
+token_t lex_first_int(char** content) {
+ int m = 10;
+ char* s = malloc(sizeof(char) * m);
+ int c = 0;
+ advance_whitespace(content);
+ while (**content >= 48 && **content <= 57) {
+ s[c] = **content;
+ (*content)++;
+ c++;
+ }
+ s[c] = '\0';
+ int* ip = malloc(sizeof(int));
+ *ip = atoi(s);
+ free(s);
+ return init_token(INT, (void*)ip);
+}