Skip to content

Commit

Permalink
Add nv_enode.c and nv_op.c
Browse files Browse the repository at this point in the history
  • Loading branch information
hikalium committed Nov 25, 2016
1 parent 70c966d commit c2e2667
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 88 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

SRCS= nv.c nv_array.c nv_dict.c nv_driver.c \
nv_fix.c nv_id.c nv_node.c nv_static.c \
SRCS= nv.c nv_array.c nv_dict.c nv_driver.c nv_enode.c \
nv_fix.c nv_id.c nv_node.c nv_op.c nv_static.c \
nv_test.c nv_variable.c
HEADERS=nv.h
CFLAGS=-Wall -Wextra -lncurses -Wunused-function
Expand Down
87 changes: 15 additions & 72 deletions nv.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ void NV_Graph_init()
NV_Graph_addStaticNode(&NODEID_TREE_TYPE_ARRAY, "TreeType(Array)");
NV_Graph_addStaticNode(&NODEID_TREE_TYPE_VARIABLE, "TreeType(Variable)");
NV_Graph_addStaticNode(&NODEID_TREE_TYPE_OP, "TreeType(Op)");
//
NV_Graph_addStaticNode(&RELID_TREE_TYPE, "relTreeType");
NV_Graph_addStaticNode(&RELID_ARRAY_NEXT, "relArrayNext");
NV_Graph_addStaticNode(&RELID_VARIABLE_DATA, "relVariableData");
NV_Graph_addStaticNode(&RELID_POINTER_TARGET, "relPointerTarget");
NV_Graph_addStaticNode(&RELID_OP_PRECEDENCE, "relPointerTarget");
NV_Graph_addStaticNode(&RELID_OP_FUNC, "relPointerTarget");
}


Expand Down Expand Up @@ -68,82 +71,12 @@ int NV_runInteractive(const NV_ID *cTypeList, const NV_ID *opList)
while(NV_gets(line, sizeof(line)) != NULL){
tokenList = NV_tokenize(cTypeList, line);
NV_convertLiteral(&tokenList, opList);
NV_Array_print(&tokenList);
NV_printNodeByID(&tokenList);

}
return 0;
}

#define NV_LANG_CHAR_LIST_LEN 3
int NV_Lang_getCharType(const NV_ID *cTypeList, char c)
{
NV_ID t;
int i;
if(c == '\0') return -1;
for(i = 0; i < NV_LANG_CHAR_LIST_LEN; i++){
t = NV_Array_getByIndex(cTypeList, i);
if(NV_Node_String_strchr(NV_Node_getByID(&t), c)) break;
}
return i;
}


NV_ID NV_createCharTypeList()
{
NV_ID ns;
NV_ID cList = NV_Array_create();
//
ns = NV_Node_createWithString(" \t\r\n");
NV_Array_push(&cList, &ns);
ns = NV_Node_createWithString("#!%&-=^~|+*:.<>/");
NV_Array_push(&cList, &ns);
ns = NV_Node_createWithString("(){}[],;\"`\\");
NV_Array_push(&cList, &ns);
//
return cList;
}

void NV_addOp(const NV_ID *opList, const char *token, int32_t prec, const NV_ID *func)
{
NV_ID opEntry;
NV_ID ePrec;
opEntry = NV_Node_create();
NV_Node_createRelation(
&opEntry, &RELID_TREE_TYPE, &NODEID_TREE_TYPE_OP);
ePrec = NV_Node_createWithInt32(prec);
NV_Node_createRelation(
&opEntry, &RELID_OP_PRECEDENCE, &ePrec);
NV_Node_createRelation(
&opEntry, &RELID_OP_FUNC, func);

//
NV_Dict_addByStringKey(opList, token, &opEntry);
}

NV_ID NV_createOpList()
{
NV_ID nv;
NV_ID opList = NV_Node_createWithString("NV_OpList");
//
nv = NV_Node_createWithString("NV_Op_add");
NV_addOp(&opList, "+", 100, &nv);
//
nv = NV_Node_createWithString("NV_Op_sub");
NV_addOp(&opList, "-", 100, &nv);
//
nv = NV_Node_createWithString("NV_Op_mul");
NV_addOp(&opList, "*", 200, &nv);
//
nv = NV_Node_createWithString("NV_Op_div");
NV_addOp(&opList, "/", 200, &nv);
//
nv = NV_Node_createWithString("NV_Op_mod");
NV_addOp(&opList, "%", 200, &nv);
//
nv = NV_Node_createWithString("NV_Op_nothing");
NV_addOp(&opList, " ", 300, &nv);
//
return opList;
}

