diff options
| -rw-r--r-- | lexer.c | 58 | ||||
| -rw-r--r-- | lexer.h | 17 | ||||
| -rw-r--r-- | operetta.c | 26 | ||||
| -rw-r--r-- | operetta.h | 4 | ||||
| -rw-r--r-- | parse.c | 67 | ||||
| -rw-r--r-- | parse.h | 5 | ||||
| -rw-r--r-- | proposition.c | 14 | ||||
| -rw-r--r-- | proposition.h | 3 | ||||
| -rw-r--r-- | testfile | 3 |
9 files changed, 162 insertions, 35 deletions
@@ -9,11 +9,41 @@ token_t init_token(tokenType_t type, void* value) { t.value = value; return t; } -linkList_t* init_linklist(void* value, linkList_t* next) { - linkList_t* ll = calloc(sizeof(linkList_t), 1); - ll->value = value; - ll->next = next; - return ll; +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 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 ASSUMPTION: + return "ASSUMPTION"; + case CONJUNCTION: + return "CONJUNCTION"; + case DISJUNCTION: + return "DISJUNCTION"; + default: + return "???"; + } } int advance_whitespace(char** linePointer) { int64_t a = (int64_t)linePointer; @@ -22,11 +52,12 @@ int advance_whitespace(char** linePointer) { } return (int)((int64_t)linePointer - a); } -linkList_t* lex_line(char* line) { +tokenSet_t lex_line(char* line) { char** p = malloc(sizeof(char*)); *p = line; - linkList_t* root = init_linklist(NULL, NULL); - linkList_t* ll = root; + 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) { @@ -36,17 +67,12 @@ linkList_t* lex_line(char* line) { if (t.type == ERR) { exit(1); } - if (ll->value != NULL) { - ll->next = init_linklist(NULL, NULL); - ll = ll->next; - } - token_t* tp = malloc(sizeof(token_t)); printf("good\n"); - *tp = t; - ll->value = tp; + tokens[i] = t; + i++; } free(p); - return root; + return init_tokenset(tokens, i); } token_t lex_first_token(char** content) { int m = 5; @@ -17,22 +17,21 @@ typedef enum TokenType { BI_IMPLICATION, EXPLOSION, ELIMINATION, - INTRODUCTION, + INTRODUCTION } tokenType_t; - typedef struct Token { tokenType_t type; void* value; } token_t; token_t init_token(tokenType_t type, void* value); -typedef struct LinkedList { - void* value; - struct LinkedList* next; -} linkList_t; - -linkList_t* init_linklist(void* value, linkList_t* next); +typedef struct TokenSet { + token_t* tokens; + int count; +} tokenSet_t; +tokenSet_t init_tokenset(token_t* tokens, int count); +char* tttos(tokenType_t tokenType); int advance_whitespace(char** linePointer); -linkList_t* lex_line(char* line); +tokenSet_t lex_line(char* line); token_t lex_first_token(char** content); token_t lex_first_string(char** content); token_t lex_first_int(char** content); @@ -9,18 +9,26 @@ int main(int argc, char** argv) { /* read file */ char* path = argv[1]; char* content = read_file(path); - linkList_t* ll = lex_line(content); - linkList_t* l1 = ll; - while (1) { - token_t* t = (token_t*)l1->value; - printf("%d \n", t->type); - if (l1->next == NULL) { - break; - } - l1 = l1->next; + int lineCount = count_lines(content); + lineSet_t lines = parse_lines(content, lineCount); + for (int i = 0; i < lines.count; i++) { + write_prop(lines.lines[i].proposition); + printf("\n"); } free(content); } +void write_prop(prop_t prop) { + if (prop.isSimple) { + printf("%s", prop.name); + } + else { + printf("("); + write_prop(*(prop.propA)); + printf(" %s ", tttos(prop.relation)); + write_prop(*(prop.propB)); + printf(")"); + } +} char* read_file(char* path) { FILE* file = fopen(path, "r"); if (file == NULL) { @@ -1,4 +1,8 @@ #ifndef OPERETTA #define OPERETTA +#include "parse.h" +#include "proposition.h" +void write_line(line_t line); +void write_prop(prop_t prop); char* read_file(char* path); #endif @@ -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; + } +} @@ -9,5 +9,10 @@ typedef struct LineSet { line_t* lines; int count; } lineSet_t; +line_t init_line(prop_t proposition, just_t justification); lineSet_t init_lineset(line_t* lines, int count); +int count_lines(char* content); +lineSet_t parse_lines(char* content, int lineCount); +line_t parse_line(char* content); +prop_t parse_proposition(tokenSet_t tokenSet, int* tokensRead); #endif diff --git a/proposition.c b/proposition.c index e69de29..0919aa8 100644 --- a/proposition.c +++ b/proposition.c @@ -0,0 +1,14 @@ +#include "proposition.h" +prop_t init_prop(prop_t* propA, prop_t* propB, tokenType_t relation) { + prop_t prop; + prop.propA = propA; + prop.propB = propB; + prop.relation = relation; + return prop; +} +prop_t init_simple_prop(char* name) { + prop_t prop; + prop.name = name; + prop.isSimple = 1; + return prop; +} diff --git a/proposition.h b/proposition.h index 5ffb009..d2f5ac6 100644 --- a/proposition.h +++ b/proposition.h @@ -5,8 +5,11 @@ typedef struct Proposition { struct Proposition* propA; struct Proposition* propB; tokenType_t relation; + char* name; char isSimple; } prop_t; +prop_t init_prop(prop_t* propA, prop_t* propB, tokenType_t relation); +prop_t init_simple_prop(char* name); typedef struct Justification { tokenType_t rule; } just_t; @@ -1,2 +1,3 @@ QvP | pr -T^L | pr +KAI^PRESTON | as +CATv(DOG^RABBIT) | pr |
