biner/tree.h

169 lines
5.3 KiB
C
Raw Normal View History

2020-12-20 00:00:00 +00:00
#pragma once
#include <assert.h>
2020-12-20 00:00:00 +00:00
#include <stdatomic.h>
#include <stdbool.h>
2020-12-20 00:00:00 +00:00
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
2020-12-20 00:00:00 +00:00
2021-01-03 00:00:00 +00:00
#include "./zone.h"
2020-12-20 00:00:00 +00:00
typedef struct biner_tree_expr_t biner_tree_expr_t;
typedef struct biner_tree_struct_member_t biner_tree_struct_member_t;
typedef struct biner_tree_struct_member_reference_t biner_tree_struct_member_reference_t;
2020-12-26 00:00:00 +00:00
typedef struct biner_tree_enum_member_t biner_tree_enum_member_t;
2020-12-20 00:00:00 +00:00
typedef struct biner_tree_decl_t biner_tree_decl_t;
typedef enum biner_tree_expr_type_t {
BINER_TREE_EXPR_TYPE_OPERAND_INTEGER,
BINER_TREE_EXPR_TYPE_OPERAND_REFERENCE,
2020-12-26 00:00:00 +00:00
BINER_TREE_EXPR_TYPE_OPERATOR_EQUAL,
BINER_TREE_EXPR_TYPE_OPERATOR_NEQUAL,
BINER_TREE_EXPR_TYPE_OPERATOR_GREATER_EQUAL,
BINER_TREE_EXPR_TYPE_OPERATOR_LESS_EQUAL,
BINER_TREE_EXPR_TYPE_OPERATOR_GREATER,
BINER_TREE_EXPR_TYPE_OPERATOR_LESS,
2020-12-31 00:00:00 +00:00
BINER_TREE_EXPR_TYPE_OPERATOR_AND,
BINER_TREE_EXPR_TYPE_OPERATOR_OR,
2020-12-26 00:00:00 +00:00
BINER_TREE_EXPR_TYPE_OPERATOR_NOT,
2020-12-20 00:00:00 +00:00
BINER_TREE_EXPR_TYPE_OPERATOR_ADD,
BINER_TREE_EXPR_TYPE_OPERATOR_SUB,
BINER_TREE_EXPR_TYPE_OPERATOR_MUL,
BINER_TREE_EXPR_TYPE_OPERATOR_DIV,
2020-12-31 00:00:00 +00:00
BINER_TREE_EXPR_TYPE_OPERATOR_BIT_AND,
BINER_TREE_EXPR_TYPE_OPERATOR_BIT_OR,
BINER_TREE_EXPR_TYPE_OPERATOR_BIT_NOT,
BINER_TREE_EXPR_TYPE_OPERATOR_BIT_XOR,
BINER_TREE_EXPR_TYPE_OPERATOR_MAX_,
2020-12-20 00:00:00 +00:00
} biner_tree_expr_type_t;
typedef struct biner_tree_expr_t {
biner_tree_expr_type_t type;
bool dynamic;
2020-12-20 00:00:00 +00:00
union {
int64_t i;
biner_zone_ptr(biner_tree_struct_member_reference_t) r;
struct {
biner_zone_ptr(biner_tree_expr_t) l;
biner_zone_ptr(biner_tree_expr_t) r;
} operands;
};
} biner_tree_expr_t;
typedef enum biner_tree_struct_member_type_name_t {
BINER_TREE_STRUCT_MEMBER_TYPE_NAME_LU8,
BINER_TREE_STRUCT_MEMBER_TYPE_NAME_LU16,
BINER_TREE_STRUCT_MEMBER_TYPE_NAME_LU32,
BINER_TREE_STRUCT_MEMBER_TYPE_NAME_LU64,
BINER_TREE_STRUCT_MEMBER_TYPE_NAME_LI8,
BINER_TREE_STRUCT_MEMBER_TYPE_NAME_LI16,
BINER_TREE_STRUCT_MEMBER_TYPE_NAME_LI32,
BINER_TREE_STRUCT_MEMBER_TYPE_NAME_LI64,
BINER_TREE_STRUCT_MEMBER_TYPE_NAME_BU8,
BINER_TREE_STRUCT_MEMBER_TYPE_NAME_BU16,
BINER_TREE_STRUCT_MEMBER_TYPE_NAME_BU32,
BINER_TREE_STRUCT_MEMBER_TYPE_NAME_BU64,
BINER_TREE_STRUCT_MEMBER_TYPE_NAME_BI8,
BINER_TREE_STRUCT_MEMBER_TYPE_NAME_BI16,
BINER_TREE_STRUCT_MEMBER_TYPE_NAME_BI32,
BINER_TREE_STRUCT_MEMBER_TYPE_NAME_BI64,
BINER_TREE_STRUCT_MEMBER_TYPE_NAME_USER_DECL,
BINER_TREE_STRUCT_MEMBER_TYPE_NAME_MAX_,
} biner_tree_struct_member_type_name_t;
typedef struct biner_tree_struct_member_type_name_meta_t {
const char* name;
size_t size;
} biner_tree_struct_member_type_name_meta_t;
extern const biner_tree_struct_member_type_name_meta_t
biner_tree_struct_member_type_name_meta_map
[BINER_TREE_STRUCT_MEMBER_TYPE_NAME_MAX_];
2020-12-20 00:00:00 +00:00
typedef enum biner_tree_struct_member_type_qualifier_t {
BINER_TREE_STRUCT_MEMBER_TYPE_QUALIFIER_NONE,
BINER_TREE_STRUCT_MEMBER_TYPE_QUALIFIER_STATIC_ARRAY,
BINER_TREE_STRUCT_MEMBER_TYPE_QUALIFIER_DYNAMIC_ARRAY,
} biner_tree_struct_member_type_qualifier_t;
typedef struct biner_tree_struct_member_type_t {
biner_tree_struct_member_type_name_t name;
2020-12-20 00:00:00 +00:00
union {
biner_zone_ptr(biner_tree_decl_t) decl;
};
biner_tree_struct_member_type_qualifier_t qualifier;
union {
size_t i;
biner_zone_ptr(biner_tree_expr_t) expr;
};
} biner_tree_struct_member_type_t;
typedef struct biner_tree_struct_member_t {
2020-12-26 00:00:00 +00:00
size_t index;
2020-12-20 00:00:00 +00:00
biner_zone_ptr(char) name;
biner_zone_ptr(biner_tree_struct_member_type_t) type;
2020-12-26 00:00:00 +00:00
biner_zone_ptr(biner_tree_expr_t) condition;
biner_zone_ptr(biner_tree_struct_member_t) prev;
2020-12-20 00:00:00 +00:00
} biner_tree_struct_member_t;
typedef struct biner_tree_struct_member_reference_t {
biner_zone_ptr(biner_tree_struct_member_t) member;
biner_zone_ptr(biner_tree_expr_t) index;
biner_zone_ptr(biner_tree_struct_member_reference_t) prev;
} biner_tree_struct_member_reference_t;
2020-12-26 00:00:00 +00:00
typedef struct biner_tree_enum_member_t {
biner_zone_ptr(char) name;
biner_zone_ptr(biner_tree_expr_t) expr;
biner_zone_ptr(biner_tree_enum_member_t) prev;
} biner_tree_enum_member_t;
2020-12-20 00:00:00 +00:00
typedef enum biner_tree_decl_type_t {
2020-12-26 00:00:00 +00:00
BINER_TREE_DECL_TYPE_CONST,
2020-12-26 00:00:00 +00:00
BINER_TREE_DECL_TYPE_ENUM,
2020-12-26 00:00:00 +00:00
BINER_TREE_DECL_TYPE_STRUCT,
2020-12-20 00:00:00 +00:00
} biner_tree_decl_type_t;
typedef struct biner_tree_decl_t {
biner_zone_ptr(char) name;
biner_tree_decl_type_t type;
union {
2020-12-26 00:00:00 +00:00
biner_zone_ptr(void) body;
2020-12-26 00:00:00 +00:00
biner_zone_ptr(biner_tree_expr_t) const_;
2020-12-26 00:00:00 +00:00
biner_zone_ptr(biner_tree_enum_member_t) enum_;
2020-12-26 00:00:00 +00:00
biner_zone_ptr(biner_tree_struct_member_t) struct_;
2020-12-20 00:00:00 +00:00
};
biner_zone_ptr(biner_tree_decl_t) prev;
} biner_tree_decl_t;
typedef struct biner_tree_root_t {
biner_zone_ptr(biner_tree_decl_t) decls;
} biner_tree_root_t;
typedef struct biner_tree_parse_context_t {
atomic_flag dirty;
biner_zone_t zone;
2020-12-22 00:00:00 +00:00
size_t line;
size_t column;
2020-12-26 00:00:00 +00:00
biner_zone_ptr(biner_tree_root_t) root;
biner_zone_ptr(biner_tree_decl_t) last_decl;
2020-12-26 00:00:00 +00:00
biner_zone_ptr(biner_tree_struct_member_t) last_struct;
biner_zone_ptr(biner_tree_enum_member_t) last_enum;
2020-12-20 00:00:00 +00:00
} biner_tree_parse_context_t;
extern biner_tree_parse_context_t biner_tree_parse_context_;
const uint8_t*
biner_tree_parse(
FILE* fp
);