shitshow

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

commit 3ed7b5866ce740ea5f3a1eb538071fb555442f3e
parent c74364ec852af223291e4b3f79c62c0c67865f3c
Author: bain <bain@bain.cz>
Date:   Fri, 30 Apr 2021 12:12:11 +0200

Add printing of parsed output

Diffstat:
Mmain.cpp | 48+++++++++++++++++++++++++++++++++++++++++++++++-
Mparser/parser.cpp | 17+++++++++++------
Mparser/parser.h | 6++++--
3 files changed, 62 insertions(+), 9 deletions(-)

diff --git a/main.cpp b/main.cpp @@ -1,5 +1,49 @@ #include <iostream> #include "lexer/lexer.h" +#include "parser/parser.h" + +std::string tab(const int &deep) { + std::string o; + for (int i = 0; i < deep; i++) o+=" "; + return o; +} + +void print_expression(parser::elements::Expression* expression, int deep) { + using namespace parser; + std::cout << tab(deep); + for (parser::elements::ParserElement* e : expression->children) { + std::cout << e->type << " "; + } + std::cout << std::endl; +} + +void print_statement(parser::elements::Statement* statement, int deep) { + using namespace parser; + for (parser::elements::ParserElement* e : statement->children) { + switch (e->type) { + case parser::DECLARATION: { + auto* declaration = (elements::Declaration*)e; + std::cout << tab(deep) << "declaring " << declaration->name << " as " << declaration->data_type << std::endl; + break; + } + case parser::ASSIGNMENT: { + auto* assignment = (elements::Assignment*)e; + std::cout << tab(deep) << "assigning " << assignment->name << " to " << std::endl; + print_expression(&assignment->value, deep+4); + break; + } + default: + std::cout << tab(deep) << "doing " << e->type << std::endl; + } + } +} +void print_block(const parser::elements::Block &block, const int &deep) { + for (parser::elements::Statement* e : block.children) { + std::cout << tab(deep) << "statement begin" << std::endl; + print_statement(e, deep+4); + std::cout << tab(deep) << "statement end" << std::endl; + } +} int main() { lexer::Lexer lxr({ @@ -13,8 +57,10 @@ int main() { {.type=lexer::TokenType::NUMBER_LITERAL, .definition=R"([0-9](?:.[0-9])?)", .handler=lexer::HandlerType::REGEX}, {.type=lexer::TokenType::STRING_LITERAL, .handler=lexer::HandlerType::STRING} }); - std::vector<lexer::Token> out = lxr.tokenize("int i = 0; print i;", 0); + std::vector<lexer::Token> out = lxr.tokenize("int i = 0; print i;"); for (const lexer::Token& token : out) { std::cout << token.type << ": " << token.value << std::endl; } + parser::elements::Block parsed = parser::parse_block(out, 0); + print_block(parsed, 0); } diff --git a/parser/parser.cpp b/parser/parser.cpp @@ -23,7 +23,7 @@ void error(const std::string &msg, const std::string &linectx, const int &column std::string reconstruct_code(const std::vector<lexer::Token>& tokens) { std::string output; - for (const auto &token : tokens) output += token.value; + for (const auto &token : tokens) output += token.value + " "; return output; } @@ -35,23 +35,27 @@ parser::elements::Block parser::parse_block(const std::vector<lexer::Token> &tok while (consumed < token_stream.size()) { lexer::Token token = token_stream[consumed]; switch (token.type) { - case lexer::SEMICOLON: - block.children.push_back(parse_statement(statement_tokens)); + case lexer::SEMICOLON: { + elements::Statement st = parse_statement(statement_tokens); + block.children.push_back(&st); statement_tokens.clear(); break; + } case lexer::LEFT_BRACKET: brackets++; statement_tokens.push_back(token); break; case lexer::RIGHT_BRACKET: if (!brackets) { - block.children.push_back(parse_statement(statement_tokens)); + elements::Statement st = parse_statement(statement_tokens); + block.children.push_back(&st); statement_tokens.clear(); } break; default: statement_tokens.push_back(token); } + consumed++; } return block; } @@ -80,7 +84,7 @@ parser::elements::Statement parser::parse_statement(const std::vector<lexer::Tok .name = token.value, .value = exp, }; - statement.children.push_back(assignment); + statement.children.push_back(&assignment); break; } default: @@ -98,7 +102,7 @@ parser::elements::Statement parser::parse_statement(const std::vector<lexer::Tok .name = token_stream[consumed+1].value, .data_type = INT }; - statement.children.push_back(declaration); + statement.children.push_back(&declaration); if (token_stream.size() > consumed+2 && token_stream[consumed+2].type == lexer::ASSIGNMENT) continue; // continue to run the main loop to get the assignment } @@ -114,6 +118,7 @@ parser::elements::Statement parser::parse_statement(const std::vector<lexer::Tok } parser::elements::Expression parser::parse_expression(const std::vector<lexer::Token> &token_stream, int start_at) { + return parser::elements::Expression(); } diff --git a/parser/parser.h b/parser/parser.h @@ -25,14 +25,16 @@ namespace parser { struct ParserElement { ParserElementType type; }; struct Expression : ParserElement { ParserElementType type = EXPRESSION; - std::vector<ParserElement> children; + // TODO: memory leaks? + std::vector<ParserElement*> children; }; struct Statement : Expression { ParserElementType type = STATEMENT; }; struct Block : ParserElement { ParserElementType type = BLOCK; - std::vector<Statement> children; + // TODO: memory leaks? + std::vector<Statement*> children; }; struct Declaration : ParserElement { ParserElementType type = DECLARATION;