add dictionary string
This commit is contained in:
@@ -0,0 +1,169 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2025 William Bell
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
#include "dictionary.h"
|
||||
#include "../../call/call.h"
|
||||
#include "../functions/functions.h"
|
||||
#include "../literals/literals.h"
|
||||
#include "../string/string.h"
|
||||
#include <inttypes.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
ArgonObject *ARGON_DICTIONARY_TYPE = NULL;
|
||||
|
||||
ArgonObject *create_ARGON_DICTIONARY_TYPE___init__(size_t argc,
|
||||
ArgonObject **argv,
|
||||
ArErr *err,
|
||||
RuntimeState *state) {
|
||||
(void)state;
|
||||
if (argc != 1) {
|
||||
*err = create_err(0, 0, 0, "", "Runtime Error",
|
||||
"__init__ expects 1 argument, got %" PRIu64, argc);
|
||||
return ARGON_NULL;
|
||||
}
|
||||
ArgonObject *object = argv[0];
|
||||
object->type = TYPE_DICTIONARY;
|
||||
object->value.as_hashmap = createHashmap_GC();
|
||||
return object;
|
||||
}
|
||||
|
||||
ArgonObject *create_ARGON_DICTIONARY_TYPE___string__(size_t argc,
|
||||
ArgonObject **argv,
|
||||
ArErr *err,
|
||||
RuntimeState *state) {
|
||||
(void)state;
|
||||
if (argc != 1) {
|
||||
*err = create_err(0, 0, 0, "", "Runtime Error",
|
||||
"__init__ expects 1 argument, got %" PRIu64, argc);
|
||||
return ARGON_NULL;
|
||||
}
|
||||
ArgonObject *object = argv[0];
|
||||
size_t string_length = 0;
|
||||
char *string = NULL;
|
||||
struct node_GC *keys;
|
||||
size_t keys_length;
|
||||
hashmap_GC_to_array(object->value.as_hashmap, &keys, &keys_length);
|
||||
char *string_obj = "{";
|
||||
size_t length = strlen(string_obj);
|
||||
string = realloc(string, string_length + length);
|
||||
memcpy(string + string_length, string_obj, length);
|
||||
string_length += length;
|
||||
for (size_t i = 0; i < keys_length; i++) {
|
||||
struct node_GC node = keys[i];
|
||||
ArgonObject *key = node.key;
|
||||
ArgonObject *value = node.val;
|
||||
|
||||
ArgonObject *string_convert_method = get_builtin_field_for_class(
|
||||
get_builtin_field(key, __class__), __repr__, key);
|
||||
|
||||
if (string_convert_method) {
|
||||
ArgonObject *string_object =
|
||||
argon_call(string_convert_method, 0, NULL, err, state);
|
||||
string =
|
||||
realloc(string, string_length + string_object->value.as_str->length);
|
||||
memcpy(string + string_length, string_object->value.as_str->data,
|
||||
string_object->value.as_str->length);
|
||||
string_length += string_object->value.as_str->length;
|
||||
} else {
|
||||
char *string_obj = "<object>";
|
||||
size_t length = strlen(string_obj);
|
||||
string = realloc(string, string_length + length);
|
||||
memcpy(string + string_length, string_obj, length);
|
||||
string_length += length;
|
||||
}
|
||||
|
||||
char *string_obj = ": ";
|
||||
size_t length = strlen(string_obj);
|
||||
string = realloc(string, string_length + length);
|
||||
memcpy(string + string_length, string_obj, length);
|
||||
string_length += length;
|
||||
|
||||
string_convert_method = get_builtin_field_for_class(
|
||||
get_builtin_field(value, __class__), __repr__, value);
|
||||
|
||||
if (string_convert_method && value != object) {
|
||||
ArgonObject *string_object =
|
||||
argon_call(string_convert_method, 0, NULL, err, state);
|
||||
string =
|
||||
realloc(string, string_length + string_object->value.as_str->length);
|
||||
memcpy(string + string_length, string_object->value.as_str->data,
|
||||
string_object->value.as_str->length);
|
||||
string_length += string_object->value.as_str->length;
|
||||
} else {
|
||||
char *string_obj = "<object>";
|
||||
size_t length = strlen(string_obj);
|
||||
string = realloc(string, string_length + length);
|
||||
memcpy(string + string_length, string_obj, length);
|
||||
string_length += length;
|
||||
}
|
||||
|
||||
if (i != keys_length - 1) {
|
||||
char *string_obj = ", ";
|
||||
size_t length = strlen(string_obj);
|
||||
string = realloc(string, string_length + length);
|
||||
memcpy(string + string_length, string_obj, length);
|
||||
string_length += length;
|
||||
}
|
||||
}
|
||||
string_obj = "}";
|
||||
length = strlen(string_obj);
|
||||
string = realloc(string, string_length + length);
|
||||
memcpy(string + string_length, string_obj, length);
|
||||
string_length += length;
|
||||
ArgonObject* result = new_string_object(string, string_length, 0, 0);
|
||||
free(string);
|
||||
return result;
|
||||
}
|
||||
|
||||
ArgonObject *create_ARGON_DICTIONARY_TYPE___get_attr__(size_t argc,
|
||||
ArgonObject **argv,
|
||||
ArErr *err,
|
||||
RuntimeState *state) {
|
||||
(void)state;
|
||||
if (argc != 2) {
|
||||
*err = create_err(0, 0, 0, "", "Runtime Error",
|
||||
"__get_attr__ expects 2 argument, got %" PRIu64, argc);
|
||||
return ARGON_NULL;
|
||||
}
|
||||
ArgonObject *object = argv[0];
|
||||
ArgonObject *key = argv[1];
|
||||
int64_t hash = hash_object(key, err, state);
|
||||
if (err->exists) {
|
||||
return ARGON_NULL;
|
||||
}
|
||||
ArgonObject *result = hashmap_lookup_GC(object->value.as_hashmap, hash);
|
||||
if (!result) {
|
||||
*err = create_err(0, 0, 0, NULL, "Attribute Error",
|
||||
"Dictionary has no attribute ''");
|
||||
return ARGON_NULL;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void create_ARGON_DICTIONARY_TYPE() {
|
||||
ARGON_DICTIONARY_TYPE = new_class();
|
||||
add_builtin_field(ARGON_DICTIONARY_TYPE, __name__,
|
||||
new_string_object_null_terminated("dictionary"));
|
||||
add_builtin_field(ARGON_DICTIONARY_TYPE, __init__,
|
||||
create_argon_native_function(
|
||||
"__init__", create_ARGON_DICTIONARY_TYPE___init__));
|
||||
add_builtin_field(
|
||||
ARGON_DICTIONARY_TYPE, __get_attr__,
|
||||
create_argon_native_function("__get_attr__",
|
||||
create_ARGON_DICTIONARY_TYPE___get_attr__));
|
||||
add_builtin_field(ARGON_DICTIONARY_TYPE, __string__,
|
||||
create_argon_native_function(
|
||||
"__string__", create_ARGON_DICTIONARY_TYPE___string__));
|
||||
}
|
||||
|
||||
ArgonObject *create_dictionary(struct hashmap_GC *hashmap) {
|
||||
ArgonObject *object = new_instance(ARGON_DICTIONARY_TYPE);
|
||||
object->type = TYPE_DICTIONARY;
|
||||
object->value.as_hashmap = hashmap;
|
||||
return object;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2025 William Bell
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
#ifndef DICTIONARY_H
|
||||
#define DICTIONARY_H
|
||||
#include "../object.h"
|
||||
|
||||
extern ArgonObject *ARGON_DICTIONARY_TYPE;
|
||||
|
||||
void create_ARGON_DICTIONARY_TYPE();
|
||||
|
||||
ArgonObject* create_dictionary(struct hashmap_GC*hashmap);
|
||||
|
||||
#endif // DICTIONARY_H
|
||||
@@ -497,9 +497,7 @@ void init_small_ints() {
|
||||
for (int64_t i = 0; i <= small_ints_max - small_ints_min; i++) {
|
||||
int64_t n = i + small_ints_min;
|
||||
small_ints[i].type = TYPE_NUMBER;
|
||||
small_ints[i].built_in_slot = NULL;
|
||||
small_ints[i].built_in_slot_length = 0;
|
||||
small_ints[i].built_in_slot_size = 0;
|
||||
small_ints[i].dict = NULL;
|
||||
small_ints[i].value.as_number = &small_ints_as_number[i];
|
||||
add_builtin_field(&small_ints[i], __class__, ARGON_NUMBER_TYPE);
|
||||
|
||||
@@ -5,7 +5,9 @@
|
||||
*/
|
||||
|
||||
#include "object.h"
|
||||
#include "../../hash_data/hash_data.h"
|
||||
#include "../../memory.h"
|
||||
#include "../call/call.h"
|
||||
#include "type/type.h"
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
@@ -29,16 +31,34 @@ int strcmp_len(const char *s1, size_t len, const char *s2) {
|
||||
ArgonObject *BASE_CLASS = NULL;
|
||||
|
||||
const char *built_in_field_names[BUILT_IN_FIELDS_COUNT] = {
|
||||
"__base__", "__class__", "__name__", "__add__",
|
||||
"__string__", "__subtract__", "__multiply__", "__division__",
|
||||
"__new__", "__init__", "__boolean__", "__get_attr__",
|
||||
"__binding__", "__function__", "address", "__call__",
|
||||
"__number__", "log", "length", "__getattribute__"};
|
||||
"__base__",
|
||||
"__class__",
|
||||
"__name__",
|
||||
"", // above is anything that gets stored in built in slots
|
||||
"__add__",
|
||||
"__string__",
|
||||
"__subtract__",
|
||||
"__multiply__",
|
||||
"__division__",
|
||||
"__new__",
|
||||
"__init__",
|
||||
"__boolean__",
|
||||
"__get_attr__",
|
||||
"__binding__",
|
||||
"__function__",
|
||||
"address",
|
||||
"__call__",
|
||||
"__number__",
|
||||
"log",
|
||||
"length",
|
||||
"__getattribute__",
|
||||
"__hash__",
|
||||
"__repr__"};
|
||||
|
||||
uint64_t built_in_field_hashes[BUILT_IN_FIELDS_COUNT];
|
||||
|
||||
ArgonObject *new_object() {
|
||||
ArgonObject *object = ar_alloc(sizeof(ArgonObject));
|
||||
object->built_in_slot = NULL;
|
||||
object->built_in_slot_size = 0;
|
||||
object->built_in_slot_length = 0;
|
||||
object->type = TYPE_OBJECT;
|
||||
object->dict = NULL;
|
||||
@@ -46,6 +66,32 @@ ArgonObject *new_object() {
|
||||
return object;
|
||||
}
|
||||
|
||||
void init_built_in_field_hashes() {
|
||||
for (int i = 0; i < BUILT_IN_FIELDS_COUNT; i++) {
|
||||
built_in_field_hashes[i] = siphash64_bytes(
|
||||
built_in_field_names[i], strlen(built_in_field_names[i]), siphash_key);
|
||||
}
|
||||
}
|
||||
|
||||
int64_t hash_object(ArgonObject *object, ArErr *err, RuntimeState *state) {
|
||||
ArgonObject *hash_function = get_builtin_field_for_class(
|
||||
get_builtin_field(object, __class__), __hash__, object);
|
||||
if (!hash_function) {
|
||||
*err = create_err(err->line, err->column, err->length, err->path,
|
||||
"Hash Error", "objects class has no __hash__ method");
|
||||
return 0;
|
||||
}
|
||||
ArgonObject *hash_result = argon_call(hash_function, 0, NULL, err, state);
|
||||
if (hash_result->type != TYPE_NUMBER ||
|
||||
!hash_result->value.as_number->is_int64) {
|
||||
*err =
|
||||
create_err(err->line, err->column, err->length, err->path, "Hash Error",
|
||||
"hash result needs to be a 64 bit integer.");
|
||||
return 0;
|
||||
}
|
||||
return hash_result->value.as_number->n.i64;
|
||||
}
|
||||
|
||||
ArgonObject *new_class() {
|
||||
ArgonObject *object = new_object();
|
||||
add_builtin_field(object, __class__, ARGON_TYPE_TYPE);
|
||||
@@ -67,30 +113,33 @@ inline void add_builtin_field(ArgonObject *target, built_in_fields field,
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (target->built_in_slot_length >= target->built_in_slot_size) {
|
||||
target->built_in_slot_size *= 2;
|
||||
if (target->built_in_slot_size == 0) target->built_in_slot_size = 2;
|
||||
else if (target->built_in_slot_size>BUILT_IN_FIELDS_COUNT) target->built_in_slot_size = BUILT_IN_FIELDS_COUNT;
|
||||
target->built_in_slot =
|
||||
ar_realloc(target->built_in_slot, target->built_in_slot_size *
|
||||
sizeof(struct built_in_slot));
|
||||
if (field > BUILT_IN_ARRAY_COUNT) {
|
||||
if (!target->dict)
|
||||
target->dict = createHashmap_GC();
|
||||
hashmap_insert_GC(target->dict, built_in_field_hashes[field],
|
||||
(char *)built_in_field_names[field], object, 0);
|
||||
return;
|
||||
}
|
||||
target->built_in_slot[target->built_in_slot_length++] = (struct built_in_slot){field, object};
|
||||
target->built_in_slot[target->built_in_slot_length++] =
|
||||
(struct built_in_slot){field, object};
|
||||
// hashmap_insert_GC(target->dict, built_in_field_hashes[field],
|
||||
// (char *)built_in_field_names[field], object, 0);
|
||||
}
|
||||
|
||||
void add_field_l(ArgonObject *target, char *name, uint64_t hash, size_t length,
|
||||
ArgonObject *object) {
|
||||
for (size_t i = 0; i < BUILT_IN_FIELDS_COUNT; i++) {
|
||||
for (size_t i = 0; i < BUILT_IN_ARRAY_COUNT; i++) {
|
||||
if (strcmp_len(name, length, built_in_field_names[i]) == 0) {
|
||||
add_builtin_field(target, i, object);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (!object->dict)
|
||||
object->dict = createHashmap_GC();
|
||||
hashmap_insert_GC(target->dict, hash, name, object, 0);
|
||||
if (!target->dict)
|
||||
target->dict = createHashmap_GC();
|
||||
char *name_copy = ar_alloc(length);
|
||||
memcpy(name_copy, name, length);
|
||||
name_copy[length] = '\0';
|
||||
hashmap_insert_GC(target->dict, hash, name_copy, object, 0);
|
||||
}
|
||||
|
||||
ArgonObject *bind_object_to_function(ArgonObject *object,
|
||||
@@ -127,9 +176,9 @@ ArgonObject *get_field_for_class_l(ArgonObject *target, char *name,
|
||||
ArgonObject *get_field_l(ArgonObject *target, char *name, uint64_t hash,
|
||||
size_t length, bool recursive,
|
||||
bool disable_method_wrapper) {
|
||||
for (size_t i = 0; i < BUILT_IN_FIELDS_COUNT; i++) {
|
||||
for (size_t i = 0; i < target->built_in_slot_length; i++) {
|
||||
if (strcmp_len(name, length, built_in_field_names[i]) == 0) {
|
||||
return get_builtin_field(target, i);
|
||||
return target->built_in_slot[i].value;
|
||||
}
|
||||
}
|
||||
if (!target->dict)
|
||||
@@ -161,22 +210,32 @@ ArgonObject *get_builtin_field_for_class(ArgonObject *target,
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
inline ArgonObject *get_builtin_field(ArgonObject *target,
|
||||
built_in_fields field) {
|
||||
return get_builtin_field_with_recursion_support(target, field, false, false);
|
||||
}
|
||||
|
||||
ArgonObject *get_builtin_field(ArgonObject *target, built_in_fields field) {
|
||||
ArgonObject *
|
||||
get_builtin_field_with_recursion_support(ArgonObject *target,
|
||||
built_in_fields field, bool recursive,
|
||||
bool disable_method_wrapper) {
|
||||
if (!target)
|
||||
return NULL;
|
||||
for (size_t i = 0; i < target->built_in_slot_length; i++) {
|
||||
if (target->built_in_slot[i].field == field) {
|
||||
return target->built_in_slot[i].value;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
// ArgonObject *object =
|
||||
// hashmap_lookup_GC(target->dict, built_in_field_hashes[field]);
|
||||
// if (!recursive || object)
|
||||
// return object;
|
||||
// ArgonObject *binding = target;
|
||||
// if (disable_method_wrapper)
|
||||
// binding = NULL;
|
||||
// return get_builtin_field_for_class(
|
||||
// hashmap_lookup_GC(target->dict, built_in_field_hashes[__class__]),
|
||||
// field, binding);
|
||||
if (!target->dict)
|
||||
return NULL;
|
||||
ArgonObject *object =
|
||||
hashmap_lookup_GC(target->dict, built_in_field_hashes[field]);
|
||||
if (!recursive || object)
|
||||
return object;
|
||||
ArgonObject *binding = target;
|
||||
if (disable_method_wrapper)
|
||||
binding = NULL;
|
||||
return get_builtin_field_for_class(
|
||||
hashmap_lookup_GC(target->dict, built_in_field_hashes[__class__]), field,
|
||||
binding);
|
||||
}
|
||||
@@ -18,6 +18,10 @@ typedef struct ArgonObject ArgonObject;
|
||||
ArgonObject *new_class();
|
||||
ArgonObject *new_instance(ArgonObject * of);
|
||||
|
||||
void init_built_in_field_hashes();
|
||||
|
||||
int64_t hash_object(ArgonObject *object, ArErr *err, RuntimeState *state);
|
||||
|
||||
void add_builtin_field(ArgonObject *target, built_in_fields field,
|
||||
ArgonObject *object);
|
||||
|
||||
@@ -41,4 +45,6 @@ ArgonObject *get_builtin_field_for_class(ArgonObject *target,
|
||||
|
||||
ArgonObject *get_builtin_field(ArgonObject *target, built_in_fields field);
|
||||
|
||||
ArgonObject *get_builtin_field_with_recursion_support(ArgonObject *target, built_in_fields field, bool recursive, bool disable_method_wrapper);
|
||||
|
||||
#endif // OBJECT_H
|
||||
Reference in New Issue
Block a user