2021-10-13 18:05:54 +08:00
|
|
|
#include "PikaParser.h"
|
|
|
|
#include "PikaObj.h"
|
|
|
|
#include "dataQueueObj.h"
|
|
|
|
#include "dataStrs.h"
|
|
|
|
|
2021-10-13 20:07:32 +08:00
|
|
|
char* strsPopStmts(Args* buffs, char* stmts) {
|
|
|
|
int32_t size = strGetSize(stmts);
|
|
|
|
if (0 == size) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
char* strOut = args_getBuff(buffs, size);
|
|
|
|
int32_t iOut = 0;
|
|
|
|
char delChar = ' ';
|
|
|
|
int32_t stmtEnd = 0;
|
|
|
|
uint8_t isGetSign = 0;
|
|
|
|
int32_t parentheseDeepth = 0;
|
|
|
|
for (int32_t i = 0; i < size; i++) {
|
|
|
|
if ('(' == stmts[i]) {
|
|
|
|
parentheseDeepth++;
|
|
|
|
}
|
|
|
|
if (')' == stmts[i]) {
|
|
|
|
parentheseDeepth--;
|
|
|
|
}
|
|
|
|
if (parentheseDeepth == 0) {
|
|
|
|
if (',' == stmts[i]) {
|
|
|
|
stmtEnd = i;
|
|
|
|
isGetSign = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isGetSign) {
|
|
|
|
stmtEnd = size;
|
|
|
|
}
|
|
|
|
for (int32_t i = 0; i < stmtEnd; i++) {
|
|
|
|
strOut[i] = stmts[i];
|
|
|
|
}
|
|
|
|
memmove(stmts, stmts + stmtEnd + 1, size);
|
|
|
|
strOut[stmtEnd] = 0;
|
|
|
|
return strOut;
|
|
|
|
}
|
|
|
|
|
2021-10-13 19:41:04 +08:00
|
|
|
AST* AST_parseStmt(AST* ast, char* stmt) {
|
2021-10-13 18:05:54 +08:00
|
|
|
Args* buffs = New_strBuff();
|
2021-10-13 18:24:38 +08:00
|
|
|
char* assignment = strsGetFirstToken(buffs, stmt, '(');
|
2021-10-13 18:05:54 +08:00
|
|
|
char* direct = NULL;
|
2021-10-13 18:24:38 +08:00
|
|
|
char* method = NULL;
|
2021-10-13 21:20:45 +08:00
|
|
|
char* ref = NULL;
|
|
|
|
uint8_t directExist = 0;
|
|
|
|
uint8_t isMethod = 0;
|
2021-10-13 18:05:54 +08:00
|
|
|
if (strIsContain(assignment, '=')) {
|
2021-10-13 21:20:45 +08:00
|
|
|
directExist = 1;
|
|
|
|
}
|
|
|
|
if (strIsContain(stmt, '(') || strIsContain(stmt, ')')) {
|
|
|
|
isMethod = 1;
|
|
|
|
}
|
|
|
|
if (directExist) {
|
2021-10-13 18:05:54 +08:00
|
|
|
direct = strsGetFirstToken(buffs, assignment, '=');
|
2021-10-13 20:07:32 +08:00
|
|
|
obj_setStr(ast, (char*)"direct", direct);
|
2021-10-13 18:05:54 +08:00
|
|
|
}
|
2021-10-13 20:18:12 +08:00
|
|
|
char* subStmts = NULL;
|
2021-10-13 21:20:45 +08:00
|
|
|
if (isMethod) {
|
|
|
|
if (directExist) {
|
|
|
|
method = strsGetLastToken(buffs, assignment, '=');
|
|
|
|
} else {
|
|
|
|
method = assignment;
|
|
|
|
}
|
|
|
|
obj_setStr(ast, (char*)"method", method);
|
2021-10-13 20:18:12 +08:00
|
|
|
subStmts = strsCut(buffs, stmt, '(', ')');
|
|
|
|
while (1) {
|
|
|
|
char* subStmt = strsPopStmts(buffs, subStmts);
|
|
|
|
if (NULL == subStmt) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
queueObj_pushObj(ast, (char*)"stmt");
|
|
|
|
AST_parseStmt(queueObj_getCurrentObj(ast), subStmt);
|
2021-10-13 20:07:32 +08:00
|
|
|
}
|
2021-10-13 21:20:45 +08:00
|
|
|
} else {
|
|
|
|
if (directExist) {
|
|
|
|
ref = strsGetLastToken(buffs, assignment, '=');
|
|
|
|
} else {
|
|
|
|
ref = assignment;
|
|
|
|
}
|
|
|
|
obj_setStr(ast, (char*)"ref", ref);
|
2021-10-13 20:07:32 +08:00
|
|
|
}
|
2021-10-13 18:05:54 +08:00
|
|
|
goto exit;
|
|
|
|
exit:
|
|
|
|
args_deinit(buffs);
|
|
|
|
return ast;
|
|
|
|
}
|
|
|
|
|
2021-10-13 19:41:04 +08:00
|
|
|
AST* pikaParse(char* line) {
|
|
|
|
AST* ast = New_queueObj();
|
|
|
|
Args* buffs = New_strBuff();
|
2021-10-13 20:07:32 +08:00
|
|
|
char* stmt = strsGetCleanCmd(buffs, line);
|
2021-10-13 19:41:04 +08:00
|
|
|
ast = AST_parseStmt(ast, stmt);
|
|
|
|
goto exit;
|
|
|
|
exit:
|
|
|
|
args_deinit(buffs);
|
|
|
|
return ast;
|
|
|
|
}
|
|
|
|
|
2021-10-13 21:20:45 +08:00
|
|
|
char* AST_appandShell(AST* ast, Args* buffs, char* sh) {
|
|
|
|
while (1) {
|
|
|
|
QueueObj* subStmt = queueObj_popObj(ast);
|
|
|
|
if (NULL == subStmt) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sh = AST_appandShell(subStmt, buffs, sh);
|
|
|
|
}
|
|
|
|
char* method = obj_getStr(ast, "method");
|
|
|
|
char* ref = obj_getStr(ast, "ref");
|
|
|
|
char* direct = obj_getStr(ast, "direct");
|
|
|
|
if (NULL != ref) {
|
|
|
|
char buff[32] = {0};
|
|
|
|
sprintf(buff, "REF %s\n", ref);
|
|
|
|
sh = strsAppend(buffs, sh, buff);
|
|
|
|
}
|
|
|
|
if (NULL != method) {
|
|
|
|
char buff[32] = {0};
|
|
|
|
sprintf(buff, "RUN %s\n", method);
|
|
|
|
sh = strsAppend(buffs, sh, buff);
|
|
|
|
}
|
|
|
|
if (NULL != direct) {
|
|
|
|
char buff[32] = {0};
|
|
|
|
sprintf(buff, "OUT %s\n", direct);
|
|
|
|
sh = strsAppend(buffs, sh, buff);
|
|
|
|
}
|
|
|
|
return sh;
|
|
|
|
}
|
|
|
|
|
2021-10-13 18:05:54 +08:00
|
|
|
char* AST_toShell(AST* ast, Args* buffs) {
|
2021-10-13 21:20:45 +08:00
|
|
|
char* sh = strsCopy(buffs, "");
|
|
|
|
return AST_appandShell(ast, buffs, sh);
|
2021-10-13 18:05:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t AST_deinit(AST* ast) {
|
|
|
|
return obj_deinit(ast);
|
|
|
|
}
|