Add basic generation of LogicalOperator tree

This commit is contained in:
Teon Banek 2017-03-11 20:49:22 +01:00
parent fbf70de089
commit 34416bc505
6 changed files with 73 additions and 10 deletions

View File

@ -107,7 +107,7 @@ class Clause : public Tree {};
class Pattern : public Tree {
public:
std::vector<std::unique_ptr<Part>> node_parts_;
std::vector<std::shared_ptr<NodePart>> node_parts_;
void Accept(TreeVisitorBase& visitor) override {
visitor.PreVisit(*this);
for (auto& node_part : node_parts_) {
@ -146,7 +146,7 @@ public:
class Return : public Clause {
public:
std::vector<std::unique_ptr<Expr>> exprs_;
std::vector<std::shared_ptr<Expr>> exprs_;
void Accept(TreeVisitorBase& visitor) override {
visitor.PreVisit(*this);
for (auto& expr : exprs_) {

View File

@ -7,7 +7,7 @@
#include <vector>
#include "query/backend/cpp/compiler_structures.hpp"
#include "query/backend/cpp/named_antlr_tokens.hpp"
#include "query/frontend/ast/named_antlr_tokens.hpp"
#include "utils/assert.hpp"
namespace backend {

View File

@ -69,6 +69,7 @@ class ScanAll : public LogicalOperator {
return std::unique_ptr<Cursor>(cursor);
}
private:
friend class ScanAll::ScanAllCursor;
std::shared_ptr<NodePart> node_part_;
};
@ -93,6 +94,11 @@ class Produce : public LogicalOperator {
private:
Produce& parent_;
};
public:
std::unique_ptr<Cursor> MakeCursor(GraphDbAccessor) override {
return std::unique_ptr<Cursor>(new ProduceCursor(*this));
}
private:
std::vector<std::shared_ptr<Expr>> exprs_;
};
}

View File

@ -0,0 +1,55 @@
#pragma once
#include <memory>
#include <stdexcept>
#include "query/frontend/ast/ast.hpp"
#include "query/frontend/logical/operator.hpp"
namespace query {
std::shared_ptr<LogicalOperator> GenMatch(
Match& match, std::shared_ptr<LogicalOperator> current_op)
{
if (current_op) {
throw std::runtime_error("Not implemented");
}
if (match.patterns_.size() != 1) {
throw std::runtime_error("Not implemented");
}
auto& pattern = match.patterns_[0];
if (pattern->node_parts_.size() != 1) {
throw std::runtime_error("Not implemented");
}
auto& node_part = pattern->node_parts_[0];
return std::shared_ptr<LogicalOperator>(new ScanAll(node_part));
}
std::shared_ptr<LogicalOperator> GenReturn(
Return& ret, std::shared_ptr<LogicalOperator> current_op)
{
if (!current_op) {
throw std::runtime_error("Not implemented");
}
return std::shared_ptr<LogicalOperator>(new Produce(current_op, ret.exprs_));
}
std::shared_ptr<LogicalOperator> Apply(Query& query)
{
std::shared_ptr<LogicalOperator> current_op;
for (auto& clause : query.clauses_) {
auto* clause_ptr = clause.get();
auto* match = dynamic_cast<Match*>(clause_ptr);
auto* ret = dynamic_cast<Return*>(clause_ptr);
if (match) {
current_op = GenMatch(*match, current_op);
} else if (ret) {
return GenReturn(*ret, current_op);
} else {
throw std::runtime_error("Not implemented");
}
}
return current_op;
}
}

View File

@ -8,14 +8,14 @@
namespace query {
struct Symbol {
Symbol() {}
Symbol(std::string& name, int position) : name_(name), position_(position) {}
Symbol(const std::string& name, int position) : name_(name), position_(position) {}
std::string name_;
int position_;
};
class SymbolTable {
public:
Symbol CreateSymbol(std::string& name) {
Symbol CreateSymbol(const std::string& name) {
int position = position_++;
return Symbol(name, position);
}

View File

@ -8,14 +8,14 @@ namespace query {
class TypeCheckVisitor : public TreeVisitorBase {
public:
TypeCheckVisitor(SymbolTable& symbol_table) : symbol_table_(symbol_table_) {}
TypeCheckVisitor(SymbolTable& symbol_table) : symbol_table_(symbol_table) {}
// Start of the tree is a Query.
void Visit(Query& query) override {}
// Expressions
void Visit(Ident& ident) override {
Symbol symbol;
} if (scope_.in_pattern) {
if (scope_.in_pattern) {
symbol = GetOrCreateSymbol(ident.identifier_);
} else {
if (!HasSymbol(ident.identifier_))
@ -60,11 +60,13 @@ class TypeCheckVisitor : public TreeVisitorBase {
Symbol GetOrCreateSymbol(const std::string& name)
{
auto search = scope_.variables.find(name)
auto search = scope_.variables.find(name);
if (search != scope_.variables.end()) {
return *search;
return search->second;
}
scope_.variables[name] = symbol_table_.CreateSymbol(name);
auto symbol = symbol_table_.CreateSymbol(name);
scope_.variables[name] = symbol;
return symbol;
}
SymbolTable& symbol_table_;
Scope scope_;