-
Notifications
You must be signed in to change notification settings - Fork 1
/
nv_context.c
95 lines (85 loc) · 2.55 KB
/
nv_context.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "nv.h"
NV_ID NV_getContextList()
{
return NV_NodeID_getRelatedNodeFrom(&NODEID_NV_STATIC_ROOT, &RELID_CONTEXT_LIST);
}
NV_ID NV_Context_create()
{
char st[128];
char s[128];
time_t td;
td = time(NULL);
strftime(st, sizeof(st), "%F %H:%M:%S", localtime(&td));
snprintf(s, sizeof(s), "%s %lu", st, clock());
NV_ID ctx = NV_Node_createWithString(s);
//
NV_ID evalStack = NV_Array_create();
NV_Dict_addUniqueIDKey(&ctx, &RELID_EVAL_STACK, &evalStack);
//
NV_ID ctxList = NV_getContextList();
NV_Array_push(&ctxList, &ctx);
//
return ctx;
}
NV_ID NV_Context_getEvalStack(const NV_ID *ctx)
{
return NV_NodeID_getRelatedNodeFrom(ctx, &RELID_EVAL_STACK);
}
NV_ID NV_Context_createChildScopeWithArgs(const NV_ID *ctx, const NV_ID *argsBlock)
{
NV_ID newScope;
NV_ID parentScope = NV_Context_getCurrentScope(ctx);
newScope = NV_Node_createWithStringFormat("scope");
NV_Dict_addUniqueIDKey(&newScope, &RELID_PARENT_SCOPE, &parentScope);
if(argsBlock){
NV_ID argsVar = NV_Variable_create();
NV_Variable_assign(&argsVar, argsBlock);
NV_Dict_addKeyByCStr(&newScope, "args", &argsVar);
}
return newScope;
}
void NV_Context_pushToEvalStack
(const NV_ID *ctx, const NV_ID *code, const NV_ID *newScope)
{
NV_ID evalStack = NV_Context_getEvalStack(ctx);
//
NV_ID currentScope;
if(newScope){
// スコープが指定されていれば、そのスコープをcodeの実行時に使うようにする。
currentScope = *newScope; // GLOBAL SCOPE
} else{
// NULLならば、現在のコンテキストを親に持つ新しいコンテキストに設定する。
currentScope = NV_Context_createChildScopeWithArgs(ctx, NULL);
}
//
NV_Dict_addUniqueIDKey(code, &RELID_CURRENT_SCOPE, ¤tScope);
NV_Array_push(&evalStack, code);
if(IS_DEBUG_MODE()){
printf("pushed to evalStack: ");
NV_Array_print(code); putchar('\n');
printf("evalStack: ");
NV_Array_print(&evalStack); putchar('\n');
}
}
NV_ID NV_Context_getCurrentCode(const NV_ID *ctx)
{
NV_ID evalStack = NV_Context_getEvalStack(ctx);
return NV_Array_last(&evalStack);
}
NV_ID NV_Context_getCurrentScope(const NV_ID *ctx)
{
NV_ID currentCode = NV_Context_getCurrentCode(ctx);
return NV_NodeID_getRelatedNodeFrom(¤tCode, &RELID_CURRENT_SCOPE);
}
NV_ID NV_Context_getLastResult(const NV_ID *ctx)
{
return NV_NodeID_getRelatedNodeFrom(ctx, &RELID_LAST_RESULT);
}
void NV_Context_setOpDict(const NV_ID *ctx, const NV_ID *opDict)
{
NV_Dict_addUniqueIDKey(ctx, &RELID_OP_DICT, opDict);
}
NV_ID NV_Context_getOpDict(const NV_ID *ctx)
{
return NV_NodeID_getRelatedNodeFrom(ctx, &RELID_OP_DICT);
}