print + build partially tested

This commit is contained in:
ProgramSnail 2023-04-02 15:10:32 +03:00
parent 0d62ae0814
commit 15e36c203a
22 changed files with 179 additions and 116 deletions

View file

@ -8,7 +8,7 @@ namespace interpreter {
class BuildVisitor : public Visitor {
public:
BuildVisitor(const parser::ParseTree& parse_tree) : parse_tree_(parse_tree) {}
explicit BuildVisitor(const parser::ParseTree& parse_tree) : parse_tree_(parse_tree) {}
void VisitSourceFile(SourceFile* source_file) override {
current_node_ = parse_tree_.GetRoot();
@ -23,7 +23,7 @@ private:
void Visit(SourceFile* node) override;
void Visit(Sources* node) override;
// Namespaces, partittions -----------------
// Namespaces, partitions -----------------
void Visit(Partition* node) override;
void Visit(Namespace* node) override;

View file

@ -10,9 +10,7 @@
#include "node.hpp"
namespace interpreter {
namespace tokens {
namespace interpreter::tokens {
// ----------------- Declarations -----------------
@ -30,7 +28,7 @@ using TypeclassIdentifier = std::string;
struct SourceFile;
struct Sources;
// Namespaces, partittions -----------------
// Namespaces, partitions -----------------
struct Partition;
struct Namespace;
@ -101,7 +99,7 @@ struct Block;
//
struct NameExpression;
struct NameSuperExpression;
struct ScopedStatement;
enum class LoopControlExpression {
@ -110,7 +108,7 @@ enum class LoopControlExpression {
};
using SubExpressionToken = std::variant<
std::unique_ptr<NameExpression>,
std::unique_ptr<NameSuperExpression>,
std::unique_ptr<ScopedStatement>>;
//
@ -576,6 +574,4 @@ struct CharLiteral : public Node {
char value;
};
} // namespace tokens
} // namespace interpereter
} // namespace interpereter::tokens

View file

@ -2,16 +2,14 @@
#include <string>
namespace parser {
namespace tokens {
namespace parser::tokens {
// Sources -----------------
const std::string SourceFile = "source_file";
const std::string Sources = "sources";
// Namespaces, partittions -----------------
// Namespaces, partitions -----------------
const std::string Partition = "partition";
const std::string Namespace = "namespace";
@ -64,7 +62,7 @@ const std::string SubExpressionToken = "subexpression_token";
const std::string SubExpression = "subexpression";
const std::string PrefixedExpression = "prefixed_expression";
const std::string Expression = "expression";
const std::string SuperExpression = "super_expression";
const std::string SuperExpression = "superexpression";
const std::string BlockStatement = "block_statement";
@ -120,9 +118,9 @@ const std::string TypeclassExpression = "typeclass_expression";
const std::string TypeIdentifierDefinition = "type_identifier_definition";
const std::string TypeclassIdentifier = "typeclass_identifer";
const std::string TypeclassIdentifier = "typeclass_identifier";
const std::string NameIdentifier = "name_identifier";
const std::string TypeIdentifier = "type_identifer";
const std::string TypeIdentifier = "type_identifier";
const std::string AbstractTypeIdentifier = "abstract_type_identifier";
const std::string OperatorIdentifier = "operator_identifier";
@ -138,6 +136,4 @@ const std::string Literal = "literal";
const std::string NameSubSuperExpression = "name_subsuperexpression";
} // namespace tokens
} // namespace parser
} // namespace parser::tokens

View file

@ -13,8 +13,16 @@ class ParseTree {
public:
class Node {
public:
Node() = default;
Node(const TSNode &node, const std::string* source) : node_(node), source_(source) {}
Node() : uninitialized_(true) {
for (unsigned int& i : node_.context) {
i = 0;
}
node_.id = nullptr;
node_.tree = nullptr;
source_ = nullptr;
};
Node(const TSNode& node, const std::string* source) : uninitialized_(false), node_(node), source_(source) {}
std::string GetType() {
return ts_node_type(node_);
@ -81,8 +89,9 @@ public:
// ?? use field id instaed of name ??
// ?? node equality check needed ??
private:
bool uninitialized_;
TSNode node_;
const std::string* source_ = nullptr;
const std::string* source_;
};
class Cursor { // ?? needed ??
public:
@ -106,15 +115,22 @@ public:
tree_ = ts_parser_parse_string(
parser,
NULL,
nullptr,
source_.c_str(),
source_.size());
ts_parser_delete(parser);
}
ParseTree(const ParseTree& parse_tree) : tree_(ts_tree_copy(parse_tree.tree_)), source_(parse_tree.source_) {}
Node GetRoot() const {
return Node(ts_tree_root_node(tree_), &source_);
}
~ParseTree() {
ts_tree_delete(tree_);
}
private:
TSTree* tree_;
std::string source_; // for token value extraction

View file

@ -9,7 +9,7 @@ namespace interpreter {
class PrintVisitor : public Visitor {
public:
PrintVisitor(std::ostream& out) : out_(out) {}
explicit PrintVisitor(std::ostream& out) : out_(out) {}
private:
void Visit(Node* node) override;
@ -19,7 +19,7 @@ private:
void Visit(SourceFile* node) override;
void Visit(Sources* node) override;
// Namespaces, partittions -----------------
// Namespaces, partitions -----------------
void Visit(Partition* node) override;
void Visit(Namespace* node) override;

View file

@ -21,7 +21,7 @@ protected:
virtual void Visit(SourceFile* node) {}
virtual void Visit(Sources* node) {}
// Namespaces, partittions -----------------
// Namespaces, partitions -----------------
virtual void Visit(Partition* node) {}
virtual void Visit(Namespace* node) {}