Missing variable error implementation
This commit is contained in:
parent
75c9eefd50
commit
881dc8975a
@ -134,6 +134,7 @@ void unreachable(Location loc)
|
|||||||
std::ostream& operator<<(std::ostream& os, Error const& err)
|
std::ostream& operator<<(std::ostream& os, Error const& err)
|
||||||
{
|
{
|
||||||
std::string_view short_description = visit(Overloaded {
|
std::string_view short_description = visit(Overloaded {
|
||||||
|
[](errors::Missing_Variable const&) { return "Cannot find variable"; },
|
||||||
[](errors::Failed_Numeric_Parsing const&) { return "Failed to parse a number"; },
|
[](errors::Failed_Numeric_Parsing const&) { return "Failed to parse a number"; },
|
||||||
[](errors::Not_Callable const&) { return "Value not callable"; },
|
[](errors::Not_Callable const&) { return "Value not callable"; },
|
||||||
[](errors::Undefined_Operator const&) { return "Undefined operator"; },
|
[](errors::Undefined_Operator const&) { return "Undefined operator"; },
|
||||||
@ -154,6 +155,17 @@ std::ostream& operator<<(std::ostream& os, Error const& err)
|
|||||||
|
|
||||||
auto const loc = err.location;
|
auto const loc = err.location;
|
||||||
visit(Overloaded {
|
visit(Overloaded {
|
||||||
|
[&](errors::Missing_Variable const& err) {
|
||||||
|
os << "I encountered '" << err.name << "' that looks like variable but\n";
|
||||||
|
os << "I can't find it in surrounding scope or in one of parent's scopes\n";
|
||||||
|
os << "\n";
|
||||||
|
|
||||||
|
Lines::the.print(os, std::string(loc->filename), loc->line, loc->line);
|
||||||
|
|
||||||
|
os << "\n";
|
||||||
|
os << "Variables can only be references in scope (block) where they been created\n";
|
||||||
|
os << "or from parent blocks to variable block\n";
|
||||||
|
},
|
||||||
[&](errors::Unrecognized_Character const& err) {
|
[&](errors::Unrecognized_Character const& err) {
|
||||||
os << "I encountered character in the source code that was not supposed to be here.\n";
|
os << "I encountered character in the source code that was not supposed to be here.\n";
|
||||||
os << " Character Unicode code: U+" << std::hex << err.invalid_character << '\n';
|
os << " Character Unicode code: U+" << std::hex << err.invalid_character << '\n';
|
||||||
|
@ -749,8 +749,15 @@ Result<Value> Interpreter::eval(Ast &&ast)
|
|||||||
return Value::from(std::move(ast.token.source).substr(1));
|
return Value::from(std::move(ast.token.source).substr(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
auto const value = env->find(std::string(ast.token.source));
|
auto name = std::string(ast.token.source);
|
||||||
assert(value, "Missing variable error is not implemented yet: variable: "s + std::string(ast.token.source));
|
|
||||||
|
auto const value = env->find(name);
|
||||||
|
if (!value) {
|
||||||
|
return Error {
|
||||||
|
.details = errors::Missing_Variable { .name = std::move(name) },
|
||||||
|
.location = ast.location
|
||||||
|
};
|
||||||
|
}
|
||||||
return *value;
|
return *value;
|
||||||
}
|
}
|
||||||
return Value{};
|
return Value{};
|
||||||
|
@ -104,6 +104,13 @@ namespace errors
|
|||||||
std::vector<std::string> possibilities;
|
std::vector<std::string> possibilities;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// When user tries to use variable that has not been defined yet.
|
||||||
|
struct Missing_Variable
|
||||||
|
{
|
||||||
|
/// Name of variable
|
||||||
|
std::string name;
|
||||||
|
};
|
||||||
|
|
||||||
/// Collection of messages that are considered internal and should not be printed to the end user.
|
/// Collection of messages that are considered internal and should not be printed to the end user.
|
||||||
namespace internal
|
namespace internal
|
||||||
{
|
{
|
||||||
@ -126,6 +133,7 @@ namespace errors
|
|||||||
Expected_Expression_Separator_Before,
|
Expected_Expression_Separator_Before,
|
||||||
Failed_Numeric_Parsing,
|
Failed_Numeric_Parsing,
|
||||||
Literal_As_Identifier,
|
Literal_As_Identifier,
|
||||||
|
Missing_Variable,
|
||||||
Not_Callable,
|
Not_Callable,
|
||||||
Undefined_Operator,
|
Undefined_Operator,
|
||||||
Unexpected_Empty_Source,
|
Unexpected_Empty_Source,
|
||||||
|
Loading…
Reference in New Issue
Block a user