diff options
| author | Kai Stevenson <kai@kaistevenson.com> | 2023-06-13 01:23:42 -0700 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2023-06-13 01:23:42 -0700 |
| commit | 8f4eb9fc953512365f9258ffd5d11ed8950f77a9 (patch) | |
| tree | 113ac7d09640e97fa678f098809b20b63c310286 | |
| parent | 3bd120957d570756d396db8a112113f2ffb8f33b (diff) | |
added indentation support for |as rule
| -rw-r--r-- | lexer.c | 13 | ||||
| -rw-r--r-- | lexer.h | 1 | ||||
| -rw-r--r-- | operetta.c | 3 | ||||
| -rw-r--r-- | parse.c | 21 | ||||
| -rw-r--r-- | parse.h | 3 | ||||
| -rw-r--r-- | testfile | 5 |
6 files changed, 30 insertions, 16 deletions
@@ -1,5 +1,4 @@ #include <stdlib.h> -#include <stdio.h> #include <string.h> #include "lexer.h" @@ -21,6 +20,8 @@ char* tttos(tokenType_t tokenType) { return "ERR"; case EOL: return "EOL"; + case INDENT: + return "INDENT"; case STRING: return "STRING"; case INT: @@ -53,7 +54,7 @@ char* tttos(tokenType_t tokenType) { } int advance_whitespace(char** linePointer) { int64_t a = (int64_t)linePointer; - while (**linePointer == ' ' || **linePointer == ' ') { + while (**linePointer == ' ') { (*linePointer)++; } return (int)((int64_t)linePointer - a); @@ -67,13 +68,11 @@ tokenSet_t lex_line(char* line) { while (1) { token_t t = lex_first_token(p); if (t.type == EOL) { - printf("eol\n"); break; } if (t.type == ERR) { exit(1); } - printf("good\n"); tokens[i] = t; i++; } @@ -89,7 +88,6 @@ token_t lex_first_token(char** content) { s[c] = **content; s[c+1] = '\0'; (*content)++; - printf("lexing: %c\n", s[c]); /* check if we need to parse a string or an int */ if (s[c] >= 65 && s[c] <= 90) { free(s); @@ -114,6 +112,10 @@ token_t lex_first_token(char** content) { 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); @@ -155,7 +157,6 @@ token_t lex_first_token(char** content) { return init_token(BI_IMPLICATION, NULL); } else if (c <= m) { - printf("c: %d\n", c); c++; continue; } @@ -3,6 +3,7 @@ typedef enum TokenType { ERR, EOL, + INDENT, STRING, INT, OPEN_PAREN, @@ -12,6 +12,9 @@ int main(int argc, char** argv) { int lineCount = count_lines(content); lineSet_t lines = parse_lines(content, lineCount); for (int i = 0; i < lines.count; i++) { + for (int j = 0; j < lines.lines[i].aLevel; j++) { + printf(" > "); + } write_prop(lines.lines[i].proposition); printf(" BECAUSE: "); write_just(lines.lines[i].justification); @@ -1,9 +1,11 @@ #include <stdlib.h> #include "parse.h" #include "lexer.h" +#include "stdio.h" -line_t init_line(prop_t proposition, just_t justification) { +line_t init_line(int aLevel, prop_t proposition, just_t justification) { line_t line; + line.aLevel = aLevel; line.proposition = proposition; line.justification = justification; return line; @@ -39,14 +41,19 @@ lineSet_t parse_lines(char* content, int lineCount) { } line_t parse_line(char* content) { tokenSet_t ts = lex_line(content); - int i = 0; - while (ts.tokens[i].type != JUSTIFICATION) { - i++; + int c = 0; + int indent = 0; + while (ts.tokens[c].type != JUSTIFICATION) { + if (ts.tokens[c].type == INDENT) { + indent++; + } + c++; } int* seek = calloc(sizeof(int), 1); - prop_t prop = parse_proposition(init_tokenset(ts.tokens, i), seek, 0); - just_t just = parse_justification(init_tokenset(ts.tokens + i + 1, ts.count - (i + 1))); - return init_line(prop, just); + prop_t prop = parse_proposition(init_tokenset(ts.tokens + indent, c - indent), seek, 0); + just_t just = parse_justification(init_tokenset(ts.tokens + c + 1, ts.count - (c + 1))); + free(seek); + return init_line(indent, prop, just); } prop_t parse_proposition(tokenSet_t tokenSet, int* seek, char isNegative) { prop_t prop; @@ -2,6 +2,7 @@ #define PARSE #include "proposition.h" typedef struct Line { + int aLevel; prop_t proposition; just_t justification; } line_t; @@ -9,7 +10,7 @@ typedef struct LineSet { line_t* lines; int count; } lineSet_t; -line_t init_line(prop_t proposition, just_t justification); +line_t init_line(int aLevel, 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); @@ -1,3 +1,4 @@ Q | pr -KAI^PRESTON | as -~CAT->(DOG^~RABBIT) | ^e 1 2 + KAI^PRESTON | as + ~CAT->(DOG^~RABBIT) | ^e 1 2 +V | pr |
