From 838dbbeb03eb7f50ced970d8a9e135c0152e7cfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Grodzi=C5=84ski?= Date: Tue, 27 May 2025 14:25:29 +0900 Subject: [PATCH] Some basic tokens --- .gitignore | 3 ++- Lexer.cpp | 7 +++++-- Lexer.h | 4 ++++ Token.cpp | 19 ++++++++++++++++++- Token.h | 9 +++++++++ main.cpp | 34 ++++++++++++++++++++++++++++++++++ make.sh | 2 +- 7 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 main.cpp diff --git a/.gitignore b/.gitignore index bf983e6..48d31f4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .DS_Store -*.o \ No newline at end of file +*.o +brb \ No newline at end of file diff --git a/Lexer.cpp b/Lexer.cpp index 7fe4979..8f005a8 100644 --- a/Lexer.cpp +++ b/Lexer.cpp @@ -1,6 +1,9 @@ #include "Lexer.h" #include "Token.h" -std::vector tokens() { - +Lexer::Lexer(std::string source) : source(source) { +} + +std::vector Lexer::tokens() { + return { Token::integer, Token::real, Token::integer, Token::eof }; } \ No newline at end of file diff --git a/Lexer.h b/Lexer.h index 0c0205b..4b3f413 100644 --- a/Lexer.h +++ b/Lexer.h @@ -6,7 +6,11 @@ class Token; class Lexer { +private: + std::string source; + public: + Lexer(std::string source); std::vector tokens(); }; diff --git a/Token.cpp b/Token.cpp index 4539029..3aa468f 100644 --- a/Token.cpp +++ b/Token.cpp @@ -1 +1,18 @@ -#include "Token.h" \ No newline at end of file +#include "Token.h" + +Token::Token(Kind kind): kind(kind) { +} + +std::string Token::toString() { + switch (kind) { + case integer: + return "INTEGER"; + break; + case real: + return "REAL"; + break; + case eof: + return "EOF"; + break; + } +} \ No newline at end of file diff --git a/Token.h b/Token.h index 259d1c3..618072b 100644 --- a/Token.h +++ b/Token.h @@ -1,6 +1,8 @@ #ifndef TOKEN_H #define TOKEN_H +#include + class Token { public: enum Kind { @@ -8,6 +10,13 @@ public: real, eof }; + +private: + Kind kind; + +public: + Token(Kind kind); + std::string toString(); }; #endif \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..07d8ec0 --- /dev/null +++ b/main.cpp @@ -0,0 +1,34 @@ +#include +#include +#include "Lexer.h" +#include "Token.h" + +std::string readFile(std::string fileName) { + std::ifstream file(fileName.c_str(), std::ios::in | std::ios::binary | std::ios::ate); + if (!file.is_open()) { + std::cerr << "Cannot open file " << fileName << std::endl; + exit(1); + } + + std::streamsize fileSize = file.tellg(); + file.seekg(0, std::ios::beg); + std::vector fileBytes(fileSize); + file.read(fileBytes.data(), fileSize); + return std::string(fileBytes.data(), fileSize); +} + +int main(int argc, char **argv) { + if (argc < 2) { + std::cerr << "Need to provide a file name" << std::endl; + exit(1); + } + + std::string source = readFile(std::string(argv[1])); + Lexer lexer(source); + std::vector tokens = lexer.tokens(); + for (Token &token : tokens) + std::cout << token.toString() << " "; + std::cout << std::endl; + + return 0; +} \ No newline at end of file diff --git a/make.sh b/make.sh index a1eaf1c..c85dd32 100755 --- a/make.sh +++ b/make.sh @@ -1,3 +1,3 @@ #!/bin/bash -cc -c -std=c++17 *.cpp \ No newline at end of file +cc -std=c++17 -lc++ *.cpp -o brb \ No newline at end of file