From 8d8a1cef6a15e23bbdd459a8b1855b1d42e213f8 Mon Sep 17 00:00:00 2001 From: Mislav Bradac Date: Sat, 11 Mar 2017 15:51:22 +0100 Subject: [PATCH] Add basic ast types --- src/query/backend/cpp/code_generator.hpp | 15 --- src/query/context.hpp | 15 +++ src/query/frontend/ast/ast.hpp | 51 +++++++--- .../ast}/cypher_main_visitor.cpp | 0 .../ast}/cypher_main_visitor.hpp | 96 ++++++++++--------- .../ast}/named_antlr_tokens.hpp | 0 6 files changed, 103 insertions(+), 74 deletions(-) delete mode 100644 src/query/backend/cpp/code_generator.hpp create mode 100644 src/query/context.hpp rename src/query/{backend/cpp => frontend/ast}/cypher_main_visitor.cpp (100%) rename src/query/{backend/cpp => frontend/ast}/cypher_main_visitor.hpp (74%) rename src/query/{backend/cpp => frontend/ast}/named_antlr_tokens.hpp (100%) diff --git a/src/query/backend/cpp/code_generator.hpp b/src/query/backend/cpp/code_generator.hpp deleted file mode 100644 index 78d11f77c..000000000 --- a/src/query/backend/cpp/code_generator.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include -#include -#include "query/frontend/opencypher/generated/CypherBaseVisitor.h" -#include "antlr4-runtime.h" - -using antlropencypher::CypherParser; - -class CodeGenerator { - void GenerateExpresssion(); - - private: - std::string code_; -}; diff --git a/src/query/context.hpp b/src/query/context.hpp new file mode 100644 index 000000000..eea60317c --- /dev/null +++ b/src/query/context.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include "antlr4-runtime.h" +#include "query/frontend/ast/cypher_main_visitor.hpp" + +class Context { + int uid_counter; +}; + +class HighLevelAstConversion { + void Apply(const Context &ctx, antlr4::tree::ParseTree *tree) { + query::frontend::CypherMainVisitor visitor(ctx); + visitor.visit(tree); + } +}; diff --git a/src/query/frontend/ast/ast.hpp b/src/query/frontend/ast/ast.hpp index 63f5d4772..e2c201227 100644 --- a/src/query/frontend/ast/ast.hpp +++ b/src/query/frontend/ast/ast.hpp @@ -13,32 +13,59 @@ class SymbolTable; class Tree { public: - Tree(const int uid) : uid_(uid) {} - int uid() const { return uid_; } + Tree(const int uid) : uid_(uid) {} + int uid() const { return uid_; } + private: - const int uid_; + const int uid_; }; class Expr : public Tree { - public: - virtual TypedValue Evaluate(Frame&, SymbolTable&) = 0; +public: + virtual TypedValue Evaluate(Frame &, SymbolTable &) = 0; }; class Ident : public Expr { public: std::string identifier_; - TypedValue Evaluate(Frame& frame, SymbolTable& symbol_table) override; + TypedValue Evaluate(Frame &frame, SymbolTable &symbol_table) override; }; -class Part { -}; +class Part {}; class NodePart : public Part { public: - Ident identifier_; - // TODO: Mislav call GraphDb::label(label_name) to populate labels_! - std::vector labels_; - // TODO: properties + Ident identifier_; + // TODO: Mislav call GraphDb::label(label_name) to populate labels_! + std::vector labels_; + // TODO: properties }; +class EdgePart : public Part { +public: + Ident identifier_; + // TODO: finish this: properties, types... +}; + +class Clause : public Tree {}; + +class Pattern : public Tree { +public: + std::vector> node_parts_; +}; + +class Query : public Tree { +public: + std::vector> clauses_; +}; + +class Match : public Clause { +public: + std::vector> patterns_; +}; + +class Return : public Clause { +public: + std::vector> exprs_; +}; } diff --git a/src/query/backend/cpp/cypher_main_visitor.cpp b/src/query/frontend/ast/cypher_main_visitor.cpp similarity index 100% rename from src/query/backend/cpp/cypher_main_visitor.cpp rename to src/query/frontend/ast/cypher_main_visitor.cpp diff --git a/src/query/backend/cpp/cypher_main_visitor.hpp b/src/query/frontend/ast/cypher_main_visitor.hpp similarity index 74% rename from src/query/backend/cpp/cypher_main_visitor.hpp rename to src/query/frontend/ast/cypher_main_visitor.hpp index 3dfab0a5d..70101c05a 100644 --- a/src/query/backend/cpp/cypher_main_visitor.hpp +++ b/src/query/frontend/ast/cypher_main_visitor.hpp @@ -1,17 +1,17 @@ #pragma once -#include -#include "query/frontend/opencypher/generated/CypherBaseVisitor.h" #include "antlr4-runtime.h" #include "query/backend/cpp/compiler_structures.hpp" +#include "query/frontend/opencypher/generated/CypherBaseVisitor.h" +#include -namespace backend { -namespace cpp { +namespace query { +namespace frontend { using antlropencypher::CypherParser; class CypherMainVisitor : public antlropencypher::CypherBaseVisitor { - private: +private: // Return new output code id. // TODO: Should we generate ids with more readable names: node_1, // relationship_5, temporary_2...? @@ -21,8 +21,9 @@ class CypherMainVisitor : public antlropencypher::CypherBaseVisitor { } template - antlrcpp::Any LeftAssociativeOperatorExpression( - std::vector children, std::vector ops) { + antlrcpp::Any + LeftAssociativeOperatorExpression(std::vector children, + std::vector ops) { assert(children.size()); std::vector children_ids; @@ -41,8 +42,9 @@ class CypherMainVisitor : public antlropencypher::CypherBaseVisitor { } template - antlrcpp::Any LeftAssociativeOperatorExpression( - std::vector children, Function op) { + antlrcpp::Any + LeftAssociativeOperatorExpression(std::vector children, + Function op) { return LeftAssociativeOperatorExpression( children, std::vector((int)children.size() - 1, op)); } @@ -53,8 +55,8 @@ class CypherMainVisitor : public antlropencypher::CypherBaseVisitor { * * @return string - node id. */ - antlrcpp::Any visitNodePattern( - CypherParser::NodePatternContext *ctx) override; + antlrcpp::Any + visitNodePattern(CypherParser::NodePatternContext *ctx) override; /** * @return vector labels. @@ -75,8 +77,8 @@ class CypherMainVisitor : public antlropencypher::CypherBaseVisitor { /** * @return string. */ - antlrcpp::Any visitSymbolicName( - CypherParser::SymbolicNameContext *ctx) override; + antlrcpp::Any + visitSymbolicName(CypherParser::SymbolicNameContext *ctx) override; /** * @return vector pattern. @@ -90,16 +92,16 @@ class CypherMainVisitor : public antlropencypher::CypherBaseVisitor { * * @return string - pattern part id. */ - antlrcpp::Any visitPatternPart( - CypherParser::PatternPartContext *ctx) override; + antlrcpp::Any + visitPatternPart(CypherParser::PatternPartContext *ctx) override; /** * Creates PatternPart. * * @return PatternPart. */ - antlrcpp::Any visitPatternElement( - CypherParser::PatternElementContext *ctx) override; + antlrcpp::Any + visitPatternElement(CypherParser::PatternElementContext *ctx) override; /** * @return pair - node and relationship ids. @@ -125,14 +127,14 @@ class CypherMainVisitor : public antlropencypher::CypherBaseVisitor { /** * @return vector. */ - antlrcpp::Any visitRelationshipTypes( - CypherParser::RelationshipTypesContext *ctx) override; + antlrcpp::Any + visitRelationshipTypes(CypherParser::RelationshipTypesContext *ctx) override; /** * @return pair. */ - antlrcpp::Any visitRangeLiteral( - CypherParser::RangeLiteralContext *ctx) override; + antlrcpp::Any + visitRangeLiteral(CypherParser::RangeLiteralContext *ctx) override; /** * Top level expression. @@ -146,40 +148,40 @@ class CypherMainVisitor : public antlropencypher::CypherBaseVisitor { * * @return string - expression id. */ - antlrcpp::Any visitExpression12( - CypherParser::Expression12Context *ctx) override; + antlrcpp::Any + visitExpression12(CypherParser::Expression12Context *ctx) override; /** * XOR. * * @return string - expression id. */ - antlrcpp::Any visitExpression11( - CypherParser::Expression11Context *ctx) override; + antlrcpp::Any + visitExpression11(CypherParser::Expression11Context *ctx) override; /** * AND. * * @return string - expression id. */ - antlrcpp::Any visitExpression10( - CypherParser::Expression10Context *ctx) override; + antlrcpp::Any + visitExpression10(CypherParser::Expression10Context *ctx) override; /** * NOT. * * @return string - expression id. */ - antlrcpp::Any visitExpression9( - CypherParser::Expression9Context *ctx) override; + antlrcpp::Any + visitExpression9(CypherParser::Expression9Context *ctx) override; /** * Comparisons. * * @return string - expression id. */ - antlrcpp::Any visitExpression8( - CypherParser::Expression8Context *ctx) override; + antlrcpp::Any + visitExpression8(CypherParser::Expression8Context *ctx) override; /** * Never call this. Everything related to generating code for comparison @@ -193,48 +195,48 @@ class CypherMainVisitor : public antlropencypher::CypherBaseVisitor { * * @return string - expression id. */ - antlrcpp::Any visitExpression7( - CypherParser::Expression7Context *ctx) override; + antlrcpp::Any + visitExpression7(CypherParser::Expression7Context *ctx) override; /** * Multiplication, division, modding. * * @return string - expression id. */ - antlrcpp::Any visitExpression6( - CypherParser::Expression6Context *ctx) override; + antlrcpp::Any + visitExpression6(CypherParser::Expression6Context *ctx) override; /** * Power. * * @return string - expression id. */ - antlrcpp::Any visitExpression5( - CypherParser::Expression5Context *ctx) override; + antlrcpp::Any + visitExpression5(CypherParser::Expression5Context *ctx) override; /** * Unary minus and plus. * * @return string - expression id. */ - antlrcpp::Any visitExpression4( - CypherParser::Expression4Context *ctx) override; + antlrcpp::Any + visitExpression4(CypherParser::Expression4Context *ctx) override; /** * Element of a list, range of a list... * * @return string - expression id. */ - antlrcpp::Any visitExpression3( - CypherParser::Expression3Context *ctx) override; + antlrcpp::Any + visitExpression3(CypherParser::Expression3Context *ctx) override; /** * Property lookup, test for node labels existence... * * @return string - expression id. */ - antlrcpp::Any visitExpression2( - CypherParser::Expression2Context *ctx) override; + antlrcpp::Any + visitExpression2(CypherParser::Expression2Context *ctx) override; /** * Literals, params, list comprehension... @@ -265,10 +267,10 @@ class CypherMainVisitor : public antlropencypher::CypherBaseVisitor { /** * @return int64_t. */ - antlrcpp::Any visitIntegerLiteral( - CypherParser::IntegerLiteralContext *ctx) override; + antlrcpp::Any + visitIntegerLiteral(CypherParser::IntegerLiteralContext *ctx) override; - public: +public: // TODO: These temporary getters should eventually be replaced with // something // else once we figure out where and how those strctures will be used. @@ -278,7 +280,7 @@ class CypherMainVisitor : public antlropencypher::CypherBaseVisitor { const auto &ids_map() const { return ids_map_; } const auto &symbol_table() const { return symbol_table_; } - private: +private: // Mapping of ids (nodes, relationships, values, lists ...) from // query // code to id that is used in generated code; diff --git a/src/query/backend/cpp/named_antlr_tokens.hpp b/src/query/frontend/ast/named_antlr_tokens.hpp similarity index 100% rename from src/query/backend/cpp/named_antlr_tokens.hpp rename to src/query/frontend/ast/named_antlr_tokens.hpp