From b4957586e4930dafdd8d7efe5290c34751fd9a57 Mon Sep 17 00:00:00 2001 From: falsycat Date: Sat, 26 Dec 2020 00:00:00 +0000 Subject: [PATCH] Adds constant feature. --- TODO.TXT | 2 +- biner.l | 1 + biner.y | 21 +++++++++++++++------ main.c | 3 +++ tree.h | 6 ++++-- 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/TODO.TXT b/TODO.TXT index dfb0ada..ba18068 100644 --- a/TODO.TXT +++ b/TODO.TXT @@ -5,7 +5,7 @@ X make enum for generic types X constant resolving in expression X check if an expression is static/dynamic X enum support - constant support +X constant support conditional switching support union support transpiler for C diff --git a/biner.l b/biner.l index b60e6b3..49d7067 100644 --- a/biner.l +++ b/biner.l @@ -24,6 +24,7 @@ H [0-9A-Fa-f] "//".* count_(); [/][*][^*]*[*]+([^*/][^*]*[*]+)*[/] count_(); /* TODO: detect unterminated comment */ +"const" { count_(); return CONST; } "enum" { count_(); return ENUM; } "struct" { count_(); return STRUCT; } diff --git a/biner.y b/biner.y index 84a43b7..e498d55 100644 --- a/biner.y +++ b/biner.y @@ -83,7 +83,7 @@ resolve_constant_( uintptr_t ptr; } -%token ENUM STRUCT +%token CONST ENUM STRUCT %token IDENT %token INTEGER; @@ -98,13 +98,13 @@ resolve_constant_( %% decl_list - : decl { + : decl ';' { *ref(biner_tree_root_t, ctx.root) = (biner_tree_root_t) { .decls = $1, }; $$ = ctx.root; } - | decl_list decl { + | decl_list decl ';' { ref(biner_tree_decl_t, $2)->prev = ref(biner_tree_root_t, $1)->decls; ref(biner_tree_root_t, $1)->decls = $2; $$ = $1; @@ -112,15 +112,19 @@ decl_list ; decl - : ENUM IDENT '{' enum_member_list '}' ';' { + : CONST IDENT '=' expr { + $$ = create_decl_($2, BINER_TREE_DECL_TYPE_CONST, $4); + if ($$ == 0) YYABORT; + } + | ENUM IDENT '{' enum_member_list '}' { $$ = create_decl_($2, BINER_TREE_DECL_TYPE_ENUM, $4); if ($$ == 0) YYABORT; } - | ENUM IDENT '{' enum_member_list ',' '}' ';' { + | ENUM IDENT '{' enum_member_list ',' '}' { $$ = create_decl_($2, BINER_TREE_DECL_TYPE_ENUM, $4); if ($$ == 0) YYABORT; } - | STRUCT IDENT '{' struct_member_list '}' ';' { + | STRUCT IDENT '{' struct_member_list '}' { $$ = create_decl_($2, BINER_TREE_DECL_TYPE_STRUCT, $4); if ($$ == 0) YYABORT; } @@ -436,6 +440,11 @@ static inline biner_zone_ptr(biner_tree_expr_t) resolve_constant_( while (itr) { const biner_tree_decl_t* decl = ref(biner_tree_decl_t, itr); switch (decl->type) { + case BINER_TREE_DECL_TYPE_CONST: + if (strcmp(ref(char, decl->name), ref(char, ident)) == 0) { + expr = decl->const_; + } + break; case BINER_TREE_DECL_TYPE_ENUM: expr = find_enum_member_by_name_(decl->enum_, ident); break; diff --git a/main.c b/main.c index 8094573..5783388 100644 --- a/main.c +++ b/main.c @@ -54,6 +54,9 @@ int main(void) { (const biner_tree_decl_t*) (zone + root->decls); while ((uintptr_t) decl != (uintptr_t) zone) { switch (decl->type) { + case BINER_TREE_DECL_TYPE_CONST: + printf("const %s\n", zone + decl->name); + break; case BINER_TREE_DECL_TYPE_ENUM: print_enum_(zone, decl); break; diff --git a/tree.h b/tree.h index 092b1d7..1cc290e 100644 --- a/tree.h +++ b/tree.h @@ -96,8 +96,9 @@ typedef struct biner_tree_enum_member_t { } biner_tree_enum_member_t; typedef enum biner_tree_decl_type_t { - BINER_TREE_DECL_TYPE_STRUCT, + BINER_TREE_DECL_TYPE_CONST, BINER_TREE_DECL_TYPE_ENUM, + BINER_TREE_DECL_TYPE_STRUCT, } biner_tree_decl_type_t; typedef struct biner_tree_decl_t { @@ -106,8 +107,9 @@ typedef struct biner_tree_decl_t { union { biner_zone_ptr(void) body; - biner_zone_ptr(biner_tree_struct_member_t) struct_; + biner_zone_ptr(biner_tree_expr_t) const_; biner_zone_ptr(biner_tree_enum_member_t) enum_; + biner_zone_ptr(biner_tree_struct_member_t) struct_; }; biner_zone_ptr(biner_tree_decl_t) prev; } biner_tree_decl_t;