strFromFile

This commit is contained in:
2022-08-29 16:01:25 +03:00
parent 0f869ebf39
commit e17260628e
2 changed files with 24 additions and 18 deletions

View File

@@ -79,7 +79,7 @@ class IoTScenario {
std::vector<ExprAST *> ScenarioElements; // корневые элементы дерава std::vector<ExprAST *> ScenarioElements; // корневые элементы дерава
String *strFromFile; String strFromFile;
char getLastChar(); char getLastChar();
int strIterator = 0; int strIterator = 0;

View File

@@ -587,8 +587,10 @@ class BracketsExprAST : public ExprAST {
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
char IoTScenario::getLastChar() { char IoTScenario::getLastChar() {
if (strIterator == strFromFile.length()) return 0;
char tmpCh = strFromFile.charAt(strIterator);
strIterator++; strIterator++;
return strFromFile->charAt(strIterator - 1); return tmpCh;
} }
/// gettok - Возвращает следующий токен из стандартного потока ввода. /// gettok - Возвращает следующий токен из стандартного потока ввода.
@@ -625,7 +627,7 @@ int IoTScenario::gettok() {
if (LastChar == '#') { if (LastChar == '#') {
// Комментарий до конца строки // Комментарий до конца строки
do LastChar = getLastChar(); do LastChar = getLastChar();
while (LastChar != EOF && LastChar != '\n' && LastChar != '\r'); while (LastChar != 0 && LastChar != '\n' && LastChar != '\r');
if (LastChar != EOF) if (LastChar != EOF)
return gettok(); return gettok();
@@ -634,7 +636,7 @@ int IoTScenario::gettok() {
if (LastChar == '"') { // "строка" if (LastChar == '"') { // "строка"
IdentifierStr = ""; IdentifierStr = "";
LastChar = getLastChar(); LastChar = getLastChar();
while (LastChar != '"') { while (LastChar != '"' && LastChar != 0) {
IdentifierStr += LastChar; IdentifierStr += LastChar;
LastChar = getLastChar(); LastChar = getLastChar();
} }
@@ -853,6 +855,7 @@ ExprAST *IoTScenario::ParseIfExpr(String *IDNames) {
ExprAST *IoTScenario::ParsePrimary(String *IDNames) { ExprAST *IoTScenario::ParsePrimary(String *IDNames) {
switch (CurTok) { switch (CurTok) {
default: default:
Serial.println(CurTok);
return Error("unknown token when expecting an expression"); return Error("unknown token when expecting an expression");
case tok_identifier: { case tok_identifier: {
if (IDNames) { if (IDNames) {
@@ -929,28 +932,31 @@ void IoTScenario::loadScenario(String fileName, String eventIdName) { // пос
File myfile = seekFile(fileName); File myfile = seekFile(fileName);
if (myfile.available()) { if (myfile.available()) {
strFromFile = new String(""); //strFromFile = new String("");
*strFromFile = myfile.readString(); strFromFile = myfile.readString();
//Serial.println(strFromF); //Serial.println(strFromF);
//jsonRead(strFromF, "scen", *strFromFile, true); //jsonRead(strFromF, "scen", *strFromFile, true);
myfile.close(); myfile.close();
strFromFile->replace("{\"scen\":\"", ""); strFromFile.replace("{\"scen\":\"", "");
//strFromFile->replace("\"}", ""); strFromFile.replace("\\n\"}", "");
//Serial.println(*strFromFile); strFromFile.replace("\\n", "\n");
strFromFile.replace("\\\"", "\"");
//Serial.println(strFromFile);
if (strFromFile->length()) { if (strFromFile.length()) {
getNextToken();
while (strIterator < strFromFile->length() - 1) { while (strIterator < strFromFile.length()) {
// Serial.printf("-%c", LastChar); // Serial.printf("-%c", LastChar);
getNextToken();
switch (CurTok) { switch (CurTok) {
// case tok_eof: break; // case tok_eof: break;
case tok_if: { case tok_if: {
String IDNames = ""; // накопитель встречающихся идентификаторов в условии String IDNames = ""; // накопитель встречающихся идентификаторов в условии
ExprAST *tmpAST = ParseIfExpr(&IDNames); ExprAST *tmpAST = ParseIfExpr(&IDNames);
if (!tmpAST) break; if (!tmpAST) break;
if (SCENARIO_BLOCK_LOAD) { if (SCENARIO_BLOCK_LOAD) {
if (tmpAST->hasEventIdName(eventIdName)) { if (tmpAST->hasEventIdName(eventIdName)) {
tmpAST->exec(); tmpAST->exec();
@@ -958,17 +964,17 @@ void IoTScenario::loadScenario(String fileName, String eventIdName) { // пос
} }
delete tmpAST; delete tmpAST;
} else ScenarioElements.push_back(tmpAST); } else ScenarioElements.push_back(tmpAST);
break;
break;
} }
default: default:
getNextToken();
break; break;
} }
} }
} }
delete strFromFile; //delete strFromFile;
strIterator = 0; strIterator = 0;
} else { } else {
Error("Open file scenario error"); Error("Open file scenario error");