memgraph/src/query_engine/code_generator.hpp

47 lines
1.2 KiB
C++
Raw Normal View History

#pragma once
2016-02-26 06:49:35 +08:00
#include "cypher/ast/ast.hpp"
#include "cypher/compiler.hpp"
#include "utils/string/file.hpp"
#include "template_engine/engine.hpp"
#include "config/config.hpp"
2016-02-26 06:49:35 +08:00
#include "traverser/code_traverser.hpp"
using std::string;
class CodeGenerator
{
public:
2016-02-11 06:34:49 +08:00
void generate_cpp(const std::string& query,
const uint64_t stripped_hash,
const std::string& path)
{
2016-02-26 06:49:35 +08:00
// get paths
2016-02-11 06:34:49 +08:00
string template_path = CONFIG(config::TEMPLATE_CPU_CPP_PATH);
string template_file = utils::read_file(template_path.c_str());
2016-02-26 06:49:35 +08:00
// traversing
auto tree = compiler.syntax_tree(query);
code_traverser.reset();
tree.root->accept(code_traverser);
// save the code
2016-02-11 06:34:49 +08:00
string generated = template_engine.render(
template_file,
{
{"class_name", "CodeCPU"},
{"stripped_hash", std::to_string(stripped_hash)},
{"query", query},
2016-02-26 14:45:43 +08:00
{"code", code_traverser.code}
}
);
utils::write_file(generated, path);
}
2016-02-26 06:49:35 +08:00
private:
template_engine::TemplateEngine template_engine;
2016-02-26 06:49:35 +08:00
ast::Ast tree;
cypher::Compiler compiler;
CodeTraverser code_traverser;
};