1
0
Fork 0

Codechange: replace Error with simple throw

pull/14202/head
Rubidium 2025-05-03 21:25:14 +02:00 committed by rubidium42
parent 9d8a612bfb
commit f4fc8f6b38
5 changed files with 45 additions and 61 deletions

View File

@ -63,10 +63,6 @@ public:
_sourcename = SQString::Create(_ss(v), sourcename); _sourcename = SQString::Create(_ss(v), sourcename);
_lineinfo = lineinfo;_raiseerror = raiseerror; _lineinfo = lineinfo;_raiseerror = raiseerror;
} }
[[noreturn]] void Error(const std::string &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()); }
bool IsDerefToken(SQInteger tok) bool IsDerefToken(SQInteger tok)
@ -111,9 +107,9 @@ public:
default: default:
etypename = _lex.Tok2Str(tok); etypename = _lex.Tok2Str(tok);
} }
Error(fmt::format("expected '{}'", etypename)); throw CompileException(fmt::format("expected '{}'", etypename));
} }
Error(fmt::format("expected '{:c}'", tok)); throw CompileException(fmt::format("expected '{:c}'", tok));
} }
} }
SQObjectPtr ret; SQObjectPtr ret;
@ -140,7 +136,7 @@ public:
{ {
if(_token == ';') { Lex(); return; } if(_token == ';') { Lex(); return; }
if(!IsEndOfStatement()) { if(!IsEndOfStatement()) {
Error("end of statement expected (; or lf)"); throw CompileException("end of statement expected (; or lf)");
} }
} }
void MoveIfCurrentTargetIsLocal() { void MoveIfCurrentTargetIsLocal() {
@ -233,7 +229,7 @@ public:
} }
break;} break;}
case TK_BREAK: case TK_BREAK:
if(_fs->_breaktargets.size() <= 0)Error("'break' has to be in a loop block"); if(_fs->_breaktargets.size() <= 0)throw CompileException("'break' has to be in a loop block");
if(_fs->_breaktargets.top() > 0){ if(_fs->_breaktargets.top() > 0){
_fs->AddInstruction(_OP_POPTRAP, _fs->_breaktargets.top(), 0); _fs->AddInstruction(_OP_POPTRAP, _fs->_breaktargets.top(), 0);
} }
@ -243,7 +239,7 @@ public:
Lex(); Lex();
break; break;
case TK_CONTINUE: case TK_CONTINUE:
if(_fs->_continuetargets.size() <= 0)Error("'continue' has to be in a loop block"); if(_fs->_continuetargets.size() <= 0)throw CompileException("'continue' has to be in a loop block");
if(_fs->_continuetargets.top() > 0) { if(_fs->_continuetargets.top() > 0) {
_fs->AddInstruction(_OP_POPTRAP, _fs->_continuetargets.top(), 0); _fs->AddInstruction(_OP_POPTRAP, _fs->_continuetargets.top(), 0);
} }
@ -356,19 +352,19 @@ public:
SQInteger op = _token; SQInteger op = _token;
SQInteger ds = _exst._deref; SQInteger ds = _exst._deref;
bool freevar = _exst._freevar; bool freevar = _exst._freevar;
if(ds == DEREF_NO_DEREF) Error("can't assign expression"); if(ds == DEREF_NO_DEREF) throw CompileException("can't assign expression");
Lex(); Expression(); Lex(); Expression();
switch(op){ switch(op){
case TK_NEWSLOT: case TK_NEWSLOT:
if(freevar) Error("free variables cannot be modified"); if(freevar) throw CompileException("free variables cannot be modified");
if(ds == DEREF_FIELD) if(ds == DEREF_FIELD)
EmitDerefOp(_OP_NEWSLOT); EmitDerefOp(_OP_NEWSLOT);
else //if _derefstate != DEREF_NO_DEREF && DEREF_FIELD so is the index of a local else //if _derefstate != DEREF_NO_DEREF && DEREF_FIELD so is the index of a local
Error("can't 'create' a local slot"); throw CompileException("can't 'create' a local slot");
break; break;
case '=': //ASSIGN case '=': //ASSIGN
if(freevar) Error("free variables cannot be modified"); if(freevar) throw CompileException("free variables cannot be modified");
if(ds == DEREF_FIELD) if(ds == DEREF_FIELD)
EmitDerefOp(_OP_SET); EmitDerefOp(_OP_SET);
else {//if _derefstate != DEREF_NO_DEREF && DEREF_FIELD so is the index of a local else {//if _derefstate != DEREF_NO_DEREF && DEREF_FIELD so is the index of a local
@ -533,7 +529,7 @@ public:
if(_token == TK_PARENT) { if(_token == TK_PARENT) {
Lex(); Lex();
if(!NeedGet()) if(!NeedGet())
Error("parent cannot be set"); throw CompileException("parent cannot be set");
SQInteger src = _fs->PopTarget(); SQInteger src = _fs->PopTarget();
_fs->AddInstruction(_OP_GETPARENT, _fs->PushTarget(), src); _fs->AddInstruction(_OP_GETPARENT, _fs->PushTarget(), src);
} }
@ -546,7 +542,7 @@ public:
} }
break; break;
case '[': case '[':
if(_lex._prevtoken == '\n') Error("cannot brake deref/or comma needed after [exp]=exp slot declaration"); if(_lex._prevtoken == '\n') throw CompileException("cannot brake deref/or comma needed after [exp]=exp slot declaration");
Lex(); Expression(); Expect(']'); Lex(); Expression(); Expect(']');
pos = -1; pos = -1;
if(NeedGet()) Emit2ArgsOP(_OP_GET); if(NeedGet()) Emit2ArgsOP(_OP_GET);
@ -638,7 +634,7 @@ public:
Expect('.'); constid = Expect(TK_IDENTIFIER); Expect('.'); constid = Expect(TK_IDENTIFIER);
if(!_table(constant)->Get(constid,constval)) { if(!_table(constant)->Get(constid,constval)) {
constval.Null(); constval.Null();
Error(fmt::format("invalid constant [{}.{}]", _stringval(id),_stringval(constid))); throw CompileException(fmt::format("invalid constant [{}.{}]", _stringval(id),_stringval(constid)));
} }
} }
else { else {
@ -742,7 +738,7 @@ public:
case TK_DELEGATE : DelegateExpr(); break; case TK_DELEGATE : DelegateExpr(); break;
case '(': Lex(); CommaExpr(); Expect(')'); case '(': Lex(); CommaExpr(); Expect(')');
break; break;
default: Error("expression expected"); default: throw CompileException("expression expected");
} }
return -1; return -1;
} }
@ -771,7 +767,7 @@ public:
nargs++; nargs++;
if(_token == ','){ if(_token == ','){
Lex(); Lex();
if(_token == ')') Error("expression expected, found ')'"); if(_token == ')') throw CompileException("expression expected, found ')'");
} }
} }
Lex(); Lex();
@ -1082,13 +1078,13 @@ public:
_exst._funcarg = false; _exst._funcarg = false;
PrefixedExpr(); PrefixedExpr();
es = PopExpState(); es = PopExpState();
if(es._deref == DEREF_NO_DEREF) Error("invalid class name"); if(es._deref == DEREF_NO_DEREF) throw CompileException("invalid class name");
if(es._deref == DEREF_FIELD) { if(es._deref == DEREF_FIELD) {
ClassExp(); ClassExp();
EmitDerefOp(_OP_NEWSLOT); EmitDerefOp(_OP_NEWSLOT);
_fs->PopTarget(); _fs->PopTarget();
} }
else Error("cannot create a class in a local with the syntax(class <local>)"); else throw CompileException("cannot create a class in a local with the syntax(class <local>)");
} }
SQObject ExpectScalar() SQObject ExpectScalar()
{ {
@ -1118,12 +1114,12 @@ public:
val._unVal.fFloat = -_lex._fvalue; val._unVal.fFloat = -_lex._fvalue;
break; break;
default: default:
Error("scalar expected : integer,float"); throw CompileException("scalar expected : integer,float");
val._type = OT_NULL; // Silent compile-warning val._type = OT_NULL; // Silent compile-warning
} }
break; break;
default: default:
Error("scalar expected : integer,float or string"); throw CompileException("scalar expected : integer,float or string");
val._type = OT_NULL; // Silent compile-warning val._type = OT_NULL; // Silent compile-warning
} }
Lex(); Lex();
@ -1226,9 +1222,9 @@ public:
_exst._funcarg = false; _exst._funcarg = false;
PrefixedExpr(); PrefixedExpr();
es = PopExpState(); es = PopExpState();
if(es._deref == DEREF_NO_DEREF) Error("can't delete an expression"); if(es._deref == DEREF_NO_DEREF) throw CompileException("can't delete an expression");
if(es._deref == DEREF_FIELD) Emit2ArgsOP(_OP_DELETE); if(es._deref == DEREF_FIELD) Emit2ArgsOP(_OP_DELETE);
else Error("cannot delete a local"); else throw CompileException("cannot delete a local");
} }
void PrefixIncDec(SQInteger token) void PrefixIncDec(SQInteger token)
{ {
@ -1255,10 +1251,10 @@ public:
SQInteger defparams = 0; SQInteger defparams = 0;
while(_token!=')') { while(_token!=')') {
if(_token == TK_VARPARAMS) { if(_token == TK_VARPARAMS) {
if(defparams > 0) Error("function with default parameters cannot have variable number of parameters"); if(defparams > 0) throw CompileException("function with default parameters cannot have variable number of parameters");
funcstate->_varparams = true; funcstate->_varparams = true;
Lex(); Lex();
if(_token != ')') Error("expected ')'"); if(_token != ')') throw CompileException("expected ')'");
break; break;
} }
else { else {
@ -1271,10 +1267,10 @@ public:
defparams++; defparams++;
} }
else { else {
if(defparams > 0) Error("expected '='"); if(defparams > 0) throw CompileException("expected '='");
} }
if(_token == ',') Lex(); if(_token == ',') Lex();
else if(_token != ')') Error("expected ')' or ','"); else if(_token != ')') throw CompileException("expected ')' or ','");
} }
} }
Expect(')'); Expect(')');
@ -1289,7 +1285,7 @@ public:
//outers are treated as implicit local variables //outers are treated as implicit local variables
funcstate->AddOuterValue(paramname); funcstate->AddOuterValue(paramname);
if(_token == ',') Lex(); if(_token == ',') Lex();
else if(_token != ')') Error("expected ')' or ','"); else if(_token != ')') throw CompileException("expected ')' or ','");
} }
Lex(); Lex();
} }

