From 3f599dbc85b01da7552abf61bf6dfd6776bc3a8b Mon Sep 17 00:00:00 2001 From: FlavioJS Date: Mon, 25 Dec 2006 06:15:46 +0000 Subject: - Applied the rest of Rayce's suggestions and fixes (http://www.eathena.ws/board/index.php?showtopic=129185) - warn_cmd_no_comma, warn_func_no_comma, warn_cmd_mismatch_paramnum are now warn_func_mismatch_paramnum and it only prevents showing the error, as it was probably intended in the first place. (correct me if i'm wrong) - Merged the parsing of function calls in the script engine, removing the parse_cmd hackery, and made "heal (.@val+rand(0xff))&0xff,0;" valid again. - Fixed a bug in eye_of_hellion.txt and a bug in hunter.txt git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@9569 54d463be-8e91-2dee-dedb-b68131a5f0ec --- src/map/script.c | 406 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 214 insertions(+), 192 deletions(-) (limited to 'src/map/script.c') diff --git a/src/map/script.c b/src/map/script.c index b9aa5e3ad..23e485bef 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -3,6 +3,7 @@ //#define DEBUG_FUNCIN //#define DEBUG_DISP +//#define DEBUG_DISASM //#define DEBUG_RUN #include @@ -94,15 +95,27 @@ struct dbt* script_get_userfunc_db(){ return userfunc_db; } static char pos[11][100] = {"Head","Body","Left hand","Right hand","Robe","Shoes","Accessory 1","Accessory 2","Head 2","Head 3","Not Equipped"}; struct Script_Config script_config; -static int parse_cmd; static jmp_buf error_jump; static char* error_msg; static const char* error_pos; +static int error_report; // if the error should produce output // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc ) // [Eoe / jA 1080, 1081, 1094, 1164] -enum curly_type { TYPE_NULL = 0 , TYPE_IF , TYPE_SWITCH , TYPE_WHILE , TYPE_FOR , TYPE_DO , TYPE_USERFUNC}; +enum curly_type { + TYPE_NULL = 0, + TYPE_IF, + TYPE_SWITCH, + TYPE_WHILE, + TYPE_FOR, + TYPE_DO, + TYPE_USERFUNC, + TYPE_ARGLIST // function argument list +}; +#define ARGLIST_UNDEFINED 0 +#define ARGLIST_NO_PAREN 1 +#define ARGLIST_PAREN 2 static struct { struct { int type; @@ -156,7 +169,6 @@ int run_func(struct script_state *st); int mapreg_setreg(int num,int val); int mapreg_setregstr(int num,const char *str); -static void disp_error_message(const char *mes,const char *pos); enum c_op { C_NOP,C_POS,C_INT,C_PARAM,C_FUNC,C_STR,C_CONSTSTR,C_ARG, @@ -239,6 +251,19 @@ static void report_src(struct script_state *st) { } } +/*========================================== + * エラーメッセージ出力 + *------------------------------------------ + */ +static void disp_error_message2(const char *mes,const char *pos,int report) +{ + error_msg = aStrdup(mes); + error_pos = pos; + error_report = report; + longjmp( error_jump, 1 ); +} +#define disp_error_message(mes,pos) disp_error_message2(mes,pos,1) + static void check_event(struct script_state *st, const char *event){ if(event != NULL && event[0] != '\0' && !stristr(event,"::On")){ ShowError("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n",event); @@ -340,14 +365,11 @@ int add_str(const char* p) * スクリプトバッファサイズの確認と拡張 *------------------------------------------ */ -static void check_script_buf(int size) +static void expand_script_buf(void) { - if(script_pos+size>=script_size){ - script_size+=SCRIPT_BLOCK_SIZE; - RECREATE(script_buf,unsigned char,script_size); - malloc_tsetdword(script_buf + script_size - SCRIPT_BLOCK_SIZE, '\0', - SCRIPT_BLOCK_SIZE); - } + script_size+=SCRIPT_BLOCK_SIZE; + RECREATE(script_buf,unsigned char,script_size); + malloc_tsetdword(script_buf + script_size - SCRIPT_BLOCK_SIZE, '\0', SCRIPT_BLOCK_SIZE); } /*========================================== @@ -355,12 +377,12 @@ static void check_script_buf(int size) *------------------------------------------ */ -#define add_scriptb(a) if( script_pos+1>=script_size ) check_script_buf(1); script_buf[script_pos++]=(uint8)(a) +#define add_scriptb(a) if( script_pos+1>=script_size ) expand_script_buf(); script_buf[script_pos++]=(uint8)(a) #if 0 static void add_scriptb(int a) { - check_script_buf(1); + expand_script_buf(); script_buf[script_pos++]=a; } #endif @@ -533,15 +555,89 @@ int add_word(const char *p) return i; } -/*========================================== - * エラーメッセージ出力 - *------------------------------------------ - */ -static void disp_error_message(const char *mes,const char *pos) +/// Parses a function call. +/// The argument list can have parenthesis or not. +/// The number of arguments is checked. +static const char* parse_callfunc(const char *p, int require_paren) { - error_msg = aStrdup(mes); - error_pos = pos; - longjmp( error_jump, 1 ); + const char* p2; + const char* arg; + int func; + + func = add_word(p); + if( str_data[func].type == C_FUNC ){ + // buildin function + add_scriptl(func); + add_scriptc(C_ARG); + arg = buildin_func[str_data[func].val].arg; + } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){ + // script defined function + int callsub = search_str("callsub"); + add_scriptl(callsub); + add_scriptc(C_ARG); + add_scriptl(func); + arg = buildin_func[str_data[callsub].val].arg; + if( *arg == 0 ) + disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p); + if( *arg != '*' ) + ++arg; // count func as argument + } else + disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p); + + p = skip_word(p); + p = skip_space(p); + syntax.curly[syntax.curly_count].type = TYPE_ARGLIST; + syntax.curly[syntax.curly_count].count = 0; + if( *p == ';' ) + {// ';' + syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN; + } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' ) + {// '(' ')' + syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN; + p = p2; + /* + } else if( 0 && require_paren && *p != '(' ) + {// + syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN; + */ + } else + {// + if( require_paren ){ + if( *p != '(' ) + disp_error_message("need '('",p); + ++p; // skip '(' + syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN; + } else if( *p == '(' ){ + syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED; + } else { + syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN; + } + ++syntax.curly_count; + while( *arg ) { + p2=parse_subexpr(p,-1); + if( p == p2 ) + break; // not an argument + if( *arg != '*' ) + ++arg; // next argument + + p=skip_space(p2); + if( *arg == 0 || *p != ',' ) + break; // no more arguments + ++p; // skip comma + } + --syntax.curly_count; + } + if( *arg && *arg != '*' ) + disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum); + if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST ) + disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p); + if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){ + if( *p != ')' ) + disp_error_message("parse_callfunc: expected ')' to close argument list",p); + ++p; + } + add_scriptc(C_FUNC); + return p; } /*========================================== @@ -560,10 +656,22 @@ const char* parse_simpleexpr(const char *p) if(*p==';' || *p==',') disp_error_message("parse_simpleexpr: unexpected expr end",p); if(*p=='('){ + if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST ) + ++syntax.curly[i].count; p=parse_subexpr(p+1,-1); p=skip_space(p); - if((*p++)!=')') - disp_error_message("parse_simpleexpr: unmatch ')'",p-1); + if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST && + syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0 + ){ + if( *p == ',' ){ + syntax.curly[i].flag = ARGLIST_PAREN; + return p; + } else + syntax.curly[i].flag = ARGLIST_NO_PAREN; + } + if( *p != ')' ) + disp_error_message("parse_simpleexpr: unmatch ')'",p); + ++p; } else if(isdigit(*p) || ((*p=='-' || *p=='+') && isdigit(p[1]))){ char *np; i=strtoul(p,&np,0); @@ -590,10 +698,11 @@ const char* parse_simpleexpr(const char *p) disp_error_message("parse_simpleexpr: unexpected character",p); l=add_word(p); - parse_cmd=l; // warn_*_mismatch_paramnumのために必要 - p=skip_word(p); + if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS) + return parse_callfunc(p,1); - if(str_data[l].type!=C_FUNC && *p=='['){ + p=skip_word(p); + if( *p == '[' ){ // array(name[i] => getelementofarray(name,i) ) add_scriptl(search_str("getelementofarray")); add_scriptc(C_ARG); @@ -601,13 +710,10 @@ const char* parse_simpleexpr(const char *p) p=parse_subexpr(p+1,-1); p=skip_space(p); - if((*p++)!=']') - disp_error_message("parse_simpleexpr: unmatch ']'",p-1); + if( *p != ']' ) + disp_error_message("parse_simpleexpr: unmatch ']'",p); + ++p; add_scriptc(C_FUNC); - } else if(str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS) { - add_scriptl(search_str("callsub")); - add_scriptc(C_ARG); - add_scriptl(l); }else add_scriptl(l); @@ -657,7 +763,6 @@ const char* parse_subexpr(const char* p,int limit) (op=C_MUL,opl=9,len=1,*p=='*') || (op=C_DIV,opl=9,len=1,*p=='/') || (op=C_MOD,opl=9,len=1,*p=='%') || - (op=C_FUNC,opl=11,len=1,*p=='(') || (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') || (op=C_AND,opl=6,len=1,*p=='&') || (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') || @@ -672,56 +777,7 @@ const char* parse_subexpr(const char* p,int limit) (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') || (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){ p+=len; - if(op==C_FUNC){ - int i=0; - int j=0; - int func=parse_cmd; - const char *plist[128]; - const char *arg = NULL; - - if(str_data[parse_cmd].type == C_FUNC){ - // 通常の関数 - add_scriptc(C_ARG); - } else if(str_data[parse_cmd].type == C_USERFUNC || str_data[parse_cmd].type == C_USERFUNC_POS) { - // ユーザー定義関数呼び出し - parse_cmd = search_str("callsub"); - i++; - } else - disp_error_message("parse_subexpr: expect command, missing function name or calling undeclared function",tmpp); - func=parse_cmd; - p=skip_space(p); - - // check number of arguments of the function - if( str_data[func].type == C_FUNC && script_config.warn_cmd_mismatch_paramnum) { - arg = buildin_func[str_data[func].val].arg; - for(j=0; arg[j]; j++) { - if(arg[j] == '*') - break; - } - } - - while(*p && *p!=')' && i<128) { - plist[i]=p; - p=parse_subexpr(p,-1); - p=skip_space(p); - if(*p==',') { - if(arg == NULL || arg[j] == '*' || i+1 < j) - p++; // the next argument is valid, skip the comma - } - else if(*p!=')' && script_config.warn_func_no_comma){ - disp_error_message("parse_subexpr: expect ',' or ')' at func params",p); - } - p=skip_space(p); - i++; - } - plist[i]=p; - if(*(p++)!=')') - disp_error_message("parse_subexpr: func request '(' ')'",p-1); - if(arg) { - if( (arg[j]==0 && i!=j) || (arg[j]=='*' && iscript_buf = script_buf; @@ -3140,18 +3174,9 @@ int script_config_read_sub(char *cfgName) else if(strcmpi(w1,"verbose_mode")==0) { script_config.verbose_mode = battle_config_switch(w2); } - else if(strcmpi(w1,"warn_func_no_comma")==0) { - script_config.warn_func_no_comma = battle_config_switch(w2); - } - else if(strcmpi(w1,"warn_cmd_no_comma")==0) { - script_config.warn_cmd_no_comma = battle_config_switch(w2); - } else if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) { script_config.warn_func_mismatch_paramnum = battle_config_switch(w2); } - else if(strcmpi(w1,"warn_cmd_mismatch_paramnum")==0) { - script_config.warn_cmd_mismatch_paramnum = battle_config_switch(w2); - } else if(strcmpi(w1,"check_cmdcount")==0) { script_config.check_cmdcount = battle_config_switch(w2); } @@ -3218,10 +3243,7 @@ int script_config_read(char *cfgName) malloc_set (&script_config, 0, sizeof(script_config)); script_config.verbose_mode = 0; - script_config.warn_func_no_comma = 1; - script_config.warn_cmd_no_comma = 1; script_config.warn_func_mismatch_paramnum = 1; - script_config.warn_cmd_mismatch_paramnum = 1; script_config.check_cmdcount = 65535; script_config.check_gotocount = 2048; -- cgit v1.2.3-70-g09d2