When parsing and compiling a script, return the length in an output parameter instead so the method can return a boolean to indicate success.

This fixes #221 where a script with no code would fail.
This commit is contained in:
smallmodel 2024-04-08 21:00:48 +02:00
parent 9eec50d69f
commit e3beec985e
No known key found for this signature in database
GPG key ID: 9F2D623CEDF08512
3 changed files with 41 additions and 32 deletions

View file

@ -1497,7 +1497,7 @@ int yyerror(const char *msg)
return 1;
}
size_t ScriptCompiler::Parse(GameScript *gameScript, char *sourceBuffer, const char *type)
bool ScriptCompiler::Parse(GameScript *gameScript, char *sourceBuffer, const char *type, size_t& outLength)
{
parsedata = yyparsedata();
@ -1515,6 +1515,8 @@ size_t ScriptCompiler::Parse(GameScript *gameScript, char *sourceBuffer, const c
script = gameScript;
stateScript = &gameScript->m_State;
outLength = 0;
yy_init_script();
parsetree_init();
@ -1531,22 +1533,23 @@ size_t ScriptCompiler::Parse(GameScript *gameScript, char *sourceBuffer, const c
}
yylex_destroy();
return 0;
return false;
}
} catch (ScriptException& exc) {
yylex_destroy();
exc;
return 0;
return false;
}
yylex_destroy();
return parsedata.total_length;
outLength = parsedata.total_length;
return true;
}
size_t ScriptCompiler::Compile(GameScript *gameScript, unsigned char *progBuffer)
bool ScriptCompiler::Compile(GameScript *gameScript, unsigned char *progBuffer, size_t& outLength)
{
size_t length;
bool success = false;
if (progBuffer == NULL) {
glbs.DPrintf("Invalid program buffer\n");
@ -1570,21 +1573,22 @@ size_t ScriptCompiler::Compile(GameScript *gameScript, unsigned char *progBuffer
if (compileSuccess) {
stateScript->AddLabel("", code_ptr);
length = code_pos - code_ptr;
outLength = code_pos - code_ptr;
success = true;
} else {
length = 0;
outLength = 0;
}
prog_end_ptr = code_pos;
} catch (ScriptException& exc) {
exc;
length = 0;
outLength = 0;
prog_end_ptr = code_pos;
}
parsetree_freeall();
return length;
return success;
}
str ScriptCompiler::GetLine(str content, int line)