-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpxcf_bridge.c
More file actions
106 lines (92 loc) · 3.12 KB
/
Copy pathpxcf_bridge.c
File metadata and controls
106 lines (92 loc) · 3.12 KB
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
96
97
98
99
100
101
102
103
104
105
106
// --------------------------------------------------
// Project: ProX Programming Language (ProXPL)
// Module: PXCF Integration Bridge
// --------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pxcf/pxcf.h"
#include "object.h"
#include "memory.h"
#include "vm.h"
// The global VM instance used by ProXPL
extern VM vm;
static Value pxcfValueToProxValue(PxcfValue* pval) {
if (!pval) return NIL_VAL;
PxcfValueType type = pxcf_value_type(pval);
switch (type) {
case PXCF_VALUE_NULL: {
return NIL_VAL;
}
case PXCF_VALUE_BOOL: {
bool b = false;
pxcf_value_get_bool(pval, &b);
return BOOL_VAL(b);
}
case PXCF_VALUE_INTEGER: {
int64_t i = 0;
pxcf_value_get_integer(pval, &i);
return NUMBER_VAL((double)i);
}
case PXCF_VALUE_FLOAT: {
double f = 0.0;
pxcf_value_get_float(pval, &f);
return NUMBER_VAL(f);
}
case PXCF_VALUE_STRING: {
size_t len = 0;
const char* str = pxcf_value_get_string(pval, &len);
if (!str) return NIL_VAL;
return OBJ_VAL(copyString(str, (int)len));
}
case PXCF_VALUE_ARRAY: {
ObjList* list = newList();
push(&vm, OBJ_VAL(list));
size_t size = pxcf_array_size(pval);
for (size_t i = 0; i < size; i++) {
PxcfValue* item = pxcf_array_get(pval, i);
Value valObj = pxcfValueToProxValue(item);
push(&vm, valObj);
appendToList(list, valObj);
pop(&vm); // valObj
}
pop(&vm); // list
return OBJ_VAL(list);
}
case PXCF_VALUE_OBJECT: {
ObjDictionary* dict = newDictionary();
push(&vm, OBJ_VAL(dict));
size_t size = pxcf_object_size(pval);
for (size_t i = 0; i < size; i++) {
const char* key = NULL;
PxcfValue* item = pxcf_object_get_index(pval, i, &key);
if (!key || !item) continue;
ObjString* keyObj = copyString(key, (int)strlen(key));
push(&vm, OBJ_VAL(keyObj));
Value valObj = pxcfValueToProxValue(item);
push(&vm, valObj);
tableSet(&dict->items, keyObj, valObj);
pop(&vm); // valObj
pop(&vm); // keyObj
}
pop(&vm); // dict
return OBJ_VAL(dict);
}
}
return NIL_VAL;
}
Value nativeLoadConfig(int argCount, Value *args) {
if (argCount != 1 || !IS_STRING(args[0])) {
return NIL_VAL;
}
ObjString* pathStr = AS_STRING(args[0]);
PxcfError err;
PxcfDocument* doc = pxcf_load_file(pathStr->chars, &err);
if (!doc) {
return NIL_VAL;
}
PxcfValue* root = pxcf_document_root(doc);
Value result = pxcfValueToProxValue(root);
pxcf_document_free(doc);
return result;
}