Skip to content

Commit

Permalink
Hide NV_Node impl from nv_relation.c
Browse files Browse the repository at this point in the history
  • Loading branch information
hikalium committed Jun 8, 2017
1 parent cc94203 commit 779924d
Show file tree
Hide file tree
Showing 12 changed files with 260 additions and 107 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ html/
latex/
*.dtps/
note/
*.txt
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ SRCS= main.c \
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_variable.c nv_term.c nv_signal.c \
nv_integer.c nv_string.c nv_relation.c nv_context.c \
nv_integer.c nv_string.c nv_relation.c \
fnv1.c \
lang/02/parse.c lang/02/eval.c
lang/02/parse.c lang/02/eval.c \
nv_anchor.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)\"" \
Expand Down
12 changes: 12 additions & 0 deletions lang/02/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ NV_ID NV_Lang02_OpFunc_prefixOp
isAnsNotInteger = 1;
NV_Dict_print(scope);
*lastEvalVal = NODEID_NULL;
} else if(strcmp(opStr, "lsdep") == 0){
isAnsNotInteger = 1;
NV_Node_printDependencyTree(&rootScope, 0);
*lastEvalVal = NODEID_NULL;
} else if(strcmp(opStr, "dump") == 0){
isAnsNotInteger = 1;
FILE *fp = fopen("dump.txt", "w");
if(fp){
NV_Node_dumpAllToFile(fp);
fclose(fp);
}
*lastEvalVal = NODEID_NULL;
} else if(strcmp(opStr, "+") == 0){
opR = NV_Term_getPrimNodeID(&opR, scope);
ans = NV_Term_getInt32(&opR ,scope);
Expand Down
23 changes: 17 additions & 6 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// main
//

NV_ID rootScope;

int main(int argc, char *argv[])
{
NV_ID cTypeList, opDict;
Expand Down Expand Up @@ -35,6 +37,7 @@ int main(int argc, char *argv[])
//
NV_Node_initRoot();
//
/*
if(filename[0]){
// restore savedata
printf("Restoring from %s ...\n", filename);
Expand All @@ -50,18 +53,19 @@ int main(int argc, char *argv[])
printf("fopen failed.\n");
}
}
*/
//
NV_insertInitialNode();
//
NV_ID topLevelScope = NV_Node_createWithString("root");
rootScope = NV_Node_createWithString("root");
//
cTypeList = NV_createCharTypeList();
//
opDict = NV_createOpDict();
//
{
NV_ID opDictName = NV_Node_createWithString("opDict");
NV_ID opDictVar = NV_Variable_createWithName(&topLevelScope, &opDictName);
NV_ID opDictVar = NV_Variable_createWithName(&rootScope, &opDictName);
NV_Variable_assign(&opDictVar, &opDict);
}
//
Expand All @@ -83,7 +87,9 @@ int main(int argc, char *argv[])
"loop={for{#args[0]=args[1]}{#args[0]<=args[2]}{#args[0]++}{args[3]()}}");
*/
// TEST CODE BEGIN vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
/*
/*
NV_ID rootAnchor = NV_Anchor_createWithName("root");
NV_ID list1 = NV_Array_create();
NV_ID e;
e = NV_Node_createWithString("test1");
Expand All @@ -96,8 +102,13 @@ int main(int argc, char *argv[])
printf("Hash: %08X\n", NV_Term_calcHash(&list1));
printf("Hash: %08X\n", NV_Term_calcHash(&opDict));
FILE *fp = fopen("dump.txt", "w");
if(fp){
NV_Node_dumpAllToFile(fp);
fclose(fp);
}
return 0;
return 0;
*/
// TEST CODE END ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand All @@ -106,11 +117,11 @@ int main(int argc, char *argv[])
while(NV_gets(line, sizeof(line)) != NULL){
NV_ID tokenList = NV_tokenize(&cTypeList, line);
NV_ID codeGraphRoot = NV_parseToCodeGraph(&tokenList, &opDict);
NV_ID result = NV_evalGraph(&codeGraphRoot, &topLevelScope);
NV_ID result = NV_evalGraph(&codeGraphRoot, &rootScope);
//
if(!(NV_globalExecFlag & NV_EXEC_FLAG_SUPRESS_AUTOPRINT)){
printf(" = ");
NV_ID prim = NV_Term_getPrimNodeID(&result, &topLevelScope);
NV_ID prim = NV_Term_getPrimNodeID(&result, &rootScope);
NV_Term_print(&prim);
printf("\n");
} else{
Expand Down
3 changes: 3 additions & 0 deletions nv.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ struct NV_OP_POINTER {
NV_ID dict;
};

// @main.c
extern NV_ID rootScope;

// @nv.c
#define NV_EXEC_FLAG_VERBOSE 0x01
#define NV_EXEC_FLAG_INTERRUPT 0x02
Expand Down
23 changes: 23 additions & 0 deletions nv_dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ NV_ID NV_Dict_getAll(const NV_ID *root, const NV_ID *key)
int NV_Dict_foreach
(const NV_ID *dict, void *d, int (*f)(void *d, const NV_ID *rel, const NV_ID *to))
{
// ノードdictを起点とするすべての関係をたどって。そのrelationとtoをfに引き渡す。
// fの戻り値がfalseの場合はそこでループを中止する。
// 戻り値は、fを呼んだ回数である。
const NV_Node *n;
Expand All @@ -123,6 +124,28 @@ int NV_Dict_foreach
return count;
}

int NV_Dict_foreachWithRelFilter
(const NV_ID *dict, void *d, int (*f)(void *d, const NV_ID *rel, const NV_ID *to), int (*filter)(const NV_ID *rel))
{
// ノードdictを起点とするすべての関係をたどって。そのrelationとtoをfに引き渡す。
// filterがtrueを返す関係のノードのみたどる。
// fの戻り値がfalseの場合はそこでループを中止する。
// 戻り値は、fを呼んだ回数である。
const NV_Node *n;
const NV_Relation *reld;
int count = 0;
for(n = nodeRoot.next; n; n = n->next){
if(n->type == kRelation){
reld = n->data;
if(NV_NodeID_isEqual(&reld->from, dict) && filter(&n->id)){
count++;
if(!f(d, &reld->rel, &reld->to)) break;
}
}
}
return count;
}

NV_ID NV_Dict_getByStringKey
(const NV_ID *root, const char *key)
{
Expand Down
38 changes: 19 additions & 19 deletions nv_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ NV_ID NV_Dict_createMergedNode(const NV_ID *a, const NV_ID *b);
NV_ID NV_Dict_getAll(const NV_ID *root, const NV_ID *key);
int NV_Dict_foreach
(const NV_ID *dict, void *d, int (*f)(void *d, const NV_ID *rel, const NV_ID *to));
int NV_Dict_foreachWithRelFilter
(const NV_ID *dict, void *d, int (*f)(void *d, const NV_ID *rel, const NV_ID *to), int (*filter)(const NV_ID *rel));
NV_ID NV_Dict_getByStringKey
(const NV_ID *root, const char *key);
void NV_Dict_print(const NV_ID *root);
Expand Down Expand Up @@ -77,26 +79,35 @@ void NV_ID_dumpIDToFile(const NV_ID *id, FILE *fp);
// @nv_node.c
NV_ID NV_NodeID_createNew(const NV_ID *id);
void NV_Node_initRoot();
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);
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);
NV_Node *NV_NodeID_getNode(const NV_ID *id);
NV_NodeType NV_Node_getType(const NV_ID *id);
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_ID *id, NV_NodeType type);
void *NV_Node_getDataAsType(const NV_Node *n, NV_NodeType type);
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);
void NV_Node_restoreFromFile(FILE *fp);
void NV_NodeID_remove(const NV_ID *baseID);
NV_ID NV_NodeID_clone(const NV_ID *baseID);
NV_ID NV_Node_restoreFromString(const char *s);
void NV_Node_fdump(FILE *fp, const NV_ID *id);
void NV_Node_dump(const NV_ID *id);
void NV_Node_printPrimVal(const NV_ID *id);
void NV_NodeID_printForDebug(const NV_ID *id);
int NV_Node_printDependencyTreeFilter(const NV_ID *rel);
int NV_Node_printDependencyTreeSub(void *d, const NV_ID *rel, const NV_ID *to);
void NV_Node_printDependencyTree(const NV_ID *root, int currentDepth);


