diff --git a/Makefile b/Makefile index 4f8fd12..d50260e 100644 --- a/Makefile +++ b/Makefile @@ -8,8 +8,7 @@ SRCS= main.c \ nv_variable.c nv_term.c nv_signal.c \ nv_integer.c nv_string.c nv_relation.c \ fnv1.c \ - lang/02/parse.c lang/02/eval.c \ - nv_anchor.c + lang/02/parse.c lang/02/eval.c HEADERS=nv.h nv_node.h nv_func.h nv_static.h CFLAGS=-Wall -Wextra -lncurses -Wunused-function CFLAGS += -DGIT_COMMIT_ID="\"$(GIT_COMMIT_ID)\"" \ diff --git a/fnv1.c b/fnv1.c index 6b9c497..479d6f0 100644 --- a/fnv1.c +++ b/fnv1.c @@ -16,7 +16,7 @@ static const uint64_t FNV_PRIME_64 = 1099511628211LLU; /* * FNV Hash Algorithm */ -uint32_t fnv_1_hash_32(uint8_t *bytes, size_t length) +uint32_t fnv_1_hash_32(const uint8_t *bytes, size_t length) { uint32_t hash; size_t i; @@ -28,7 +28,7 @@ uint32_t fnv_1_hash_32(uint8_t *bytes, size_t length) return hash; } -uint64_t fnv_1_hash_64(uint8_t *bytes, size_t length) +uint64_t fnv_1_hash_64(const uint8_t *bytes, size_t length) { uint64_t hash; size_t i; diff --git a/lang/02/parse.c b/lang/02/parse.c index 8b5c37f..7bc410e 100644 --- a/lang/02/parse.c +++ b/lang/02/parse.c @@ -280,7 +280,7 @@ NV_ID NV_parseToCodeGraph(const NV_ID *baseTokenList, const NV_ID *opDict) printf("parsing tokens: "); NV_Array_print(&tokenList); putchar('\n'); */ - printf("tokenList hash = %08X\n", NV_Term_calcHash(baseTokenList)); + //printf("tokenList hash = %08X\n", NV_Term_calcHash(baseTokenList)); NV_ID codeGraphRoot = NV_Node_createWithString("eval"); NV_ID lastNode = codeGraphRoot; NV_OpPointer p; diff --git a/main.c b/main.c index 349ae50..f63c159 100644 --- a/main.c +++ b/main.c @@ -88,28 +88,67 @@ int main(int argc, char *argv[]) */ // TEST CODE BEGIN vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv /* - NV_ID rootAnchor = NV_Anchor_createWithName("root"); + NV_ID nodeA = NV_Node_createWithString("A"); + NV_ID nodeB = NV_Node_createWithString("B"); + NV_ID nodeC = NV_Node_createWithString("C"); + NV_ID nodeD = NV_Node_createWithString("D"); + NV_ID nodeE = NV_Node_createWithString("E"); + NV_NodeID_createRelation(&nodeA, &nodeB, &nodeC); - NV_ID list1 = NV_Array_create(); - NV_ID e; - e = NV_Node_createWithString("test1"); - NV_Array_push(&list1, &e); - e = NV_Node_createWithString("hello"); - NV_Array_push(&list1, &e); - - NV_Array_print(&list1); putchar('\n'); - NV_Dict_printWithDepth(&list1, 6, 0); - printf("Hash: %08X\n", NV_Term_calcHash(&list1)); - printf("Hash: %08X\n", NV_Term_calcHash(&opDict)); + { + NV_ID r = NV_NodeID_getRelatedNodeFrom(&nodeA, &nodeB); + NV_NodeID_printForDebug(&r); + if(NV_NodeID_isEqual(&nodeC, &r)){ + printf("OK"); + } else{ + printf("BAD"); + } + } - FILE *fp = fopen("dump.txt", "w"); - if(fp){ - NV_Node_dumpAllToFile(fp); - fclose(fp); + NV_Dict_addUniqueIDKey(&nodeA, &nodeB, &nodeD); + { + NV_ID r = NV_NodeID_getRelatedNodeFrom(&nodeA, &nodeB); + NV_NodeID_printForDebug(&r); + if(NV_NodeID_isEqual(&nodeD, &r)){ + printf("OK"); + } else{ + printf("BAD"); + } } - return 0; - */ + NV_Dict_addUniqueIDKey(&nodeA, &nodeB, &nodeE); + { + NV_ID r = NV_NodeID_getRelatedNodeFrom(&nodeA, &nodeB); + NV_NodeID_printForDebug(&r); + if(NV_NodeID_isEqual(&nodeE, &r)){ + printf("OK"); + } else{ + printf("BAD"); + } + } +*/ + /* + { + NV_ID ary = NV_Array_create(); + NV_ID nodeA = NV_Node_createWithString("A"); + NV_ID nodeB = NV_Node_createWithString("B"); + NV_Array_push(&ary, &nodeA); + NV_Array_push(&ary, &nodeB); + { + NV_ID r0 = NV_Array_getByIndex(&ary, 0); + NV_ID r1 = NV_Array_getByIndex(&ary, 1); + NV_NodeID_printForDebug(&r0); + NV_NodeID_printForDebug(&r1); + if(NV_NodeID_isEqual(&nodeA, &r0) && NV_NodeID_isEqual(&nodeB, &r1)){ + printf("OK"); + } else{ + printf("BAD"); + } + } + NV_Array_print(&ary); + } +*/ +// return 0; // TEST CODE END ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ char line[MAX_INPUT_LEN]; diff --git a/nv.c b/nv.c index 1022574..0f8c189 100644 --- a/nv.c +++ b/nv.c @@ -3,33 +3,7 @@ // main // volatile sig_atomic_t NV_globalExecFlag; -/* -// int NV_interactiveInput(const NV_ID *cTypeList, const NV_ID *ctx) -{ - char line[MAX_INPUT_LEN]; - // - if(NV_gets(line, sizeof(line)) != NULL){ - NV_evalLine(cTypeList, ctx, line); - return 0; - } - return 1; -} -*/ -/* -// int NV_evalLine(const NV_ID *cTypeList, const NV_ID *ctx, const char *line) -{ - NV_ID tokenList; - tokenList = NV_tokenize(cTypeList, line); - if(IS_DEBUG_MODE()){ - NV_Term_print(&tokenList); putchar('\n'); - } - NV_Context_pushToEvalStack(ctx, &tokenList, &NODEID_NULL); - if(IS_DEBUG_MODE()){ - NV_Term_print(&tokenList); putchar('\n'); - } - return 0; -} -*/ + NV_ID NV_tokenize(const NV_ID *cTypeList, const char *input) { // retv: tokenized str array diff --git a/nv_func.h b/nv_func.h index 5f7ffab..98a9af3 100644 --- a/nv_func.h +++ b/nv_func.h @@ -83,7 +83,10 @@ NV_Node *NV_Node_getNextNode(const NV_Node *n); int NV_Node_getNodeCount(); NV_ID NV_NodeID_create(const NV_ID *id); NV_ID NV_Node_create(); -NV_ID NV_Node_createWithTypeAndData(NV_NodeType type, void *data, int32_t size); +NV_ID NV_Node_createWithTypeAndData +(NV_NodeType type, const void *data, int32_t size); +NV_ID NV_Node_createWith_ID_Type_Data_Size +(const NV_ID *id, NV_NodeType type, const void *data, int32_t size); int NV_NodeID_isEqual(const NV_ID *a, const NV_ID *b); int NV_NodeID_isEqualInValue(const NV_ID *a, const NV_ID *b); int NV_NodeID_exists(const NV_ID *id); @@ -93,8 +96,8 @@ const NV_Node *NV_Node_getRelCache(const NV_Node *n); void NV_Node_setRelCache(NV_Node *n, const NV_Node *rel); int32_t NV_Node_calcHash(const NV_Node *n); int32_t NV_NodeID_calcHash(const NV_ID *id); -void *NV_Node_getDataAsType(const NV_Node *n, NV_NodeType type); -void *NV_NodeID_getDataAsType(const NV_ID *id, NV_NodeType type); +const void *NV_Node_getDataAsType(const NV_Node *n, NV_NodeType type); +const void *NV_NodeID_getDataAsType(const NV_ID *id, NV_NodeType type); NV_ID NV_Node_getID(const NV_Node *n); void NV_Node_dumpAll(); void NV_Node_dumpAllToFile(FILE *fp); @@ -182,8 +185,8 @@ int32_t NV_NodeID_getInt32(const NV_ID *id); // @nv_string.c int NV_NodeID_isString(const NV_ID *id); NV_ID NV_Node_createWithString(const char *s); +NV_ID NV_Node_createWith_ID_String(const NV_ID *id, const char *s); NV_ID NV_Node_createWithStringFormat(const char *fmt, ...); -void NV_NodeID_createAndString(const NV_ID *id, const char *s); const char *NV_NodeID_getCStr(const NV_ID *id); int NV_Node_String_compare(const NV_ID *ida, const NV_ID *idb); int NV_Node_String_compareWithCStr(const NV_ID *ida, const char *s); @@ -217,8 +220,8 @@ NV_ID NV_NodeID_getEqRelatedNodeFrom(const NV_ID *from, const NV_ID *rel); // @fnv1.c -uint32_t fnv_1_hash_32(uint8_t *bytes, size_t length); -uint64_t fnv_1_hash_64(uint8_t *bytes, size_t length); +uint32_t fnv_1_hash_32(const uint8_t *bytes, size_t length); +uint64_t fnv_1_hash_64(const uint8_t *bytes, size_t length); // @lang/02/parse.c @@ -256,8 +259,3 @@ NV_ID NV_Lang02_OpFunc_parentheses (NV_ID * const p, NV_ID * const lastEvalVal, const NV_ID *scope); NV_ID NV_evalGraph(const NV_ID *codeGraphRoot, const NV_ID *scope); - -// @nv_anchor.c -int NV_Anchor_isAnchor(const NV_ID *id); -NV_ID NV_Anchor_createWithName(const char *s); - diff --git a/nv_node.c b/nv_node.c index 8c104e4..46fc7c0 100644 --- a/nv_node.c +++ b/nv_node.c @@ -35,7 +35,7 @@ void NV_Node_Internal_resetData(NV_Node *n) } */ NV_DbgInfo("Free Data type: %s", NV_NodeTypeList[n->type]); - NV_free(n->data); + NV_free((void *)n->data); n->data = NULL; n->size = 0; } @@ -69,6 +69,15 @@ int NV_Node_Internal_isEqualInValue(const NV_Node *na, const NV_Node *nb) return 0; } +void NV_Node_Internal_set_Type_Data_Size(const NV_ID *id, NV_NodeType type, const void *data, int32_t size) +{ + NV_Node *n; + n = NV_NodeID_getNode(id); + n->type = type; + n->size = size; + n->data = data; +} + // // Node // @@ -120,31 +129,22 @@ NV_ID NV_Node_create() return NV_NodeID_createNew(&id); } -NV_ID NV_Node_createWithTypeAndData(NV_NodeType type, void *data, int32_t size) +NV_ID NV_Node_createWithTypeAndData +(NV_NodeType type, const void *data, int32_t size) { - NV_ID id; - NV_Node *n; - id = NV_Node_create(); - n = NV_NodeID_getNode(&id); - n->type = type; - n->size = size; - n->data = data; + NV_ID id = NV_Node_create(); + NV_Node_Internal_set_Type_Data_Size(&id, type, data, size); return id; } -/* -//NV_ID NV_Node_createWith_ID_Type_Data -//(NV_ID *id, NV_NodeType type, void *data, int32_t size) + +NV_ID NV_Node_createWith_ID_Type_Data_Size +(const NV_ID *id, NV_NodeType type, const void *data, int32_t size) { - NV_ID id; - NV_Node *n; - id = NV_Node_create(); - n = NV_NodeID_getNode(&id); - n->type = type; - n->size = size; - n->data = data; - return id; + NV_NodeID_create(id); + NV_Node_Internal_set_Type_Data_Size(id, type, data, size); + return *id; } -*/ + int NV_NodeID_isEqual(const NV_ID *a, const NV_ID *b) { int i; @@ -241,13 +241,13 @@ int32_t NV_NodeID_calcHash(const NV_ID *id) n->data = data; } */ -void *NV_Node_getDataAsType(const NV_Node *n, NV_NodeType type) +const void *NV_Node_getDataAsType(const NV_Node *n, NV_NodeType type) { if(!n || n->type != type) return NULL; return n->data; } -void *NV_NodeID_getDataAsType(const NV_ID *id, NV_NodeType type) +const void *NV_NodeID_getDataAsType(const NV_ID *id, NV_NodeType type) { NV_Node *n; n = NV_NodeID_getNode(id); @@ -296,17 +296,14 @@ void NV_NodeID_remove(const NV_ID *baseID) NV_ID NV_NodeID_clone(const NV_ID *baseID) { - NV_Node *base, *new; - NV_ID newID = NV_Node_create(); - new = NV_NodeID_getNode(&newID); + NV_Node *base; base = NV_NodeID_getNode(baseID); - new->type = base->type; + void *data = NULL; if(base->data){ - new->data = NV_malloc(base->size); - new->size = base->size; - memcpy(new->data, base->data, new->size); + data = NV_malloc(base->size); + memcpy(data, base->data, base->size); } - return newID; + return NV_Node_createWithTypeAndData(base->type, data, base->size); } diff --git a/nv_node.h b/nv_node.h index f491c51..cd560e7 100644 --- a/nv_node.h +++ b/nv_node.h @@ -4,7 +4,7 @@ struct NV_NODE { NV_ID id; - void *data; + const void *data; NV_Node *prev; NV_Node *next; NV_NodeType type; diff --git a/nv_relation.c b/nv_relation.c index 5551b33..1cde41d 100644 --- a/nv_relation.c +++ b/nv_relation.c @@ -34,6 +34,14 @@ NV_ID NV_NodeID_createRelation reld->rel = *rel; reld->to = *to; + if(IS_DEBUG_MODE()){ + printf("Rel created: "); + NV_NodeID_printForDebug(from); + NV_NodeID_printForDebug(rel); + NV_NodeID_printForDebug(to); + printf("\n"); + } + return NV_Node_createWithTypeAndData(kRelation, reld, size); } @@ -88,7 +96,7 @@ NV_ID NV_NodeID_createUniqueEqRelation */ NV_Node *NV_NodeID_Relation_getLinkFrom(const NV_ID *relnid) { - NV_Relation *reld; + const NV_Relation *reld; // reld = NV_NodeID_getDataAsType(relnid, kRelation); if(!reld) return NULL; @@ -97,7 +105,7 @@ NV_Node *NV_NodeID_Relation_getLinkFrom(const NV_ID *relnid) NV_ID NV_NodeID_Relation_getIDLinkTo(const NV_ID *relnid) { - NV_Relation *reld; + const NV_Relation *reld; // reld = NV_NodeID_getDataAsType(relnid, kRelation); if(!reld) return NODEID_NOT_FOUND; @@ -113,7 +121,7 @@ NV_Node *NV_NodeID_Relation_getLinkTo(const NV_ID *relnid) NV_ID NV_NodeID_Relation_getIDLinkRel(const NV_ID *relnid) { - NV_Relation *reld; + const NV_Relation *reld; // reld = NV_NodeID_getDataAsType(relnid, kRelation); return reld->rel; @@ -132,7 +140,7 @@ NV_ID NV_NodeID_updateRelationTo(const NV_ID *relnid, const NV_ID *to) // 既存のものを削除して、新しいノードを生成し、以前と同一の関係性を持たせる。 NV_ID from, rel; NV_Node *n; - NV_Relation *reld; + const NV_Relation *reld; // n = NV_NodeID_getNode(relnid); reld = NV_Node_getDataAsType(n, kRelation); diff --git a/nv_static.c b/nv_static.c index f3e2d2f..7ccb24a 100644 --- a/nv_static.c +++ b/nv_static.c @@ -112,7 +112,7 @@ void NV_Graph_addStaticNode(const NV_ID *id, const char *s) { int f = NV_NodeID_exists(id); // - NV_NodeID_createAndString(id, s); + NV_Node_createWith_ID_String(id, s); if(!f){ NV_NodeID_createRelation(&NODEID_NV_STATIC_ROOT, &NODEID_NULL, id); } @@ -120,7 +120,7 @@ void NV_Graph_addStaticNode(const NV_ID *id, const char *s) void NV_Graph_initStaticNodes() { - NV_NodeID_createAndString(&NODEID_NV_STATIC_ROOT, "NV_StaticRoot"); + NV_Node_createWith_ID_String(&NODEID_NV_STATIC_ROOT, "NV_StaticRoot"); // NV_StaticNodeNameTag *t; int i; diff --git a/nv_string.c b/nv_string.c index cd77f0a..68f1eb7 100644 --- a/nv_string.c +++ b/nv_string.c @@ -1,10 +1,10 @@ #include "nv.h" -#include "nv_node.h" // // Internal // -void NV_Node_Internal_setStrToID(const NV_ID *id, const char *s) +/* +//void NV_Node_Internal_setStrToID(const NV_ID *id, const char *s) { NV_Node *n; // @@ -22,16 +22,18 @@ void NV_Node_Internal_setStrToID(const NV_ID *id, const char *s) ((char *)n->data)[n->size - 1] = 0; } } +*/ long NV_Node_String_Internal_strtol(const NV_Node *ns, int *endptrindex, int base) { long v; char *ep; - if(!ns || ns->type != kString){ + const char *str = NV_Node_getDataAsType(ns, kString); + if(!str){ if(endptrindex) *endptrindex = 0; return 0; } - v = strtol(ns->data, &ep, base); - if(endptrindex) *endptrindex = ep - (char *)ns->data; + v = strtol(str, &ep, base); + if(endptrindex) *endptrindex = ep - str; return v; } @@ -42,19 +44,24 @@ long NV_Node_String_Internal_strtol(const NV_Node *ns, int *endptrindex, int bas int NV_NodeID_isString(const NV_ID *id) { - NV_Node *n; - // - n = NV_NodeID_getNode(id); - if(!n || n->type != kString) return 0; - return 1; + return (NV_Node_getType(id) == kString); } NV_ID NV_Node_createWithString(const char *s) { - NV_ID id; - id = NV_Node_create(); - NV_Node_Internal_setStrToID(&id, s); - return id; + // string will be copied + NV_ID id = NV_ID_generateRandom(); + return NV_Node_createWith_ID_String(&id, s); +} + +NV_ID NV_Node_createWith_ID_String(const NV_ID *id, const char *s) +{ + // string will be copied + size_t len = strlen(s); + size_t size = len + 1; + char *buf = NV_malloc(size); + NV_strncpy(buf, s, size, len); + return NV_Node_createWith_ID_Type_Data_Size(id, kString, buf, size); } NV_ID NV_Node_createWithStringFormat(const char *fmt, ...) @@ -78,19 +85,9 @@ NV_ID NV_Node_createWithStringFormat(const char *fmt, ...) return newID; } -void NV_NodeID_createAndString(const NV_ID *id, const char *s) -{ - NV_NodeID_create(id); - NV_Node_Internal_setStrToID(id, s); -} - const char *NV_NodeID_getCStr(const NV_ID *id) { - NV_Node *n; - // - n = NV_NodeID_getNode(id); - if(!n || n->type != kString) return NULL; - return n->data; + return NV_NodeID_getDataAsType(id, kString); } int NV_Node_String_compare(const NV_ID *ida, const NV_ID *idb) @@ -98,10 +95,10 @@ int NV_Node_String_compare(const NV_ID *ida, const NV_ID *idb) // compatible with strcmp // but if node->data is null, returns -1. // "" == "" -> true - NV_Node *na = NV_NodeID_getNode(ida); - NV_Node *nb = NV_NodeID_getNode(idb); - if(!na || !nb || na->type != kString || nb->type != kString) return -1; - return strcmp(na->data, nb->data); + const char *strA = NV_NodeID_getDataAsType(ida, kString); + const char *strB = NV_NodeID_getDataAsType(idb, kString); + if(!strA || !strB) return -1; + return strcmp(strA, strB); } int NV_Node_String_compareWithCStr(const NV_ID *ida, const char *s) @@ -109,16 +106,16 @@ int NV_Node_String_compareWithCStr(const NV_ID *ida, const char *s) // compatible with strcmp // but if node->data is null, returns -1. // "" == "" -> true - NV_Node *na = NV_NodeID_getNode(ida); - if(!na || !s || na->type != kString) return -1; - return strcmp(na->data, s); + const char *str = NV_NodeID_getDataAsType(ida, kString); + if(!str) return -1; + return strcmp(str, s); } char *NV_Node_String_strchr(const NV_ID *id, char c) { - NV_Node *ns = NV_NodeID_getNode(id); - if(!ns || ns->type != kString) return NULL; - return strchr(ns->data, c); + const char *str = NV_NodeID_getDataAsType(id, kString); + if(!str) return NULL; + return strchr(str, c); } long NV_Node_String_strtol(const NV_ID *ns, int *endptrindex, int base) @@ -129,8 +126,8 @@ long NV_Node_String_strtol(const NV_ID *ns, int *endptrindex, int base) size_t NV_Node_String_strlen(const NV_ID *id) { - NV_Node *ns = NV_NodeID_getNode(id); - if(!ns || ns->type != kString) return 0; - return strlen(ns->data); + const char *str = NV_NodeID_getDataAsType(id, kString); + if(!str) return 0; + return strlen(str); } diff --git a/test/.gitignore b/test/.gitignore deleted file mode 100644 index c604443..0000000 --- a/test/.gitignore +++ /dev/null @@ -1 +0,0 @@ -testbin diff --git a/test/Makefile b/test/Makefile deleted file mode 100644 index ca6dce5..0000000 --- a/test/Makefile +++ /dev/null @@ -1,19 +0,0 @@ - -GIT_COMMIT_ID := $(shell git log -1 --format='%H') -GIT_COMMIT_DATE := $(shell git log -1 --format='%ad') - -SRCS= nv.c nv_array.c nv_dict.c nv_driver.c \ - nv_fix.c nv_id.c nv_node.c nv_op.c nv_static.c \ - nv_test.c nv_variable.c nv_graph.c nv_term.c nv_signal.c \ - nv_path.c -HEADERS=nv.h -CFLAGS=-Wall -Wextra -lncurses -Wunused-function -CFLAGS += -DGIT_COMMIT_ID="\"$(GIT_COMMIT_ID)\"" \ - -DGIT_COMMIT_DATE="\"$(GIT_COMMIT_DATE)\"" - -SRCS_PATH =$(addprefix ../, $(SRCS)) -HEADERS_PATH=$(addprefix ../, $(HEADERS)) - -testbin : $(SRCS_PATH) $(HEADERS_PATH) test.c Makefile - cc $(CFLAGS) -Os -o testbin $(SRCS_PATH) test.c - diff --git a/test/test.c b/test/test.c deleted file mode 100644 index f2156a8..0000000 --- a/test/test.c +++ /dev/null @@ -1,25 +0,0 @@ -#include "../nv.h" - -int main(int argc, char *argv[]) -{ - NV_Graph_insertInitialNode(); - // - NV_ID a, s; - a = NV_Array_create(); - NV_Array_print(&a); putchar('\n'); - printf("%d\n", NV_Array_count(&a)); - // - s = NV_Node_createWithString("test"); - NV_Array_push(&a, &s); - NV_Array_print(&a); putchar('\n'); - printf("%d\n", NV_Array_count(&a)); - // - s = NV_Node_createWithString("hoge"); - NV_Array_push(&a, &s); - NV_Array_print(&a); putchar('\n'); - printf("%d\n", NV_Array_count(&a)); - // - NV_Dict_print(&a); putchar('\n'); - // - return 0; -}