Adds constant feature.

This commit is contained in:
falsycat 2020-12-26 00:00:00 +00:00
parent 9d21e9809a
commit b4957586e4
5 changed files with 24 additions and 9 deletions

View File

@ -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

View File

@ -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; }

21
biner.y
View File

@ -83,7 +83,7 @@ resolve_constant_(
uintptr_t ptr;
}
%token ENUM STRUCT
%token CONST ENUM STRUCT
%token <ptr> IDENT
%token <i> 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;

3
main.c
View File

@ -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;

6
tree.h
View File

@ -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;