Skip to content

Commit

Permalink
Error hints
Browse files Browse the repository at this point in the history
  • Loading branch information
SingularityT3 committed Mar 30, 2024
1 parent 3380840 commit 9f2511e
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 10 deletions.
8 changes: 7 additions & 1 deletion src/lexer/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ const std::vector<Token*> Lexer::makeTokens() {
} else if (currentChar == ']') {
tokens.emplace_back(new Token(TokenType::RSQRBRACKET, line, column));
} else if (currentChar == '=') {
tokens.emplace_back(new Token(TokenType::EQUALS, line, column));
advance();
if (idx >= expr->size() || currentChar != '=') {
tokens.emplace_back(new Token(TokenType::EQUALS, line, column));
continue;
} else {
throw PSC::LexerError(line, column - 1, "Invalid token '=='\nIf you want to check for equality, use single '=' instead");
}
} else if (currentChar == ':') {
tokens.emplace_back(new Token(TokenType::COLON, line, column));
} else if (currentChar == ',') {
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/functions/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ std::unique_ptr<NodeResult> FunctionNode::evaluate(PSC::Context &ctx) {

PSC::DataType returnDataType = ctx.getType(returnType);
if (returnDataType == PSC::DataType::NONE)
throw PSC::NotDefinedError(returnType, ctx, "Type '" + returnType.value + "'");
throw PSC::TypeNotDefinedError(returnType, ctx, returnType.value);

size_t parametersSize = parameterNames.size();
std::vector<PSC::Parameter> parameters;
Expand All @@ -39,7 +39,7 @@ std::unique_ptr<NodeResult> FunctionNode::evaluate(PSC::Context &ctx) {
const Token *typeToken = parameterTypes[i];
PSC::DataType type = ctx.getType(*typeToken);
if (type == PSC::DataType::NONE)
throw PSC::NotDefinedError(*typeToken, ctx, "Type '" + typeToken->value + "'");
throw PSC::TypeNotDefinedError(*typeToken, ctx, typeToken->value);
parameters.emplace_back(parameterNames[i], type, parameterPassTypes[i]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/functions/procedure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ std::unique_ptr<NodeResult> ProcedureNode::evaluate(PSC::Context &ctx) {
const Token *typeToken = parameterTypes[i];
PSC::DataType type = ctx.getType(*typeToken);
if (type == PSC::DataType::NONE)
throw PSC::NotDefinedError(*typeToken, ctx, "Type '" + typeToken->value + "'");
throw PSC::TypeNotDefinedError(*typeToken, ctx, typeToken->value);
parameters.emplace_back(parameterNames[i], type, parameterPassTypes[i]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/variable/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ std::unique_ptr<NodeResult> ArrayDeclareNode::evaluate(PSC::Context &ctx) {
for (auto identifier : identifiers) {
PSC::DataType dataType = ctx.getType(type);
if (dataType.type == PSC::DataType::NONE)
throw PSC::NotDefinedError(token, ctx, "Type '" + type.value + "'");
throw PSC::TypeNotDefinedError(token, ctx, type.value);

auto array = std::make_unique<PSC::Array>(identifier->value, dataType, dimensions);
array->init(ctx);
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/variable/pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ PointerDefineNode::PointerDefineNode(const Token &token, const Token &name, cons
std::unique_ptr<NodeResult> PointerDefineNode::evaluate(PSC::Context &ctx) {
PSC::DataType pointerType = ctx.getType(type);
if (pointerType == PSC::DataType::NONE)
throw PSC::NotDefinedError(token, ctx, "Type '" + type.value + "'");
throw PSC::TypeNotDefinedError(token, ctx, type.value);

if (ctx.isIdentifierType(name, false))
throw PSC::RedefinitionError(token, ctx, name.value);
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/variable/variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ std::unique_ptr<NodeResult> DeclareNode::evaluate(PSC::Context &ctx) {

PSC::DataType dataType = ctx.getType(type);
if (dataType == PSC::DataType::NONE)
throw PSC::NotDefinedError(token, ctx, "Type '" + type.value + "'");
throw PSC::TypeNotDefinedError(token, ctx, type.value);

ctx.addVariable(new PSC::Variable(identifier->value, dataType, false, &ctx));
}
Expand Down
25 changes: 23 additions & 2 deletions src/psc/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,29 @@ ConstAssignError::ConstAssignError(const Token &token, const Context &context, c
: RuntimeError(token, context, "Assignment to constant '" + constant + "'")
{}

NotDefinedError::NotDefinedError(const Token &token, const Context &context, const std::string &identifier)
: RuntimeError(token, context, identifier + " is not defined")
NotDefinedError::NotDefinedError(const Token &token, const Context &context, const std::string &identifier, const std::string &hint)
: RuntimeError(token, context, identifier + " is not defined" + hint)
{}

inline std::string getTypeHint(const std::string &s) {
std::string hint = "";

if (s == "INT")
hint = "\nDid you mean 'INTEGER'?";
else if (s == "FLOAT")
hint = "\nDid you mean 'REAL'?";
else if (s == "CHARACTER")
hint = "\nDid you mean 'CHAR'?";
else if (s == "BOOL")
hint = "\nDid you mean 'BOOLEAN'?";
else if (s == "STR")
hint = "\nDid you mean 'STRING'?";

return hint;
}

TypeNotDefinedError::TypeNotDefinedError(const Token &token, const Context &context, const std::string &type)
: NotDefinedError(token, context, "Type '" + type + "'", getTypeHint(type))
{}

ArrayDirectAccessError::ArrayDirectAccessError(const Token &token, const Context &context)
Expand Down
7 changes: 6 additions & 1 deletion src/psc/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ namespace PSC {

class NotDefinedError : public RuntimeError {
public:
NotDefinedError(const Token &token, const Context &context, const std::string &identifier);
NotDefinedError(const Token &token, const Context &context, const std::string &identifier, const std::string &hint = "");
};

class TypeNotDefinedError : public NotDefinedError {
public:
TypeNotDefinedError(const Token &token, const Context &context, const std::string &type);
};

class ArrayDirectAccessError : public RuntimeError {
Expand Down

0 comments on commit 9f2511e

Please sign in to comment.