GSC Interpreter
A Turing-complete interpreter developed for a compiler course
Loading...
Searching...
No Matches
error.cpp File Reference
#include "gsc/error.hpp"
#include <iostream>
Include dependency graph for error.cpp:

Go to the source code of this file.

Functions

void report (const int &line, const std::string &where, const std::string &message)
 Report an error in the given line and location.
 
void error (const int &line, const std::string &message)
 Report an error in the given line.
 
void error (const Token &token, const std::string &message)
 Report an error in the given token.
 
void runtimeError (const RuntimeError &error)
 Report a runtime error given a RuntimeError object.
 

Function Documentation

◆ error() [1/2]

void error ( const int &  line,
const std::string &  message 
)

Report an error in the given line.

Parameters
lineThe line number where the error occurred.
messageThe error message to display.
Note
This function sets the global variable hadError to true and writes in stderr.
See also
report

Definition at line 12 of file error.cpp.

12 {
13 report(line, "", message);
14}
void report(const int &line, const std::string &where, const std::string &message)
Report an error in the given line and location.
Definition error.cpp:4

◆ error() [2/2]

void error ( const Token token,
const std::string &  message 
)

Report an error in the given token.

Parameters
tokenThe token where the error occurred.
messageThe error message to display.
Note
This function sets the global variable hadError to true and writes in stderr.
See also
report

Definition at line 16 of file error.cpp.

16 {
17 if (token.getType() == TokenType::END_OF_FILE) {
18 report(token.getLine(), "at end", message);
19 } else {
20 report(token.getLine(), "at '" + token.getLexeme() + "'", message);
21 }
22}
std::string getLexeme() const
Definition token.cpp:10
TokenType getType() const
Definition token.cpp:8
int getLine() const
Definition token.cpp:14
@ END_OF_FILE
Definition tokenType.hpp:50

◆ report()

void report ( const int &  line,
const std::string &  where,
const std::string &  message 
)

Report an error in the given line and location.

Parameters
lineThe line number where the error occurred.
whereThe location of the error.
messageThe error message to display.
Note
This function sets the global variable hadError to true and writes in stderr.

Definition at line 4 of file error.cpp.

5 {
6 std::cerr << "[line " << line << "] Error " << where << ": " << message
7 << "\n";
8
9 hadError = true;
10}
bool hadError
Definition error.hpp:6

◆ runtimeError()

void runtimeError ( const RuntimeError error)

Report a runtime error given a RuntimeError object.

Parameters
errorThe RuntimeError object containing the error details.
Note
This function sets the global variable hadRuntimeError to true and writes in stderr.

Definition at line 24 of file error.cpp.

24 {
25 std::cerr << error.getMessage() << "\n[line " << error.getToken()->getLine()
26 << "]" << std::endl;
27 hadRuntimeError = true;
28}
void error(const int &line, const std::string &message)
Report an error in the given line.
Definition error.cpp:12
bool hadRuntimeError
Definition error.hpp:7