HklExpression* hkl_expression_new(HklExpressionType type, ...) { HklExpression* expr = hkl_alloc_object(HklExpression); expr->type = type; va_list argp; va_start(argp, type); switch (type) { case HKL_EXPR_INT: expr->arg[0].integer = va_arg(argp, int); break; case HKL_EXPR_REAL: expr->arg[0].real = va_arg(argp, double); break; case HKL_EXPR_STRING: expr->arg[0].string = va_arg(argp, HklString*); break; default: break; } va_end(argp); return expr; }
HklTree* hkl_tree_new() { HklTree* tree = hkl_alloc_object(HklTree); tree->root = NULL; return tree; }
HklDeque* hkl_deque_new() { HklDeque* deque = hkl_alloc_object(HklDeque); deque->head = deque->tail = NULL; deque->size = 0; return deque; }
HklList* hkl_list_new() { HklList* list = hkl_alloc_object(HklList); list->head = list->tail = NULL; list->size = 0; return list; }
void hklr_scope_push() { HklScope* scope = hkl_alloc_object(HklScope); scope->locals = hkl_hash_new(); scope->upvals = hkl_hash_new(); hkl_list_push_back(HKLR.scopes, scope); }
HklPair* hkl_pair_new() { HklPair* pair = hkl_alloc_object(HklPair); pair->key = hkl_string_new(); pair->value = NULL; return pair; }
static HklDequeNode* hkl_dequenode_new(void* data) { HklDequeNode* node = hkl_alloc_object(HklDequeNode); node->data = data; node->next = NULL; return node; }
HklPutsStmt* hkl_puts_stmt_new(HklExpression* expr) { HklPutsStmt* puts_stmt = hkl_alloc_object(HklPutsStmt); assert(puts_stmt != NULL); assert(expr != NULL); puts_stmt->expr = expr; return puts_stmt; }
HklPair* hkl_pair_new_from_utf8(const char* key, void* value) { assert(key != NULL); HklPair* pair = hkl_alloc_object(HklPair); pair->key = hkl_string_new_from_utf8(key); pair->value = value; return pair; }
static HklListNode* hkl_listnode_new(void* data) { HklListNode* node = hkl_alloc_object(HklListNode); node->data = data; node->next = NULL; node->last = NULL; return node; }
HklString* hkl_string_new() { HklString* const string = hkl_alloc_object(HklString); assert(string != NULL); string->utf8_data = NULL; string->size = 0; string->hash = 0; return string; }
HklrObject* hklr_object_new(HklType type, HklFlag flags, ...) { assert(type != HKL_TYPE_NONE); HklrObject* object = hkl_alloc_object(HklrObject); object->prev = NULL; object->next = NULL; object->rc = 0; object->is_buffered = false; object->color = HKL_COLOR_BLACK; object->type = type; object->flags = flags; HKLR.gc_created++; object->flags = flags; object->type = type; hklr_gc_inc(object); va_list argp; va_start(argp, flags); //void* pointer = NULL; switch (type) { case HKL_TYPE_NIL: break; case HKL_TYPE_INT: object->as.integer = va_arg(argp, int); break; case HKL_TYPE_REAL: object->as.real = va_arg(argp, double); break; case HKL_TYPE_STRING: object->as.string = va_arg(argp, HklString*); break; default: assert(false); break; } va_end(argp); return object; }
HklString* hkl_string_new() { HklString* const string = hkl_alloc_object(HklString); assert(string != NULL); // allocate a null character string->utf8_data = malloc(1); string->utf8_data[0] = '\0'; string->size = 1; string->length = 0; string->hash = 0; return string; }
HklrFunction* hklr_function_new(HklList* args_list, HklTree* closure_list, HklList* stmt_list) { HklrFunction* function = hkl_alloc_object(HklrFunction); function->stmt_list = stmt_list; //hkl_list_traverse(stmt_list, copy_statement_list, function->stmt_list); function->args_list = args_list; function->closure_list = closure_list; hkl_tree_traverse(closure_list, process_closures, NULL); return function; }
// This is the form for moved pairs // This does not make a copy of a pair static HklTreeNode* hkl_treenode_new_from_pair(HklPair* pair) { assert(pair != NULL); HklTreeNode* node = hkl_alloc_object(HklTreeNode); node->pair = pair; node->left = node->right = NULL; node->isred = true; return node; }
static HklTreeNode* hkl_treenode_new(HklString* key, void* value) { assert(key != NULL); HklTreeNode* node = hkl_alloc_object(HklTreeNode); // Duplicate the string to store in the node node->pair = hkl_pair_new_from_data(key, value); node->left = node->right = NULL; node->isred = true; return node; }
HklrStatement* hklr_statement_new(HklStatementType type, ...) { assert(type != HKL_STMT_NONE); HklrStatement* stmt = hkl_alloc_object(HklrStatement); stmt->type = type; va_list argp; va_start(argp, type); switch (type) { case HKL_STMT_PUTS: // puts requires 1 expression stmt->arg[0].expression = va_arg(argp, HklrExpression*); break; case HKL_STMT_ASSIGN: stmt->arg[0].expression = va_arg(argp, HklrExpression*); stmt->arg[1].expression = va_arg(argp, HklrExpression*); break; case HKL_STMT_INIT: stmt->arg[0].flags = va_arg(argp, HklFlag); stmt->arg[1].string = va_arg(argp, HklString*); stmt->arg[2].expression = va_arg(argp, HklrExpression*); break; case HKL_STMT_IF: case HKL_STMT_WHILE: stmt->arg[0].expression = va_arg(argp, HklrExpression*); stmt->arg[1].list = va_arg(argp, HklList*); break; default: break; } va_end(argp); return stmt; }
HklHash* hkl_hash_new() { HklHash* hash = hkl_alloc_object(HklHash); hash->size = 16; hash->length = 0; // Allocate space for each bucket hash->buckets = (HklHashElement*) malloc(sizeof(HklHashElement)*hash->size); // Initialize the buckets size_t i; for (i = 0; i < hash->size; ++i) { hash->buckets[i].is_tree = false; hash->buckets[i].data = NULL; } return hash; }
void hklr_scope_push() { HklScope* scope = hkl_alloc_object(HklScope); scope->prev = HKLR.scopes; scope->next = NULL; scope->locals = hkl_hash_new(); scope->upvals = hkl_hash_new(); if (HKLR.scopes != NULL) { HKLR.scopes->next = scope; } else { HKLR.scopes = scope; } HKLR.scope_level++; }
HklValue* hkl_value_new(HklType type, ...) { assert(type != HKL_TYPE_NONE); HklValue* value = hkl_alloc_object(HklValue); value->type = type; va_list argp; va_start(argp, type); switch (type) { case HKL_TYPE_INT: value->as.integer = va_arg(argp, int); break; case HKL_TYPE_REAL: value->as.real = va_arg(argp, double); break; case HKL_TYPE_STRING: value->as.string = va_arg(argp, HklString*); break; case HKL_TYPE_REF: value->as.object = va_arg(argp, HklrObject*); break; default: break; } va_end(argp); return value; }
HklrExpression* hklr_expression_new(HklExpressionType type, ...) { assert(type != HKL_EXPR_NONE); HklrExpression* expr = hkl_alloc_object(HklrExpression); expr->type = type; va_list argp; va_start(argp, type); switch (type) { case HKL_EXPR_INT: expr->arg[0].integer = va_arg(argp, int); break; case HKL_EXPR_TYPE: expr->arg[0].type = va_arg(argp, HklType); break; case HKL_EXPR_REAL: expr->arg[0].real = va_arg(argp, double); break; case HKL_EXPR_STRING: expr->arg[0].string = va_arg(argp, HklString*); break; case HKL_EXPR_VAR: expr->arg[0].string = va_arg(argp, HklString*); expr->arg[1].list = va_arg(argp, HklList*); break; case HKL_EXPR_ARRAY: expr->arg[0].list = va_arg(argp, HklList*); break; case HKL_EXPR_HASH: expr->arg[0].list = va_arg(argp, HklList*); break; case HKL_EXPR_FUNCTION: expr->arg[0].list = va_arg(argp, HklList*); // args expr->arg[1].tree = va_arg(argp, HklTree*); // closures expr->arg[2].list = va_arg(argp, HklList*); // stmts break; case HKL_EXPR_UNARY: expr->arg[0].op = va_arg(argp, HklOperatorType); expr->arg[1].expression = va_arg(argp, HklrExpression*); break; case HKL_EXPR_BINARY: expr->arg[0].expression = va_arg(argp, HklrExpression*); expr->arg[1].op = va_arg(argp, HklOperatorType); expr->arg[2].expression = va_arg(argp, HklrExpression*); break; default: break; } va_end(argp); return expr; }