From 1a0207687c19d6570e9fe2a9d3eb0ff1eaeba43e Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 8 Jun 2025 15:09:15 +0100 Subject: [PATCH] Move error handling and interpreter functionality to different files --- cclox_src/error_handler.cc | 11 +++++++ cclox_src/error_handler.hh | 11 +++++++ cclox_src/interpreter.cc | 56 ++++++++++++++++++++++++++++++++ cclox_src/interpreter.hh | 3 ++ cclox_src/main.cc | 66 +++----------------------------------- 5 files changed, 85 insertions(+), 62 deletions(-) create mode 100644 cclox_src/error_handler.cc create mode 100644 cclox_src/error_handler.hh create mode 100644 cclox_src/interpreter.cc create mode 100644 cclox_src/interpreter.hh diff --git a/cclox_src/error_handler.cc b/cclox_src/error_handler.cc new file mode 100644 index 0000000..5f2b0dd --- /dev/null +++ b/cclox_src/error_handler.cc @@ -0,0 +1,11 @@ +#include "error_handler.hh" +#include + +void ErrorHandler::error(int line, const std::string &message) { + report(line, "", message); +} + +void ErrorHandler::report(int line, const std::string &where, const std::string &message) { + std::cout << "[line " << line << "] Error" << where << ": " << message << '\n'; + had_error = true; +} diff --git a/cclox_src/error_handler.hh b/cclox_src/error_handler.hh new file mode 100644 index 0000000..503ac03 --- /dev/null +++ b/cclox_src/error_handler.hh @@ -0,0 +1,11 @@ +#pragma once + +#include + +struct ErrorHandler { + ErrorHandler() : had_error{false} {}; + void error(int line, const std::string &message); + void report(int line, const std::string &where, const std::string &message); + + bool had_error; +}; diff --git a/cclox_src/interpreter.cc b/cclox_src/interpreter.cc new file mode 100644 index 0000000..bb6b9f0 --- /dev/null +++ b/cclox_src/interpreter.cc @@ -0,0 +1,56 @@ +#include "interpreter.hh" +#include "error_handler.hh" +#include "scanner.hh" +#include +#include +#include +#include + +#define PROMPT "> " + +extern ErrorHandler error_handler; + +void run_file(const char *path); +void run_prompt(); +void run(const std::string &code); + +void run_interpreter(int argc, char *argv[]) { + if (argc == 2) { + run_file(argv[1]); + } else { + run_prompt(); + } +} + +void run_file(const char *path) { + if (std::ifstream source_code{path, std::ios::ate}) { + uint64_t size = source_code.tellg(); + std::string code(size, '\0'); + source_code.seekg(0); + source_code.read(&code[0], size); + + run(code); + if (error_handler.had_error) { + exit(EX_DATAERR); + } + } +} + +void run_prompt() { + std::string line{}; + std::cout << PROMPT; + while (std::getline(std::cin, line)) { + run(line); + std::cout << PROMPT; + error_handler.had_error = false; + } +} + +void run(const std::string &code) { + Scanner scanner{code}; + std::vector tokens = scanner.scan_tokens(); + + for (const auto &token : tokens) { + std::cout << token << '\n'; + } +} diff --git a/cclox_src/interpreter.hh b/cclox_src/interpreter.hh new file mode 100644 index 0000000..8917050 --- /dev/null +++ b/cclox_src/interpreter.hh @@ -0,0 +1,3 @@ +#pragma once + +void run_interpreter(int argc, char *argv[]); diff --git a/cclox_src/main.cc b/cclox_src/main.cc index e2694f5..faed62b 100644 --- a/cclox_src/main.cc +++ b/cclox_src/main.cc @@ -1,21 +1,9 @@ -#include "token.hh" -#include "scanner.hh" +#include "interpreter.hh" +#include "error_handler.hh" #include -#include -#include #include -#include -#include -#define PROMPT "> " - -static bool had_error = false; - -void run_file(const char *path); -void run_prompt(); -void run(const std::string &code); -void error(int line, const std::string &message); -void report(int line, const std::string &where, const std::string &message); +ErrorHandler error_handler{}; int main(int argc, char *argv[]) { if (argc > 2) { @@ -23,53 +11,7 @@ int main(int argc, char *argv[]) { exit(EX_USAGE); } - if (argc == 2) { - run_file(argv[1]); - } else { - run_prompt(); - } + run_interpreter(argc, argv); return 0; } - -void run_file(const char *path) { - if (std::ifstream source_code{path, std::ios::ate}) { - uint64_t size = source_code.tellg(); - std::string code(size, '\0'); - source_code.seekg(0); - source_code.read(&code[0], size); - - run(code); - if (had_error) { - exit(EX_DATAERR); - } - } -} - -void run_prompt() { - std::string line{}; - std::cout << PROMPT; - while (std::getline(std::cin, line)) { - run(line); - std::cout << PROMPT; - had_error = false; - } -} - -void run(const std::string &code) { - Scanner scanner{code}; - std::vector tokens = scanner.scan_tokens(); - - for (auto token : tokens) { - std::cout << token << '\n'; - } -} - -void error(int line, const std::string &message) { - report(line, "", message); -} - -void report(int line, const std::string &where, const std::string &message) { - std::cout << "[line " << line << "] Error" << where << ": " << message << '\n'; - had_error = true; -}