diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 126acc5..91f6799 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,17 +1,14 @@ -cmake_minimum_required(VERSION 3.25.1) -project(mdtex-compiler) +cmake_minimum_required(VERSION 3.25) + +project(mdtex_compiler) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_CXX_FLAGS "-g -Wall -Wextra -Werror -pedantic -fsanitize=address") -file(GLOB SRC source/*.cpp) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -Wextra -Wpedantic -fsanitize=address") -add_executable(mdtex-compiler - ${SRC} - main.cpp -) +file(GLOB SRC CONFIGURE_DEPENDS source/*.cpp) -include_directories( - ./include/ -) \ No newline at end of file +add_executable(mdtex_compiler ${SRC} main.cpp) + +target_include_directories(mdtex_compiler PRIVATE ${CMAKE_SOURCE_DIR}/include) \ No newline at end of file diff --git a/include/ast/BlockNode/BlockNode.h b/include/ast/BlockNode/BlockNode.h index 38a3b5b..84b810e 100644 --- a/include/ast/BlockNode/BlockNode.h +++ b/include/ast/BlockNode/BlockNode.h @@ -1,7 +1,7 @@ #pragma once -#include "Node.h" -#include "InlineNode.h" +#include "ast/Node.h" +// #include "ast/InlineNode/InlineNode.h" #include #include diff --git a/include/ast/BlockNode/CodeBlock.h b/include/ast/BlockNode/CodeBlock.h index 3848dcb..0dfcaf0 100644 --- a/include/ast/BlockNode/CodeBlock.h +++ b/include/ast/BlockNode/CodeBlock.h @@ -1,11 +1,17 @@ #pragma once -#include "BlockNode.h" +#include "ast/BlockNode/BlockNode.h" +#include "utils/shorten.hpp" struct CodeBlock : public BlockNode { + std::string language; std::string code; - NodeKind getKind() const { + NodeKind getKind() const override { return NodeKind::CodeBlock; } + + void repr(std::ostream &os, int indent = 0) const override { + os << getIndent(indent) << toString(this->getKind()) << " lang: " << this->language << " code: " << shorten(this->code) << '\n'; + } }; \ No newline at end of file diff --git a/include/ast/BlockNode/Header.h b/include/ast/BlockNode/Header.h index 1f055d7..e696b38 100644 --- a/include/ast/BlockNode/Header.h +++ b/include/ast/BlockNode/Header.h @@ -1,12 +1,20 @@ #pragma once -#include "BlockNode.h" +#include "ast/BlockNode/BlockNode.h" +#include "ast/InlineNode/InlineNode.h" struct Header : public BlockNode { - std::string headerText; + int level = 1; std::vector> children; - NodeKind getKind() const { + NodeKind getKind() const override { return NodeKind::Header; } + + void repr(std::ostream &os, int indent = 0) const override { + os << getIndent(indent) << toString(this->getKind()) << " level: " << this->level << '\n'; + for (const std::unique_ptr &child : this->children) { + child->repr(os, indent + 1); + } + } }; \ No newline at end of file diff --git a/include/ast/BlockNode/List.h b/include/ast/BlockNode/List.h index 1200c5f..5d16f1d 100644 --- a/include/ast/BlockNode/List.h +++ b/include/ast/BlockNode/List.h @@ -1,12 +1,19 @@ #pragma once -#include "BlockNode.h" -#include "ListItem.h" +#include "ast/BlockNode/BlockNode.h" +#include "ast/BlockNode/ListItem.h" struct List : public BlockNode { std::vector> children; - NodeKind getKind() const { + NodeKind getKind() const override { return NodeKind::List; } + + void repr(std::ostream &os, int indent) const override { + os << getIndent(indent) << toString(getKind()) << '\n'; + for (const std::unique_ptr &child : this->children) { + child->repr(os, indent + 1); + } + } }; \ No newline at end of file diff --git a/include/ast/BlockNode/ListItem.h b/include/ast/BlockNode/ListItem.h index 9779d34..ee4e2d6 100644 --- a/include/ast/BlockNode/ListItem.h +++ b/include/ast/BlockNode/ListItem.h @@ -1,11 +1,18 @@ #pragma once -#include "BlockNode.h" +#include "ast/BlockNode/BlockNode.h" struct ListItem : public BlockNode { - std::vector> children; + std::vector> children; - NodeKind getKind() const { + NodeKind getKind() const override { return NodeKind::ListItem; } + + void repr(std::ostream &os, int indent) const override { + os << getIndent(indent) << toString(getKind()) << '\n'; + for (const std::unique_ptr &child : this->children) { + child->repr(os, indent + 1); + } + } }; \ No newline at end of file diff --git a/include/ast/BlockNode/Paragraph.h b/include/ast/BlockNode/Paragraph.h index 868a127..2d7ba18 100644 --- a/include/ast/BlockNode/Paragraph.h +++ b/include/ast/BlockNode/Paragraph.h @@ -1,11 +1,19 @@ #pragma once -#include "BlockNode.h" +#include "ast/BlockNode/BlockNode.h" +#include "ast/InlineNode/InlineNode.h" struct Paragraph : public BlockNode { std::vector> children; - NodeKind getKind() const { + NodeKind getKind() const override { return NodeKind::Paragraph; } + + void repr(std::ostream &os, int indent) const override { + os << getIndent(indent) << toString(getKind()) << '\n'; + for (const std::unique_ptr &child : children) { + child->repr(os, indent + 1); + } + } }; \ No newline at end of file diff --git a/include/ast/Document.h b/include/ast/Document.h new file mode 100644 index 0000000..18b3c21 --- /dev/null +++ b/include/ast/Document.h @@ -0,0 +1,17 @@ +#pragma once + +#include + +#include +#include +#include + +struct Document { + std::vector> children; + + void repr(std::ostream &os) { + for (const std::unique_ptr &child : children) { + child->repr(os, 0); + } + } +}; \ No newline at end of file diff --git a/include/ast/InlineNode/Bold.h b/include/ast/InlineNode/Bold.h index 45d6d74..9c89699 100644 --- a/include/ast/InlineNode/Bold.h +++ b/include/ast/InlineNode/Bold.h @@ -1,11 +1,18 @@ #pragma once -#include "InlineNode.h" +#include "ast/InlineNode/InlineNode.h" struct Bold : public InlineNode { std::vector> children; - NodeKind getKind() const { + NodeKind getKind() const override { return NodeKind::Bold; } + + void repr(std::ostream &os, int indent) const override { + os << getIndent(indent) << toString(getKind()) << '\n'; + for (const std::unique_ptr &child : children) { + child->repr(os, indent + 1); + } + } }; \ No newline at end of file diff --git a/include/ast/InlineNode/InlineCode.h b/include/ast/InlineNode/InlineCode.h index 1f7568e..62fc4f9 100644 --- a/include/ast/InlineNode/InlineCode.h +++ b/include/ast/InlineNode/InlineCode.h @@ -1,11 +1,15 @@ #pragma once -#include "InlineNode.h" +#include "ast/InlineNode/InlineNode.h" struct InlineCode : public InlineNode { std::string inlineCodeString; - NodeKind getKind() const { + NodeKind getKind() const override { return NodeKind::InlineCode; } + + void repr(std::ostream &os, int indent) const override { + os << getIndent(indent) << toString(getKind()) << " inline code: " << this->inlineCodeString << '\n'; + } }; \ No newline at end of file diff --git a/include/ast/InlineNode/InlineNode.h b/include/ast/InlineNode/InlineNode.h index abe86f3..12d775a 100644 --- a/include/ast/InlineNode/InlineNode.h +++ b/include/ast/InlineNode/InlineNode.h @@ -1,6 +1,6 @@ #pragma once -#include "Node.h" +#include "ast/Node.h" struct InlineNode : public Node { virtual ~InlineNode() = default; diff --git a/include/ast/InlineNode/Italic.h b/include/ast/InlineNode/Italic.h index 7dbc762..6311ad3 100644 --- a/include/ast/InlineNode/Italic.h +++ b/include/ast/InlineNode/Italic.h @@ -1,11 +1,18 @@ #pragma once -#include "InlineNode.h" +#include "ast/InlineNode/InlineNode.h" struct Italic : public InlineNode { std::vector> children; - NodeKind getKind() const { + NodeKind getKind() const override { return NodeKind::Italic; } + + void repr(std::ostream &os, int indent) const override { + os << getIndent(indent) << toString(this->getKind()) << '\n'; + for (const std::unique_ptr &child : this->children) { + child->repr(os, indent + 1); + } + } }; \ No newline at end of file diff --git a/include/ast/InlineNode/Link.h b/include/ast/InlineNode/Link.h index 8ef3943..9a83662 100644 --- a/include/ast/InlineNode/Link.h +++ b/include/ast/InlineNode/Link.h @@ -1,12 +1,20 @@ #pragma once -#include "InlineNode.h" +#include "ast/InlineNode/InlineNode.h" +#include "utils/shorten.hpp" struct Link : public InlineNode { std::string url; std::vector> children; - NodeKind getKind() const { + NodeKind getKind() const override { return NodeKind::Link; } + + void repr(std::ostream &os, int indent) const override { + os << getIndent(indent) << toString(this->getKind()) << " link: " << shorten(this->url) << '\n'; + for (const std::unique_ptr &child : this->children) { + child->repr(os, indent + 1); + } + } }; \ No newline at end of file diff --git a/include/ast/InlineNode/Text.h b/include/ast/InlineNode/Text.h index b1c6342..8980e7d 100644 --- a/include/ast/InlineNode/Text.h +++ b/include/ast/InlineNode/Text.h @@ -1,11 +1,16 @@ #pragma once -#include "InlineNode.h" +#include "ast/InlineNode/InlineNode.h" +#include "utils/shorten.hpp" struct Text : public InlineNode { std::string text; - NodeKind getKind() const { + NodeKind getKind() const override { return NodeKind::Text; } + + void repr(std::ostream &os, int indent) const override { + os << getIndent(indent) << toString(this->getKind()) << " text: " << shorten(this->text) << '\n'; + } }; \ No newline at end of file diff --git a/include/ast/Node.h b/include/ast/Node.h index 1bc30c5..e545661 100644 --- a/include/ast/Node.h +++ b/include/ast/Node.h @@ -1,10 +1,14 @@ #pragma once +#include #include #include #include "NodeKind.h" +#include "utils/getIndent.hpp" struct Node { virtual ~Node() = default; + virtual NodeKind getKind() const = 0; + virtual void repr(std::ostream& os, int indent = 0) const = 0; }; \ No newline at end of file diff --git a/include/ast/NodeKind.h b/include/ast/NodeKind.h index e57c9fe..9cde75a 100644 --- a/include/ast/NodeKind.h +++ b/include/ast/NodeKind.h @@ -1,5 +1,8 @@ #pragma once +#include +#include + enum class NodeKind { CodeBlock, Header, @@ -12,4 +15,33 @@ enum class NodeKind { Italic, Link, Text -}; \ No newline at end of file +}; + +const std::string toString(NodeKind kind) { + switch (kind) { + case NodeKind::CodeBlock: + return "CodeBlock"; + case NodeKind::Header: + return "Header"; + case NodeKind::List: + return "List"; + case NodeKind::ListItem: + return "ListItem"; + case NodeKind::Paragraph: + return "Paragraph"; + + case NodeKind::Bold: + return "Bold"; + case NodeKind::InlineCode: + return "InlineCode"; + case NodeKind::Italic: + return "Italic"; + case NodeKind::Link: + return "Link"; + case NodeKind::Text: + return "Text"; + + default: + return ""; + } +} \ No newline at end of file diff --git a/include/utils/getIndent.hpp b/include/utils/getIndent.hpp new file mode 100644 index 0000000..7537a0a --- /dev/null +++ b/include/utils/getIndent.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include + +std::string getIndent(int indentSize) { + std::string indents; + for (int i = 0; i < indentSize; ++i) { + indents += " "; + } + return indents; +} \ No newline at end of file diff --git a/include/utils/shorten.hpp b/include/utils/shorten.hpp new file mode 100644 index 0000000..98eb1be --- /dev/null +++ b/include/utils/shorten.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include + +std::string shorten(const std::string &string) { + if (string.size() > 9) { + std::string shortString; + for (int i = 0; i < 5; ++i) { + shortString.push_back(string[i]); + } + for (int i = 0; i < 3; ++i) { + shortString.push_back('.'); + } + for (int i = string.size() - 6; i < string.size(); ++i) { + shortString.push_back(string[i]); + } + return shortString; + } + return string; +} \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..2526683 --- /dev/null +++ b/main.cpp @@ -0,0 +1,7 @@ +#include + +int main(int argc, char** argv) { + printf("%d", argc); + (void)argv; + return 0; +} \ No newline at end of file