Skip to content

Commit 3339d9a

Browse files
authored
Add files via upload
1 parent 51be699 commit 3339d9a

File tree

1 file changed

+355
-0
lines changed

1 file changed

+355
-0
lines changed

main.cpp

Lines changed: 355 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,355 @@
1+
2+
#include "mimalloc.h"
3+
4+
#include <vector>
5+
#include <string>
6+
#include <string_view>
7+
#include <iostream>
8+
#include <cstdint>
9+
#include <unordered_map>
10+
11+
#include "claujson.h"
12+
13+
namespace clau {
14+
15+
enum class OrderType : uint64_t { NONE = 0,
16+
ADD, // todo.. number? binary operation?
17+
RETURN, EXIT,
18+
VARIABLE,
19+
IF, WHILE, COND_END, EQ, NOTEQ,
20+
STRING, INT, UINT, FLOAT, BOOL, NULL_,
21+
PRINT,
22+
NEW_LOCAL_ARRAY, NEW_LOCAL_OBJECT,
23+
CD, // ? and dir?
24+
SIZE };
25+
26+
struct Order {
27+
union {
28+
OrderType type; // OrderType
29+
uint64_t pos; // pos of data in DataVector.
30+
};
31+
32+
Order(OrderType type) : type(type) { }
33+
Order(uint64_t pos) : pos(pos) { }
34+
};
35+
36+
class TapeA {
37+
private:
38+
std::vector<Order> m_order_list;
39+
public:
40+
void write(Order e) {
41+
m_order_list.push_back(e);
42+
}
43+
44+
const Order& operator[](uint64_t idx) const {
45+
return m_order_list[idx];
46+
}
47+
48+
uint64_t size() const {
49+
return m_order_list.size();
50+
}
51+
};
52+
53+
class TapeB {
54+
private:
55+
std::vector<claujson::_Value> m_data_list;
56+
public:
57+
void write(claujson::_Value e) {
58+
m_data_list.push_back(std::move(e));
59+
}
60+
61+
const claujson::_Value& operator[](uint64_t idx) const {
62+
return m_data_list[idx];
63+
}
64+
};
65+
66+
struct VM_Func {
67+
TapeA m_order_tape;
68+
TapeB m_data_tape;
69+
};
70+
71+
class Explorer {
72+
private:
73+
claujson::_Value* root = nullptr;
74+
std::vector<std::pair<uint64_t, claujson::Structured*>> _stack;
75+
76+
public:
77+
Explorer(claujson::_Value* root) : root(root) {
78+
if (root->is_primitive()) {
79+
_stack.push_back({ 0, nullptr });
80+
}
81+
else {
82+
_stack.push_back({ 0, root->as_structured_ptr() });
83+
}
84+
}
85+
Explorer() {
86+
//
87+
}
88+
89+
private:
90+
91+
claujson::Structured* Now() {
92+
return _stack.back().second;
93+
}
94+
95+
const claujson::Structured* Now() const {
96+
return _stack.back().second;
97+
}
98+
99+
public:
100+
bool IsPrimitiveRoot() const {
101+
return root->is_primitive();
102+
}
103+
104+
uint64_t GetIdx() const {
105+
if (_stack.empty()) { return 0; }
106+
return _stack.back().first;
107+
}
108+
109+
void SetIdx(const uint64_t idx) {
110+
if (_stack.empty()) { return; }
111+
_stack.back().first = idx;
112+
}
113+
114+
claujson::_Value& Get() {
115+
if (IsPrimitiveRoot()) {
116+
return *root;
117+
}
118+
return Now()->get_value_list(GetIdx());
119+
}
120+
121+
const claujson::_Value& Get() const {
122+
if (IsPrimitiveRoot()) {
123+
return *root;
124+
}
125+
return Now()->get_value_list(GetIdx());
126+
}
127+
128+
const claujson::_Value& GetKey() const {
129+
static claujson::_Value nkey(nullptr, false);
130+
if (!Now() || Now()->is_array()) {
131+
return nkey;
132+
}
133+
return Now()->get_const_key_list(GetIdx());
134+
}
135+
136+
void ChangeKey(claujson::Value new_key) {
137+
if (Now()) {
138+
Now()->change_key(GetKey(), std::move(new_key));
139+
}
140+
}
141+
142+
void Delete() {
143+
if (Now()) {
144+
Now()->erase(GetIdx(), true);
145+
}
146+
}
147+
148+
void Enter() {
149+
if (Get().is_structured()) {
150+
_stack.push_back({ 0, Get().as_structured_ptr() });
151+
}
152+
}
153+
154+
void Quit() {
155+
if (_stack.empty()) {
156+
return;
157+
}
158+
_stack.pop_back();
159+
}
160+
161+
// Group <- Array or Object!
162+
bool IsLastElementInGroup() { // END Of GROUP?
163+
if (IsPrimitiveRoot()) {
164+
return true;
165+
}
166+
if (nullptr == Now()) {
167+
return true;
168+
}
169+
return GetIdx() >= Now()->get_data_size();
170+
}
171+
172+
bool Next() {
173+
if (!IsLastElementInGroup()) {
174+
SetIdx(GetIdx() + 1);
175+
return true;
176+
}
177+
return false;
178+
}
179+
180+
// goto using json pointer?, dir is STRING or UNSIGNED_INTEGER
181+
void Goto(const std::vector<claujson::_Value>& dir) {
182+
183+
}
184+
185+
void Dump(std::ofstream& out) {
186+
while (!IsLastElementInGroup()) {
187+
if (GetKey().is_str()) {
188+
out << GetKey() << " : ";
189+
}
190+
if (Get().is_primitive()) {
191+
out << Get() << " ";
192+
}
193+
else {
194+
if (Get().is_array()) {
195+
out << " [ ";
196+
}
197+
else {
198+
out << " { ";
199+
}
200+
201+
Enter();
202+
203+
Explorer temp = *this;
204+
temp.Dump(out);
205+
206+
Quit();
207+
208+
if (Get().is_array()) {
209+
out << " ] \n";
210+
}
211+
else {
212+
out << " } \n";
213+
}
214+
}
215+
Next();
216+
}
217+
}
218+
};
219+
220+
// VM to edit json data.
221+
class VM {
222+
private:
223+
std::unordered_map<std::string, VM_Func> functions;
224+
private:
225+
std::vector<claujson::_Value> m_stack; // for calcul? 3+4 ?
226+
claujson::Value m_root;
227+
Explorer m_explorer;
228+
public:
229+
VM(claujson::Value v) : m_root(std::move(v)) {
230+
m_explorer = &m_root.Get();
231+
}
232+
public:
233+
// Register VM Function.
234+
void Register(const std::string& name, TapeA&& order_tape, TapeB&& data_tape) {
235+
functions.insert(std::make_pair(name, VM_Func{ std::move(order_tape), std::move(data_tape) }));
236+
}
237+
void Run(const std::string& start_func_name = "main") {
238+
const VM_Func& now_func = functions[start_func_name];
239+
uint64_t program_counter = 0;
240+
241+
while (program_counter < now_func.m_order_tape.size()) {
242+
switch (now_func.m_order_tape[program_counter].type) {
243+
// RETURN
244+
case OrderType::EXIT:
245+
program_counter++;
246+
return;
247+
break;
248+
// STRING, INT, UINT, FLOAT, BOOL, NULL_
249+
case OrderType::INT:
250+
if (now_func.m_data_tape[now_func.m_order_tape[program_counter + 1].pos].is_int()) {
251+
program_counter++;
252+
m_stack.push_back(claujson::_Value(now_func.m_data_tape[now_func.m_order_tape[program_counter].pos].get_integer()));
253+
}
254+
else {
255+
// log, error !
256+
}
257+
program_counter++;
258+
break;
259+
// + - * / %
260+
case OrderType::ADD:
261+
{
262+
claujson::_Value x, y, z;
263+
y = std::move(m_stack.back()); m_stack.pop_back();
264+
x = std::move(m_stack.back()); m_stack.pop_back();
265+
266+
if (x.type() == y.type()) {
267+
switch (x.type()) {
268+
case claujson::_ValueType::INT:
269+
z = claujson::_Value(x.get_integer() + y.get_integer());
270+
break;
271+
case claujson::_ValueType::UINT:
272+
z = claujson::_Value(x.get_unsigned_integer() + y.get_unsigned_integer());
273+
break;
274+
case claujson::_ValueType::FLOAT:
275+
z = claujson::_Value(x.get_floating() + y.get_floating());
276+
break;
277+
default:
278+
//
279+
break;
280+
}
281+
}
282+
m_stack.push_back(std::move(z));
283+
}
284+
program_counter++;
285+
break;
286+
// PRINT,
287+
case OrderType::PRINT:
288+
std::cout << m_stack.back();
289+
m_stack.pop_back();
290+
program_counter++;
291+
break;
292+
// VARIALBLE..
293+
294+
295+
296+
// manipulate Explorer?
297+
298+
299+
default:
300+
std::cout << "error";
301+
break;
302+
}
303+
}
304+
}
305+
306+
void ExplorerDump(std::ofstream& out) {
307+
m_explorer.Dump(out);
308+
}
309+
};
310+
}
311+
312+
313+
int main(void)
314+
{
315+
claujson::parser p(16);
316+
claujson::Document d;
317+
p.parse("citylots.json", d, 16); // chk exception process..
318+
319+
clau::VM vm_test(std::move(d.Get()));
320+
{
321+
clau::TapeA test;
322+
test.write(clau::OrderType::INT);
323+
test.write(0);
324+
test.write(clau::OrderType::INT);
325+
test.write(1);
326+
test.write(clau::OrderType::ADD);
327+
test.write(clau::OrderType::PRINT);
328+
test.write(clau::OrderType::EXIT);
329+
330+
clau::TapeB test2;
331+
test2.write(claujson::_Value(10));
332+
test2.write(claujson::_Value(20));
333+
334+
vm_test.Register("main", std::move(test), std::move(test2));
335+
}
336+
337+
try {
338+
vm_test.Run();
339+
{
340+
std::ofstream out;
341+
out.open("save.json", std::ios::binary);
342+
if (out) {
343+
vm_test.ExplorerDump(out);
344+
out.close();
345+
}
346+
}
347+
}
348+
catch (...) {
349+
return -1;
350+
}
351+
352+
return 0;
353+
}
354+
355+

0 commit comments

Comments
 (0)