├── .gitignore ├── CMakeLists.txt ├── README.md ├── its_free.jpg ├── main.cpp ├── transpiler.cpp └── transpiler.h /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/* 2 | .idea 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(PHPtoCpp) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES 7 | main.cpp 8 | README.md 9 | transpiler.cpp 10 | transpiler.h node.cpp node.h list.cpp list.h) 11 | 12 | add_executable(PHPtoCpp ${SOURCE_FILES}) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHPtoCpp 2 | 3 | PHPtoCpp it is a simple transpiler that transforms your **PHP** file to **C++** file. 4 | 5 | ### Usage 6 | ```sh 7 | $ git clone https://github.com/antonyalkmim/PHPtoCpp 8 | $ cd PHPtoCpp 9 | $ cmake CMakeLists.txt 10 | $ make 11 | $ PHPtoCpp index.php 12 | ``` 13 | Will generate a ```index.php.cpp``` file. 14 | 15 | ### Example 16 | 17 | **PHP** infile( ```index.php``` ): 18 | ```php 19 | 28 | ``` 29 | 30 | **C++** outfile (```index.php.cpp```): 31 | ```cpp 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | 39 | using namespace std; 40 | 41 | int main(){ 42 | string foo; 43 | cin >> foo; 44 | 45 | int bar = 1 + 2 ; 46 | 47 | if ( foo ) { 48 | int fb = bar ; 49 | cout << fb ; 50 | } 51 | } 52 | ``` 53 | 54 | 55 | ### Issues 56 | - It is converting only sequencial and simple PHP commands 57 | - All tokens should be separeted by a single space ```if ( true ) { ... } ``` 58 | - Datatypes should be declared in the final of variable declaration the line: 59 | - Ex: ```$foo = 1 ; //int``` 60 | - Datatypes: ```//int```,```//float```, ```//double```, ```//bool```, ```//string``` 61 | - Input data has to be: ```$_REQUEST['name']```, ```$_POST['name']``` or ```$_GET['name']```, Example: 62 | - ```$data = $_POST['data'] ; //string``` will be convert to ```string data; cin >> data;``` 63 | - It is included unecessary c++ libraries when converts. 64 | - Variable names should be more then one character: 65 | - ```$a = 1 ; // segmentation error``` 66 | - ```$aa = 1 ; // convert normally``` 67 | 68 | 69 | License 70 | ---- 71 | WTFPL 72 | 73 | ![alt tag](https://github.com/antonyalkmim/PHPtoCpp/blob/master/its_free.jpg) 74 | 75 | **It's free** 76 | -------------------------------------------------------------------------------- /its_free.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antonyalkmim/PHPtoCpp/7f11359112ada4c012f77be66d47eb900cac2c2e/its_free.jpg -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "transpiler.h" 7 | 8 | using namespace std; 9 | 10 | int main(int argc, char* argv[]) { 11 | 12 | string filename = argc > 1 ? argv[1] : "index.php"; //nome do arquivo a ser convertido 13 | ifstream file(filename); //arquivo php a ser convertido 14 | ofstream outfile(filename + ".cpp"); //arquivo .cpp com codigo convertido de php para cpp 15 | 16 | transpiler tp; 17 | 18 | string token = ""; 19 | char aux; //ler cada caractere 20 | 21 | vector expression; 22 | // vector outExpression; 23 | 24 | if(file.is_open() && outfile.is_open()){ 25 | 26 | while(!file.eof()){ 27 | 28 | file >> noskipws >> aux; //ler char por char 29 | 30 | /* Quando encontrar um espaco em branco identifica o final de um token */ 31 | if(aux == ' ' || aux == '\n' || aux == '\0'){ 32 | 33 | if(tp.isOperator(token)){ /* Operators */ 34 | if(!expression.empty()){ 35 | expression.push_back(tp.operators[token]); 36 | 37 | if(token == ")"){ 38 | for(string exp : expression) 39 | outfile << exp << " "; 40 | expression.clear(); 41 | } 42 | } 43 | else{ 44 | outfile << tp.operators[token] << " "; 45 | } 46 | } 47 | else if(tp.isKeyword(token)){ /* Keyword */ 48 | if(token == "> ... 82 | outfile << "cin >> " << expression[0] << ";" << endl; 83 | 84 | expression.clear(); 85 | }else{ 86 | if(find(tp.variables.begin(), tp.variables.end(), expression[0]) == tp.variables.end()) { 87 | tp.variables.push_back(expression[0]); 88 | outfile << token.substr(2, token.length()) << " "; 89 | } 90 | 91 | for(string exp : expression) 92 | outfile << exp << " "; 93 | 94 | expression.clear(); 95 | } 96 | } 97 | else{ 98 | if(!expression.empty()){ 99 | expression.push_back(token); 100 | } 101 | else{ 102 | outfile << token << " "; 103 | } 104 | 105 | if(token == ";" && expression[0] == tp.keywords["echo"]){ 106 | for(string exp : expression) 107 | outfile << exp << " "; 108 | expression.clear(); 109 | } 110 | } 111 | 112 | if(aux == '\n') 113 | outfile << endl; 114 | 115 | token.clear(); 116 | } 117 | else if (aux == '"') { //string 118 | token.push_back(aux); 119 | 120 | file >> noskipws >> aux; 121 | token.push_back(aux); 122 | while (aux != '"') { 123 | file >> noskipws >> aux; 124 | token.push_back(aux); 125 | } 126 | 127 | if(!expression.empty()){ 128 | expression.push_back(token); 129 | } 130 | else{ 131 | outfile << token << " "; 132 | } 133 | 134 | token.clear(); 135 | } 136 | else { 137 | token.push_back(aux); 138 | } 139 | } 140 | cout << "File " << filename << " was convert to PHP in " << filename << ".cpp" << endl; 141 | file.close(); 142 | }else{ 143 | cout << "Erro ao tentar abrir arquivo: " << filename << endl; 144 | } 145 | } -------------------------------------------------------------------------------- /transpiler.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Antony Alkmim on 18/05/16. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "transpiler.h" 10 | 11 | using namespace std; 12 | 13 | transpiler::transpiler() { 14 | 15 | //types 16 | types = {"//int","//float","//string","//double","//char", "//bool"}; 17 | 18 | //LIBS 19 | libs = { 20 | "#include ", 21 | "#include ", 22 | "#include ", 23 | "#include ", 24 | "#include ", 25 | "#include "}; 26 | 27 | 28 | //KEYWORDS 29 | keywords[""] = "}"; 31 | keywords["if"] = "if"; 32 | keywords["echo"] = "cout << "; 33 | keywords["$_REQUEST"] = "cin >> "; 34 | keywords["$_GET"] = "cin >> "; 35 | keywords["$_POST"] = "cin >> "; 36 | keywords["true"] = "true"; 37 | keywords["false"] = "false"; 38 | 39 | //operators 40 | operators["."] = "+"; 41 | operators["-"] = "-"; 42 | operators["/"] = "/"; 43 | operators["%"] = "%"; 44 | operators["*"] = "*"; 45 | operators["*="] = "*="; 46 | operators["+="] = "+="; 47 | operators["-="] = "-="; 48 | operators["/="] = "/="; 49 | operators[">"] = ">"; 50 | operators["<"] = "<"; 51 | operators["!"] = "!"; 52 | operators["&&"] = "&&"; 53 | operators["||"] = "||"; 54 | operators["=="] = "=="; 55 | operators["!="] = "!="; 56 | operators["<="] = "<="; 57 | operators[">="] = ">="; 58 | operators["."] = "+"; 59 | operators[".="] = "+="; 60 | operators["="] = "="; 61 | //jumbs 62 | operators["("] = "("; 63 | operators[")"] = ")"; 64 | operators["}"] = "}"; 65 | operators["{"] = "{"; 66 | } 67 | 68 | bool transpiler::isOperator(string token) { 69 | return (operators.find(token) != operators.end()); 70 | } 71 | 72 | bool transpiler::isKeyword(string token) { 73 | return (keywords.find(token) != keywords.end()); 74 | } 75 | 76 | bool transpiler::isVariable(string token) { 77 | regex variable("\\$(_|[a-zA-Z])+[a-zA-Z0-9]+"); 78 | return regex_match(token, variable); 79 | } 80 | 81 | string transpiler::getLibraries() { 82 | string aux = ""; 83 | 84 | for(string i : libs) 85 | aux.append(i).append("\n"); 86 | 87 | return aux; 88 | } 89 | 90 | bool transpiler::isType(string token) { 91 | return find(types.begin(), types.end(), token) != types.end(); 92 | } 93 | 94 | bool transpiler::isIO(string token) { 95 | regex io("\\$(_POST|_GET|_REQUEST)\\[(\'[0-9a-zA-Z]+\')\\]"); 96 | return regex_match(token, io); 97 | } 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | -------------------------------------------------------------------------------- /transpiler.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Antony Alkmim on 18/05/16. 3 | // 4 | 5 | #ifndef PHPTOCPP_TRANSPILER_H 6 | #define PHPTOCPP_TRANSPILER_H 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | class transpiler { 15 | public: 16 | map operators; // [+,-,/,*...] 17 | map keywords; // if, echo 18 | vector libs; //#include ..... 19 | vector types; // int, float .... 20 | vector variables; 21 | public: 22 | transpiler(); 23 | string getLibraries(); 24 | bool isType(string token); 25 | bool isIO(string token); 26 | bool isOperator(string token); 27 | bool isKeyword(string token); 28 | bool isVariable(string token); 29 | }; 30 | 31 | 32 | #endif //PHPTOCPP_TRANSPILER_H 33 | --------------------------------------------------------------------------------