1
#include "Transformer.h"
2
#include <Puma/CProject.h>
3
#include <Puma/CTranslationUnit.h>
4
#include <Puma/CCParser.h>
5
//#include <Puma/CPrintVisitor.h>
6
//#include <Puma/CCSemVisitor.h>
7
//#include <Puma/PrePrintVisitor.h>
8
//#include <Puma/PreTree.h>
12
int main(int argc, char** argv) {
13
// This is the error stream object used throughout
14
// the whole system, it supports several error
15
// severities and auto-formatted error location
17
Puma::ErrorStream err;
19
// The project handles all file related task like
20
// finding, opening, writing, and closing files.
21
// The special CProject additionally is able to
22
// scan files and strings, i.e. it provides an easy
23
// to use interface to the lexical analyzer (scanner).
24
Puma::CProject project(err, argc, argv);
25
// Set the initial source/destination path pair. If
26
// a file shall be written using the project it is
27
// first checked if this file is located in one of
28
// source paths known to the project. If not, the file
29
// is not written. This mechanism is necessary to
30
// avoid system headers to be overwritten (except for
31
// the case that the corresponding system include
32
// path is added as a source path).
33
project.addPath(".", ".");
35
// Check the arguments, need an input file.
37
std::cout << "Usage: " << argv[0] << " FILE" << std::endl;
41
// Scan the input file. The result is a token list
42
// representation of the input file.
43
Puma::Unit* unit = project.scanFile(argv[1]);
45
std::cerr << "Aborted: Unable to scan input file" << std::endl;
49
// After the input file was successfully scanned, it is
50
// now tried to parse it. The result is the so-called
51
// translation unit encapsulating the preprocessor,
52
// syntax, and semantic trees.
53
Puma::CCParser parser;
54
Puma::CTranslationUnit* tu = parser.parse(*unit, project);
57
// During the parsing of the input file only those
58
// semantic analyses are performed that are necessary
59
// to build the correct syntax tree and to recognize
60
// the correct types. Expression evaluation and so on
61
// is performed in an additional sematic analysis. It
62
// is optional and not needed for this application.
63
//Puma::CCSemVisitor semantics(err);
64
//semantics.run(tu->tree());
66
// The generated syntax tree is an attributed syntax
67
// tree referring to both tokens and semantic information.
68
// It can be traversed using a corresponding tree visitor.
69
// Formatted printing of the tree is performed by the
70
// CPrintVisitor tree visitor.
71
//CPrintVisitor cprinter;
72
//cprinter.print(tu->tree(), std::cout);
74
// The collected semantic information also is organized
75
// as a tree reflecting the scope structure of the parsed
76
// code. The interface to the semantic tree is the class
77
// CClassDatabase. It can also be used to print this tree.
78
//tu->db().Dump(std::cout, 10);
80
// Now transform the "if" statements so that a comment
81
// exists before every "if" statement.
82
Transformer transformer(err);
83
transformer.transform(tu->tree());
85
// At last the transformed unit has to be saved as file
86
// again. This is also realized using the project class.
87
// The original file shall be renamed to filename.bak
88
// before the transformed version of this file is written.
89
project.saveMode(Puma::SaveMode::RENAME_OLD, ".bak");
93
// Beside the syntax and semantic trees there is also
94
// a separate tree for the preprocessor directives.
95
// This tree can be printed using the PrePrintVisitor.
96
//Puma::PrePrintVisitor preprinter;
97
//tu->cpp_tree()->accept(preprinter);
103
// Return 1 if errors or fatal errors occurred.
104
return err.severity() > Puma::sev_warning ? 1 : 0;