Adds constant feature.
This commit is contained in:
parent
9d21e9809a
commit
b4957586e4
2
TODO.TXT
2
TODO.TXT
@ -5,7 +5,7 @@ X make enum for generic types
|
|||||||
X constant resolving in expression
|
X constant resolving in expression
|
||||||
X check if an expression is static/dynamic
|
X check if an expression is static/dynamic
|
||||||
X enum support
|
X enum support
|
||||||
constant support
|
X constant support
|
||||||
conditional switching support
|
conditional switching support
|
||||||
union support
|
union support
|
||||||
transpiler for C
|
transpiler for C
|
||||||
|
1
biner.l
1
biner.l
@ -24,6 +24,7 @@ H [0-9A-Fa-f]
|
|||||||
"//".* count_();
|
"//".* count_();
|
||||||
[/][*][^*]*[*]+([^*/][^*]*[*]+)*[/] count_(); /* TODO: detect unterminated comment */
|
[/][*][^*]*[*]+([^*/][^*]*[*]+)*[/] count_(); /* TODO: detect unterminated comment */
|
||||||
|
|
||||||
|
"const" { count_(); return CONST; }
|
||||||
"enum" { count_(); return ENUM; }
|
"enum" { count_(); return ENUM; }
|
||||||
"struct" { count_(); return STRUCT; }
|
"struct" { count_(); return STRUCT; }
|
||||||
|
|
||||||
|
21
biner.y
21
biner.y
@ -83,7 +83,7 @@ resolve_constant_(
|
|||||||
uintptr_t ptr;
|
uintptr_t ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
%token ENUM STRUCT
|
%token CONST ENUM STRUCT
|
||||||
%token <ptr> IDENT
|
%token <ptr> IDENT
|
||||||
%token <i> INTEGER;
|
%token <i> INTEGER;
|
||||||
|
|
||||||
@ -98,13 +98,13 @@ resolve_constant_(
|
|||||||
%%
|
%%
|
||||||
|
|
||||||
decl_list
|
decl_list
|
||||||
: decl {
|
: decl ';' {
|
||||||
*ref(biner_tree_root_t, ctx.root) = (biner_tree_root_t) {
|
*ref(biner_tree_root_t, ctx.root) = (biner_tree_root_t) {
|
||||||
.decls = $1,
|
.decls = $1,
|
||||||
};
|
};
|
||||||
$$ = ctx.root;
|
$$ = 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_decl_t, $2)->prev = ref(biner_tree_root_t, $1)->decls;
|
||||||
ref(biner_tree_root_t, $1)->decls = $2;
|
ref(biner_tree_root_t, $1)->decls = $2;
|
||||||
$$ = $1;
|
$$ = $1;
|
||||||
@ -112,15 +112,19 @@ decl_list
|
|||||||
;
|
;
|
||||||
|
|
||||||
decl
|
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);
|
$$ = create_decl_($2, BINER_TREE_DECL_TYPE_ENUM, $4);
|
||||||
if ($$ == 0) YYABORT;
|
if ($$ == 0) YYABORT;
|
||||||
}
|
}
|
||||||
| ENUM IDENT '{' enum_member_list ',' '}' ';' {
|
| ENUM IDENT '{' enum_member_list ',' '}' {
|
||||||
$$ = create_decl_($2, BINER_TREE_DECL_TYPE_ENUM, $4);
|
$$ = create_decl_($2, BINER_TREE_DECL_TYPE_ENUM, $4);
|
||||||
if ($$ == 0) YYABORT;
|
if ($$ == 0) YYABORT;
|
||||||
}
|
}
|
||||||
| STRUCT IDENT '{' struct_member_list '}' ';' {
|
| STRUCT IDENT '{' struct_member_list '}' {
|
||||||
$$ = create_decl_($2, BINER_TREE_DECL_TYPE_STRUCT, $4);
|
$$ = create_decl_($2, BINER_TREE_DECL_TYPE_STRUCT, $4);
|
||||||
if ($$ == 0) YYABORT;
|
if ($$ == 0) YYABORT;
|
||||||
}
|
}
|
||||||
@ -436,6 +440,11 @@ static inline biner_zone_ptr(biner_tree_expr_t) resolve_constant_(
|
|||||||
while (itr) {
|
while (itr) {
|
||||||
const biner_tree_decl_t* decl = ref(biner_tree_decl_t, itr);
|
const biner_tree_decl_t* decl = ref(biner_tree_decl_t, itr);
|
||||||
switch (decl->type) {
|
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:
|
case BINER_TREE_DECL_TYPE_ENUM:
|
||||||
expr = find_enum_member_by_name_(decl->enum_, ident);
|
expr = find_enum_member_by_name_(decl->enum_, ident);
|
||||||
break;
|
break;
|
||||||
|
3
main.c
3
main.c
@ -54,6 +54,9 @@ int main(void) {
|
|||||||
(const biner_tree_decl_t*) (zone + root->decls);
|
(const biner_tree_decl_t*) (zone + root->decls);
|
||||||
while ((uintptr_t) decl != (uintptr_t) zone) {
|
while ((uintptr_t) decl != (uintptr_t) zone) {
|
||||||
switch (decl->type) {
|
switch (decl->type) {
|
||||||
|
case BINER_TREE_DECL_TYPE_CONST:
|
||||||
|
printf("const %s\n", zone + decl->name);
|
||||||
|
break;
|
||||||
case BINER_TREE_DECL_TYPE_ENUM:
|
case BINER_TREE_DECL_TYPE_ENUM:
|
||||||
print_enum_(zone, decl);
|
print_enum_(zone, decl);
|
||||||
break;
|
break;
|
||||||
|
6
tree.h
6
tree.h
@ -96,8 +96,9 @@ typedef struct biner_tree_enum_member_t {
|
|||||||
} biner_tree_enum_member_t;
|
} biner_tree_enum_member_t;
|
||||||
|
|
||||||
typedef enum biner_tree_decl_type_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_ENUM,
|
||||||
|
BINER_TREE_DECL_TYPE_STRUCT,
|
||||||
} biner_tree_decl_type_t;
|
} biner_tree_decl_type_t;
|
||||||
|
|
||||||
typedef struct biner_tree_decl_t {
|
typedef struct biner_tree_decl_t {
|
||||||
@ -106,8 +107,9 @@ typedef struct biner_tree_decl_t {
|
|||||||
union {
|
union {
|
||||||
biner_zone_ptr(void) body;
|
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_enum_member_t) enum_;
|
||||||
|
biner_zone_ptr(biner_tree_struct_member_t) struct_;
|
||||||
};
|
};
|
||||||
biner_zone_ptr(biner_tree_decl_t) prev;
|
biner_zone_ptr(biner_tree_decl_t) prev;
|
||||||
} biner_tree_decl_t;
|
} biner_tree_decl_t;
|
||||||
|
Loading…
Reference in New Issue
Block a user