Makes parser to check expression's staticity.

This commit is contained in:
falsycat 2020-12-25 00:00:00 +00:00
parent 4b0dada5c2
commit c9ec7332d9
3 changed files with 41 additions and 27 deletions

View File

@ -3,9 +3,10 @@ X error handling (compiler message)
X user-defined type resolving X user-defined type resolving
X make enum for generic types X make enum for generic types
X constant resolving in expression X constant resolving in expression
check if an expression is static/dynamic X check if an expression is static/dynamic
enum support enum support
union support
constant support constant support
conditional switching support
union support
transpiler for C transpiler for C
release 1.0.0 release 1.0.0

62
biner.y
View File

@ -52,6 +52,13 @@ unwrap_struct_member_ref_(
biner_zone_ptr(biner_tree_struct_member_reference_t) memref biner_zone_ptr(biner_tree_struct_member_reference_t) memref
); );
static inline biner_zone_ptr(biner_tree_expr_t)
create_operator_(
biner_zone_ptr(biner_tree_expr_t) l,
biner_tree_expr_type_t type,
biner_zone_ptr(biner_tree_expr_t) r
);
static biner_zone_ptr(biner_tree_expr_t) static biner_zone_ptr(biner_tree_expr_t)
resolve_constant_( resolve_constant_(
biner_zone_ptr(char) ident biner_zone_ptr(char) ident
@ -141,8 +148,12 @@ array_struct_member_type
$$ = $1; $$ = $1;
biner_tree_struct_member_type_t* t = biner_tree_struct_member_type_t* t =
ref(biner_tree_struct_member_type_t, $$); ref(biner_tree_struct_member_type_t, $$);
t->qualifier = BINER_TREE_STRUCT_MEMBER_TYPE_QUALIFIER_DYNAMIC_ARRAY; t->expr = $3;
t->expr = $3;
const biner_tree_expr_t* expr = ref(biner_tree_expr_t, $3);
t->qualifier = expr->dynamic?
BINER_TREE_STRUCT_MEMBER_TYPE_QUALIFIER_DYNAMIC_ARRAY:
BINER_TREE_STRUCT_MEMBER_TYPE_QUALIFIER_STATIC_ARRAY;
} }
; ;
@ -175,36 +186,20 @@ expr
add_expr add_expr
: mul_expr : mul_expr
| add_expr '+' mul_expr { | add_expr '+' mul_expr {
$$ = alloc_(biner_tree_expr_t); $$ = create_operator_($1, BINER_TREE_EXPR_TYPE_OPERATOR_ADD, $3);
*ref(biner_tree_expr_t, $$) = (biner_tree_expr_t) {
.type = BINER_TREE_EXPR_TYPE_OPERATOR_ADD,
.operands = {$1, $3},
};
} }
| add_expr '-' mul_expr { | add_expr '-' mul_expr {
$$ = alloc_(biner_tree_expr_t); $$ = create_operator_($1, BINER_TREE_EXPR_TYPE_OPERATOR_SUB, $3);
*ref(biner_tree_expr_t, $$) = (biner_tree_expr_t) {
.type = BINER_TREE_EXPR_TYPE_OPERATOR_SUB,
.operands = {$1, $3},
};
} }
; ;
mul_expr mul_expr
: operand : operand
| mul_expr '*' operand { | mul_expr '*' operand {
$$ = alloc_(biner_tree_expr_t); $$ = create_operator_($1, BINER_TREE_EXPR_TYPE_OPERATOR_MUL, $3);
*ref(biner_tree_expr_t, $$) = (biner_tree_expr_t) {
.type = BINER_TREE_EXPR_TYPE_OPERATOR_MUL,
.operands = {$1, $3},
};
} }
| mul_expr '/' operand { | mul_expr '/' operand {
$$ = alloc_(biner_tree_expr_t); $$ = create_operator_($1, BINER_TREE_EXPR_TYPE_OPERATOR_DIV, $3);
*ref(biner_tree_expr_t, $$) = (biner_tree_expr_t) {
.type = BINER_TREE_EXPR_TYPE_OPERATOR_DIV,
.operands = {$1, $3},
};
} }
; ;
@ -227,8 +222,9 @@ operand
$$ = alloc_(biner_tree_expr_t); $$ = alloc_(biner_tree_expr_t);
*ref(biner_tree_expr_t, $$) = (biner_tree_expr_t) { *ref(biner_tree_expr_t, $$) = (biner_tree_expr_t) {
.type = BINER_TREE_EXPR_TYPE_OPERAND_REFERENCE, .type = BINER_TREE_EXPR_TYPE_OPERAND_REFERENCE,
.r = mref, .dynamic = true,
.r = mref,
}; };
} else { } else {
$$ = resolve_constant_($1); $$ = resolve_constant_($1);
@ -332,9 +328,25 @@ unwrap_struct_member_ref_(
return r->member; return r->member;
} }
static inline biner_zone_ptr(biner_tree_expr_t) create_operator_(
biner_zone_ptr(biner_tree_expr_t) l,
biner_tree_expr_type_t type,
biner_zone_ptr(biner_tree_expr_t) r) {
const biner_tree_expr_t* lexpr = ref(biner_tree_expr_t, l);
const biner_tree_expr_t* rexpr = ref(biner_tree_expr_t, r);
biner_zone_ptr(biner_tree_expr_t) expr = alloc_(biner_tree_expr_t);
*ref(biner_tree_expr_t, expr) = (biner_tree_expr_t) {
.type = type,
.dynamic = lexpr->dynamic || rexpr->dynamic,
.operands = {l, r},
};
return expr;
}
static inline biner_zone_ptr(biner_tree_expr_t) resolve_constant_( static inline biner_zone_ptr(biner_tree_expr_t) resolve_constant_(
biner_zone_ptr(char) ident) { biner_zone_ptr(char) ident) {
(void) ident; (void) ident;
/* TODO: check out enums and constants */ /* TODO: find out enums and constants */
return 0; return 0;
} }

1
tree.h
View File

@ -27,6 +27,7 @@ typedef enum biner_tree_expr_type_t {
typedef struct biner_tree_expr_t { typedef struct biner_tree_expr_t {
biner_tree_expr_type_t type; biner_tree_expr_type_t type;
bool dynamic;
union { union {
int64_t i; int64_t i;
biner_zone_ptr(biner_tree_struct_member_reference_t) r; biner_zone_ptr(biner_tree_struct_member_reference_t) r;