commit 05071072441230e13cdb46e48a02a02642e961e6
parent a58ba03db785901e78e4b9b45329ba4a1e334f59
Author: thing1 <l.standen@posteo.com>
Date: Tue, 21 Oct 2025 22:31:49 +0100
added more full function support via string slicing
Diffstat:
4 files changed, 25 insertions(+), 15 deletions(-)
diff --git a/TODO.md b/TODO.md
@@ -0,0 +1 @@
+fix strslice to make it not have a space (might need lex tweaks)
diff --git a/ast.h b/ast.h
@@ -46,7 +46,3 @@ struct ast_decs {
struct ast_dec *dec;
};
-typedef struct ast {
- struct ast_decs *decs;
- struct ast_exprs *exprs;
-} ast;
diff --git a/fela.y b/fela.y
@@ -10,10 +10,10 @@
extern char *yytext;
extern int yylex();
int yyerror(const char *);
+struct ast_decs *body;
%}
%union {
- struct ast *tree;
struct ast_dec *dec;
struct ast_expr *expr;
struct ast_decs *decs;
@@ -28,7 +28,7 @@ int yyerror(const char *);
%token <l> NUMBER
%token FUNC
-%type <tree> body
+%type <decs> body
%type <expr> expr
%type <expr> func
%type <exprs> exprs
@@ -39,13 +39,11 @@ int yyerror(const char *);
%left '+' '-'
%left '*' '/'
%left '(' ')'
+%left '='
%%
-body : decs {
- $$ = alloczt(struct ast);
- $$->decs = $1;
- }
+body : decs {body = $1;}
;
decs : decs dec ';' {
@@ -62,15 +60,16 @@ decs : decs dec ';' {
dec : NAME NAME {
$$ = alloczt(struct ast_dec);
- $$->name = strdup($2);
+ $$->name = strslice($2, yylen);
}
| NAME NAME '=' expr {
$$ = alloczt(struct ast_dec);
- $$->name = strdup($2);
+ $$->name = strslice($2, yylen);
$$->value = $4;
}
| NAME NAME '=' func {
$$ = alloczt(struct ast_dec);
+ $$->name = strslice($2, yylen);
$$->value = $4;
}
@@ -100,7 +99,14 @@ func : FUNC '(' ')' '{' exprs '}' {
}
;
-expr : expr '+' expr {
+expr : NAME '=' expr {
+ $$ = alloczt(struct ast_expr);
+ $$->op = ADD;
+ $$->children = allocztn(struct ast_expr, 2);
+ $$->fn = strslice($1, yylen);
+ $$->children[0] = *$3;
+ }
+ | expr '+' expr {
$$ = alloczt(struct ast_expr);
$$->op = ADD;
$$->children = allocztn(struct ast_expr, 2);
@@ -143,12 +149,12 @@ expr : expr '+' expr {
| NAME {
$$ = alloczt(struct ast_expr);
$$->op = VAR;
- $$->fn = $1;
+ $$->fn = strslice($1, yylen);
}
| NAME '(' ')' {
$$ = alloczt(struct ast_expr);
$$->op = FUNC;
- $$->fn = $1;
+ $$->fn = strslice($1, yylen);
}
;
%%
diff --git a/util.h b/util.h
@@ -12,4 +12,11 @@ allocz(const size_t size) {
return p;
}
+char *
+strslice(char *s, int len) {
+ char *slice = allocz(len + 1);
+ memcpy(slice, s, len);
+ return slice;
+}
+
/* TODO make this allocate to an arena that can be freed */