Fixed a crash/logic bug in algebraic parser stack underflow

Removed some debug code
This commit is contained in:
David Anderson 2004-08-11 09:06:00 +00:00
parent c47eb57871
commit 3a04fa0788
2 changed files with 36 additions and 22 deletions

View File

@ -205,7 +205,7 @@ bool Compiler::Compile()
std::string amxname; std::string amxname;
amxname.assign(filename); amxname.assign(filename);
int pos = amxname.find(".asm"); int pos = (int)amxname.find(".asm");
if (pos != std::string::npos) if (pos != std::string::npos)
{ {
amxname.replace(pos, 4, ".amx"); amxname.replace(pos, 4, ".amx");
@ -1461,6 +1461,8 @@ bool Compiler::Parse()
CLabels->CompleteQueue(); CLabels->CompleteQueue();
CError->PrintReport(); CError->PrintReport();
if (CError->GetStatus() >= Err_Error)
return false;
return true; return true;
} }
@ -1932,7 +1934,7 @@ int Compiler::Eval(std::string &str, SymbolType sym)
e.Evaluate(sym); e.Evaluate(sym);
r->vals.push_back(e); r->vals.push_back(e);
} }
r->ops.push_back(str[i]); r->ops.push_back((char)str[i]);
if (str[i] == '>' || str[i] == '<') if (str[i] == '>' || str[i] == '<')
{ {
i++; i++;
@ -1965,24 +1967,28 @@ int Compiler::Eval(std::string &str, SymbolType sym)
e.Evaluate(sym); e.Evaluate(sym);
r->vals.push_back(e); r->vals.push_back(e);
} }
CExpr t;
t = EvalRpn(r, sym);
delete r;
if (Stack.size() < 2) if (Stack.size() < 2)
{ {
while (Stack.size()) while (!Stack.empty())
{ {
if (Stack.top()) rpn *t = Stack.top();
delete Stack.top(); if (t)
delete t;
t = 0;
Stack.pop(); Stack.pop();
} }
CError->ErrorMsg(Err_Unmatched_Token, str[i]); CError->ErrorMsg(Err_Unmatched_Token, str[i]);
return 0; return 0;
} } else {
CExpr t;
t = EvalRpn(r, sym);
delete r;
r = 0;
Stack.pop(); Stack.pop();
r = Stack.top(); r = Stack.top();
r->vals.push_back(t); r->vals.push_back(t);
pos = i + 1; pos = i + 1;
}
} else if (i == (int)(str.size() - 1)) { } else if (i == (int)(str.size() - 1)) {
if (pos < i) if (pos < i)
{ {
@ -2004,20 +2010,26 @@ int Compiler::Eval(std::string &str, SymbolType sym)
if (Stack.size() != 1) if (Stack.size() != 1)
{ {
rpn *t = 0;
while (!Stack.empty()) while (!Stack.empty())
{ {
delete Stack.top(); t = Stack.top();
if (t)
delete t;
t = 0;
Stack.pop(); Stack.pop();
} }
CError->ErrorMsg(Err_Unmatched_Token, '('); CError->ErrorMsg(Err_Unmatched_Token, '(');
return 0; return 0;
} }
rpn *r2 = Stack.top();
Stack.pop(); Stack.pop();
CExpr final; CExpr final;
final = EvalRpn(r, sym); final = EvalRpn(r, sym);
delete r;
r = 0;
return final.GetNumber(); return final.GetNumber();
} }
@ -2188,3 +2200,9 @@ bool Compiler::IsSymbol(std::string &str)
return true; return true;
} }
rpn::~rpn()
{
// ops.clear();
// vals.clear();
}

View File

@ -28,8 +28,6 @@ int main(int argc, char **argv)
{ {
Compiler Program; Compiler Program;
getchar();
get_options(argc, argv, Program); get_options(argc, argv, Program);
if (filename.size() < 1) if (filename.size() < 1)
@ -41,15 +39,13 @@ int main(int argc, char **argv)
Program.Load(filename); Program.Load(filename);
if (Program.Parse()) if (Program.Parse())
{ {
//Program.PrintCodeList();
if (Program.Compile()) if (Program.Compile())
printf("Done.\n"); printf("Done.\n");
else
printf("Compile build failed.\n");
} else {
printf("Compile failed.\n");
} }
/*ErrorMngr CError(&Program);
std::string f("232+4*4");
int val = Program.Eval(f);
printf("Evaluation: %d\n", val);*/
exit(0); exit(0);
} }