diff --git a/Parsing.cpp b/Parsing.cpp index b244272..32e6742 100644 --- a/Parsing.cpp +++ b/Parsing.cpp @@ -1,28 +1,29 @@ #include +#include +#include #include #include #include "./core/Tokenizer.cpp" +#include "./core/Parser.cpp" int main() { - std::string girdi; + std::ifstream dosyaOku("source.sqt", std::ios::in | std::ios::binary); + std::string icerik; - std::cout << "\nsaQut Compiler\n\n"; - - while(true) - { - std::cout << ">> "; - std::getline(std::cin, girdi); - - Tokenizer token; - token.parse(girdi); - - if (girdi == ".exit") - { - exit(0); - }; - std::cout << "\n"; + if (dosyaOku.is_open()) { + std::stringstream buffer; + buffer << dosyaOku.rdbuf(); // Dosya içeriğini buffer'a boşalt + icerik = buffer.str(); + dosyaOku.close(); } + Tokenizer tokenizer; + Parser parser; + + auto tokens = tokenizer.scan(icerik); + parser.parse(tokens); + + return 0; } \ No newline at end of file diff --git a/core/Lexer.cpp b/core/Lexer.cpp index 459c453..dfbcfec 100644 --- a/core/Lexer.cpp +++ b/core/Lexer.cpp @@ -2,6 +2,8 @@ #include #include +#ifndef LEXER +#define LEXER struct INumber { int start = 0; int end = 0; @@ -385,4 +387,5 @@ public: numberToken.end = this->getLastPosition(); return numberToken; } -}; \ No newline at end of file +}; +#endif \ No newline at end of file diff --git a/core/Parser.cpp b/core/Parser.cpp new file mode 100644 index 0000000..d010314 --- /dev/null +++ b/core/Parser.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include "./Tokenizer.cpp" + +#ifndef PARSER +#define PARSER + +std::string padRight(std::string str, size_t totalLen) { + if (str.size() < totalLen) { + str.append(totalLen - str.size(), ' '); + } + return str; +} + +class Parser { + public: + void parse(std::vector tokens) + { + for(Token token : tokens) + { + std::cout << padRight(token.token,20) << token.gettype() << "\n"; + } + } +}; + + +#endif \ No newline at end of file diff --git a/core/Tokenizer.cpp b/core/Tokenizer.cpp index 3819c76..a1b806c 100644 --- a/core/Tokenizer.cpp +++ b/core/Tokenizer.cpp @@ -2,9 +2,11 @@ #include #include #include - #include "./Lexer.cpp" +#ifndef TOKENIZER +#define TOKENIZER + class Token { protected: std::string type = ""; @@ -157,22 +159,41 @@ const constexpr std::string_view keywords[] = { class Tokenizer { public: Lexer hmx; - void parse(std::string input) + std::vector scan(std::string input) { + std::vector tokens; this->hmx.setText(input); while(1) { Token token = this->scope(); - std::cout << token.gettype() << " -> " << token.token << "\n"; + tokens.push_back(token); if(this->hmx.isEnd()) { break; } } + return tokens; } Token scope() { this->hmx.skipWhiteSpace(); + + // Yorum satırları + if(this->hmx.include("//", true)) + { + this->skipOneLineComment(); + } + if(this->hmx.include("/*", true)) + { + this->skipMultiLineComment(); + } + + if(this->hmx.isEnd()){ + Token token; + token.token = "EOL"; + return token; + }; + // Stringler if(this->hmx.getchar() == '"') { @@ -338,4 +359,35 @@ public: this->hmx.acceptPosition(); return stringToken; } -}; \ No newline at end of file + void skipOneLineComment() + { + std::cout << "SkipLineComment\n"; + while(this->hmx.isEnd() == false) + { + if(this->hmx.getchar() == '\n') + { + this->hmx.nextChar(); + this->hmx.skipWhiteSpace(); + return; + }else{ + this->hmx.nextChar(); + } + } + } + void skipMultiLineComment() + { + std::cout << "SkipBlockComment\n"; + while(this->hmx.isEnd() == false) + { + if(this->hmx.include("*/",true)) + { + this->hmx.skipWhiteSpace(); + return; + }else{ + this->hmx.nextChar(); + } + } + } +}; + +#endif \ No newline at end of file