Skip to content

Commit

Permalink
more error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
madeso committed Oct 13, 2020
1 parent 132f2ec commit 57c8a3e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
38 changes: 19 additions & 19 deletions fel/interpreter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace fel
BinaryHelper
(
Log* log,
const Where& where,
const Token& op,
std::shared_ptr<Expression> lhs,
std::shared_ptr<Expression> rhs,
std::shared_ptr<Object> left,
Expand All @@ -95,7 +95,7 @@ namespace fel
{
if(left == nullptr || right == nullptr)
{
log->AddError(where, log::Type::InvalidOperationOnNull);
log->AddError(op.where, log::Type::InvalidOperationOnNull, {op.lexeme});
log->AddError(lhs->GetLocation(), log::Type::ThisEvaluatesTo, {TypeToString(left), Stringify(left)});
log->AddError(rhs->GetLocation(), log::Type::ThisEvaluatesTo, {TypeToString(right), Stringify(right)});
return nullptr;
Expand Down Expand Up @@ -132,7 +132,7 @@ namespace fel
}
}

log->AddError(where, log::Type::InvalidBinaryOperation);
log->AddError(op.where, log::Type::InvalidBinaryOperation, {op.lexeme});
log->AddError(lhs->GetLocation(), log::Type::ThisEvaluatesTo, {TypeToString(left), Stringify(left)});
log->AddError(rhs->GetLocation(), log::Type::ThisEvaluatesTo, {TypeToString(right), Stringify(right)});
return nullptr;
Expand All @@ -143,7 +143,7 @@ namespace fel
CompareHelper
(
Log* log,
const Where& where,
const Token& op,
std::shared_ptr<Expression> lhs,
std::shared_ptr<Expression> rhs,
std::shared_ptr<Object> left,
Expand All @@ -153,7 +153,7 @@ namespace fel
{
if(left == nullptr || right == nullptr)
{
log->AddError(where, log::Type::InvalidOperationOnNull);
log->AddError(op.where, log::Type::InvalidOperationOnNull, {op.lexeme});
log->AddError(lhs->GetLocation(), log::Type::ThisEvaluatesTo, {TypeToString(left), Stringify(left)});
log->AddError(rhs->GetLocation(), log::Type::ThisEvaluatesTo, {TypeToString(right), Stringify(right)});
return nullptr;
Expand All @@ -170,7 +170,7 @@ namespace fel

// todo(Gustav): allow comparing of strings?

log->AddError(where, log::Type::InvalidBinaryOperation);
log->AddError(op.where, log::Type::InvalidBinaryOperation, {op.lexeme});
log->AddError(lhs->GetLocation(), log::Type::ThisEvaluatesTo, {TypeToString(left), Stringify(left)});
log->AddError(rhs->GetLocation(), log::Type::ThisEvaluatesTo, {TypeToString(right), Stringify(right)});
return nullptr;
Expand All @@ -181,7 +181,7 @@ namespace fel
EqualHelper
(
Log* log,
const Where& where,
const Token& op,
std::shared_ptr<Expression> lhs,
std::shared_ptr<Expression> rhs,
std::shared_ptr<Object> left,
Expand Down Expand Up @@ -227,7 +227,7 @@ namespace fel

// todo(Gustav): allow comparing of strings?

log->AddError(where, log::Type::InvalidBinaryOperation);
log->AddError(op.where, log::Type::InvalidBinaryOperation, {op.lexeme});
log->AddError(lhs->GetLocation(), log::Type::ThisEvaluatesTo, {TypeToString(left), Stringify(left)});
log->AddError(rhs->GetLocation(), log::Type::ThisEvaluatesTo, {TypeToString(right), Stringify(right)});
return nullptr;
Expand All @@ -244,39 +244,39 @@ namespace fel
{
case TokenType::Minus: return BinaryHelper
(
log, exp->op.where,
log, exp->op,
exp->left, exp->right,
left, right,
[](int lhs, int rhs) -> int {return lhs - rhs;},
[](float lhs, float rhs) -> float { return lhs - rhs;}
);
case TokenType::Mult: return BinaryHelper
(
log, exp->op.where,
log, exp->op,
exp->left, exp->right,
left, right,
[](int lhs, int rhs) -> int {return lhs * rhs;},
[](float lhs, float rhs) -> float { return lhs * rhs;}
);
case TokenType::Div: return BinaryHelper
(
log, exp->op.where,
log, exp->op,
exp->left, exp->right,
left, right,
nullptr,
[](float lhs, float rhs) -> float { return lhs - rhs;}
);
case TokenType::Mod: return BinaryHelper
(
log, exp->op.where,
log, exp->op,
exp->left, exp->right,
left, right,
[](int lhs, int rhs) -> int {return lhs % rhs;},
nullptr
);
case TokenType::Plus: return BinaryHelper
(
log, exp->op.where,
log, exp->op,
exp->left, exp->right,
left, right,
[](int lhs, int rhs) -> int {return lhs + rhs;},
Expand All @@ -298,42 +298,42 @@ namespace fel
);
case TokenType::Less: return CompareHelper
(
log, exp->op.where,
log, exp->op,
exp->left, exp->right,
left, right,
[](float lhs, float rhs) -> bool { return lhs < rhs; }
);
case TokenType::LessEqual: return CompareHelper
(
log, exp->op.where,
log, exp->op,
exp->left, exp->right,
left, right,
[](float lhs, float rhs) -> bool { return lhs <= rhs; }
);
case TokenType::Greater: return CompareHelper
(
log, exp->op.where,
log, exp->op,
exp->left, exp->right,
left, right,
[](float lhs, float rhs) -> bool { return lhs > rhs; }
);
case TokenType::GreaterEqual: return CompareHelper
(
log, exp->op.where,
log, exp->op,
exp->left, exp->right,
left, right,
[](float lhs, float rhs) -> bool { return lhs >= rhs; }
);
case TokenType::Equal: return EqualHelper
(
log, exp->op.where,
log, exp->op,
exp->left, exp->right,
left, right,
false
);
case TokenType::NotEqual: return EqualHelper
(
log, exp->op.where,
log, exp->op,
exp->left, exp->right,
left, right,
true
Expand Down
8 changes: 4 additions & 4 deletions fel/log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ namespace fel::log
o << "Expected expression";
break;
case Type::InvalidOperationOnNull:
assert(entry.arguments.size() == 0);
o << "Invalid operation on null";
assert(entry.arguments.size() == 1);
o << "Invalid operation on null: " << Arg(entry, 0);
break;
case Type::InvalidBinaryOperation:
assert(entry.arguments.size() == 0);
o << "Invalid binary operation";
assert(entry.arguments.size() == 1);
o << "Invalid binary operation: " << Arg(entry, 0);
break;
case Type::ThisEvaluatesTo:
assert(entry.arguments.size() == 2);
Expand Down

0 comments on commit 57c8a3e

Please sign in to comment.