Initial commit
This commit is contained in:
9
Parser/CMakeLists.txt
Normal file
9
Parser/CMakeLists.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
add_library(
|
||||
Parser
|
||||
Parser.h
|
||||
Parser.cpp
|
||||
Lexer.h
|
||||
Lexer.cpp
|
||||
)
|
||||
|
||||
target_include_directories(Parser PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/")
|
||||
20
Parser/Expression/Binary.h
Normal file
20
Parser/Expression/Binary.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#include "Expression.h"
|
||||
#include "Lexer.h"
|
||||
#include <memory>
|
||||
|
||||
namespace Parser
|
||||
{
|
||||
struct Binary : public Expression
|
||||
{
|
||||
std::unique_ptr<Expression> left;
|
||||
std::unique_ptr<Expression> right;
|
||||
Lexer::Token::TokenType op;
|
||||
|
||||
Binary(std::unique_ptr<Expression>&& left, std::unique_ptr<Expression>&& right, Lexer::Token::TokenType op) : Expression()
|
||||
{
|
||||
this->left = std::move(left);
|
||||
this->right = std::move(right);
|
||||
this->op = op;
|
||||
}
|
||||
};
|
||||
}
|
||||
3
Parser/Expression/Expression.h
Normal file
3
Parser/Expression/Expression.h
Normal file
@@ -0,0 +1,3 @@
|
||||
namespace Parser{
|
||||
struct Expression{};
|
||||
}
|
||||
12
Parser/Expression/Grouping.h
Normal file
12
Parser/Expression/Grouping.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#include "Expression.h"
|
||||
#include <memory>
|
||||
|
||||
namespace Parser
|
||||
{
|
||||
struct Grouping : public Expression{
|
||||
std::unique_ptr<Expression> expr;
|
||||
Grouping(std::unique_ptr<Expression>&& expr) : Expression(){
|
||||
this->expr = std::move(expr);
|
||||
};
|
||||
};
|
||||
}
|
||||
122
Parser/Lexer.cpp
Normal file
122
Parser/Lexer.cpp
Normal file
@@ -0,0 +1,122 @@
|
||||
#include "Lexer.h"
|
||||
#include <iostream>
|
||||
|
||||
namespace Parser
|
||||
{
|
||||
using Parser::Lexer;
|
||||
std::list<Lexer::Token> Lexer::tokenize(const std::string_view &text)
|
||||
{
|
||||
tokens.clear();
|
||||
this->text = text;
|
||||
|
||||
while (current != text.length())
|
||||
{
|
||||
if (text.at(current) == ' ')
|
||||
{
|
||||
advance();
|
||||
continue;
|
||||
}
|
||||
if (text.at(current) == '\n')
|
||||
{
|
||||
advance();
|
||||
line++;
|
||||
continue;
|
||||
}
|
||||
|
||||
start = current;
|
||||
|
||||
switch (Lexer::peek())
|
||||
{
|
||||
case '+':
|
||||
advance();
|
||||
token(Token::PLUS);
|
||||
break;
|
||||
case '-':
|
||||
advance();
|
||||
token(Token::MINUS);
|
||||
break;
|
||||
case '*':
|
||||
advance();
|
||||
token(Token::MULTIPLY);
|
||||
break;
|
||||
case '/':
|
||||
advance();
|
||||
token(Token::DIVIDE);
|
||||
break;
|
||||
case '=':
|
||||
advance();
|
||||
token(Token::ASSIGNMENT);
|
||||
break;
|
||||
case '(':
|
||||
advance();
|
||||
token(Token::BRACKET_LEFT);
|
||||
break;
|
||||
case ')':
|
||||
advance();
|
||||
token(Token::BRACKET_RIGHT);
|
||||
break;
|
||||
|
||||
default:
|
||||
if (isDigit(peek()))
|
||||
{
|
||||
advance();
|
||||
number();
|
||||
}
|
||||
|
||||
if (isAlpha(peek()))
|
||||
{
|
||||
advance();
|
||||
identifier();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return tokens;
|
||||
}
|
||||
void Lexer::advance()
|
||||
{
|
||||
current++;
|
||||
}
|
||||
|
||||
char Lexer::peek()
|
||||
{
|
||||
if (current == text.length())
|
||||
return 0;
|
||||
return text.at(current);
|
||||
}
|
||||
|
||||
void Lexer::token(const Token::TokenType type)
|
||||
{
|
||||
tokens.emplace_back(Token(type, line, text.substr(start, (current - start))));
|
||||
}
|
||||
|
||||
bool Lexer::isDigit(const char &c)
|
||||
{
|
||||
return c >= '0' && c <= '9';
|
||||
}
|
||||
|
||||
void Lexer::number()
|
||||
{
|
||||
while (isDigit(peek()))
|
||||
{
|
||||
advance();
|
||||
}
|
||||
|
||||
token(Token::INT);
|
||||
}
|
||||
|
||||
bool Lexer::isAlpha(const char &c)
|
||||
{
|
||||
return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
|
||||
}
|
||||
|
||||
void Lexer::identifier()
|
||||
{
|
||||
while (isAlpha(peek()) || isDigit(peek()))
|
||||
{
|
||||
advance();
|
||||
}
|
||||
|
||||
token(Token::IDENTIFIER);
|
||||
}
|
||||
}
|
||||
66
Parser/Lexer.h
Normal file
66
Parser/Lexer.h
Normal file
@@ -0,0 +1,66 @@
|
||||
#pragma once
|
||||
#include <list>
|
||||
#include <string_view>
|
||||
#include <ranges>
|
||||
#include <sys/types.h>
|
||||
#include <string>
|
||||
|
||||
namespace Parser
|
||||
{
|
||||
class Lexer
|
||||
{
|
||||
public:
|
||||
struct Token
|
||||
{
|
||||
enum TokenType
|
||||
{
|
||||
INT,
|
||||
PLUS,
|
||||
MINUS,
|
||||
MULTIPLY,
|
||||
DIVIDE,
|
||||
ASSIGNMENT,
|
||||
BRACKET_LEFT,
|
||||
BRACKET_RIGHT,
|
||||
IDENTIFIER
|
||||
};
|
||||
|
||||
TokenType type;
|
||||
uint line;
|
||||
std::string_view occurence;
|
||||
|
||||
Token(TokenType type, uint line, std::string_view&& str)
|
||||
{
|
||||
this->type = type;
|
||||
this->line = line;
|
||||
occurence = str;
|
||||
}
|
||||
|
||||
std::string toString(){
|
||||
return std::to_string(type) + " " + std::string(occurence);
|
||||
}
|
||||
};
|
||||
|
||||
Lexer() = default;
|
||||
std::list<Lexer::Token> tokenize(const std::string_view &text);
|
||||
|
||||
private:
|
||||
uint current{};
|
||||
uint start{};
|
||||
uint line{};
|
||||
|
||||
std::string_view text;
|
||||
std::list<Token> tokens;
|
||||
|
||||
|
||||
|
||||
private:
|
||||
void identifier();
|
||||
bool isAlpha(const char& c);
|
||||
bool isDigit(const char& c);
|
||||
void number();
|
||||
void advance();
|
||||
char peek();
|
||||
void token(const Token::TokenType type);
|
||||
};
|
||||
}
|
||||
9
Parser/Parser.cpp
Normal file
9
Parser/Parser.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "Parser.h"
|
||||
#include <iostream>
|
||||
|
||||
namespace Parser{
|
||||
void Parser::parse(std::string_view source) {
|
||||
lexer.tokenize(source);
|
||||
|
||||
}
|
||||
}
|
||||
18
Parser/Parser.h
Normal file
18
Parser/Parser.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
#include "Lexer.h"
|
||||
#include "Expression/Expression.h"
|
||||
|
||||
namespace Parser
|
||||
{
|
||||
class Parser
|
||||
{
|
||||
Lexer lexer;
|
||||
std::string_view source;
|
||||
std::list<Expression> exprs;
|
||||
public:
|
||||
Parser() = default;
|
||||
void parse(std::string_view source);
|
||||
private:
|
||||
generateAST();
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user