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 <fstream>
#include <sstream>
#include <string>
#include <stdlib.h>
#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;
}

View File

@ -2,6 +2,8 @@
#include <string>
#include <vector>
#ifndef LEXER
#define LEXER
struct INumber {
int start = 0;
int end = 0;
@ -386,3 +388,4 @@ public:
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 <stdlib.h>
#include <vector>
#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<Token> scan(std::string input)
{
std::vector<Token> 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;
}
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