forked from lcompilers/lpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpptranslate.cpp
More file actions
85 lines (67 loc) · 2.49 KB
/
Copy pathcpptranslate.cpp
File metadata and controls
85 lines (67 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <chrono>
#define CLI11_HAS_FILESYSTEM 0
#include <bin/CLI11.hpp>
#include <lpython/parser/parser.h>
#include <lpython/pickle.h>
#include <lpython/semantics/ast_to_asr.h>
#include <lpython/ast_to_src.h>
#include <lpython/ast_to_openmp.h>
#include <libasr/config.h>
std::string read_file(const std::string &filename)
{
std::ifstream ifs(filename.c_str(), std::ios::in | std::ios::binary
| std::ios::ate);
std::ifstream::pos_type filesize = ifs.tellg();
if (filesize < 0) return std::string();
ifs.seekg(0, std::ios::beg);
std::vector<char> bytes(filesize);
ifs.read(&bytes[0], filesize);
return std::string(&bytes[0], filesize);
}
int emit_ast_openmp(const std::string &infile)
{
std::string input = read_file(infile);
// Src -> AST
LFortran::diag::Diagnostics diagnostics;
Allocator al(64*1024*1024);
LFortran::AST::TranslationUnit_t* ast;
ast = LFortran::TRY(LFortran::parse(al, input, diagnostics));
// AST -> Source
// FIXME: For now we only transform the first node in the list:
std::string source = LFortran::ast_to_openmp(*ast->m_items[0]);
std::cout << source << std::endl;
return 0;
}
int main(int argc, char *argv[])
{
std::string arg_file;
bool arg_version = false;
bool show_tokens = false;
bool show_ast = false;
bool show_asr = false;
bool show_ast_f90 = false;
bool show_ast_openmp = false;
CLI::App app{"cpptranslate: Fortran to C++ translation"};
app.add_option("file", arg_file, "Source file");
app.add_flag("--version", arg_version, "Display compiler version information");
app.add_flag("--show-tokens", show_tokens, "Show tokens for the given file and exit");
app.add_flag("--show-ast", show_ast, "Show AST for the given file and exit");
app.add_flag("--show-asr", show_asr, "Show ASR for the given file and exit");
app.add_flag("--show-ast-f90", show_ast_f90, "Show AST -> Fortran for the given file and exit");
app.add_flag("--show-ast-openmp", show_ast_openmp, "Show AST -> Fortran with OpenMP for the given file and exit");
CLI11_PARSE(app, argc, argv);
if (arg_version) {
std::string version = LFORTRAN_VERSION;
std::cout << "LFortran version: " << version << std::endl;
return 0;
}
if (arg_file.size() == 0) {
std::cerr << "Run 'cpptranslate -h' for help" << std::endl;
return 1;
}
if (show_ast_openmp) {
return emit_ast_openmp(arg_file);
}
return 0;
}