NV_ID NV_tokenize(const NV_ID *cTypeList, const char *input)
{
Expand Down Expand Up @@ -219,6 +152,16 @@ int NV_convertLiteral(const NV_ID *tokenizedList, const NV_ID *opList)
// Evaluate
//
/*
void NV_evaluateSetence(const NV_ID *tokenizedList)
{
int i;
NV_ID t;
for(i = 0;;){
t =
}
}
*/
/*
void NV_evaluateSentence(const NV_ID *tokenizedList)
{
int pIndex;
Expand Down
15 changes: 13 additions & 2 deletions nv.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ extern NV_Node nodeRoot;
void NV_Graph_init();
void NV_Graph_dump();
int NV_isTreeType(const NV_ID *node, const NV_ID *tType);
int NV_runInteractive(const NV_ID *cTypeList, const NV_ID *opList);
NV_ID NV_tokenize(const NV_ID *cTypeList, const char *input);
int NV_runInteractive(const NV_ID *cTypeList, const NV_ID *opList);
int NV_convertLiteral(const NV_ID *tokenizedList, const NV_ID *opList);

// @nv_array.c
Expand All @@ -93,6 +93,10 @@ void NV_Dict_print(const NV_ID *root);
// @nv_driver.c
char *NV_gets(char *str, int size);

// @nv_enode.c
void NV_printNodeByID(const NV_ID *id);
void NV_printNode(const NV_Node *n);

// @nv_fix.c
char *NV_strncpy(char *dst, const char *src, size_t dst_size, size_t copy_size);
int NV_getMallocCount();
Expand Down Expand Up @@ -138,6 +142,13 @@ NV_ID NV_Node_createWithInt32(int32_t v);
void NV_Node_setInt32ToID(const NV_ID *id, int32_t v);
int32_t NV_Node_getInt32FromID(const NV_ID *id);

// nv_op.c
int NV_Lang_getCharType(const NV_ID *cTypeList, char c);
NV_ID NV_createCharTypeList();
void NV_addOp(const NV_ID *opList, const char *token, int32_t prec, const NV_ID *func);
NV_ID NV_createOpList();
void NV_Op_print(const NV_ID *op);

// @nv_static.c
extern const NV_ID NODEID_NULL;
extern const NV_ID NODEID_REL_MASTERLINK;
Expand All @@ -161,5 +172,5 @@ NV_ID NV_Variable_create();
void NV_Variable_assign(const NV_ID *vid, const NV_ID *data);
NV_ID NV_Variable_getData(const NV_ID *vid);
void NV_Variable_print(const NV_ID *vid);
void NV_Variable_printiPrimVal(const NV_ID *vid);
void NV_Variable_printPrimVal(const NV_ID *vid);

3 changes: 2 additions & 1 deletion nv_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ void NV_Array_print(const NV_ID *array)
printf("[");
t = NV_Node_getRelatedNodeFrom(array, &RELID_ARRAY_NEXT);
for(;!NV_ID_isEqual(&t, &NODEID_NULL);){
NV_Variable_printiPrimVal(&t);
NV_printNodeByID(&t);
t = NV_Node_getRelatedNodeFrom(&t, &RELID_ARRAY_NEXT);
if(!NV_ID_isEqual(&t, &NODEID_NULL)){
printf(",");
}
}
printf("]\n");
}

2 changes: 1 addition & 1 deletion nv_dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void NV_Dict_print(const NV_ID *root)
printf("|--- ");
NV_ID_printPrimVal(&reld->rel);
printf(": ");
NV_ID_printPrimVal(&reld->to);
NV_printNodeByID(&reld->to);
printf("\n");
}
}
Expand Down
24 changes: 24 additions & 0 deletions nv_enode.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "nv.h"

void NV_printNodeByID(const NV_ID *id)
{
NV_Node *n = NV_Node_getByID(id);
NV_printNode(n);
}

void NV_printNode(const NV_Node *n)
{
if(!n){
printf("(null pointer node)");
return;
}
if(NV_isTreeType(&n->id, &NODEID_TREE_TYPE_ARRAY)){
NV_Array_print(&n->id);
} else if(NV_isTreeType(&n->id, &NODEID_TREE_TYPE_VARIABLE)){
NV_Variable_printPrimVal(&n->id);
} else if(NV_isTreeType(&n->id, &NODEID_TREE_TYPE_OP)){
NV_Op_print(&n->id);
} else{
NV_Node_printPrimVal(n);
}
}
87 changes: 87 additions & 0 deletions nv_op.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include "nv.h"

#define NV_LANG_CHAR_LIST_LEN 3
int NV_Lang_getCharType(const NV_ID *cTypeList, char c)
{
NV_ID t;
int i;
if(c == '\0') return -1;
for(i = 0; i < NV_LANG_CHAR_LIST_LEN; i++){
t = NV_Array_getByIndex(cTypeList, i);
if(NV_Node_String_strchr(NV_Node_getByID(&t), c)) break;
}
return i;
}


NV_ID NV_createCharTypeList()
{
NV_ID ns;
NV_ID cList = NV_Array_create();
//
ns = NV_Node_createWithString(" \t\r\n");
NV_Array_push(&cList, &ns);
ns = NV_Node_createWithString("#!%&-=^~|+*:.<>/");
NV_Array_push(&cList, &ns);
ns = NV_Node_createWithString("(){}[],;\"`\\");
NV_Array_push(&cList, &ns);
//
return cList;
}

void NV_addOp(const NV_ID *opList, const char *token, int32_t prec, const NV_ID *func)
{
NV_ID opEntry;
NV_ID ePrec;
opEntry = NV_Node_create();
NV_Node_createRelation(
&opEntry, &RELID_TREE_TYPE, &NODEID_TREE_TYPE_OP);
ePrec = NV_Node_createWithInt32(prec);
NV_Node_createRelation(
&opEntry, &RELID_OP_PRECEDENCE, &ePrec);
NV_Node_createRelation(
&opEntry, &RELID_OP_FUNC, func);

//
NV_Dict_addByStringKey(opList, token, &opEntry);
}

NV_ID NV_createOpList()
{
NV_ID nv;
NV_ID opList = NV_Node_createWithString("NV_OpList");
//
nv = NV_Node_createWithString("NV_Op_add");
NV_addOp(&opList, "+", 100, &nv);
//
nv = NV_Node_createWithString("NV_Op_sub");
NV_addOp(&opList, "-", 100, &nv);
//
nv = NV_Node_createWithString("NV_Op_mul");
NV_addOp(&opList, "*", 200, &nv);
//
nv = NV_Node_createWithString("NV_Op_div");
NV_addOp(&opList, "/", 200, &nv);
//
nv = NV_Node_createWithString("NV_Op_mod");
NV_addOp(&opList, "%", 200, &nv);
//
nv = NV_Node_createWithString("NV_Op_nothing");
NV_addOp(&opList, " ", 300, &nv);
//
NV_Dict_print(&opList);
return opList;
}

void NV_Op_print(const NV_ID *op)
{
NV_ID eFunc;
NV_ID ePrec;
eFunc = NV_Node_getRelatedNodeFrom(op, &RELID_OP_FUNC);
ePrec = NV_Node_getRelatedNodeFrom(op, &RELID_OP_PRECEDENCE);
printf("(op ");
NV_printNodeByID(&eFunc);
printf("/");
NV_printNodeByID(&ePrec);
printf(")");
}
13 changes: 5 additions & 8 deletions nv_static.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#include "nv.h"

const NV_ID NODEID_NV_STATIC_ROOT
= {{0xB257ACBF, 0x5D434C81, 0x8D79C638, 0xA2BF94B3}};
const NV_ID NODEID_NULL
= {{0x00000000, 0, 0, 0}};
const NV_ID NODEID_REL_MASTERLINK
= {{0xffffffff, 0, 0, 0}};

const NV_ID NODEID_TREE_TYPE_ARRAY
= {{0xBA7C82D7, 0, 0, 0}};
const NV_ID NODEID_TREE_TYPE_VARIABLE
= {{0x67DEB167, 0, 0, 0}};
const NV_ID NODEID_TREE_TYPE_OP
= {{0x83E75537, 0x03CB43A8, 0x9F0B84ED, 0xEA0C635A}};
//
const NV_ID RELID_TREE_TYPE
= {{0xE804DE81, 0, 0, 0}};
const NV_ID RELID_ARRAY_NEXT
= {{0xA71CE915, 0, 0, 0}};
const NV_ID RELID_VARIABLE_DATA
Expand All @@ -21,11 +23,6 @@ const NV_ID RELID_OP_PRECEDENCE
= {{0x46452917, 0x15084D12, 0xAB03F07E, 0xA0D58BC7}};
const NV_ID RELID_OP_FUNC
= {{0x803C7EBA, 0xFAAF4DA3, 0x9D8486BB, 0xEF6DC077}};
const NV_ID RELID_TREE_TYPE
= {{0xE804DE81, 0, 0, 0}};

const NV_ID NODEID_NV_STATIC_ROOT
= {{0xB257ACBF, 0x5D434C81, 0x8D79C638, 0xA2BF94B3}};

const char *NV_NodeTypeList[kNodeTypeCount] = {
"None",
Expand Down
4 changes: 2 additions & 2 deletions nv_variable.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ void NV_Variable_print(const NV_ID *vid)
putchar('\n');
}

void NV_Variable_printiPrimVal(const NV_ID *vid)
void NV_Variable_printPrimVal(const NV_ID *vid)
{
NV_ID targetID;
if(!NV_isTreeType(vid, &NODEID_TREE_TYPE_VARIABLE)){
printf("id: %08X is not Variable.", vid->d[0]);
return;
}
targetID = NV_Node_getRelatedNodeFrom(vid, &RELID_VARIABLE_DATA);
NV_Node_printPrimVal(NV_Node_getByID(&targetID));
NV_printNode(NV_Node_getByID(&targetID));
}

0 comments on commit c2e2667

Please sign in to comment.