summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2023-06-13 01:23:42 -0700
committerKai Stevenson <kai@kaistevenson.com>2023-06-13 01:23:42 -0700
commit8f4eb9fc953512365f9258ffd5d11ed8950f77a9 (patch)
tree113ac7d09640e97fa678f098809b20b63c310286
parent3bd120957d570756d396db8a112113f2ffb8f33b (diff)
added indentation support for |as rule
-rw-r--r--lexer.c13
-rw-r--r--lexer.h1
-rw-r--r--operetta.c3
-rw-r--r--parse.c21
-rw-r--r--parse.h3
-rw-r--r--testfile5
6 files changed, 30 insertions, 16 deletions
diff --git a/lexer.c b/lexer.c
index 5df1103..c6c4ffe 100644
--- a/lexer.c
+++ b/lexer.c
@@ -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;
}
diff --git a/lexer.h b/lexer.h
index ff7ab85..e5d64dc 100644
--- a/lexer.h
+++ b/lexer.h
@@ -3,6 +3,7 @@
typedef enum TokenType {
ERR,
EOL,
+ INDENT,
STRING,
INT,
OPEN_PAREN,
diff --git a/operetta.c b/operetta.c
index f399093..2c4f555 100644
--- a/operetta.c
+++ b/operetta.c
@@ -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);
diff --git a/parse.c b/parse.c
index 6bea4af..75be330 100644
--- a/parse.c
+++ b/parse.c
@@ -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;
diff --git a/parse.h b/parse.h
index 80f92e2..320ca45 100644
--- a/parse.h
+++ b/parse.h
@@ -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);
diff --git a/testfile b/testfile
index af46bfb..5f07502 100644
--- a/testfile
+++ b/testfile
@@ -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