Skip to content

Commit

Permalink
Start supporting array types
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Oct 14, 2024
1 parent b96e2e4 commit 30192ef
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
7 changes: 6 additions & 1 deletion Sources/backends/hlsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,12 @@ static void write_globals(char *hlsl, size_t *offset, function *main, function *
*offset += sprintf(&hlsl[*offset], "RWTexture2D<float4> _%" PRIu64 " : register(u%i);\n\n", g->var_index, register_index);
}
else {
*offset += sprintf(&hlsl[*offset], "Texture2D<float4> _%" PRIu64 " : register(t%i);\n\n", g->var_index, register_index);
if (get_type(g->type)->kind == TYPE_ARRAY && get_type(g->type)->array.array_size == -1) {
*offset += sprintf(&hlsl[*offset], "Texture2D<float4> _%" PRIu64 "[] : register(t%i, space1);\n\n", g->var_index, register_index);
}
else {
*offset += sprintf(&hlsl[*offset], "Texture2D<float4> _%" PRIu64 " : register(t%i);\n\n", g->var_index, register_index);
}
}
}
else if (g->type == tex2darray_type_id) {
Expand Down
30 changes: 28 additions & 2 deletions Sources/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,22 @@ static definition parse_const(state_t *state, attribute_list attributes) {
advance_state(state);
}

bool array = false;
int array_size = -1;

if (current(state).kind == TOKEN_LEFT_SQUARE) {
array = true;
advance_state(state);

if (current(state).kind == TOKEN_INT) {
array_size = (int)current(state).number;
advance_state(state);
}

match_token(state, TOKEN_RIGHT_SQUARE, "Expected a right square bracket");
advance_state(state);
}

expression *value = NULL;
if (current(state).kind == TOKEN_OPERATOR && current(state).op == OPERATOR_ASSIGN) {
advance_state(state);
Expand All @@ -1160,7 +1176,17 @@ static definition parse_const(state_t *state, attribute_list attributes) {
}
else if (type_name == add_name("tex2d")) {
d.kind = DEFINITION_TEX2D;
d.global = add_global(tex2d_type_id, attributes, name.identifier);

type_id t_id = tex2d_type_id;
if (array) {
type_id array_type_id = add_type(get_type(t_id)->name);
get_type(array_type_id)->kind = TYPE_ARRAY;
get_type(array_type_id)->array.base = t_id;
get_type(array_type_id)->array.array_size = array_size;
t_id = array_type_id;
}

d.global = add_global(t_id, attributes, name.identifier);
}
else if (type_name == add_name("tex2darray")) {
d.kind = DEFINITION_TEX2DARRAY;
Expand All @@ -1181,7 +1207,7 @@ static definition parse_const(state_t *state, attribute_list attributes) {
else if (type_name == add_name("float")) {
debug_context context = {0};
check(value != NULL, context, "const float requires an initialization value");
check(value->kind == EXPRESSION_FLOAT || value->kind == EXPRESSION_INT, context, "const float3 requires a number");
check(value->kind == EXPRESSION_FLOAT || value->kind == EXPRESSION_INT, context, "const float requires a number");

global_value float_value;
float_value.kind = GLOBAL_VALUE_FLOAT;
Expand Down
1 change: 1 addition & 0 deletions Sources/types.c
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ type_id add_type(name_id name) {
type_id s = next_type_index;
++next_type_index;

types[s].kind = TYPE_OBJECT;
types[s].name = name;
types[s].attributes.attributes_count = 0;
types[s].members.size = 0;
Expand Down
10 changes: 9 additions & 1 deletion Sources/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,16 @@ static attribute *find_attribute(attribute_list *attributes, name_id name) {
typedef struct type {
attribute_list attributes;
name_id name;
members members;
bool built_in;

enum { TYPE_OBJECT, TYPE_ARRAY } kind;
union {
members members;
struct {
type_id base;
int array_size;
} array;
};
} type;

void types_init(void);
Expand Down

0 comments on commit 30192ef

Please sign in to comment.