-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
370 additions
and
17,017 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,36 @@ | ||
CC = clang | ||
CFLAGS = -Wall -Wextra -O2 -I runtime/builtin $(shell pkg-config --cflags bdw-gc) | ||
LDFLAGS = $(shell pkg-config --libs bdw-gc) | ||
LDFLAGS = $(shell pkg-config --libs bdw-gc) -lgc | ||
RUNTIME_DIR = runtime/builtin | ||
OBJECTS = $(RUNTIME_DIR)/objects/listobject.o $(RUNTIME_DIR)/objects/dictobject.o $(RUNTIME_DIR)/objects/unicodeobject.o $(RUNTIME_DIR)/objects/object.o runtime/runtime.o | ||
|
||
# ビルドターゲット | ||
all: runtime.o | ||
|
||
# ランタイムライブラリのビルド | ||
runtime.o: $(RUNTIME_DIR)/functions.o $(RUNTIME_DIR)/types.o | ||
runtime.o: $(OBJECTS) | ||
ld -r $^ -o $@ | ||
|
||
# 個別のオブジェクトファイル | ||
$(RUNTIME_DIR)/functions.o: $(RUNTIME_DIR)/functions.c $(RUNTIME_DIR)/functions.h $(RUNTIME_DIR)/types.h | ||
$(RUNTIME_DIR)/objects/listobject.o: $(RUNTIME_DIR)/objects/listobject.c $(RUNTIME_DIR)/objects/listobject.h | ||
$(CC) $(CFLAGS) -c $< -o $@ | ||
|
||
$(RUNTIME_DIR)/types.o: $(RUNTIME_DIR)/types.c $(RUNTIME_DIR)/types.h | ||
$(RUNTIME_DIR)/objects/dictobject.o: $(RUNTIME_DIR)/objects/dictobject.c $(RUNTIME_DIR)/objects/dictobject.h | ||
$(CC) $(CFLAGS) -c $< -o $@ | ||
|
||
$(RUNTIME_DIR)/objects/unicodeobject.o: $(RUNTIME_DIR)/objects/unicodeobject.c $(RUNTIME_DIR)/objects/unicodeobject.h | ||
$(CC) $(CFLAGS) -c $< -o $@ | ||
|
||
$(RUNTIME_DIR)/objects/object.o: $(RUNTIME_DIR)/objects/object.c $(RUNTIME_DIR)/objects/object.h | ||
$(CC) $(CFLAGS) -c $< -o $@ | ||
|
||
runtime/runtime.o: runtime/runtime.c runtime/builtin/objects/object.h runtime/builtin/objects/unicodeobject.h runtime/builtin/objects/listobject.h | ||
$(CC) $(CFLAGS) -c $< -o $@ | ||
|
||
# クリーンアップ | ||
clean: | ||
find . -name '__pycache__' -type d -exec rm -r {} + | ||
rm -f runtime.o | ||
rm -f $(RUNTIME_DIR)/*.o | ||
rm -f $(RUNTIME_DIR)/objects/*.o | ||
|
||
.PHONY: all clean |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include <gc.h> | ||
#include "object.h" | ||
#include <string.h> | ||
|
||
typedef struct { | ||
PyObject base; | ||
void** keys; | ||
void** values; | ||
size_t size; | ||
size_t capacity; | ||
} PyDictObject; | ||
|
||
unsigned int PyDict_Hash(PyDictObject* dict) { | ||
unsigned int hash = 5381; | ||
for (size_t i = 0; i < dict->capacity; i++) { | ||
if (dict->keys[i] != NULL) { | ||
hash = ((hash << 5) + hash) + (unsigned int)(uintptr_t)dict->keys[i]; | ||
hash = ((hash << 5) + hash) + (unsigned int)(uintptr_t)dict->values[i]; | ||
} | ||
} | ||
return hash; | ||
} | ||
|
||
PyDictObject* PyDict_New(size_t initial_capacity) { | ||
PyDictObject* dict = (PyDictObject*)GC_malloc(sizeof(PyDictObject)); | ||
PyObject_Init((PyObject*)dict, NULL); // Initialize with no vtable for now | ||
dict->size = 0; | ||
dict->capacity = initial_capacity > 0 ? initial_capacity : 8; | ||
dict->keys = (void**)GC_malloc(sizeof(void*) * dict->capacity); | ||
dict->values = (void**)GC_malloc(sizeof(void*) * dict->capacity); | ||
memset(dict->keys, 0, sizeof(void*) * dict->capacity); | ||
memset(dict->values, 0, sizeof(void*) * dict->capacity); | ||
return dict; | ||
} | ||
|
||
void PyDict_SetItem(PyDictObject* dict, void* key, void* value) { | ||
if (dict->size * 2 >= dict->capacity) { | ||
size_t new_capacity = dict->capacity * 2; | ||
void** new_keys = (void**)GC_malloc(sizeof(void*) * new_capacity); | ||
void** new_values = (void**)GC_malloc(sizeof(void*) * new_capacity); | ||
memset(new_keys, 0, sizeof(void*) * new_capacity); | ||
memset(new_values, 0, sizeof(void*) * new_capacity); | ||
for (size_t i = 0; i < dict->capacity; i++) { | ||
if (dict->keys[i] != NULL) { | ||
size_t j = ((size_t)dict->keys[i]) % new_capacity; | ||
while (new_keys[j] != NULL) { | ||
j = (j + 1) % new_capacity; | ||
} | ||
new_keys[j] = dict->keys[i]; | ||
new_values[j] = dict->values[i]; | ||
} | ||
} | ||
dict->keys = new_keys; | ||
dict->values = new_values; | ||
dict->capacity = new_capacity; | ||
} | ||
size_t index = ((size_t)key) % dict->capacity; | ||
while (dict->keys[index] != NULL) { | ||
if (dict->keys[index] == key) { | ||
dict->values[index] = value; | ||
return; | ||
} | ||
index = (index + 1) % dict->capacity; | ||
} | ||
dict->keys[index] = key; | ||
dict->values[index] = value; | ||
dict->size++; | ||
} | ||
|
||
void* PyDict_GetItem(PyDictObject* dict, void* key) { | ||
size_t index = ((size_t)key) % dict->capacity; | ||
while (dict->keys[index] != NULL) { | ||
if (dict->keys[index] == key) { | ||
return dict->values[index]; | ||
} | ||
index = (index + 1) % dict->capacity; | ||
} | ||
return NULL; // Return NULL if key is not found | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef DICTOBJECT_H | ||
#define DICTOBJECT_H | ||
|
||
#include <stddef.h> | ||
#include "object.h" | ||
|
||
typedef struct { | ||
PyObject base; | ||
void** keys; | ||
void** values; | ||
size_t size; | ||
size_t capacity; | ||
} PyDictObject; | ||
|
||
unsigned int PyDict_Hash(PyDictObject* dict); | ||
PyDictObject* PyDict_New(size_t initial_capacity); | ||
void PyDict_SetItem(PyDictObject* dict, void* key, void* value); | ||
void* PyDict_GetItem(PyDictObject* dict, void* key); | ||
|
||
#endif // DICTOBJECT_H |
Oops, something went wrong.