I have made an equivalent of Python's "eval" command in Arduino (which in itself is quite a feat) but the part that decodes the output of it is not small enough to fit in another 30 functions since this will be running on an Arduino Micro.
Any suggestions on compacting it? (btw I don't care about the actual function, rather the bit in "void Loop()" since that part is gonna be repeated a lot.)
struct evals {
int pointer;
bool boolout;
String stringout;
int intout;
float floatout;
};
String variables[50];
String LocalVariables[50];
void setup() {
for (int Variable = 0; Variable < 50; Variable++) {
LocalVariables[Variable] = "";
}
Serial.begin(9600);
LocalVariables[0] = "TestVariable";
variables[0] = "true";
}
void loop() {
if (Serial.available()) {
String read = Serial.readString();
evals read2 = eval(read);
switch (read2.pointer) {
case 0:
Serial.println("NULL");
break;
case 1:
Serial.print("INT ");
Serial.println(read2.intout);
break;
case 2:
Serial.print("FLOAT ");
Serial.println(read2.floatout);
break;
case 3:
Serial.print("STRING ");
Serial.println(read2.stringout);
break;
case 4:
Serial.print("BOOL ");
Serial.println(read2.boolout);
break;
case 5:
read2 = eval(variables[read2.intout]);
switch (read2.pointer) {
case 0:
Serial.println("NULL");
break;
case 1:
Serial.print("INT ");
Serial.println(read2.intout);
break;
case 2:
Serial.print("FLOAT ");
Serial.println(read2.floatout);
break;
case 3:
Serial.print("STRING ");
Serial.println(read2.stringout);
break;
case 4:
Serial.print("BOOL ");
Serial.println(read2.boolout);
break;
}
}
}
}
struct evals eval(String input) {
evals output;
input.replace("\n", "");
input.replace("\r", "");
char input2[input.length() + 1];
input.toCharArray(input2, input.length() + 1);
if (input.length() == 0) {
output.pointer = 0;
return output;
}
if (input2[0] == '"' and input2[strlen(input2) - 1] == '"') {
input.remove(0, 1);
input.remove(strlen(input2) - 2, 1);
output.pointer = 3;
output.stringout = input;
return output;
} else if (input == "true") {
output.pointer = 4;
output.boolout = true;
return output;
} else if (input == "false") {
output.pointer = 4;
output.boolout = false;
return output;
} else {
String inputs = input;
inputs.replace("0", "");
inputs.replace("1", "");
inputs.replace("2", "");
inputs.replace("3", "");
inputs.replace("4", "");
inputs.replace("5", "");
inputs.replace("6", "");
inputs.replace("7", "");
inputs.replace("8", "");
inputs.replace("9", "");
if (inputs.length() == 0) {
output.pointer = 1;
output.intout = input.toInt();
return output;
} else {
if (inputs[0] == '.' and inputs.length() == 1) {
output.pointer = 2;
output.floatout = input.toFloat();
return output;
} else {
for (int Variable = 0; Variable < 50; Variable++) {
if (LocalVariables[Variable] == "") {
continue;
} else if (LocalVariables[Variable] == input) {
output.pointer = 5;
output.intout = Variable;
return output;
}
}
output.pointer = 0;
return output;
}
}
}
}