Diagnostics

- Added diagnostic token dump to 'calc' in debug mode, after token substitution
  so the new tokens can be seen.
- Added unit tests to look for parser syntax errors.
- Added test for '2--3'.
This commit is contained in:
Paul Beckingham 2014-04-06 23:57:15 -04:00
parent 70ba19fd5e
commit 25b8082fbd
2 changed files with 17 additions and 5 deletions

View file

@ -106,14 +106,16 @@ void Eval::evaluateInfixExpression (const std::string& e, Variant& v) const
std::string token;
Lexer::Type type;
while (l.token (token, type))
{
tokens.push_back (std::pair <std::string, Lexer::Type> (token, type));
if (_debug)
std::cout << "# token infix '" << token << "' " << Lexer::type_name (type) << "\n";
}
// Parse for syntax checking and operator replacement.
infixParse (tokens);
if (_debug)
{
std::vector <std::pair <std::string, Lexer::Type> >::iterator i;
for (i = tokens.begin (); i != tokens.end (); ++i)
std::cout << "# token infix '" << i->first << "' " << Lexer::type_name (i->second) << "\n";
}
// Convert infix --> postfix.
infixToPostfix (tokens);

View file

@ -27,7 +27,7 @@
use strict;
use warnings;
use Test::More tests => 14;
use Test::More tests => 22;
# '15min' is seen as '15', 'min', not '15min' duration.
my $output = qx{../src/calc --debug --noambiguous '12 * 3600 + 34 * 60 + 56'};
@ -36,6 +36,7 @@ like ($output, qr/token infix '3600' Number/, 'Number 3600');
like ($output, qr/token infix '34' Number/, 'Number 60');
like ($output, qr/token infix '60' Number/, 'Number 60');
like ($output, qr/token infix '56' Number/, 'Number 56');
like ($output, qr/no errors/ms, 'No syntax errors');
like ($output, qr/^45296$/ms, 'Result 45296');
unlike ($output, qr/Error/, 'No errors');
@ -48,5 +49,14 @@ like ($output, qr/token postfix '56' Number/, 'Number 56');
like ($output, qr/^45296$/ms, 'Result 45296');
unlike ($output, qr/Error/, 'No errors');
$output = qx{../src/calc --debug --noambiguous '2--3'};
like ($output, qr/token infix '2' Number/ms, 'Number 2');
like ($output, qr/token infix '-' Operator/ms, 'Operator -');
like ($output, qr/token infix '_neg_' Operator/ms, 'operator _neg_');
like ($output, qr/token infix '3' Number/ms, 'Number 3');
like ($output, qr/no errors/ms, 'No syntax errors');
like ($output, qr/^5$/ms, 'Result 5');
unlike ($output, qr/Error/, 'No errors');
exit 0;