c-repl/src/errors.cpp

56 lines
2.8 KiB
C++
Raw Normal View History

2023-11-15 14:37:20 +01:00
#include <vector>
#include <string>
#include <iostream>
using namespace std;
2023-12-15 15:36:09 +01:00
#include "include/errors.h"
2023-11-15 14:37:20 +01:00
#include "include/colors.h"
#include "include/tokenize.h"
2023-12-26 12:34:05 +01:00
string UserError::get_message(void) const {
switch (this->type) {
2023-12-15 15:36:09 +01:00
case ErrorType::NotImplemented: return "Not implemented";
case ErrorType::UnknownToken: return "Unknown token";
case ErrorType::IntegerTooLarge: return "Integer constant is too large";
case ErrorType::EmptyInput: return "Input must not be empty";
case ErrorType::InvalidSyntax: return "Invalid syntax";
case ErrorType::ExceptedLParen: return "Expected '(";
case ErrorType::ExpectedRParen: return "Expected ')'";
2023-12-27 18:43:56 +01:00
case ErrorType::ExpectedLCurlyBracket: return "Expected '{'";
2023-12-15 15:36:09 +01:00
case ErrorType::ExpectedRCurlyBracket: return "Expected '}'";
case ErrorType::ExpectedSemicolon: return "Expected ';'";
case ErrorType::DependentDeclaration: return "A dependant statement may not be a declaration";
2023-12-26 12:34:05 +01:00
case ErrorType::UnknownType: return "Unknown type '"+get<string>(this->data)+"'";
case ErrorType::TypeNotCastable: return "Can't find an explicit cast to "+get<string>(this->data)+"'";
2023-12-15 15:36:09 +01:00
case ErrorType::TypesNotComparable: return "Types not comparable";
case ErrorType::ExpectedIntegralType: return "Expression must have integral type";
2024-01-03 12:48:00 +01:00
case ErrorType::NestedFunction: return "Function definition is not allowed here";
case ErrorType::FunctionRedefinition: return "Redefinition of '"+get<string>(this->data)+"'";
case ErrorType::IncompatibleRedefinition: return "Redefinition of '"+get<string>(this->data)+"' as different kind of symbol";
2023-12-15 15:36:09 +01:00
case ErrorType::ExpectedArithmeticType: return "Expression must have arithmetic type";
2023-12-26 12:34:05 +01:00
case ErrorType::UnknownIdentifier: return "Unknown identifier '"+get<string>(this->data)+"'";
case ErrorType::AlreadyDeclaredIdentifier: return "Already declared identifier '"+get<string>(this->data)+"'";
case ErrorType::UninitializedIdentifier: return "Accessing uninitialized identifier '"+get<string>(this->data)+"'";
2023-12-15 15:36:09 +01:00
case ErrorType::DivisionByZero: return "Division by 0";
case ErrorType::ModuloByZero: return "Modulo by 0";
case ErrorType::BreakNotWithinLoop: return "Break statement not within loop";
case ErrorType::ContinueNotWithinLoop: return "Continue statement not within loop";
default: return "Unknown error type";
}
2023-12-26 12:34:05 +01:00
}
void print_error_position(vector<string> history, CodePosition pos) {
if (pos.column == -1 || pos.line == -1)
return;
cout << endl;
string line = history[pos.line];
printf(BOLD "%4d " RESET , pos.line+1);
cout << line << endl;
for (int i=0; i < pos.column + 5; i++)
cout << " ";
cout << BOLD RED "^--" RESET << endl;
2023-11-15 14:37:20 +01:00
}