View File

@ -110,11 +110,6 @@ SQFuncState::SQFuncState(SQSharedState *ss,SQFuncState *parent)
} }
void SQFuncState::Error(const SQChar *err)
{
throw CompileException(err);
}
#ifdef _DEBUG_DUMP #ifdef _DEBUG_DUMP
void SQFuncState::Dump(SQFunctionProto *func) void SQFuncState::Dump(SQFunctionProto *func)
{ {
@ -234,7 +229,7 @@ SQInteger SQFuncState::GetConstant(const SQObject &cons)
_nliterals++; _nliterals++;
if(_nliterals > MAX_LITERALS) { if(_nliterals > MAX_LITERALS) {
val.Null(); val.Null();
Error("internal compiler error: too many literals"); throw CompileException("internal compiler error: too many literals");
} }
} }
return _integer(val); return _integer(val);
@ -264,7 +259,7 @@ SQInteger SQFuncState::AllocStackPos()
SQInteger npos=_vlocals.size(); SQInteger npos=_vlocals.size();
_vlocals.push_back(SQLocalVarInfo()); _vlocals.push_back(SQLocalVarInfo());
if(_vlocals.size()>((SQUnsignedInteger)_stacksize)) { if(_vlocals.size()>((SQUnsignedInteger)_stacksize)) {
if(_stacksize>MAX_FUNC_STACKSIZE) Error("internal compiler error: too many locals"); if(_stacksize>MAX_FUNC_STACKSIZE) throw CompileException("internal compiler error: too many locals");
_stacksize=_vlocals.size(); _stacksize=_vlocals.size();
} }
return npos; return npos;

