void env_setsymbol(ENV* env, const char* name, NODE* node) { int i, l, sz; assert(env != NULL); assert(name != NULL); NODE_CHECK(node); l = strlen(name); i= env_getindex(env, name); if( i>= 0 ) { //redefine the environment value NODE* oldnode; if( env->names[i] != NULL ) GC_FREE(env->names[i]); sz = sizeof(char) * (strlen(name)+1); env->names[i] = GC_MALLOC_ATOMIC(sz); assert(env->names[i] != NULL); memcpy(env->names[i], name, sz); env->names[i][sz-1] = '\0'; NODE_CHECK(node); oldnode = env->nodes[i]; env->nodes[i] = node; node_free(oldnode); node->refcount++; } else if( env->count < env->size ) { i = env->count; env->count = env->count + 1; sz = sizeof(char) * (strlen(name)+1); env->names[i] = GC_MALLOC_ATOMIC(sizeof(char) * (strlen(name)+1)); assert(env->names[i] != NULL); memcpy(env->names[i], name, sz); env->names[i][sz-1] = '\0'; NODE_CHECK(node); env->nodes[i] = node; node->refcount++; //char *tmp = node_tostring(node); //printf("env_setsymbol: '%s'(%d) = %s\n", name, strlen(name), tmp); //GC_FREE(tmp); } else { printf("env_setsymbol: buffer overflow.\n"); exit(1); } node->refcount++; }
int Node_Delete (root_t* tree, tree_t* node) { TREE_CHECK (tree); NODE_CHECK (node); if (node->left) { TEC (Node_Delete (tree, node->left), node->left); } if (node->right) { TEC (Node_Delete (tree, node->right), node->right); } if (node->parent->left == node) node->parent->left = NULL; if (node->parent->right == node) node->parent->right = NULL; Tree_Update_Sizes (tree, node->parent, -1); TEC (Node_Destruct (node), node); TREE_CHECK (tree); return TREE_OK; }
int Node_Add_Right (root_t* tree, tree_t* node, tree_t* child) { TREE_CHECK (tree); NODE_CHECK (node); NODE_CHECK (child); if (node->right == NULL) { node->right = child; node->right->parent = node; TEC (Tree_Update_Sizes (tree, node, child->size), node); } TREE_CHECK (tree); NODE_CHECK (node); return TREE_OK; }
int Node_Print_Postfix (FILE* outstream, tree_t* node) { ASSERT (outstream); NODE_CHECK (node); if (node->left) Node_Print_Postfix (outstream, node->left); if (node->right) Node_Print_Postfix (outstream, node->right); NODE_PRINT; return NODE_OK; }
int Node_Add_Left (root_t* tree, tree_t* node, tree_t* child) { TREE_CHECK (tree); NODE_CHECK (node); NODE_CHECK (child); if (node->left == NULL) { node->left = child; node->left->parent = node; TEC (Tree_Update_Sizes (tree, node, child->size), node); } Tree_Dump (stdout, tree); TREE_CHECK (tree); NODE_CHECK (node); return TREE_OK; }
int Node_Destruct (tree_t* node) { NODE_CHECK (node); node->info = Poison; node->size = Poison; node->type = Poison; node->left = NULL; node->right = NULL; node->parent = NULL; free (node); return NODE_OK; }
int Node_Print (FILE* outstream, tree_t* node) { ASSERT (outstream); NODE_CHECK (node); fprintf (outstream, "("); if (node->left) Node_Print (outstream, node->left); NODE_PRINT; if (node->right) Node_Print (outstream, node->right); fprintf (outstream, ")"); return NODE_OK; }
int Node_Construct (tree_t** new_node) { ASSERT (new_node); *new_node = (tree_t*) calloc (1, sizeof (tree_t)); (*new_node)->info = Set_value; (*new_node)->size = 1; (*new_node)->type = NO_TYPE; (*new_node)->left = NULL; (*new_node)->right = NULL; (*new_node)->parent = NULL; NODE_CHECK (*new_node); return NODE_OK; }
NODE* env_find(ENV* env, const char* name) { int index; assert(env != NULL); assert(name != NULL && strcmp(name, "") != 0); index = env_getindex(env, name); if( index >= 0 ) { //printf("env_find: name=%s found address=%x\n", name, env->nodes[index]); NODE_CHECK(env->nodes[index]); return env->nodes[index]; } if( env->parent != NULL ) { return env_find(env->parent, name); } //printf("env_find: name=%s return NULL\n", name); return NULL; }
int Node_Print_Dot (FILE* outstream, tree_t* node) { ASSERT (outstream); NODE_CHECK (node); DOT_PRINT_NODE; if (node->left) { Node_Print_Dot (outstream, node->left); fprintf (outstream, "%d -> %d;\n", (int) node, (int) node->left); } if (node->right) { Node_Print_Dot (outstream, node->right); fprintf (outstream, "%d -> %d;\n", (int) node, (int) node->right); } return NODE_OK; }