diff options
| author | Kai Stevenson <kai@kaistevenson.com> | 2023-06-11 16:18:21 -0700 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2023-06-11 16:18:21 -0700 |
| commit | d063e3c2c6f4b1566c28d0ea92186f356017f99f (patch) | |
| tree | af7b093f016d16a4902a9094d120f1a33bee64b6 /parse.c | |
| parent | 2a232f8f42c0a7d5a7b648358a192c21865a55f6 (diff) | |
parsing propositions works now. added some debugging tools
Diffstat (limited to 'parse.c')
| -rw-r--r-- | parse.c | 67 |
1 files changed, 67 insertions, 0 deletions
@@ -1,8 +1,75 @@ #include <stdlib.h> #include "parse.h" +#include "lexer.h" + +line_t init_line(prop_t proposition, just_t justification) { + line_t line; + line.proposition = proposition; + line.justification = justification; + return line; +} lineSet_t init_lineset(line_t* lines, int count) { lineSet_t lineSet; lineSet.lines = lines; lineSet.count = count; return lineSet; } +int count_lines(char* content) { + int i = 0; + while (*content != '\0') { + if (*content == '\n') { + i++; + } + content++; + } + return i; +} +lineSet_t parse_lines(char* content, int lineCount) { + line_t* lines = malloc(sizeof(line_t) * lineCount); + for (int i = 0; i < lineCount; i++) { + /* parse the line */ + lines[i] = parse_line(content); + /* advance to the next line */ + while (*content != '\n') { + content++; + } + content++; + } + return init_lineset(lines, lineCount); +} +line_t parse_line(char* content) { + tokenSet_t ts = lex_line(content); + int i = 0; + while (ts.tokens[i].type != JUSTIFICATION) { + i++; + } + int* seek = calloc(sizeof(int), 1); + prop_t prop = parse_proposition(init_tokenset(ts.tokens, i), seek); + just_t just; + return init_line(prop, just); +} +prop_t parse_proposition(tokenSet_t tokenSet, int* seek) { + prop_t prop; + if (tokenSet.tokens[*seek].type == OPEN_PAREN) { + (*seek)++; + prop = parse_proposition(tokenSet, seek); + } + else { + prop = init_simple_prop(tokenSet.tokens[*seek].value); + (*seek)++; + } + /* check if we were reading a simple prop, or if there is a relation */ + if (*seek == tokenSet.count || tokenSet.tokens[*seek].type == CLOSE_PAREN) { + return prop; + } + else { + tokenType_t rel = tokenSet.tokens[*seek].type; + (*seek)++; + prop_t* prop1p = malloc(sizeof(prop_t)); + *prop1p = prop; + prop_t* prop2p = malloc(sizeof(prop_t)); + *prop2p = parse_proposition(tokenSet, seek); + prop_t metaProp = init_prop(prop1p, prop2p, rel); + return metaProp; + } +} |
