mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Simplify error throwing/catching in squirrel compiler.
parent
f2db624e85
commit
8a4f0c4b02
|
@ -57,19 +57,15 @@ typedef sqvector<ExpState> ExpStateVec;
|
||||||
class SQCompiler
|
class SQCompiler
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SQCompiler(SQVM *v, SQLEXREADFUNC rg, SQUserPointer up, const SQChar* sourcename, bool raiseerror, bool lineinfo) : _token(0), _fs(nullptr), _lex(_ss(v), rg, up, ThrowError, this), _debugline(0), _debugop(0)
|
SQCompiler(SQVM *v, SQLEXREADFUNC rg, SQUserPointer up, const SQChar* sourcename, bool raiseerror, bool lineinfo) : _token(0), _fs(nullptr), _lex(_ss(v), rg, up), _debugline(0), _debugop(0)
|
||||||
{
|
{
|
||||||
_vm=v;
|
_vm=v;
|
||||||
_sourcename = SQString::Create(_ss(v), sourcename);
|
_sourcename = SQString::Create(_ss(v), sourcename);
|
||||||
_lineinfo = lineinfo;_raiseerror = raiseerror;
|
_lineinfo = lineinfo;_raiseerror = raiseerror;
|
||||||
}
|
}
|
||||||
NORETURN static void ThrowError(void *ud, const SQChar *s) {
|
[[noreturn]] void Error(const std::string &msg)
|
||||||
SQCompiler *c = (SQCompiler *)ud;
|
|
||||||
c->Error(s);
|
|
||||||
}
|
|
||||||
NORETURN void Error(const std::string &msg)
|
|
||||||
{
|
{
|
||||||
throw msg;
|
throw CompileException(msg);
|
||||||
}
|
}
|
||||||
void Lex(){ _token = _lex.Lex();}
|
void Lex(){ _token = _lex.Lex();}
|
||||||
void PushExpState(){ _expstates.push_back(ExpState()); }
|
void PushExpState(){ _expstates.push_back(ExpState()); }
|
||||||
|
@ -159,7 +155,7 @@ public:
|
||||||
_debugline = 1;
|
_debugline = 1;
|
||||||
_debugop = 0;
|
_debugop = 0;
|
||||||
|
|
||||||
SQFuncState funcstate(_ss(_vm), nullptr,ThrowError,this);
|
SQFuncState funcstate(_ss(_vm), nullptr);
|
||||||
funcstate._name = SQString::Create(_ss(_vm), "main");
|
funcstate._name = SQString::Create(_ss(_vm), "main");
|
||||||
_fs = &funcstate;
|
_fs = &funcstate;
|
||||||
_fs->AddParameter(_fs->CreateString("this"));
|
_fs->AddParameter(_fs->CreateString("this"));
|
||||||
|
@ -181,12 +177,12 @@ public:
|
||||||
#endif
|
#endif
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (const std::string &compilererror) {
|
catch (const CompileException &compilererror) {
|
||||||
if(_raiseerror && _ss(_vm)->_compilererrorhandler) {
|
if(_raiseerror && _ss(_vm)->_compilererrorhandler) {
|
||||||
_ss(_vm)->_compilererrorhandler(_vm, compilererror.c_str(), type(_sourcename) == OT_STRING ? _stringval(_sourcename) : "unknown",
|
_ss(_vm)->_compilererrorhandler(_vm, compilererror.what(), type(_sourcename) == OT_STRING ? _stringval(_sourcename) : "unknown",
|
||||||
_lex._currentline, _lex._currentcolumn);
|
_lex._currentline, _lex._currentcolumn);
|
||||||
}
|
}
|
||||||
_vm->_lasterror = SQString::Create(_ss(_vm), compilererror);
|
_vm->_lasterror = SQString::Create(_ss(_vm), compilererror.what());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,12 +71,7 @@ struct SQVM;
|
||||||
#define TK_ENUM 323
|
#define TK_ENUM 323
|
||||||
#define TK_CONST 324
|
#define TK_CONST 324
|
||||||
|
|
||||||
/* MSVC doesn't like NORETURN for function prototypes, but we kinda need it for GCC. */
|
using CompileException = std::runtime_error;
|
||||||
#if defined(_MSC_VER) && !defined(__clang__)
|
|
||||||
typedef void(*CompilerErrorFunc)(void *ud, const SQChar *s);
|
|
||||||
#else
|
|
||||||
typedef NORETURN void(*CompilerErrorFunc)(void *ud, const SQChar *s);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
bool Compile(SQVM *vm, SQLEXREADFUNC rg, SQUserPointer up, const SQChar *sourcename, SQObjectPtr &out, bool raiseerror, bool lineinfo);
|
bool Compile(SQVM *vm, SQLEXREADFUNC rg, SQUserPointer up, const SQChar *sourcename, SQObjectPtr &out, bool raiseerror, bool lineinfo);
|
||||||
#endif //_SQCOMPILER_H_
|
#endif //_SQCOMPILER_H_
|
||||||
|
|
|
@ -93,7 +93,7 @@ void DumpLiteral(SQObjectPtr &o)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
SQFuncState::SQFuncState(SQSharedState *ss,SQFuncState *parent,CompilerErrorFunc efunc,void *ed)
|
SQFuncState::SQFuncState(SQSharedState *ss,SQFuncState *parent)
|
||||||
{
|
{
|
||||||
_nliterals = 0;
|
_nliterals = 0;
|
||||||
_literals = SQTable::Create(ss,0);
|
_literals = SQTable::Create(ss,0);
|
||||||
|
@ -106,15 +106,13 @@ SQFuncState::SQFuncState(SQSharedState *ss,SQFuncState *parent,CompilerErrorFunc
|
||||||
_traps = 0;
|
_traps = 0;
|
||||||
_returnexp = 0;
|
_returnexp = 0;
|
||||||
_varparams = false;
|
_varparams = false;
|
||||||
_errfunc = efunc;
|
|
||||||
_errtarget = ed;
|
|
||||||
_bgenerator = false;
|
_bgenerator = false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SQFuncState::Error(const SQChar *err)
|
void SQFuncState::Error(const SQChar *err)
|
||||||
{
|
{
|
||||||
_errfunc(_errtarget,err);
|
throw CompileException(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _DEBUG_DUMP
|
#ifdef _DEBUG_DUMP
|
||||||
|
@ -550,7 +548,7 @@ SQFunctionProto *SQFuncState::BuildProto()
|
||||||
SQFuncState *SQFuncState::PushChildState(SQSharedState *ss)
|
SQFuncState *SQFuncState::PushChildState(SQSharedState *ss)
|
||||||
{
|
{
|
||||||
SQFuncState *child = (SQFuncState *)sq_malloc(sizeof(SQFuncState));
|
SQFuncState *child = (SQFuncState *)sq_malloc(sizeof(SQFuncState));
|
||||||
new (child) SQFuncState(ss,this,_errfunc,_errtarget);
|
new (child) SQFuncState(ss,this);
|
||||||
_childstates.push_back(child);
|
_childstates.push_back(child);
|
||||||
return child;
|
return child;
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,12 +6,12 @@
|
||||||
|
|
||||||
struct SQFuncState
|
struct SQFuncState
|
||||||
{
|
{
|
||||||
SQFuncState(SQSharedState *ss,SQFuncState *parent,CompilerErrorFunc efunc,void *ed);
|
SQFuncState(SQSharedState *ss,SQFuncState *parent);
|
||||||
~SQFuncState();
|
~SQFuncState();
|
||||||
#ifdef _DEBUG_DUMP
|
#ifdef _DEBUG_DUMP
|
||||||
void Dump(SQFunctionProto *func);
|
void Dump(SQFunctionProto *func);
|
||||||
#endif
|
#endif
|
||||||
void Error(const SQChar *err);
|
[[noreturn]] void Error(const SQChar *err);
|
||||||
SQFuncState *PushChildState(SQSharedState *ss);
|
SQFuncState *PushChildState(SQSharedState *ss);
|
||||||
void PopChildState();
|
void PopChildState();
|
||||||
void AddInstruction(SQOpcode _op,SQInteger arg0=0,SQInteger arg1=0,SQInteger arg2=0,SQInteger arg3=0){SQInstruction i(_op,arg0,arg1,arg2,arg3);AddInstruction(i);}
|
void AddInstruction(SQOpcode _op,SQInteger arg0=0,SQInteger arg1=0,SQInteger arg2=0,SQInteger arg3=0){SQInstruction i(_op,arg0,arg1,arg2,arg3);AddInstruction(i);}
|
||||||
|
@ -75,9 +75,6 @@ struct SQFuncState
|
||||||
SQSharedState *_sharedstate;
|
SQSharedState *_sharedstate;
|
||||||
sqvector<SQFuncState*> _childstates;
|
sqvector<SQFuncState*> _childstates;
|
||||||
SQInteger GetConstant(const SQObject &cons);
|
SQInteger GetConstant(const SQObject &cons);
|
||||||
private:
|
|
||||||
CompilerErrorFunc _errfunc;
|
|
||||||
void *_errtarget;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -35,10 +35,8 @@ void SQLexer::APPEND_CHAR(char32_t c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SQLexer::SQLexer(SQSharedState *ss, SQLEXREADFUNC rg, SQUserPointer up,CompilerErrorFunc efunc,void *ed)
|
SQLexer::SQLexer(SQSharedState *ss, SQLEXREADFUNC rg, SQUserPointer up)
|
||||||
{
|
{
|
||||||
_errfunc = efunc;
|
|
||||||
_errtarget = ed;
|
|
||||||
_sharedstate = ss;
|
_sharedstate = ss;
|
||||||
_keywords = SQTable::Create(ss, 26);
|
_keywords = SQTable::Create(ss, 26);
|
||||||
ADD_KEYWORD(while, TK_WHILE);
|
ADD_KEYWORD(while, TK_WHILE);
|
||||||
|
@ -96,7 +94,7 @@ SQLexer::SQLexer(SQSharedState *ss, SQLEXREADFUNC rg, SQUserPointer up,CompilerE
|
||||||
|
|
||||||
NORETURN void SQLexer::Error(const SQChar *err)
|
NORETURN void SQLexer::Error(const SQChar *err)
|
||||||
{
|
{
|
||||||
_errfunc(_errtarget,err);
|
throw CompileException(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SQLexer::Next()
|
void SQLexer::Next()
|
||||||
|
|
|
@ -5,8 +5,8 @@
|
||||||
struct SQLexer
|
struct SQLexer
|
||||||
{
|
{
|
||||||
~SQLexer();
|
~SQLexer();
|
||||||
SQLexer(SQSharedState *ss,SQLEXREADFUNC rg,SQUserPointer up,CompilerErrorFunc efunc,void *ed);
|
SQLexer(SQSharedState *ss,SQLEXREADFUNC rg,SQUserPointer up);
|
||||||
NORETURN void Error(const SQChar *err);
|
[[noreturn]] void Error(const SQChar *err);
|
||||||
SQInteger Lex();
|
SQInteger Lex();
|
||||||
const SQChar *Tok2Str(SQInteger tok);
|
const SQChar *Tok2Str(SQInteger tok);
|
||||||
private:
|
private:
|
||||||
|
@ -35,8 +35,6 @@ public:
|
||||||
char32_t _currdata;
|
char32_t _currdata;
|
||||||
SQSharedState *_sharedstate;
|
SQSharedState *_sharedstate;
|
||||||
sqvector<SQChar> _longstr;
|
sqvector<SQChar> _longstr;
|
||||||
CompilerErrorFunc _errfunc;
|
|
||||||
void *_errtarget;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue