|
@@ -53,21 +53,17 @@ void pasersCommandList(ParserMessage *pm, Inter *inter, bool global, Statement *
|
|
|
readBackToken(token_type, pm);
|
|
|
if (token_type == MATHER_EOF){
|
|
|
writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Command List: <EOF>\n", NULL);
|
|
|
- Token *tmp;
|
|
|
- popAheadToken(tmp, pm);
|
|
|
- freeToken(tmp, true, false);
|
|
|
+ delToken(pm);
|
|
|
goto return_;
|
|
|
}
|
|
|
else if (token_type == MATHER_ENTER){
|
|
|
writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Command List: <ENTER>\n", NULL);
|
|
|
- Token *tmp;
|
|
|
- popAheadToken(tmp, pm);
|
|
|
- freeToken(tmp, true, false);
|
|
|
+ delToken(pm);
|
|
|
continue;
|
|
|
}
|
|
|
else{
|
|
|
writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Command List: call command\n", NULL);
|
|
|
- Token *command_token,*stop_token;
|
|
|
+ Token *command_token;
|
|
|
parserCommand(CALLPASERSSIGNATURE);
|
|
|
if (!call_success(pm)){
|
|
|
goto return_;
|
|
@@ -82,13 +78,11 @@ void pasersCommandList(ParserMessage *pm, Inter *inter, bool global, Statement *
|
|
|
popAheadToken(command_token, pm);
|
|
|
|
|
|
readBackToken(stop, pm);
|
|
|
- if (stop == MATHER_ENTER){
|
|
|
- popAheadToken(stop_token, pm);
|
|
|
- freeToken(stop_token, true, false);
|
|
|
+ if (stop == MATHER_ENTER || stop == MATHER_SEMICOLON){
|
|
|
+ delToken(pm);
|
|
|
}
|
|
|
else if(stop == MATHER_EOF){
|
|
|
- popAheadToken(stop_token, pm);
|
|
|
- backToken_(pm, stop_token);
|
|
|
+ PASS;
|
|
|
}
|
|
|
else{
|
|
|
if (global) {
|
|
@@ -96,9 +90,7 @@ void pasersCommandList(ParserMessage *pm, Inter *inter, bool global, Statement *
|
|
|
freeToken(command_token, true, true);
|
|
|
}
|
|
|
else{
|
|
|
- // 若非global模式, 即可以匹配大括号
|
|
|
- popAheadToken(stop_token, pm);
|
|
|
- backToken_(pm, stop_token);
|
|
|
+ // 若非global模式, 即可以匹配大括号, token保留在ahead中
|
|
|
connectStatement(base_st, command_token->data.st);
|
|
|
freeToken(command_token, true, false);
|
|
|
writeLog_(pm->grammar_debug, GRAMMAR_DEBUG,
|
|
@@ -126,21 +118,29 @@ void parserCommand(PASERSSIGNATURE){
|
|
|
int token_type;
|
|
|
Statement *st = NULL;
|
|
|
readBackToken(token_type, pm);
|
|
|
- if (false){
|
|
|
- PASS;
|
|
|
+ if (MATHER_DEF == token_type){
|
|
|
+ int def;
|
|
|
+ Token *def_token = NULL;
|
|
|
+ parserDef(CALLPASERSSIGNATURE);
|
|
|
+ if (!call_success(pm))
|
|
|
+ goto return_;
|
|
|
+ readBackToken(def, pm);
|
|
|
+ if (def != FUNCTION)
|
|
|
+ goto return_;
|
|
|
+ popAheadToken(def_token, pm);
|
|
|
+ st = def_token->data.st;
|
|
|
+ freeToken(def_token, true, false);
|
|
|
}
|
|
|
else{
|
|
|
writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Command: call operation\n", NULL);
|
|
|
- int command_int;
|
|
|
- Token *command_token;
|
|
|
+ int command_type;
|
|
|
+ Token *command_token = NULL;
|
|
|
parserOperation(CALLPASERSSIGNATURE);
|
|
|
- if (!call_success(pm)){
|
|
|
+ if (!call_success(pm))
|
|
|
goto return_;
|
|
|
- }
|
|
|
- readBackToken(command_int, pm);
|
|
|
- if (command_int != OPERATION){
|
|
|
+ readBackToken(command_type, pm);
|
|
|
+ if (command_type != OPERATION)
|
|
|
goto return_;
|
|
|
- }
|
|
|
popAheadToken(command_token, pm);
|
|
|
/*...do something for command...*/
|
|
|
st = command_token->data.st;
|
|
@@ -152,16 +152,123 @@ void parserCommand(PASERSSIGNATURE){
|
|
|
writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Command: get return\n", NULL);
|
|
|
}
|
|
|
|
|
|
+void parserIf(PASERSSIGNATURE){
|
|
|
+// int if_type, code;
|
|
|
+// Token *if_token = NULL, *code_token = NULL;
|
|
|
+// struct Statement *st = NULL, *tmp = NULL;
|
|
|
+// StatementList *sl = NULL;
|
|
|
+//
|
|
|
+// checkToken(pm, MATHER_IF, return_);
|
|
|
+// callChild(pm, if_type, if_token, parserOperation, OPERATION, return_, error_);
|
|
|
+// if (if_type != OPERATION){
|
|
|
+// goto return_;
|
|
|
+// }
|
|
|
+// callChild(pm, code, code_token, parserCode, CODE, error_, error_);
|
|
|
+//
|
|
|
+// sl = makeConnectStatementList(sl, if_token->data.st, NULL, code_token->data.st);
|
|
|
+//
|
|
|
+// st = makeFunctionStatement(name_token->data.st, code_token->data.st);
|
|
|
+// addStatementToken(FUNCTION, st, pm);
|
|
|
+// freeToken(code_token, true, false);
|
|
|
+// freeToken(name_token, true, false);
|
|
|
+// return_: return;
|
|
|
+//
|
|
|
+// error_:
|
|
|
+// freeToken(if_token, true, true);
|
|
|
+// freeToken(code_token, true, true);
|
|
|
+// syntaxError(pm, "Don't get a if titile", syntax_error);
|
|
|
+// return;
|
|
|
+}
|
|
|
+
|
|
|
+void parserDef(PASERSSIGNATURE){
|
|
|
+ int name_type, code;
|
|
|
+ Token *name_token = NULL, *code_token = NULL;
|
|
|
+ struct Statement *st = NULL;
|
|
|
+
|
|
|
+ checkToken(pm, MATHER_DEF, return_);
|
|
|
+ parserBaseValue(CALLPASERSSIGNATURE);
|
|
|
+ if (!call_success(pm))
|
|
|
+ goto return_;
|
|
|
+ readBackToken(name_type, pm);
|
|
|
+ if (name_type != BASEVALUE){
|
|
|
+ syntaxError(pm, "Don't get a function name", syntax_error);
|
|
|
+ goto return_;
|
|
|
+ }
|
|
|
+ popAheadToken(name_token, pm);
|
|
|
+
|
|
|
+ checkToken(pm, MATHER_LP, error_);
|
|
|
+ checkToken(pm, MATHER_RP, error_);
|
|
|
+ parserCode(CALLPASERSSIGNATURE);
|
|
|
+ if (!call_success(pm)){
|
|
|
+ goto error_;
|
|
|
+ }
|
|
|
+ readBackToken(code, pm);
|
|
|
+ if (code != CODE){
|
|
|
+ freeToken(name_token, true, true);
|
|
|
+ error_:
|
|
|
+ syntaxError(pm, "Don't get a function code", syntax_error);
|
|
|
+ goto return_;
|
|
|
+ }
|
|
|
+ popAheadToken(code_token, pm);
|
|
|
+
|
|
|
+ st = makeFunctionStatement(name_token->data.st, code_token->data.st);
|
|
|
+ addStatementToken(FUNCTION, st, pm);
|
|
|
+ freeToken(code_token, true, false);
|
|
|
+ freeToken(name_token, true, false);
|
|
|
+
|
|
|
+ return_:
|
|
|
+ return;
|
|
|
+}
|
|
|
+
|
|
|
+void parserCode(PASERSSIGNATURE){
|
|
|
+ int code_type;
|
|
|
+ Token *code_token = NULL;
|
|
|
+ struct Statement *st = makeStatement();
|
|
|
+ while (true){
|
|
|
+ checkToken(pm, MATHER_LC, again_); // 若匹配的不是{则检查是否匹配到\n
|
|
|
+ break; // 若匹配到{则跳出循环
|
|
|
+ again_: checkToken(pm, MATHER_ENTER, return_); // 若不是\n则跳到return_
|
|
|
+ }
|
|
|
+ pasersCommandList(CALLPASERSSIGNATURE, false, st);
|
|
|
+ if (!call_success(pm)){
|
|
|
+ goto return_;
|
|
|
+ }
|
|
|
+ readBackToken(code_type, pm);
|
|
|
+ if (code_type != COMMANDLIST){
|
|
|
+ syntaxError(pm, "Not CommandList\n", syntax_error);
|
|
|
+ goto return_;
|
|
|
+ }
|
|
|
+ popAheadToken(code_token, pm);
|
|
|
+ checkToken(pm, MATHER_RC, error_);
|
|
|
+
|
|
|
+ return_:
|
|
|
+ addStatementToken(CODE, st, pm);
|
|
|
+ freeToken(code_token, true, false);
|
|
|
+ return;
|
|
|
+
|
|
|
+ error_:
|
|
|
+ syntaxError(pm, "Don't get the }", syntax_error);
|
|
|
+ freeToken(code_token, true, true);
|
|
|
+ return;
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* 表达式匹配
|
|
|
* parserOperation:
|
|
|
* | parserAssignment
|
|
|
*/
|
|
|
void parserOperation(PASERSSIGNATURE){
|
|
|
- int operation_int;
|
|
|
- Token *operation_token;
|
|
|
+ int operation_type;
|
|
|
+ Token *operation_token = NULL;
|
|
|
writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Operation: call assignment\n", NULL);
|
|
|
- callChild(pm, operation_int, operation_token, parserAssignment, ASSIGNMENT, return_);
|
|
|
+ parserAssignment(CALLPASERSSIGNATURE);
|
|
|
+ if (!call_success(pm))
|
|
|
+ goto return_;
|
|
|
+ readBackToken(operation_type, pm);
|
|
|
+ if (operation_type != ASSIGNMENT){
|
|
|
+ goto return_;
|
|
|
+ }
|
|
|
+ popAheadToken(operation_token, pm);
|
|
|
/*...do something for operation...*/
|
|
|
|
|
|
addStatementToken(OPERATION, operation_token->data.st, pm);
|
|
@@ -239,8 +346,53 @@ bool switchFactor(PASERSSIGNATURE, int symbol, Statement **st){
|
|
|
return true;
|
|
|
}
|
|
|
void parserFactor(PASERSSIGNATURE){
|
|
|
- return twoOperation(CALLPASERSSIGNATURE, parserBaseValue, switchFactor, BASEVALUE, FACTOR,
|
|
|
- "base value", "factor");
|
|
|
+ return twoOperation(CALLPASERSSIGNATURE, parserCallBack, switchFactor, CALLBACK, FACTOR,
|
|
|
+ "call back", "factor");
|
|
|
+}
|
|
|
+
|
|
|
+int tailCall(PASERSSIGNATURE, Token *left_token, Statement **st){
|
|
|
+ int symbol;
|
|
|
+ readBackToken(symbol, pm);
|
|
|
+ if (symbol != MATHER_LP){
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ delToken(pm);
|
|
|
+ checkToken(pm, MATHER_RP, error_);
|
|
|
+ *st = makeCallStatement(left_token->data.st);
|
|
|
+ return 1;
|
|
|
+
|
|
|
+ error_:
|
|
|
+ syntaxError(pm, "Don't get ) from call back", syntax_error);
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+void parserCallBack(PASERSSIGNATURE){
|
|
|
+ return tailOperation(CALLPASERSSIGNATURE, parserBaseValue, tailCall, BASEVALUE, CALLBACK,
|
|
|
+ "Base Value", "Call Back");
|
|
|
+}
|
|
|
+
|
|
|
+int getOperation(PASERSSIGNATURE, int right_type, Statement **st, char *name){
|
|
|
+ int operation_type, rp;
|
|
|
+ Token *operation_token;
|
|
|
+
|
|
|
+ parserOperation(CALLPASERSSIGNATURE);
|
|
|
+ if (!call_success(pm)){
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ readBackToken(operation_type, pm);
|
|
|
+ if (operation_type != OPERATION){
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ popAheadToken(operation_token, pm);
|
|
|
+
|
|
|
+ readBackToken(rp, pm);
|
|
|
+ if (right_type != rp){
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ delToken(pm);
|
|
|
+
|
|
|
+ *st = operation_token->data.st;
|
|
|
+ freeToken(operation_token, true, false);
|
|
|
+ return 1;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -251,7 +403,7 @@ void parserFactor(PASERSSIGNATURE){
|
|
|
*/
|
|
|
void parserBaseValue(PASERSSIGNATURE){
|
|
|
int token_type;
|
|
|
- Token *value_token;
|
|
|
+ Token *value_token = NULL;
|
|
|
struct Statement *st = NULL;
|
|
|
readBackToken(token_type, pm);
|
|
|
if(MATHER_NUMBER == token_type){
|
|
@@ -277,6 +429,50 @@ void parserBaseValue(PASERSSIGNATURE){
|
|
|
st->u.base_var.name = memStrcpy(value_token->data.str, 0, false, false);
|
|
|
st->u.base_var.times = NULL;
|
|
|
}
|
|
|
+ else if(MATHER_LB == token_type){
|
|
|
+ int var, tmp;
|
|
|
+ Statement *tmp_st = NULL;
|
|
|
+ writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "base value: get operation\n", NULL);
|
|
|
+ popAheadToken(value_token, pm);
|
|
|
+
|
|
|
+ tmp = getOperation(CALLPASERSSIGNATURE, MATHER_RB, &tmp_st, "base value");
|
|
|
+ if (tmp == 0){
|
|
|
+ freeToken(value_token, true, true);
|
|
|
+ syntaxError(pm, "Don't get operation from list/var", syntax_error);
|
|
|
+ goto return_;
|
|
|
+ }
|
|
|
+ else if(tmp == -1){
|
|
|
+ freeToken(value_token, true, true);
|
|
|
+ syntaxError(pm, "Don't get ] from list/var", syntax_error);
|
|
|
+ goto return_;
|
|
|
+ }
|
|
|
+ readBackToken(var, pm);
|
|
|
+ if (MATHER_VAR == var){
|
|
|
+ Token *var_token;
|
|
|
+ popAheadToken(var_token, pm);
|
|
|
+ st = makeStatement();
|
|
|
+ st->type = base_var;
|
|
|
+ st->u.base_var.name = memStrcpy(var_token->data.str, 0, false, false);
|
|
|
+ st->u.base_var.times = tmp_st;
|
|
|
+ freeToken(var_token, true, false);
|
|
|
+ }
|
|
|
+ // TODO-szh list处理
|
|
|
+ }
|
|
|
+ else if(MATHER_LP == token_type){
|
|
|
+ writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "base value: get operation\n", NULL);
|
|
|
+ popAheadToken(value_token, pm);
|
|
|
+ int tmp = getOperation(CALLPASERSSIGNATURE, MATHER_RP, &st, "base value");
|
|
|
+ if (tmp == 0){
|
|
|
+ freeToken(value_token, true, true);
|
|
|
+ syntaxError(pm, "Don't get operation from Base Value", syntax_error);
|
|
|
+ goto return_;
|
|
|
+ }
|
|
|
+ else if(tmp == -1){
|
|
|
+ freeToken(value_token, true, true);
|
|
|
+ syntaxError(pm, "Don't get ) from Base Value", syntax_error);
|
|
|
+ goto return_;
|
|
|
+ }
|
|
|
+ }
|
|
|
else{
|
|
|
writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Base Value: else\n", NULL);
|
|
|
goto return_;
|
|
@@ -291,22 +487,29 @@ void parserBaseValue(PASERSSIGNATURE){
|
|
|
inline void twoOperation(PASERSSIGNATURE, void (*callBack)(PASERSSIGNATURE), int (*getSymbol)(PASERSSIGNATURE, int symbol, Statement **st), int type, int self_type, char *call_name, char *self_name){
|
|
|
while(true){
|
|
|
int left, symbol, right;
|
|
|
- Token *left_token, *symbol_token, *right_token;
|
|
|
+ Token *left_token = NULL, *symbol_token = NULL, *right_token = NULL;
|
|
|
struct Statement *st = NULL;
|
|
|
|
|
|
readBackToken(left, pm);
|
|
|
if (left != self_type){
|
|
|
writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: call %s(left)\n", self_name, call_name);
|
|
|
- callChild(pm, left, left_token, callBack, type, return_);
|
|
|
+ callBack(CALLPASERSSIGNATURE);
|
|
|
+ if (!call_success(pm))
|
|
|
+ goto return_;
|
|
|
+ readBackToken(left, pm);
|
|
|
+ if (left != type){
|
|
|
+ goto return_;
|
|
|
+ }
|
|
|
+ popAheadToken(left_token, pm);
|
|
|
addStatementToken(self_type, left_token->data.st, pm);
|
|
|
freeToken(left_token, true, false);
|
|
|
writeLog_(pm->grammar_debug, GRAMMAR_DEBUG,
|
|
|
"%s: get %s(left) success[push %s]\n", self_name, call_name, self_name);
|
|
|
continue;
|
|
|
}
|
|
|
- writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: call symbol\n", self_name);
|
|
|
popAheadToken(left_token, pm);
|
|
|
|
|
|
+ writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: call symbol\n", self_name);
|
|
|
readBackToken(symbol, pm);
|
|
|
if (getSymbol(CALLPASERSSIGNATURE, symbol, &st)){
|
|
|
delToken(pm);
|
|
@@ -345,6 +548,52 @@ inline void twoOperation(PASERSSIGNATURE, void (*callBack)(PASERSSIGNATURE), int
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
+inline void tailOperation(PASERSSIGNATURE, void (*callBack)(PASERSSIGNATURE), int (*tailFunction)(PASERSSIGNATURE, Token *left_token, Statement **st), int type, int self_type, char *call_name, char *self_name){
|
|
|
+ while(true){
|
|
|
+ int left, symbol;
|
|
|
+ Token *left_token = NULL, *symbol_token = NULL, *right_token = NULL;
|
|
|
+ struct Statement *st = NULL;
|
|
|
+
|
|
|
+ readBackToken(left, pm);
|
|
|
+ if (left != self_type){
|
|
|
+ writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: call %s(left)\n", self_name, call_name);
|
|
|
+ callBack(CALLPASERSSIGNATURE);
|
|
|
+ if (!call_success(pm))
|
|
|
+ goto return_;
|
|
|
+ readBackToken(left, pm);
|
|
|
+ if (left != type){
|
|
|
+ goto return_;
|
|
|
+ }
|
|
|
+ popAheadToken(left_token, pm);
|
|
|
+ addStatementToken(self_type, left_token->data.st, pm);
|
|
|
+ freeToken(left_token, true, false);
|
|
|
+ writeLog_(pm->grammar_debug, GRAMMAR_DEBUG,
|
|
|
+ "%s: get %s(left) success[push %s]\n", self_name, call_name, self_name);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ popAheadToken(left_token, pm);
|
|
|
+
|
|
|
+ int tail_status = tailFunction(CALLPASERSSIGNATURE, left_token, &st);
|
|
|
+ if (tail_status == -1){
|
|
|
+ backToken_(pm, left_token);
|
|
|
+ goto return_;
|
|
|
+ }
|
|
|
+ else if(!tail_status){
|
|
|
+ goto error_;
|
|
|
+ }
|
|
|
+ addStatementToken(self_type, st, pm);
|
|
|
+ freeToken(left_token, true, false);
|
|
|
+ writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: call tail success\n", self_name);
|
|
|
+ continue;
|
|
|
+
|
|
|
+ error_:
|
|
|
+ freeToken(left_token, true, true);
|
|
|
+ goto return_;
|
|
|
+ }
|
|
|
+ return_:
|
|
|
+ return;
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* syntax错误处理器
|
|
|
* @param pm
|