Create parser

This commit is contained in:
abdussamedulutas 2025-12-26 20:15:28 +03:00
parent b5bdc3f5b4
commit 8adeabaff1
4 changed files with 105 additions and 21 deletions

View File

@ -1,28 +1,29 @@
#include <iostream> #include <iostream>
#include <fstream>
#include <sstream>
#include <string> #include <string>
#include <stdlib.h> #include <stdlib.h>
#include "./core/Tokenizer.cpp" #include "./core/Tokenizer.cpp"
#include "./core/Parser.cpp"
int main() 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"; if (dosyaOku.is_open()) {
std::stringstream buffer;
while(true) buffer << dosyaOku.rdbuf(); // Dosya içeriğini buffer'a boşalt
{ icerik = buffer.str();
std::cout << ">> "; dosyaOku.close();
std::getline(std::cin, girdi);
Tokenizer token;
token.parse(girdi);
if (girdi == ".exit")
{
exit(0);
};
std::cout << "\n";
} }
Tokenizer tokenizer;
Parser parser;
auto tokens = tokenizer.scan(icerik);
parser.parse(tokens);
return 0; return 0;
} }

View File

@ -2,6 +2,8 @@
#include <string> #include <string>
#include <vector> #include <vector>
#ifndef LEXER
#define LEXER
struct INumber { struct INumber {
int start = 0; int start = 0;
int end = 0; int end = 0;
@ -385,4 +387,5 @@ public:
numberToken.end = this->getLastPosition(); numberToken.end = this->getLastPosition();
return numberToken; return numberToken;
} }
}; };
#endif

28
core/Parser.cpp Normal file
View File

@ -0,0 +1,28 @@
#include <iostream>
#include <string>
#include <stdlib.h>
#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<Token> tokens)
{
for(Token token : tokens)
{
std::cout << padRight(token.token,20) << token.gettype() << "\n";
}
}
};
#endif

View File

@ -2,9 +2,11 @@
#include <string> #include <string>
#include <stdlib.h> #include <stdlib.h>
#include <vector> #include <vector>
#include "./Lexer.cpp" #include "./Lexer.cpp"
#ifndef TOKENIZER
#define TOKENIZER
class Token { class Token {
protected: protected:
std::string type = ""; std::string type = "";
@ -157,22 +159,41 @@ const constexpr std::string_view keywords[] = {
class Tokenizer { class Tokenizer {
public: public:
Lexer hmx; Lexer hmx;
void parse(std::string input) std::vector<Token> scan(std::string input)
{ {
std::vector<Token> tokens;
this->hmx.setText(input); this->hmx.setText(input);
while(1) while(1)
{ {
Token token = this->scope(); Token token = this->scope();
std::cout << token.gettype() << " -> " << token.token << "\n"; tokens.push_back(token);
if(this->hmx.isEnd()) if(this->hmx.isEnd())
{ {
break; break;
} }
} }
return tokens;
} }
Token scope() Token scope()
{ {
this->hmx.skipWhiteSpace(); 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 // Stringler
if(this->hmx.getchar() == '"') if(this->hmx.getchar() == '"')
{ {
@ -338,4 +359,35 @@ public:
this->hmx.acceptPosition(); this->hmx.acceptPosition();
return stringToken; return stringToken;
} }
}; 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