2016-01-27 06:40:11 +08:00
|
|
|
#pragma once
|
|
|
|
|
2016-06-06 17:29:52 +08:00
|
|
|
#include "config/config.hpp"
|
2016-02-26 06:49:35 +08:00
|
|
|
#include "cypher/ast/ast.hpp"
|
|
|
|
#include "cypher/compiler.hpp"
|
2016-07-24 10:47:48 +08:00
|
|
|
#include "query_engine/exceptions/exceptions.hpp"
|
2016-02-08 05:56:52 +08:00
|
|
|
#include "template_engine/engine.hpp"
|
2016-07-24 10:47:48 +08:00
|
|
|
#include "traverser/cpp_traverser.hpp"
|
2016-06-06 17:29:52 +08:00
|
|
|
#include "utils/string/file.hpp"
|
2016-07-18 01:32:35 +08:00
|
|
|
|
|
|
|
// TODO:
|
|
|
|
// * logger
|
|
|
|
#include <iostream>
|
2016-02-08 05:56:52 +08:00
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
2016-01-27 06:40:11 +08:00
|
|
|
class CodeGenerator
|
|
|
|
{
|
2016-02-08 05:56:52 +08:00
|
|
|
public:
|
2016-06-06 17:29:52 +08:00
|
|
|
void generate_cpp(const std::string &query, const uint64_t stripped_hash,
|
|
|
|
const std::string &path)
|
2016-02-11 06:34:49 +08:00
|
|
|
{
|
2016-07-24 10:47:48 +08:00
|
|
|
// TODO: optimize initialize only once -> be careful that object has
|
|
|
|
// a state
|
|
|
|
// TODO: multithread test
|
|
|
|
CppTraverser cpp_traverser;
|
|
|
|
|
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
|
|
|
|
2016-07-18 01:32:35 +08:00
|
|
|
// syntax tree generation
|
|
|
|
try {
|
|
|
|
tree = compiler.syntax_tree(query);
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
// TODO: extract more information
|
|
|
|
throw QueryEngineException("Syntax tree generation error");
|
|
|
|
}
|
|
|
|
|
2016-07-24 10:47:48 +08:00
|
|
|
cpp_traverser.reset();
|
2016-07-18 01:32:35 +08:00
|
|
|
|
|
|
|
// code generation
|
|
|
|
try {
|
2016-07-24 10:47:48 +08:00
|
|
|
tree.root->accept(cpp_traverser);
|
|
|
|
} catch (const SemanticException &e) {
|
|
|
|
throw e;
|
2016-07-18 01:32:35 +08:00
|
|
|
} catch (const std::exception &e) {
|
|
|
|
throw QueryEngineException("Code generation error");
|
|
|
|
}
|
2016-02-26 06:49:35 +08:00
|
|
|
|
|
|
|
// save the code
|
2016-02-11 06:34:49 +08:00
|
|
|
string generated = template_engine.render(
|
2016-06-06 17:29:52 +08:00
|
|
|
template_file, {{"class_name", "CodeCPU"},
|
|
|
|
{"stripped_hash", std::to_string(stripped_hash)},
|
|
|
|
{"query", query},
|
2016-07-24 10:47:48 +08:00
|
|
|
{"code", cpp_traverser.code}});
|
2016-07-18 01:32:35 +08:00
|
|
|
|
|
|
|
// TODO: use logger, ifndef
|
2016-07-17 08:22:43 +08:00
|
|
|
std::cout << generated << std::endl;
|
|
|
|
|
2016-02-08 05:56:52 +08:00
|
|
|
utils::write_file(generated, path);
|
|
|
|
}
|
2016-02-26 06:49:35 +08:00
|
|
|
|
2016-02-08 05:56:52 +08:00
|
|
|
private:
|
2016-06-06 17:29:52 +08:00
|
|
|
template_engine::TemplateEngine template_engine;
|
2016-02-26 06:49:35 +08:00
|
|
|
ast::Ast tree;
|
|
|
|
cypher::Compiler compiler;
|
2016-01-27 06:40:11 +08:00
|
|
|
};
|