ast updated

This commit is contained in:
2026-05-17 16:30:49 +03:00
parent 208d34864e
commit 24750ddcbe
20 changed files with 194 additions and 38 deletions
+1
View File
@@ -0,0 +1 @@
build/
+8 -11
View File
@@ -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/
)
add_executable(mdtex_compiler ${SRC} main.cpp)
target_include_directories(mdtex_compiler PRIVATE ${CMAKE_SOURCE_DIR}/include)
+2 -2
View File
@@ -1,7 +1,7 @@
#pragma once
#include "Node.h"
#include "InlineNode.h"
#include "ast/Node.h"
// #include "ast/InlineNode/InlineNode.h"
#include <vector>
#include <string>
+8 -2
View File
@@ -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';
}
};
+11 -3
View File
@@ -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<std::unique_ptr<InlineNode>> 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<InlineNode> &child : this->children) {
child->repr(os, indent + 1);
}
}
};
+10 -3
View File
@@ -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<std::unique_ptr<ListItem>> 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<ListItem> &child : this->children) {
child->repr(os, indent + 1);
}
}
};
+10 -3
View File
@@ -1,11 +1,18 @@
#pragma once
#include "BlockNode.h"
#include "ast/BlockNode/BlockNode.h"
struct ListItem : public BlockNode {
std::vector<std::unique_ptr<InlineNode>> children;
std::vector<std::unique_ptr<BlockNode>> 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<BlockNode> &child : this->children) {
child->repr(os, indent + 1);
}
}
};
+10 -2
View File
@@ -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<std::unique_ptr<InlineNode>> 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<InlineNode> &child : children) {
child->repr(os, indent + 1);
}
}
};
+17
View File
@@ -0,0 +1,17 @@
#pragma once
#include <ast/BlockNode/BlockNode.h>
#include <iostream>
#include <vector>
#include <memory>
struct Document {
std::vector<std::unique_ptr<BlockNode>> children;
void repr(std::ostream &os) {
for (const std::unique_ptr<BlockNode> &child : children) {
child->repr(os, 0);
}
}
};
+9 -2
View File
@@ -1,11 +1,18 @@
#pragma once
#include "InlineNode.h"
#include "ast/InlineNode/InlineNode.h"
struct Bold : public InlineNode {
std::vector<std::unique_ptr<InlineNode>> 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<InlineNode> &child : children) {
child->repr(os, indent + 1);
}
}
};
+6 -2
View File
@@ -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';
}
};
+1 -1
View File
@@ -1,6 +1,6 @@
#pragma once
#include "Node.h"
#include "ast/Node.h"
struct InlineNode : public Node {
virtual ~InlineNode() = default;
+9 -2
View File
@@ -1,11 +1,18 @@
#pragma once
#include "InlineNode.h"
#include "ast/InlineNode/InlineNode.h"
struct Italic : public InlineNode {
std::vector<std::unique_ptr<InlineNode>> 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<InlineNode> &child : this->children) {
child->repr(os, indent + 1);
}
}
};
+10 -2
View File
@@ -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<std::unique_ptr<InlineNode>> 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<InlineNode> &child : this->children) {
child->repr(os, indent + 1);
}
}
};
+7 -2
View File
@@ -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';
}
};
+4
View File
@@ -1,10 +1,14 @@
#pragma once
#include <iostream>
#include <memory>
#include <vector>
#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;
};
+33 -1
View File
@@ -1,5 +1,8 @@
#pragma once
#include <unordered_map>
#include <string>
enum class NodeKind {
CodeBlock,
Header,
@@ -12,4 +15,33 @@ enum class NodeKind {
Italic,
Link,
Text
};
};
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 "";
}
}
+11
View File
@@ -0,0 +1,11 @@
#pragma once
#include <string>
std::string getIndent(int indentSize) {
std::string indents;
for (int i = 0; i < indentSize; ++i) {
indents += " ";
}
return indents;
}
+20
View File
@@ -0,0 +1,20 @@
#pragma once
#include <string>
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;
}
+7
View File
@@ -0,0 +1,7 @@
#include <stdio.h>
int main(int argc, char** argv) {
printf("%d", argc);
(void)argv;
return 0;
}