View File

@ -11,7 +11,6 @@ struct SQFuncState
#ifdef _DEBUG_DUMP #ifdef _DEBUG_DUMP
void Dump(SQFunctionProto *func); void Dump(SQFunctionProto *func);
#endif #endif
[[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);}

View File

@ -92,15 +92,10 @@ SQLexer::SQLexer(SQSharedState *ss, SQLEXREADFUNC rg, SQUserPointer up)
Next(); Next();
} }
[[noreturn]] void SQLexer::Error(const SQChar *err)
{
throw CompileException(err);
}
void SQLexer::Next() void SQLexer::Next()
{ {
char32_t t = _readf(_up); char32_t t = _readf(_up);
if(t > MAX_CHAR) Error("Invalid character"); if(t > MAX_CHAR) throw CompileException("Invalid character");
if(t != 0) { if(t != 0) {
_currdata = t; _currdata = t;
return; return;
@ -127,7 +122,7 @@ void SQLexer::LexBlockComment()
switch(CUR_CHAR) { switch(CUR_CHAR) {
case '*': { NEXT(); if(CUR_CHAR == '/') { done = true; NEXT(); }}; continue; case '*': { NEXT(); if(CUR_CHAR == '/') { done = true; NEXT(); }}; continue;
case '\n': _currentline++; NEXT(); continue; case '\n': _currentline++; NEXT(); continue;
case SQUIRREL_EOB: Error("missing \"*/\" in comment"); case SQUIRREL_EOB: throw CompileException("missing \"*/\" in comment");
default: NEXT(); default: NEXT();
} }
} }
@ -196,11 +191,11 @@ SQInteger SQLexer::Lex()
SQInteger stype; SQInteger stype;
NEXT(); NEXT();
if(CUR_CHAR != '"') if(CUR_CHAR != '"')
Error("string expected"); throw CompileException("string expected");
if((stype=ReadString('"',true))!=-1) { if((stype=ReadString('"',true))!=-1) {
RETURN_TOKEN(stype); RETURN_TOKEN(stype);
} }
Error("error parsing the string"); throw CompileException("error parsing the string");
} }
case '"': case '"':
case '\'': { case '\'': {
@ -208,7 +203,7 @@ SQInteger SQLexer::Lex()
if((stype=ReadString(CUR_CHAR,false))!=-1){ if((stype=ReadString(CUR_CHAR,false))!=-1){
RETURN_TOKEN(stype); RETURN_TOKEN(stype);
} }
Error("error parsing the string"); throw CompileException("error parsing the string");
} }
case '{': case '}': case '(': case ')': case '[': case ']': case '{': case '}': case '(': case ')': case '[': case ']':
case ';': case ',': case '?': case '^': case '~': case ';': case ',': case '?': case '^': case '~':
@ -218,7 +213,7 @@ SQInteger SQLexer::Lex()
NEXT(); NEXT();
if (CUR_CHAR != '.'){ RETURN_TOKEN('.') } if (CUR_CHAR != '.'){ RETURN_TOKEN('.') }
NEXT(); NEXT();
if (CUR_CHAR != '.'){ Error("invalid token '..'"); } if (CUR_CHAR != '.'){ throw CompileException("invalid token '..'"); }
NEXT(); NEXT();
RETURN_TOKEN(TK_VARPARAMS); RETURN_TOKEN(TK_VARPARAMS);
case '&': case '&':
@ -264,7 +259,7 @@ SQInteger SQLexer::Lex()
} }
else { else {
SQInteger c = CUR_CHAR; SQInteger c = CUR_CHAR;
if (iscntrl((int)c)) Error("unexpected character(control)"); if (iscntrl((int)c)) throw CompileException("unexpected character(control)");
NEXT(); NEXT();
RETURN_TOKEN(c); RETURN_TOKEN(c);
} }
@ -294,10 +289,10 @@ SQInteger SQLexer::ReadString(char32_t ndelim,bool verbatim)
while(CUR_CHAR != ndelim) { while(CUR_CHAR != ndelim) {
switch(CUR_CHAR) { switch(CUR_CHAR) {
case SQUIRREL_EOB: case SQUIRREL_EOB:
Error("unfinished string"); throw CompileException("unfinished string");
return -1; return -1;
case '\n': case '\n':
if(!verbatim) Error("newline in a constant"); if(!verbatim) throw CompileException("newline in a constant");
APPEND_CHAR(CUR_CHAR); NEXT(); APPEND_CHAR(CUR_CHAR); NEXT();
_currentline++; _currentline++;
break; break;
@ -309,7 +304,7 @@ SQInteger SQLexer::ReadString(char32_t ndelim,bool verbatim)
NEXT(); NEXT();
switch(CUR_CHAR) { switch(CUR_CHAR) {
case 'x': NEXT(); { case 'x': NEXT(); {
if(!isxdigit(CUR_CHAR)) Error("hexadecimal number expected"); if(!isxdigit(CUR_CHAR)) throw CompileException("hexadecimal number expected");
const SQInteger maxdigits = 4; const SQInteger maxdigits = 4;
SQChar temp[maxdigits]; SQChar temp[maxdigits];
size_t n = 0; size_t n = 0;
@ -319,7 +314,7 @@ SQInteger SQLexer::ReadString(char32_t ndelim,bool verbatim)
NEXT(); NEXT();
} }
auto val = ParseInteger(std::string_view{temp, n}, 16); auto val = ParseInteger(std::string_view{temp, n}, 16);
if (!val.has_value()) Error("hexadecimal number expected"); if (!val.has_value()) throw CompileException("hexadecimal number expected");
APPEND_CHAR(static_cast<SQChar>(*val)); APPEND_CHAR(static_cast<SQChar>(*val));
} }
break; break;
@ -335,7 +330,7 @@ SQInteger SQLexer::ReadString(char32_t ndelim,bool verbatim)
case '"': APPEND_CHAR('"'); NEXT(); break; case '"': APPEND_CHAR('"'); NEXT(); break;
case '\'': APPEND_CHAR('\''); NEXT(); break; case '\'': APPEND_CHAR('\''); NEXT(); break;
default: default:
Error("unrecognised escaper char"); throw CompileException("unrecognised escaper char");
break; break;
} }
} }
@ -357,8 +352,8 @@ SQInteger SQLexer::ReadString(char32_t ndelim,bool verbatim)
TERMINATE_BUFFER(); TERMINATE_BUFFER();
SQInteger len = _longstr.size()-1; SQInteger len = _longstr.size()-1;
if(ndelim == '\'') { if(ndelim == '\'') {
if(len == 0) Error("empty constant"); if(len == 0) throw CompileException("empty constant");
if(len > 1) Error("constant too long"); if(len > 1) throw CompileException("constant too long");
_nvalue = _longstr[0]; _nvalue = _longstr[0];
return TK_INTEGER; return TK_INTEGER;
} }
@ -420,7 +415,7 @@ SQInteger SQLexer::ReadNumber()
APPEND_CHAR(CUR_CHAR); APPEND_CHAR(CUR_CHAR);
NEXT(); NEXT();
} }
if(isdigit(CUR_CHAR)) Error("invalid octal number"); if(isdigit(CUR_CHAR)) throw CompileException("invalid octal number");
} }
else { else {
NEXT(); NEXT();
@ -429,7 +424,7 @@ SQInteger SQLexer::ReadNumber()
APPEND_CHAR(CUR_CHAR); APPEND_CHAR(CUR_CHAR);
NEXT(); NEXT();
} }
if(_longstr.size() > MAX_HEX_DIGITS) Error("too many digits for an Hex number"); if(_longstr.size() > MAX_HEX_DIGITS) throw CompileException("too many digits for an Hex number");
} }
} }
else { else {
@ -437,7 +432,7 @@ SQInteger SQLexer::ReadNumber()
while (CUR_CHAR == '.' || isdigit(CUR_CHAR) || isexponent(CUR_CHAR)) { while (CUR_CHAR == '.' || isdigit(CUR_CHAR) || isexponent(CUR_CHAR)) {
if(CUR_CHAR == '.' || isexponent(CUR_CHAR)) type = TFLOAT; if(CUR_CHAR == '.' || isexponent(CUR_CHAR)) type = TFLOAT;
if(isexponent(CUR_CHAR)) { if(isexponent(CUR_CHAR)) {
if(type != TFLOAT) Error("invalid numeric format"); if(type != TFLOAT) throw CompileException("invalid numeric format");
type = TSCIENTIFIC; type = TSCIENTIFIC;
APPEND_CHAR(CUR_CHAR); APPEND_CHAR(CUR_CHAR);
NEXT(); NEXT();
@ -445,7 +440,7 @@ SQInteger SQLexer::ReadNumber()
APPEND_CHAR(CUR_CHAR); APPEND_CHAR(CUR_CHAR);
NEXT(); NEXT();
} }
if(!isdigit(CUR_CHAR)) Error("exponent expected"); if(!isdigit(CUR_CHAR)) throw CompileException("exponent expected");
} }
APPEND_CHAR(CUR_CHAR); APPEND_CHAR(CUR_CHAR);

View File

@ -6,7 +6,6 @@ struct SQLexer
{ {
~SQLexer(); ~SQLexer();
SQLexer(SQSharedState *ss,SQLEXREADFUNC rg,SQUserPointer up); SQLexer(SQSharedState *ss,SQLEXREADFUNC rg,SQUserPointer up);
[[noreturn]] void Error(const SQChar *err);
SQInteger Lex(); SQInteger Lex();
const SQChar *Tok2Str(SQInteger tok); const SQChar *Tok2Str(SQInteger tok);
private: private: