From b79902cd36b2c5b54bbf0f07078fc528754977b2 Mon Sep 17 00:00:00 2001 From: hachem Date: Sat, 19 Sep 2026 14:38:55 +0200 Subject: feat: variables and shi --- sfinae.y | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'sfinae.y') diff --git a/sfinae.y b/sfinae.y index 0d7f0ee..e142aaa 100644 --- a/sfinae.y +++ b/sfinae.y @@ -3,6 +3,7 @@ #include "lexer.h" #include +#include struct Node* program_root = NULL; @@ -12,6 +13,7 @@ void yyerror(const char* message); %union { char character; + struct Name name; struct Node* node; } @@ -19,6 +21,9 @@ void yyerror(const char* message); %token TOK_EMIT %token TOK_STATIC_ASSERT %token TOK_COMMIT +%token TOK_DECLARE +%token TOK_NAME +%token TOK_VALUE_OF %token TOK_VOID %token TOK_INT32 %token TOK_BYTE @@ -43,6 +48,7 @@ void yyerror(const char* message); %token TOK_LESS %token TOK_CHAR +%type name char_list %type statements statement type value condition %% @@ -64,6 +70,30 @@ statement { $$ = node_unary(STMT_EMIT, $4); } | TOK_COLONCOLON TOK_STATIC_ASSERT '<' condition '>' TOK_COLONCOLON TOK_COMMIT { $$ = node_unary(STMT_STATIC_ASSERT, $4); } + | TOK_COLONCOLON TOK_DECLARE '<' name ',' type '>' TOK_COLONCOLON TOK_COMMIT + { $$ = node_named(STMT_DECLARE, &$4, $6); } + ; + +name + : TOK_COLONCOLON TOK_NAME '<' char_list '>' + { $$ = $4; } + ; + +char_list + : TOK_CHAR + { + name_init(&$$); + (void)name_push(&$$, $1); + } + | char_list ',' TOK_CHAR + { + $$ = $1; + if (!name_push(&$$, $3)) + { + yyerror("that name is too long, who are you"); + YYERROR; + } + } ; type @@ -94,6 +124,8 @@ value { $$ = node_binary(VALUE_PRODUCT, $4, $6); } | TOK_COLONCOLON TOK_QUOTIENT '<' value ',' value '>' { $$ = node_binary(VALUE_QUOTIENT, $4, $6); } + | TOK_COLONCOLON TOK_VALUE_OF '<' name '>' + { $$ = node_named(VALUE_VARIABLE, &$4, NULL); } | TOK_COLONCOLON TOK_EXTENT '<' type '>' TOK_COLONCOLON TOK_VALUE { $$ = node_unary(VALUE_EXTENT, $4); } | TOK_COLONCOLON TOK_TUPLE_SIZE '<' type '>' TOK_COLONCOLON TOK_VALUE @@ -124,6 +156,7 @@ void yyerror(const char* message) { if (!lexer_failed()) { - (void)fprintf(stderr, "line %d: %s, did you forget to commit\n", yylineno, message); + (void)fprintf(stderr, "line %d: %s%s\n", yylineno, message, + strcmp(message, "syntax error") == 0 ? ", did you forget to commit" : ""); } } -- cgit v1.3