get array initialisation worrking

This commit is contained in:
Bill Currie 2002-01-18 08:26:37 +00:00
parent e3b2bfb81a
commit cef918df83
8 changed files with 128 additions and 5 deletions

View file

@ -1,2 +1,7 @@
all: menu.dat
menu.dat: menu.src menu.qc
qfcc -gP menu.src
clean:
rm -f *.dat *.h *.sym

View file

@ -13,14 +13,14 @@ entity self;
.float frame;
.void () think;
string [6] dot;/* = {
string [6] dot = {
"gfx/menudot0.lpm",
"gfx/menudot1.lpm",
"gfx/menudot2.lpm",
"gfx/menudot3.lpm",
"gfx/menudot4.lpm",
"gfx/menudot5.lpm",
};*/
};
void (integer x, integer y) spinner =
{

View file

@ -117,8 +117,8 @@ extern etype_t qc_types[];
extern struct type_s *types[];
extern expr_type expr_types[];
type_t *get_type (expr_t *e);
etype_t extract_type (expr_t *e);
type_t *get_type (expr_t *e) __attribute__ ((const));
etype_t extract_type (expr_t *e) __attribute__ ((const));
expr_t *new_expr (void);
const char *new_label_name (void);
@ -150,6 +150,8 @@ expr_t *address_expr (expr_t *e1, expr_t *e2, type_t *t);
expr_t *assign_expr (expr_t *e1, expr_t *e2);
expr_t *cast_expr (type_t *t, expr_t *e);
void init_elements (def_t *def, expr_t *eles);
def_t *emit_statement (int line, opcode_t *op, def_t *var_a, def_t *var_b, def_t *var_c);
void emit_expr (expr_t *e);

View file

@ -1766,3 +1766,37 @@ cast_expr (type_t *t, expr_t *e)
c->e.expr.type = t;
return c;
}
void
init_elements (def_t *def, expr_t *eles)
{
expr_t *e;
int count, i;
float *g = &G_FLOAT (G_INT (def->ofs));
for (count = 0, e = eles->e.block.head; e; count++, e = e->next)
;
if (count > def->type->num_parms) {
warning (eles, "excessive elements in initializer");
count = def->type->num_parms;
}
for (i = 0, e = eles->e.block.head; i < count; i++, e = e->next) {
if (e->type == ex_block) {
warning (e, "not yet implemented");
} else if (e->type >= ex_string) {
if (get_type (e) != def->type->aux_type) {
error (e, "type mismatch in initializer");
g += pr_type_size[def->type->aux_type->type];
} else {
if (e->type == ex_string) {
*(int*)g = ReuseString (e->e.string_val);
} else {
memcpy (g, &e->e, pr_type_size[extract_type (e)] * 4);
}
g += pr_type_size[extract_type (e)];
}
} else {
error (e, "non-constant initializer");
}
}
}

View file

@ -187,6 +187,19 @@ PR_GetDef (type_t *type, const char *name, def_t *scope, int *allocate)
pr.size_fields += size;
}
} else if (type->type == ev_pointer) {
dstatement_t *st;
statref_t *ref;
if (pr_scope) {
st = (dstatement_t *) &G_INT (def->ofs);
ref = PR_NewStatref (st, 4);
ref->next = def->refs;
def->refs = ref;
G_INT (def->ofs) = 1;
} else {
G_INT (def->ofs) = *allocate;
}
size = PR_GetTypeSize (type->aux_type);
*allocate += type->num_parms * size;
if (type->num_parms)

View file

@ -118,7 +118,7 @@ typedef struct {
%type <def> param param_list def_name
%type <def> var_def_item var_def_list
%type <def> func_def_item func_def_list
%type <expr> const opt_expr expr arg_list
%type <expr> const opt_expr expr arg_list element_list element_list1 element
%type <expr> statement statements statement_block
%type <expr> break_label continue_label
%type <function> begin_function
@ -137,6 +137,7 @@ expr_t *break_label;
expr_t *continue_label;
switch_block_t *switch_block;
type_t *struct_type;
expr_t *current_init;
def_t *pr_scope; // the function being parsed, or NULL
string_t s_file; // filename for function definition
@ -326,6 +327,58 @@ opt_var_initializer
}
}
}
| '=' '{' { current_init = new_block_expr (); } element_list '}'
{
init_elements (current_def, $4);
current_init = 0;
}
;
element_list
: /* empty */
{
$$ = new_block_expr ();
}
| element_list1 opt_comma
{
$$ = current_init;
}
;
element_list1
: element
{
append_expr (current_init, $1);
}
| element_list1 ',' element
{
append_expr (current_init, $3);
}
;
element
: '{'
{
$$ = current_init;
current_init = new_block_expr ();
}
element_list
{
current_init = $<expr>2;
}
'}'
{
$$ = $3;
}
| expr
{
$$ = $1;
}
;
opt_comma
: /* empty */
| ','
;
opt_func_initializer

View file

@ -485,6 +485,7 @@ void
PR_RelocateRefs (def_t *def)
{
statref_t *ref;
int *d;
for (ref = def->refs; ref; ref = ref->next) {
switch (ref->field) {
@ -497,6 +498,10 @@ PR_RelocateRefs (def_t *def)
case 2:
ref->statement->c = def->ofs;
break;
case 4:
d = (int*)ref->statement;
*d += def->ofs;
break;
default:
abort ();
}

View file

@ -11,6 +11,15 @@ struct foo = {
void (foo bar) eek;
float (float a, float b) boing;
string [6] dot = {
"gfx/menudot0.lpm",
"gfx/menudot1.lpm",
"gfx/menudot2.lpm",
"gfx/menudot3.lpm",
"gfx/menudot4.lpm",
"gfx/menudot5.lpm",
};
float () main =
{
local float messed_or;
@ -32,5 +41,7 @@ float () main =
print (buffer);
} while (read_result == 1024);
close (handle);
for (handle = 0; handle < 6; handle++)
print (dot[handle] + "\n");
return 0;
};