Initial commit

This commit is contained in:
2025-10-15 17:20:56 +02:00
commit 2e55268805
11 changed files with 281 additions and 0 deletions

9
CMakeLists.txt Normal file
View File

@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.14)
project(Interpreter)
set(CXX_STANDARD 20)
add_subdirectory(Parser)
add_subdirectory(Program)

9
Parser/CMakeLists.txt Normal file
View 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}/")

View 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;
}
};
}

View File

@@ -0,0 +1,3 @@
namespace Parser{
struct Expression{};
}

View 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
View 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
View 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
View 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
View 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();
};
};

3
Program/CMakeLists.txt Normal file
View File

@@ -0,0 +1,3 @@
add_executable(Interpreter main.cpp)
target_link_libraries(Interpreter PRIVATE Parser)

10
Program/main.cpp Normal file
View File

@@ -0,0 +1,10 @@
#include <iostream>
#include "Parser.h"
int main() {
Parser::Parser parser;
std::string input = "123 + 5432 - 33 / 434 (asd)";
parser.parse(input);
}