shitshow

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

main.cpp (2573B)


      1 #include <iostream>
      2 #include "lexer/lexer.h"
      3 #include "parser/parser.h"
      4 #include "parser/print_stuff.h"
      5 
      6 int main() {
      7     lexer::Lexer lxr({
      8                              {.type=lexer::TokenType::SEMICOLON, .definition=";", .handler=lexer::HandlerType::SINGLE_CHAR},
      9                              {.type=lexer::TokenType::EQ, .definition="==", .handler=lexer::HandlerType::MULTI_CHAR},
     10                              {.type=lexer::TokenType::ASSIGNMENT, .definition="=", .handler=lexer::HandlerType::SINGLE_CHAR},
     11                              {.type=lexer::TokenType::LEFT_PARENT, .definition="(", .handler=lexer::HandlerType::SINGLE_CHAR},
     12                              {.type=lexer::TokenType::RIGHT_PARENT, .definition=")", .handler=lexer::HandlerType::SINGLE_CHAR},
     13                              {.type=lexer::TokenType::LEFT_BRACKET, .definition="{", .handler=lexer::HandlerType::SINGLE_CHAR},
     14                              {.type=lexer::TokenType::RIGHT_BRACKET, .definition="}", .handler=lexer::HandlerType::SINGLE_CHAR},
     15                              {.type=lexer::TokenType::ARITHMETIC, .definition=R"(\+|\-|\*|\/)", .handler=lexer::HandlerType::REGEX},
     16                              {.type=lexer::TokenType::NAME, .definition=R"([A-Za-z_](?:[\w]+)?)", .handler=lexer::HandlerType::REGEX},
     17                              {.type=lexer::TokenType::NUMBER_LITERAL, .definition=R"([0-9]+)", .handler=lexer::HandlerType::REGEX},
     18                              {.type=lexer::TokenType::STRING_LITERAL, .handler=lexer::HandlerType::STRING}
     19                      });
     20     lxr.reserved = {
     21             {lexer::TokenType::TYPE,  {"int", "char"}},
     22             {lexer::TokenType::PRINT, {"print"}},
     23             {lexer::TokenType::IF,    {"if"}},
     24             {lexer::TokenType::WHILE,    {"while"}}
     25     };
     26     std::vector<lexer::Token> out = lxr.tokenize(
     27             "int integer = 120 + 2 - 1/2; print asdfa; if (integer == 2) {while (1) print \"hii\";}");
     28     lxr.convert_reserved(out);
     29     for (const lexer::Token &token : out) {
     30         std::cout << token.value << " ";
     31     }
     32     std::cout << std::endl;
     33     int offset = 0;
     34     for (const lexer::Token &token : out) {
     35         std::cout << parser_print::tab(offset) << token.type;
     36         int num = token.type;
     37         int digits = num == 0;
     38         while (num > 0) {
     39             ++digits;
     40             num = num / 10;
     41         }
     42         offset = token.value.length() - digits + 1;
     43     }
     44     std::cout << std::endl << std::endl;
     45     parser::elements::Block *parsed = parser::parse_block(out, 0);
     46     parser_print::print_block(parsed, 0);
     47 }