commit 7b3dafb8d828d47f183c4d38045c3fbb8b926bfb
parent 06ce9ce7de6801f702b53ff84edb6107d9682838
Author: thing1 <thing1@seacrossedlovers.xyz>
Date: Fri, 21 Nov 2025 16:41:43 +0000
made brackets work
Diffstat:
| M | Makefile | | | 4 | +++- |
| M | spl.c | | | 44 | +++++++++++++++++++++++++++++++------------- |
2 files changed, 34 insertions(+), 14 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,4 +1,6 @@
-CFLAGS=-ggdb
+CFLAGS = -fsanitize=address -ggdb -pedantic
+CPPFLAGS = -D_POSIX_C_SOURCE
+LDFLAGS = -fsanitize=address
SRC = spl.c lexer.c util.c
OBJ = ${SRC:.c=.o}
diff --git a/spl.c b/spl.c
@@ -21,6 +21,8 @@ typedef struct rexpr {
};
} rexpr;
+rexpr *pivotParse(tok *start, tok *end, mctx *ctx);
+
void
parserErr(const char *msg) {
fprintf(stderr, "%s\n", msg);
@@ -30,8 +32,9 @@ parserErr(const char *msg) {
int
getpres(tok t) {
switch (t.op) {
- case OBRACE:
- return 0;
+ case LEOF:
+ return -1;
+
case ADD:
return 1;
case SUB:
@@ -42,11 +45,12 @@ getpres(tok t) {
case DIV:
return 2;
- case LEOF:
- return -1;
case INT:
+ case OBRACE:
+ case CBRACE:
return 3;
}
+ parserErr("Not an op");
}
int
@@ -55,6 +59,18 @@ isop(tok t) {
}
rexpr *
+parseBrace(tok *lowest, mctx *ctx) {
+ tok *endbrace;
+ int d = 1;
+ for (int i = 1; d != 0; i++) {
+ if (lowest[i].op == '(') d++;
+ else if (lowest[i].op == ')') d--;
+ endbrace = &lowest[i];
+ }
+ return pivotParse(&lowest[1], &endbrace[-1], ctx);
+}
+
+rexpr *
pivotParse(tok *start, tok *end, mctx *ctx) {
rexpr *e = alloczctx(ctx, sizeof(rexpr));
tok *lowest = start;
@@ -62,6 +78,13 @@ pivotParse(tok *start, tok *end, mctx *ctx) {
for (tok *t = lowest; t != end; i++, t = &start[i]) {
if (getpres(*t) < getpres(*lowest))
lowest = t;
+ if (t->op == '(') {
+ while (t->op != ')') {
+ i++;
+ t = &start[i];
+ }
+ i--;
+ }
}
if (lowest == start) {
switch (lowest->op) {
@@ -72,21 +95,16 @@ pivotParse(tok *start, tok *end, mctx *ctx) {
e->n = lowest->n;
break;
case OBRACE:
- tok *endbrace;
- int d = 1;
- for (int i = 1; d != 0; i++) { /* this doesn't work */
- if (lowest[i].op == '(') d++;
- else if (lowest[i].op == ')') d--;
- endbrace = &lowest[i];
- } /* make it find the end brace -1 */
- e = pivotParse(lowest, endbrace, ctx);
+ e = parseBrace(lowest, ctx);
break;
default:
parserErr("Unexpected token type");
}
} else {
- if (!isop(*lowest))
+ if (!isop(*lowest)) {
+ printf("%c, %d\n", lowest->op, lowest->n);
parserErr("Expected op");
+ }
e->op = lowest->op;
e->expr[0] = pivotParse(start, &lowest[-1], ctx);
e->expr[1] = pivotParse(&lowest[1], end, ctx);