bug com o operator corrigido o suficiente
[CppToLua.git] / cpptolua.y
blob89a1453043002724b0add1703204a1f0f7fb5a7a
1 %{
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <stdarg.h>
6 #include <string.h>
7 #include <string>
8 #include <vector>
9 #include "node.h"
10 using namespace std;
12 #define YY_(x) (char *) (x)
14 int yylex(void);
15 void yyerror(char *);
17 /* if the member has public scope */
18 bool member_scope_public = false;
23 /* yyval type */
24 %union {
25 char *s;
26 struct sNode *n;
27 struct sStringVec *sv;
28 struct sStringVecVec *svv;
29 struct sIdf *i;
32 %token <sv> IDF
33 %token CLASS VISIBILITY
34 %type <sv> idf_list idf_list_opt
35 %type <svv> idf_comma_list idf_comma_list_opt
36 %type <i> function
37 %type <n> class_body
41 //problemas: herdar metodos de outras classes....
42 // nao deve pegar metodos private ou protected.... DONE
43 // tratar tipos com mais de uma palavra (char *)
44 // tratar retorno de tipo composto (classe, diferenciar de enum)
45 // quando o método tiver a palavra operator e nao for um operatorX() ele vai ignorar.... DONE
47 program:
48 CLASS IDF hierarchy_opt '{' class_body '}' ';'
50 printf("ClassName: %s\n", $2->vs[0].c_str());
51 string className = $2->vs[0].c_str();
52 for(int i=0; i<$5->vi.size(); i++)
54 string type = $5->vi[i].type;
55 string fooName = $5->vi[i].name;
56 if(type == "Constructor"
57 || fooName == "" /*caso seja um enum*/
58 //|| fooName.find("operator") != string::npos /*caso seja um operator*/
59 || fooName.substr(0, strlen("operator")) == "operator" /*caso seja um operator*/
60 || !$5->vi[i].isPublic /* caso nao seja publico */)
61 continue;
63 printf("int %s_%s(lua_State* L) {\n", className.c_str(), fooName.c_str());
64 printf("\t%s *c = (%s*) lua_touserdata(L, 1);\n", className.c_str(), className.c_str());
65 for(int j=0; j<$5->vi[i].param.size(); j++)
67 string paramType = $5->vi[i].param[j].type;
68 string paramName = $5->vi[i].param[j].name;
69 if(paramType=="int" || paramType=="float" || paramType=="double")
70 printf("\t%s %s = luaL_checknumber(L, %d);\n", paramType.c_str(), paramName.c_str(), j+2);
73 printf("\t");
74 if(type == "boolean")
75 printf("lua_pushboolean(L, ");
76 else if(type == "char*")
77 printf("lua_pushstring(L, ");
78 else //if(type == "double" || type == "int" || type == "float")
79 printf("lua_pushnumber(L, ");
80 //problema: enums devem ser tratados com pushnumber mas classes definidas pelo usuário
81 //devem ser tratadas com pushlightuserdata. Como fazer????
83 printf("c->%s(", fooName.c_str());
84 for(int j=0; j<$5->vi[i].param.size(); j++)
86 if(j>0) printf(", ");
87 printf("%s", $5->vi[i].param[j].name.c_str());
89 printf(")");
91 if(type != "void") printf(");\n");
92 else printf(";\n");
94 printf("}\n\n");
96 printf("%s: %s %s( ", $2->vs[0].c_str(), $5->vi[i].type.c_str(), $5->vi[i].name.c_str());
97 for(int j=0; j<$5->vi[i].param.size(); j++)
98 printf("%s %s ", $5->vi[i].param[j].type.c_str(), $5->vi[i].param[j].name.c_str());
99 printf(")\n");
105 class_body:
106 VISIBILITY ':' class_body { $$ = $3; }
107 | function class_body { $$ = $2; $$->vi.push_back(*$1); }
108 | idf_comma_list ';' class_body { $$ = $3; }
109 | /* empty */ { $$ = new Node; }
112 hierarchy_opt:
113 ':' VISIBILITY idf_comma_list
114 | /* empty */
117 idf_comma_list_opt:
118 idf_comma_list { $$ = $1; }
119 | /* empty */ { $$ = new StringVecVec; }
122 idf_comma_list:
123 idf_list { $$ = new StringVecVec; $$->vvs.push_back($1->vs); }
124 | idf_comma_list ',' idf_list { $$ = $1; $$->vvs.push_back($3->vs); }
127 idf_list_opt:
128 idf_list { $$ = $1; }
129 | /* empty */ { $$ = new StringVec; }
132 idf_list:
133 idf_list IDF { $$ = $1; $$->vs.push_back($2->vs[0]); }
134 | IDF { $$ = $1; }
137 function:
138 idf_list '(' idf_comma_list_opt ')' idf_list_opt function_body
141 $$ = new Idf;
142 if($1->vs.size() >= 2) //se nao for construtor/destrutor, vai ter um tipo
143 $$->type = $1->vs[$1->vs.size()-2];
144 else
145 $$->type = "Constructor";
146 $$->name = $1->vs[$1->vs.size()-1];
147 //for(int i=0; i<$1->vs.size(); i++)
148 // printf("%s ", $1->vs[i].c_str());
149 vector<Idf> vp;
150 for(int i=0; i<$3->vvs.size(); i++)
152 Idf p;
153 p.type = $3->vvs[i][$3->vvs[i].size()-2];
154 p.name = $3->vvs[i][$3->vvs[i].size()-1];
155 vp.push_back(p);
157 $$->param = vp;
158 $$->isPublic = member_scope_public;
159 /* This shows what functions are in the data structure
160 printf("%s %s( ", $$->type.c_str(), $$->name.c_str());
161 for(int i=0; i<$$->param.size(); i++)
162 printf("%s %s ", $$->param[i].type.c_str(), $$->param[i].name.c_str());
163 printf(")\n");
165 //for(int i=0; i<$3->vvs.size(); i++)
166 // for(int j=0; j<$3->vvs[i].size(); j++)
167 //printf("%s ", $3->vvs[i][j].c_str());
168 //printf("\n");
170 | idf_list '{' '}' ';' { $$ = new Idf; } // for enum
173 function_body:
175 | '{' block '}'
178 block:
179 idf_list ';'
180 | idf_comma_list ';'
181 | '{' block '}'
182 | /* empty */
187 int main(int argc, char **argv)
189 yyparse();
190 return 0;
193 void yyerror(char *s) {
194 fprintf(stdout, "%s\n", s);