Add basic interpreter skeleton.
This commit is contained in:
parent
09dbe2e722
commit
1428ce0639
src/query/frontend
43
src/query/frontend/ast/ast.hpp
Normal file
43
src/query/frontend/ast/ast.hpp
Normal file
@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "database/graph_db.hpp"
|
||||
|
||||
namespace query {
|
||||
|
||||
template <typename T>
|
||||
using sptr = std::shared_ptr<T>;
|
||||
|
||||
template <typename T>
|
||||
using uptr = std::unique_ptr<T>;
|
||||
|
||||
class Tree {
|
||||
public:
|
||||
Tree(const int uid) : uid_(uid) {}
|
||||
int uid() const { return uid_; }
|
||||
private:
|
||||
const int uid_;
|
||||
};
|
||||
|
||||
class Expr : public Tree {
|
||||
};
|
||||
|
||||
class Ident : public Tree {
|
||||
public:
|
||||
std::string identifier_;
|
||||
};
|
||||
|
||||
class Part {
|
||||
};
|
||||
|
||||
class NodePart : public Part {
|
||||
public:
|
||||
Ident identifier_;
|
||||
// TODO: Mislav call GraphDb::label(label_name) to populate labels_!
|
||||
std::vector<GraphDb::Label> labels_;
|
||||
// TODO: properties
|
||||
};
|
||||
|
||||
}
|
18
src/query/frontend/interpret/interpret.hpp
Normal file
18
src/query/frontend/interpret/interpret.hpp
Normal file
@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "query/backend/cpp/typed_value.hpp"
|
||||
|
||||
class Frame {
|
||||
public:
|
||||
Frame(int size) : size_(size), elems_(size_) {}
|
||||
|
||||
auto& operator[](int pos) { return elems_[pos]; }
|
||||
const auto& operator[](int pos) const { return elems_[pos]; }
|
||||
|
||||
private:
|
||||
int size_;
|
||||
std::vector<TypedValue> elems_;
|
||||
};
|
||||
|
82
src/query/frontend/logical/operator.hpp
Normal file
82
src/query/frontend/logical/operator.hpp
Normal file
@ -0,0 +1,82 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "database/graph_db_accessor.hpp"
|
||||
#include "query/frontend/ast/ast.hpp"
|
||||
#include "query/frontend/interpret/interpret.hpp"
|
||||
#include "query/frontend/typecheck/symbol_table.hpp"
|
||||
|
||||
namespace query {
|
||||
class Cursor {
|
||||
public:
|
||||
virtual bool pull(Frame&, SymbolTable&) = 0;
|
||||
virtual ~Cursor() {}
|
||||
};
|
||||
|
||||
class LogicalOperator {
|
||||
public:
|
||||
auto children() { return children_; };
|
||||
virtual uptr<Cursor> MakeCursor(GraphDbAccessor db) = 0;
|
||||
virtual ~LogicalOperator() {}
|
||||
|
||||
protected:
|
||||
std::vector<std::shared_ptr<LogicalOperator>> children_;
|
||||
};
|
||||
|
||||
class ScanAll : public LogicalOperator {
|
||||
public:
|
||||
ScanAll(sptr<NodePart> node_part) : node_part_(node_part) {}
|
||||
|
||||
private:
|
||||
class ScanAllCursor : public Cursor {
|
||||
public:
|
||||
ScanAllCursor(ScanAll& parent, GraphDbAccessor db)
|
||||
: parent_(parent), db_(db), vertices_(db.vertices()) {}
|
||||
bool pull(Frame& frame, SymbolTable& symbol_table) override {
|
||||
while (vertices_ != vertices_.end()) {
|
||||
auto& vertex = *vertices_++;
|
||||
if (evaluate(frame, symbol_table, vertex)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
ScanAll& parent_;
|
||||
GraphDbAccessor db_;
|
||||
decltype(db_.vertices()) vertices_;
|
||||
|
||||
bool evaluate(Frame& frame, SymbolTable& symbol_table,
|
||||
VertexAccessor& vertex) {
|
||||
auto node_part = parent_.node_part_;
|
||||
for (auto label : node_part->labels_) {
|
||||
if (!vertex.has_label(label)) return false;
|
||||
}
|
||||
frame[symbol_table[parent_.node_part_->identifier_].position_] = vertex;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
public:
|
||||
uptr<Cursor> MakeCursor(GraphDbAccessor db) override {
|
||||
return new ScanAllCursor(*this, db);
|
||||
}
|
||||
|
||||
friend class ScanAll::ScanAllCursor;
|
||||
sptr<NodePart> node_part_;
|
||||
};
|
||||
|
||||
class Produce : public LogicalOperator {
|
||||
public:
|
||||
Produce(sptr<LogicalOperator> op, std::vector<sptr<Expr>> exprs)
|
||||
: exprs_(exprs) {
|
||||
children_.emplace_back(op);
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<sptr<Expr>> exprs_;
|
||||
};
|
||||
}
|
34
src/query/frontend/typecheck/symbol_table.hpp
Normal file
34
src/query/frontend/typecheck/symbol_table.hpp
Normal file
@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include "query/frontend/ast/ast.hpp"
|
||||
|
||||
namespace query {
|
||||
struct Symbol {
|
||||
Symbol(std::string& name, int position) : name_(name), position_(position) {}
|
||||
std::string name_;
|
||||
int position_;
|
||||
};
|
||||
|
||||
class SymbolTable {
|
||||
public:
|
||||
Symbol CreateSymbol(std::string& name) {
|
||||
int position = position_++;
|
||||
return Symbol(name, position);
|
||||
}
|
||||
|
||||
void AssignSymbol(const Tree& tree, Symbol symbol) {
|
||||
table_[tree.uid()] = symbol;
|
||||
}
|
||||
|
||||
auto& operator[](const Tree& tree) { return table_[tree.uid()]; }
|
||||
|
||||
int max_position() const { return position_; }
|
||||
|
||||
private:
|
||||
int position_{0};
|
||||
std::map<int, Symbol> table_;
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue
Block a user