-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
130 lines (107 loc) · 2.98 KB
/
main.cpp
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>
#include <stdio.h>
#include "ArduinoJson/ArduinoJson.h"
#define print(STR) cout << STR
#define println(STR) cout << STR << endl
#define get(VAR) cin >> VAR
#define handlerTemplate(STR)"\tvoid handler(" STR " *x){\n" "\t\t*x = %s;\n" "\t}\n"
using namespace std;
typedef struct eventNode{
string word;
eventNode* next;
}*eventList,eventNode;
void event1(JsonObject& root){
int i;
string format;
const char* name = root["name"];
JsonArray& items = root["items"];
for(i = 0 ; i < items.size();i++){
JsonObject& obj = items[i];
format = obj["format"].asString();
//format = items[i]["format"];
//printf(format);
}
}
void event2(JsonObject& root){
int i;
int nesting = root["nesting"];
char tabulation[10];
for(i = 0; i < nesting;tabulation[i++] = '\t');
tabulation[i] = '\0';
}
JsonObject& jsonCallback(string json){
StaticJsonBuffer<200000> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
return root;
}
void buildCode(JsonObject& root){
//JsonObject& root = *jsonPtr;
const char* name = root["name"];
string sStr = root["handler"]["str"];// if(exprStr == "(null)"){exprStr = "0"};
string sInt = root["handler"]["int"];
string sBool = root["handler"]["bool"];
/*
* Attributes the correct expression to the function
* If the expression is empty attributes the "0" value
*
*/
const char* exprStr = (sStr.c_str()[0] == '\0') ? "0" : sStr.c_str();
const char* exprInt = (sInt.c_str()[0] == '\0') ? "0" : sInt.c_str();
const char* exprBool = (sBool.c_str()[0] == '\0') ? "0" : sBool.c_str();
/*
<example>
class LDR : public Sensor{
public:
static const char* name;
LDR (int pin) {this->pin = pin;}
void handle(char* x){
return;
}
void handle(int* x){
*x = 1;
return;
}
void handle(bool* x){
return;
}
};
</example>
*/
//.h
printf("class " "%s" " : public Sensor{\n"
"public:\n\n"
"\tstatic const char* name;\n"
"\t%s (int pin) {this->pin = pin;}\n\n"
handlerTemplate("char")
handlerTemplate("int")
handlerTemplate("bool")
"}\n"
,name,name
,exprStr,exprInt,exprBool);
//.cpp
printf("const char* %s::name = \"%s\";\n",name,name);
}
/*void buildBlock(){
string json;
get(json);
JsonObject& root = jsonCallback(json);
buildCode(root);
}*/
void buildBlock(){
int i;
string json;
get(json);
JsonObject& root = jsonCallback(json);
if (!root.success()){
println("parseObject() failed - A famigerada merda");
return;
}
JsonArray& sensors = root["sensors"];
for(i = 0; i < sensors.size();i++){
buildCode(sensors[i]);
}
}
int main(){
buildBlock();
return 0;
}