fela

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 2cfda5fdf657c12a49ff6ac35c78a906e4f6414e
parent 5490ad7eeb50cb24f76f40e38af28af1f74069bd
Author: thing1 <thing1@seacrossedlovers.xyz>
Date:   Mon,  3 Nov 2025 21:59:21 +0000

worked on the parser some more

Diffstat:
MMakefile | 2+-
Aast.c | 10++++++++++
Mast.h | 23++++++++++++++++++++---
Mconfig.mk | 10+++++-----
Mparse.y | 31+++++++++++++++++++++++++++++--
Mtest.fe | 4++--
6 files changed, 67 insertions(+), 13 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,6 +1,6 @@ include config.mk -SRC = parse.c lex.c types.c +SRC = parse.c lex.c types.c ast.c OBJ = ${SRC:.c=.o} all: fela diff --git a/ast.c b/ast.c @@ -0,0 +1,10 @@ +#include <string.h> +#include <stdlib.h> +#include "ast.h" + +struct ast_args * +append_arg(struct ast_args *args, struct ast_arg *arg) { + args->args = realloc(args->args, ++args->argc * sizeof(struct ast_arg)); + memcpy(&args->args[args->argc], arg, sizeof(struct ast_arg)); + return args; +} diff --git a/ast.h b/ast.h @@ -1,3 +1,8 @@ +#ifndef _AST_H_ +#define _AST_H_ + +#include <string.h> + enum expr_ops { VALUE, VAR, @@ -41,13 +46,23 @@ struct ast_type { enum type_types t; union { struct { char *type; }; /* for basic types */ - struct { /* for functions and structs */ - int argc; - struct ast_type *argv, *ret; + struct { /* for structs and funcs */ + struct ast_type *ret; + struct ast_args *args; }; }; }; +struct ast_arg { + struct ast_type *type; + char *name; +}; + +struct ast_args { + int argc; + struct ast_arg *args; +}; + struct ast_dec { char *name; struct ast_type *type; @@ -60,3 +75,5 @@ struct ast_decs { }; +struct ast_args *append_arg(struct ast_args *, struct ast_arg *); +#endif diff --git a/config.mk b/config.mk @@ -1,15 +1,15 @@ -LIBS = -lfl -ly +LIBS = -lfl CC = cc CFLAGS = -ggdb -Werror -Wpedantic # bison -# YACC = bison -# YFLAGS = -d -g -t -y +YACC = bison +YFLAGS = -d -g -t -y # posix yacc -YACC = ../miniyacc/yacc -YFLAGS = -d +#YACC = ../miniyacc/yacc +#YFLAGS = -d LEX = flex LFLAGS = -X diff --git a/parse.y b/parse.y @@ -25,6 +25,8 @@ struct ast_decs *body; struct ast_decs *decs; struct ast_exprs *exprs; struct ast_type *type; + struct ast_arg *arg; + struct ast_args *args; char *s; long l; @@ -41,6 +43,8 @@ struct ast_decs *body; %type <dec> dec %type <decs> decs %type <type> type +%type <args> args +%type <arg> arg %type <s> name @@ -175,7 +179,7 @@ expr : name '=' expr { $$->op = VAR; $$->fn = $1; } - | name '(' ')' { + | name '(' ')' { $$ = alloczt(struct ast_expr); $$->op = FUNC; $$->fn = $1; @@ -193,18 +197,41 @@ type : NAME { $$->t = BASIC_T; $$->type = strslice($1, yyleng); } + | FUNC '(' args ')' RETTYPE type { + $$ = alloczt(struct ast_type); + $$->t = FUNCTION_T; + $$->ret = $6; + $$->args = $3; + } | FUNC '(' ')' RETTYPE type { $$ = alloczt(struct ast_type); $$->t = FUNCTION_T; $$->ret = $5; } + ; + +args : args ',' arg { + $$ = append_arg($1, $3); + } + | arg { + $$ = alloczt(struct ast_args); + $$ = append_arg($$, $1); + } + ; + +arg : type NAME { + $$ = alloczt(struct ast_arg); + $$->type = $1; + $$->name = $2; + } + ; %% int yyerror(const char *msg) { if (strcmp(msg, "syntax error") == 0) { - fprintf(stderr, "%d:%d: syntax error: %s\n", lineno + 1, offset - yyleng, yytext); + fprintf(stderr, "%ld:%ld: syntax error: %s\n", lineno + 1, offset - yyleng, yytext); } else fprintf(stderr, "%s\n", msg); diff --git a/test.fe b/test.fe @@ -1,4 +1,4 @@ -func() -> int main = -(int a) -> int { +func(int a) -> int +main = () -> int { rand(); };