// @nv_op.c
Expand Down Expand Up @@ -191,14 +202,12 @@ NV_ID NV_NodeID_createUniqueIDRelation
(const NV_ID *from, const NV_ID *rel, const NV_ID *to);
NV_ID NV_NodeID_createUniqueEqRelation
(const NV_ID *from, const NV_ID *rel, const NV_ID *to);
void NV_Node_setRelation
(const NV_ID *relnid, const NV_ID *from, const NV_ID *rel, const NV_ID *to);
NV_Node *NV_NodeID_Relation_getLinkFrom(const NV_ID *relnid);
NV_ID NV_NodeID_Relation_getIDLinkTo(const NV_ID *relnid);
NV_Node *NV_NodeID_Relation_getLinkTo(const NV_ID *relnid);
NV_ID NV_NodeID_Relation_getIDLinkRel(const NV_ID *relnid);
NV_Node *NV_NodeID_Relation_getLinkRel(const NV_ID *relnid);
void NV_NodeID_updateRelationTo(const NV_ID *relnid, const NV_ID *to);
NV_ID NV_NodeID_updateRelationTo(const NV_ID *relnid, const NV_ID *to);
const NV_Node *NV_NodeID_getRelNodeFromWithCmp
(const NV_ID *from, const NV_ID *rel, int (*cmp)(const NV_ID *p, const NV_ID *q));
NV_ID NV_NodeID_getRelationFrom(const NV_ID *from, const NV_ID *rel);
Expand All @@ -207,20 +216,6 @@ NV_ID NV_NodeID_getEqRelationFrom(const NV_ID *from, const NV_ID *rel);
NV_ID NV_NodeID_getEqRelatedNodeFrom(const NV_ID *from, const NV_ID *rel);


// @nv_context.c
NV_ID NV_getContextList();
NV_ID NV_Context_create();
NV_ID NV_Context_getEvalStack(const NV_ID *ctx);
NV_ID NV_Context_createChildScopeWithArgs(const NV_ID *ctx, const NV_ID *argsBlock);
void NV_Context_pushToEvalStack
(const NV_ID *ctx, const NV_ID *code, const NV_ID *newScope);
NV_ID NV_Context_getCurrentCode(const NV_ID *ctx);
NV_ID NV_Context_getCurrentScope(const NV_ID *ctx);
NV_ID NV_Context_getLastResult(const NV_ID *ctx);
void NV_Context_setOpDict(const NV_ID *ctx, const NV_ID *opDict);
NV_ID NV_Context_getOpDict(const NV_ID *ctx);


// @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);
Expand Down Expand Up @@ -261,3 +256,8 @@ 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);

Loading

0 comments on commit 779924d

Please sign in to comment.