commit 4cf50d04a1684887be767daceeb06c18e9814a2b
parent 06ea4b80b2ded074148bf4dad8c03b2a680d89ed
Author: thing1 <l.standen@posteo.com>
Date: Wed, 22 Oct 2025 19:39:08 +0100
moved the parser some more, need to fix a sr conflict though, something
around decs and the semi colons....
Diffstat:
6 files changed, 47 insertions(+), 25 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,4 +1,4 @@
-YFLAGS=-g
+YFLAGS=-g -Wcounterexamples
CFLAGS=-ggdb
LIBS=-lfl
@@ -12,7 +12,7 @@ parser: fela.y
dot y.gv -Tpdf > g.pdf
fela: lexer parser
- cc lex.yy.c y.tab.c -o fela ${LIBS} ${CFLAGS}
+ cc lex.yy.c y.tab.c gv.c -o fela ${LIBS} ${CFLAGS}
clean:
rm -rf lex.yy.* y.* fela *.html *.pdf
diff --git a/ast.h b/ast.h
@@ -1,8 +1,3 @@
-enum ast_type {
- DEC,
- EXPR,
-};
-
enum expr_ops {
VALUE,
VAR,
diff --git a/fela.y b/fela.y
@@ -4,6 +4,7 @@
#include "ast.h"
#include "util.h"
+#include "gv.h"
#define YYDEBUG 1
@@ -48,24 +49,24 @@ struct ast_decs *body;
body : decs {body = $1;}
;
-decs : decs dec ';' {
- $$ = alloczt(struct ast_decs);
- $$->decs = $1;
- $$->dec = $2;
- }
- | dec ';' {
+decs : dec ';' {
$$ = alloczt(struct ast_decs);
$$->dec = $1;
}
+ | dec ';' decs {
+ $$ = alloczt(struct ast_decs);
+ $$->decs = $3;
+ $$->dec = $1;
+ }
;
-dec : name name {
+dec : name name {
$$ = alloczt(struct ast_dec);
$$->name = $2;
$$->type = $1;
}
- | name name '=' expr {
+ | name name '=' expr {
$$ = alloczt(struct ast_dec);
$$->name = $2;
$$->value = $4;
@@ -80,10 +81,10 @@ dec : name name {
;
-exprs : exprs expr ';' {
+exprs : expr ';' exprs {
$$ = alloczt(struct ast_exprs);
- $$->exprs = $1;
- $$->expr = $2;
+ $$->exprs = $3;
+ $$->expr = $1;
}
| expr ';' {
$$ = alloczt(struct ast_exprs);
@@ -97,7 +98,7 @@ func : FUNC '(' ')' '{' decs exprs '}' {
$$->decs = $5;
$$->exprs = $6;
}
-func : FUNC '(' ')' '{' exprs '}' {
+ | FUNC '(' ')' '{' exprs '}' {
$$ = alloczt(struct ast_expr);
$$->op = FUNCTION;
$$->exprs = $5;
@@ -177,5 +178,7 @@ yyerror(const char *msg) {
int
main() {
yyparse();
- printf("%s\n", body->dec->type);
+ printf("%s", prolog);
+ gv(body, 0, -1);
+ printf("%s", epilog);
}
diff --git a/gv.c b/gv.c
@@ -3,11 +3,25 @@
#include "ast.h"
void
+gv_expr(struct ast_expr *expr, int node, int parent) {
+ switch (expr->op) {
+ case VALUE:
+ printf("%d [label=\"Lit\\l\\n%d\\l\"]\n", node, expr->value);
+ break;
+ }
+ printf("%d -> %d [style=solid]\n", parent, node);
+}
+
+void
gv(struct ast_decs *body, int node, int parent) {
if (parent != -1)
printf("%d -> %d [style=solid]\n", parent, node);
printf("%d ", node);
- printf("[label=\"dec\"\n\l type: %s, name: %s\l", body->dec->type,
+ printf("[label=\"dec\\n\\l type: %s, name: %s\\l\"]\n", body->dec->type,
body->dec->name);
- // if statment to deside if its a function or an expression
+
+ gv_expr(body->dec->value, node + 1, node);
+
+ if (body->decs)
+ gv(body->decs, node + 1, -1);
}
diff --git a/gv.h b/gv.h
@@ -0,0 +1,9 @@
+const char *prolog = "digraph \"fela tree\"\n"
+"{\n"
+"node [fontname = courier, shape = box, colorscheme = paired6]\n"
+"edge [fontname = courier]\n";
+
+const char *epilog = "}\n";
+
+
+void gv(struct ast_decs *, int, int);
diff --git a/test.fe b/test.fe
@@ -1,4 +1,5 @@
-byte num=func() {
- int a = 0;
- rand();
+byte foo = func() {
+ int bar = 10;
+ bar = 20;
};
+