shitshow

A shitty programming language
git clone git://git.bain.cz/shitshow.git
Log | Files | Refs

commit 4b8d1cecbde28562f062eb67b594bdbb723c58c7
parent 7f952e501defa8e5814de456b865ca10cf40abe3
Author: bain3 <31798786+bain3@users.noreply.github.com>
Date:   Mon,  3 May 2021 13:15:46 +0200

Reformat

Diffstat:
Mlexer/lexer.cpp | 11++++++++---
Mlexer/lexer.h | 16++++++++++++----
Mmain.cpp | 49+++++++++++++++++++++++++++++--------------------
Mparser/parser.cpp | 8++++++--
4 files changed, 55 insertions(+), 29 deletions(-)

diff --git a/lexer/lexer.cpp b/lexer/lexer.cpp @@ -9,7 +9,8 @@ lexer::Lexer::Lexer(std::vector<GrammarRule> grammar_rules) { this->rules = std::move(grammar_rules); } -std::vector<lexer::Token> lexer::Lexer::tokenize_line(const std::string& line) { // NOLINT(readability-convert-member-functions-to-static) +std::vector<lexer::Token> +lexer::Lexer::tokenize_line(const std::string &line) { // NOLINT(readability-convert-member-functions-to-static) return std::vector<lexer::Token>(); } @@ -20,9 +21,13 @@ std::vector<lexer::Token> lexer::Lexer::tokenize(const std::string &part) { int total_consumed = 0; while (!cpy.empty()) { int consumed = 0; - if (cpy[0] == ' ') {total_consumed++; cpy.erase(0, 1); continue;} // skip whitespace + if (cpy[0] == ' ') { + total_consumed++; + cpy.erase(0, 1); + continue; + } // skip whitespace Token token{}; - for (const GrammarRule& rule : rules) { + for (const GrammarRule &rule : rules) { switch (rule.handler) { case SINGLE_CHAR: consumed = handlers::single_char(rule, token, cpy); diff --git a/lexer/lexer.h b/lexer/lexer.h @@ -39,20 +39,28 @@ namespace lexer { TokenType type; std::vector<std::string> reserved_names; }; + class Lexer { std::vector<GrammarRule> rules; public: std::vector<ReservedToken> reserved; + explicit Lexer(std::vector<GrammarRule> grammar_rules); - std::vector<Token> tokenize_line(const std::string& line); + + std::vector<Token> tokenize_line(const std::string &line); + std::vector<Token> tokenize(const std::string &part); + void convert_reserved(std::vector<Token> &tokens); }; namespace handlers { - int single_char (const GrammarRule &rule, Token &token, const std::string &input); - int multi_char (const GrammarRule &rule, Token &token, const std::string &input); - int regex (const GrammarRule &rule, Token &token, const std::string &input); + int single_char(const GrammarRule &rule, Token &token, const std::string &input); + + int multi_char(const GrammarRule &rule, Token &token, const std::string &input); + + int regex(const GrammarRule &rule, Token &token, const std::string &input); + int string_handler(const GrammarRule &rule, Token &token, const std::string &input); } } diff --git a/main.cpp b/main.cpp @@ -2,7 +2,7 @@ #include "lexer/lexer.h" #include "parser/parser.h" -void print_block(parser::elements::Block* block, const int &deep); +void print_block(parser::elements::Block *block, const int &deep); std::string tab(const int &deep) { std::string o; @@ -12,7 +12,7 @@ std::string tab(const int &deep) { void print_name_or_const(parser::elements::ParserElement *el, int deep) { if (el->type == parser::ParserElementType::CONST_DEFINE) { - auto *constant = (parser::elements::ConstDefine *)el; + auto *constant = (parser::elements::ConstDefine *) el; std::cout << tab(deep) << "constant " << (constant->data_type == parser::DataType::INT ? "int: " : "string: ") << constant->value << std::endl; @@ -27,33 +27,41 @@ void print_name_or_const(parser::elements::ParserElement *el, int deep) { void print_operation(parser::elements::Arithmetic *arithmetic, int deep) { std::cout << tab(deep); switch (arithmetic->specific_type) { - case parser::ADD: std::cout << "addition"; break; - case parser::SUBTRACT: std::cout << "subtraction"; break; - case parser::DIVIDE: std::cout << "division"; break; - case parser::MULTIPLY: std::cout << "multiplication"; break; + case parser::ADD: + std::cout << "addition"; + break; + case parser::SUBTRACT: + std::cout << "subtraction"; + break; + case parser::DIVIDE: + std::cout << "division"; + break; + case parser::MULTIPLY: + std::cout << "multiplication"; + break; } std::cout << std::endl; switch (arithmetic->left->type) { case parser::CONST_DEFINE: case parser::NAME: - print_name_or_const(arithmetic->left.get(), deep+4); + print_name_or_const(arithmetic->left.get(), deep + 4); break; case parser::ARITHMETIC: - print_operation((parser::elements::Arithmetic*)arithmetic->left.get(), deep+4); + print_operation((parser::elements::Arithmetic *) arithmetic->left.get(), deep + 4); break; default: - std::cout << tab(deep+4) << "element of type " << arithmetic->left->type << std::endl; + std::cout << tab(deep + 4) << "element of type " << arithmetic->left->type << std::endl; } switch (arithmetic->right->type) { case parser::CONST_DEFINE: case parser::NAME: - print_name_or_const(arithmetic->right.get(), deep+4); + print_name_or_const(arithmetic->right.get(), deep + 4); break; case parser::ARITHMETIC: - print_operation((parser::elements::Arithmetic*)arithmetic->right.get(), deep+4); + print_operation((parser::elements::Arithmetic *) arithmetic->right.get(), deep + 4); break; default: - std::cout << tab(deep+4) << "element of type " << arithmetic->right->type << std::endl; + std::cout << tab(deep + 4) << "element of type " << arithmetic->right->type << std::endl; } } @@ -87,7 +95,7 @@ void print_statement(const std::unique_ptr<parser::elements::Statement> &stateme for (std::unique_ptr<elements::ParserElement> &e : statement->children) { switch (e->type) { case parser::BLOCK: { - auto *block = (elements::Block *)e.get(); + auto *block = (elements::Block *) e.get(); print_block(block, deep); break; } @@ -118,9 +126,9 @@ void print_statement(const std::unique_ptr<parser::elements::Statement> &stateme break; } case parser::IF: { - auto *if_ = (elements::If *)e.get(); + auto *if_ = (elements::If *) e.get(); std::cout << tab(deep) << "if condition" << std::endl; - print_expression(if_->expression, deep+4); + print_expression(if_->expression, deep + 4); std::cout << tab(deep) << "end condition" << std::endl; break; } @@ -130,7 +138,7 @@ void print_statement(const std::unique_ptr<parser::elements::Statement> &stateme } } -void print_block(parser::elements::Block* block, const int &deep) { +void print_block(parser::elements::Block *block, const int &deep) { std::cout << tab(deep) << "block begin" << std::endl; for (const std::unique_ptr<parser::elements::Statement> &e : block->children) { std::cout << tab(deep + 4) << "statement begin" << std::endl; @@ -159,7 +167,8 @@ int main() { {lexer::TokenType::PRINT, {"print"}}, {lexer::TokenType::IF, {"if"}} }; - std::vector<lexer::Token> out = lxr.tokenize("int integer = 120 + 2 - 1/2; print asdfa; if (integer == 2) {print \"hii\";}"); + std::vector<lexer::Token> out = lxr.tokenize( + "int integer = 120 + 2 - 1/2; print asdfa; if (integer == 2) {print \"hii\";}"); lxr.convert_reserved(out); for (const lexer::Token &token : out) { std::cout << token.value << " "; @@ -169,14 +178,14 @@ int main() { for (const lexer::Token &token : out) { std::cout << tab(offset) << token.type; int num = token.type; - int digits = num==0; + int digits = num == 0; while (num > 0) { ++digits; num = num / 10; } - offset = token.value.length()-digits+1; + offset = token.value.length() - digits + 1; } std::cout << std::endl << std::endl; - parser::elements::Block* parsed = parser::parse_block(out, 0); + parser::elements::Block *parsed = parser::parse_block(out, 0); print_block(parsed, 0); } diff --git a/parser/parser.cpp b/parser/parser.cpp @@ -224,7 +224,9 @@ parser::parse_expression(const std::vector<lexer::Token> &token_stream, int star i != expression->children.end(); i++) { // NOLINT(cppcoreguidelines-narrowing-conversions) if ((*i)->type == ARITHMETIC) { auto *c = (elements::Arithmetic *) i->get(); - if (c->left != nullptr || c->right != nullptr || (c->specific_type != MULTIPLY && c->specific_type != DIVIDE)) continue; + if (c->left != nullptr || c->right != nullptr || + (c->specific_type != MULTIPLY && c->specific_type != DIVIDE)) + continue; if (std::distance(begin, i) <= 0 || std::distance(end, i) <= 0) {// || std::distance(end, i) >= 0) { std::cout << std::distance(end, i) << std::endl; error("ROP1: Missing operands!", reconstruct_code(token_stream)); @@ -251,7 +253,9 @@ parser::parse_expression(const std::vector<lexer::Token> &token_stream, int star i != expression->children.end(); i++) { // NOLINT(cppcoreguidelines-narrowing-conversions) if ((*i)->type == ARITHMETIC) { auto *c = (elements::Arithmetic *) i->get(); - if (c->left != nullptr || c->right != nullptr || (c->specific_type != ADD && c->specific_type != SUBTRACT)) continue; + if (c->left != nullptr || c->right != nullptr || + (c->specific_type != ADD && c->specific_type != SUBTRACT)) + continue; if (std::distance(begin, i) <= 0 || std::distance(end, i) <= 0) {// || std::distance(end, i) >= 0) { std::cout << std::distance(end, i) << std::endl; error("ROP1: Missing operands!", reconstruct_code(token_stream));