From 912c3ac496189430a9ab390f27476203cda6d22a Mon Sep 17 00:00:00 2001 From: ChUrl Date: Mon, 20 Mar 2023 20:00:49 +0100 Subject: [PATCH] Lex the input file --- src/main.cpp | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index c5ff3a5..d6f9d5c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,12 +1,31 @@ #include #include #include +#include #include +#include #include "lexer/Lexer.h" namespace po = boost::program_options; -int main(int argc, char **argv) { +auto lex(std::string_view input_string, std::vector tokens) -> bool { + Lexer lexer(input_string); + while (true) { + const Token token = lexer.next(); + std::cout << std::setw(10) << token.getTypeName() << ": " << static_cast(token) << std::endl; + + if (token.getType() == Token::UNEXPECTED) { + return false; + } + if (token.getType() == Token::END) { + return true; + } + + tokens.push_back(token); + } +} + +auto main(int argc, char **argv) -> int { // Argument parsing straight from the Boost manual: https://www.boost.org/doc/libs/1_60_0/doc/html/program_options/tutorial.html // Declare the supported options. @@ -26,6 +45,17 @@ int main(int argc, char **argv) { std::cout << desc << std::endl; return 1; } + if (!vm.count("input")) { + std::cout << "Did not provide input file!" << std::endl; + return -1; + } + if (!vm.count("output")) { + std::cout << "Did not provide output file!" << std::endl; + return -1; + } + + std::cout << "Input File: " << vm["input"].as() << std::endl; + std::cout << "Output File: " << vm["output"].as() << std::endl; // Read the input file std::ifstream ifs; @@ -42,7 +72,15 @@ int main(int argc, char **argv) { } ifs.close(); -int main() { - std::cout << "Hello, World!" << std::endl; + // Lexing + std::cout << "Lexing:" << std::endl; + std::vector tokens; + if (!lex(input_string, tokens)) { + std::cout << "Lexer Error: Unexpected Token!" << std::endl; + return -1; + } + + // Parsing + return 0; }