Skip to content

Commit

Permalink
Change: Remove invalid macro arg at the top level
Browse files Browse the repository at this point in the history
Note: Some error cases may need reconsidered for diagnostics.  But in
general the new behavior is probably much less intrusive.
  • Loading branch information
p4plus2 committed Aug 28, 2023
1 parent e6655eb commit b1be24a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
37 changes: 21 additions & 16 deletions src/asar/macro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,16 @@ void callmacro(const char * data)
current_macro = thismacro;
current_macro_args = args;
current_macro_numargs = numargs;

callstack_push cs_push(callstack_entry_type::MACRO_CALL, data);

{
callstack_push cs_push(callstack_entry_type::FILE, thismacro->fname);

for (int i=0;i<thismacro->numlines;i++)
{
bool was_loop_end = do_line_logic(thismacro->lines[i], thismacro->fname, thismacro->startline+i+1);

if (was_loop_end && whilestatus[numif].cond)
// RPG Hacker: -1 to compensate for the i++, and another -1
// because ->lines doesn't include the macro header.
Expand Down Expand Up @@ -211,7 +211,7 @@ void callmacro(const char * data)
}

string generate_macro_arg_string(const char* named_arg, int depth)
{
{
string ret="<";
for (int i = 0; i < depth;++i)
{
Expand All @@ -223,7 +223,7 @@ string generate_macro_arg_string(const char* named_arg, int depth)
}

string generate_macro_arg_string(int var_arg, int depth)
{
{
string ret="<";
for (int i = 0; i < depth;++i)
{
Expand All @@ -249,10 +249,10 @@ string generate_macro_hint_string(const char* named_arg, const macrodata* thisma
ret += generate_macro_arg_string(thisone->arguments[j], 0);
ret += "'?";
return ret;
}
}
}
}

// RPG Hacker: Technically, we could skip a level here and go straight
// to the parent, but maybe at some point we'll want to expand this to
// also look for similar args in the current level, so I'll leave it
Expand All @@ -271,7 +271,7 @@ string generate_macro_hint_string(const char* named_arg, const macrodata* thisma
}
return generate_macro_hint_string(named_arg, thismacro->parent_macro, desired_depth, current_depth+1);
}

return "";
}

Expand All @@ -288,12 +288,17 @@ string generate_macro_hint_string(int var_arg, const macrodata* thismacro, int d
}
return generate_macro_hint_string(var_arg, thismacro->parent_macro, desired_depth, current_depth+1);
}

return "";
}

string replace_macro_args(const char* line) {
string out;
if(!inmacro)
{
out += line;
return out;
}
for (const char * in=line;*in;)
{
if (*in=='<' && in[1]=='<' && in[2] != ':')
Expand Down Expand Up @@ -329,13 +334,13 @@ string replace_macro_args(const char* line) {
out+=*(in++);
continue;
}

int depth = 0;
for (const char* depth_str = in+1; *depth_str=='^'; depth_str++)
{
depth++;
}

if (depth != in_macro_def)
{
string temp(in, end-in+1);
Expand All @@ -344,26 +349,26 @@ string replace_macro_args(const char* line) {
if (depth > in_macro_def)
{
if (in_macro_def > 0) asar_throw_error(0, error_type_line, error_id_invalid_depth_resolve, "macro parameter", "macro parameter", depth, in_macro_def-1);
else asar_throw_error(0, error_type_block, error_id_macro_param_outside_macro);
//else asar_throw_error(0, error_type_block, error_id_macro_param_outside_macro);
}
continue;
}

if (depth > 0 && !inmacro) asar_throw_error(0, error_type_line, error_id_invalid_depth_resolve, "macro parameter", "macro parameter", depth, in_macro_def-1);
in += depth+1;

bool is_variadic_arg = false;
if (in[0] == '.' && in[1] == '.' && in[2] == '.' && in[3] == '[')
{
if (end[-1] != ']')
asar_throw_error(0, error_type_block, error_id_unclosed_vararg);

is_variadic_arg = true;
in += 4;
end--;
}

if(!inmacro) asar_throw_error(0, error_type_block, error_id_macro_param_outside_macro);
//if(!inmacro) asar_throw_error(0, error_type_block, error_id_macro_param_outside_macro);
if(is_variadic_arg && !current_macro->variadic) asar_throw_error(0, error_type_block, error_id_macro_not_varadic, "<...[math]>");
//*end=0;
string param;
Expand Down
12 changes: 6 additions & 6 deletions tests/macro_fuckery_fail.asm
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
;`errEmacro_param_outside_macro
;`errEmacro_param_outside_macro
;`errEmacro_param_outside_macro
;`errEinvalid_depth_resolve
;`errEinvalid_depth_resolve
;`errEinvalid_depth_resolve
;`errEinvalid_depth_resolve
;`errEinvalid_depth_resolve
Expand All @@ -17,6 +12,11 @@
;`errEunclosed_macro
;`errEvararg_out_of_bounds
;`errEvararg_out_of_bounds
;`errEinvalid_number
;`errEinvalid_number
;`errEinvalid_number
;`errEinvalid_number
;`errEinvalid_number

org $008000

Expand Down Expand Up @@ -75,4 +75,4 @@ endmacro


macro first_unclosed()
macro second_unclosed()
macro second_unclosed()

0 comments on commit b1be24a

Please sign in to comment.