From: Peep P. <so...@us...> - 2004-07-21 11:54:11
|
Update of /cvsroot/agd/server/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8182 Modified Files: vars.c vars.h Log Message: Adding and substracting arrays. (add_vars and sub_vars) Index: vars.c =================================================================== RCS file: /cvsroot/agd/server/src/vars.c,v retrieving revision 1.15 retrieving revision 1.16 diff -u -d -r1.15 -r1.16 --- vars.c 14 Jun 2004 20:48:08 -0000 1.15 +++ vars.c 21 Jul 2004 11:54:01 -0000 1.16 @@ -1,16 +1,44 @@ -/* #include "config.h" */ -/* #include "compile_options.h" */ +/* +#include "config.h" +#include "compile_options.h" +#include "arch.h" +*/ #include <stdlib.h> -/*#include "arch.h"*/ #include "sys.h" #include "list.h" #include "lpc.h" #include "object.h" +#include "array.h" + +void init_var(variable_t *var) +{ + if(var->type & T_ARRAY) { + var->u.a = xmalloc(sizeof(array_t)); + var->u.a->length = 0; + } else switch(var->type) { + case T_INT: + var->u.i = 0; + break; + case T_STRING: + var->u.s = ""; + var->string_type = ST_STATIC; + break; + case T_OBJECT: + var->u.ob = NULL; + break; + case T_VOID: + printf("trying to init T_VOID\n"); + break; + default: + printf("init_var(): unhandled var type %d\n", var->type); + break; + } +} void free_var(variable_t *v) { if(v->type & T_ARRAY) { - /* unref_array(v->u.a); */ + unref_array(v->u.a); } else switch(v->type & ~T_ARRAY) { case T_OBJECT: unref_ob(v->u.ob, v); @@ -98,24 +126,6 @@ } #endif -void init_var(variable_t *var) -{ - switch(var->type) { - case T_INT: var->u.i = 0; break; - case T_STRING: - var->u.s = ""; - var->string_type = ST_STATIC; - break; - case T_OBJECT: var->u.ob = NULL; break; - case T_VOID: - printf("trying to init T_VOID\n"); - break; - default: - printf("init_var(): unhandled var type %d\n", var->type); - break; - } -} - /* runtime addition of variables. */ variable_t *add_vars(variable_t *v1, variable_t *v2) { @@ -126,7 +136,27 @@ return NULL; ret = xmalloc(sizeof(variable_t)); - ret->name = NULL; /* Just for debugging. Remove later. */ +#ifdef DEBUG + ret->name = NULL; +#endif + + if(v1->type & T_ARRAY && v2->type & T_ARRAY) { + int i; + ret->type = v1->type; + ret->u.a = xmalloc(sizeof(array_t)); + ret->u.a->ref = 1; + ret->u.a->length = v1->u.a->length + v2->u.a->length; + ret->u.a->data = xmalloc(sizeof(variable_t) * ret->u.a->length); + + for(i=0;i<ret->u.a->length;i++) { + /* use do_assign? */ + if(i < v1->u.a->length) + ret->u.a->data[i] = v1->u.a->data[i]; + else + ret->u.a->data[i] = v2->u.a->data[i - v1->u.a->length]; + } + return ret; + } switch(v1->type) { case T_INT: @@ -210,39 +240,23 @@ free(ret); return NULL; } -#if 0 -void assign_var(variable_t *dest, variable_t *src) -{ - if(!src) - return; - switch(src->type & ~T_ARRAY) { - case 0: - printf("assign_var(): type 0\n"); - return; - case T_INT: - dest->u.i = src->u.i; - break; - case T_STRING: - if(src->u.s) { - dest->u.s = stringdup(src->u.s); - dest->string_type = ST_MALLOC; - } - break; - case T_OBJECT: - dest->u.ob = src->u.ob; - break; - case T_VOID: - printf("assign_var(): type T_VOID\n"); - break; - default: - printf("assign_var(): unknown type %d\n", src->type & ~T_ARRAY); - break; +variable_t *sub_vars(variable_t *var1, variable_t *var2) +{ + variable_t *ret; + + ret = xmalloc(sizeof(variable_t)); + + if(var1->type & T_ARRAY && var2->type & T_ARRAY) { + ret->type = var1->type; + ret->u.a = sub_arrays(var1->u.a, var2->u.a); + } else { + ret->type = T_INT; + ret->u.i = var1->u.i - var2->u.i; } + return ret; } -#endif -/* lang.y uses this. */ char *type2str(int t) { if(t & T_ARRAY) { Index: vars.h =================================================================== RCS file: /cvsroot/agd/server/src/vars.h,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- vars.h 7 Jun 2004 15:46:15 -0000 1.8 +++ vars.h 21 Jul 2004 11:54:01 -0000 1.9 @@ -7,6 +7,7 @@ int test_var(variable_t *v); int compare_vars(variable_t *v1, variable_t *v2); variable_t *add_vars(variable_t *v1, variable_t *v2); +variable_t *sub_vars(variable_t *v1, variable_t *v2); char *type2str(int t); #endif |