tests/TestCompiler.vala
[app.Builder.js] / tests / TestCompiler.vala
1
2 // valac TreeBuilder.vala --pkg libvala-0.24 --pkg posix -o /tmp/treebuilder
3
4 namespace Palete {
5          
6         
7          
8
9         public class TestCompiler : Vala.CodeVisitor {
10
11                 Vala.CodeContext context;
12                 string file;
13                 
14                 public TestCompiler(string file) {
15                         base();
16                         this.file = file;
17                         
18
19                 }
20                 public void dumpCode(string str) {
21                         var ls = str.split("\n");
22                         for (var i=0;i < ls.length; i++) {
23                                 print("%d : %s\n", i+1, ls[i]);
24                         }
25                 }
26                 
27                  
28                 
29                 
30                 public Gee.HashMap<int,string> checkString( )
31                 {
32                         // init context:
33                         var valac = "valac " ;
34                         
35                         context = new Vala.CodeContext ();
36                         Vala.CodeContext.push (context);
37                 
38                         context.experimental = false;
39                         context.experimental_non_null = false;
40                         
41 #if VALA_0_28
42                         var ver=28;
43 #elif VALA_0_26 
44                         var ver=26;
45 #elif VALA_0_24
46                         var ver=24;
47 #elif VALA_0_22 
48                         var ver=22;
49 #endif
50                         
51                         for (int i = 2; i <= ver; i += 2) {
52                                 context.add_define ("VALA_0_%d".printf (i));
53                         }
54                         
55                         
56                         
57                         
58                         
59                         
60                          
61                                 
62                         
63                         // or context.get_vapi_path("glib-2.0"); // should return path..
64                         //context.vapi_directories = vapidirs;
65                         context.report.enable_warnings = true;
66                         context.metadata_directories = { };
67                         context.gir_directories = {};
68                         context.thread = true;
69                         
70                         
71                         this.report = new ValaSourceReport(this.file);
72                         context.report = this.report;
73                         
74                         
75                         context.basedir = "/tmp"; //Posix.realpath (".");
76                 
77                         context.directory = context.basedir;
78                 
79
80                         // add default packages:
81                         //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
82                         context.profile = Vala.Profile.GOBJECT;
83                          
84                         var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
85                         context.root.add_using_directive (ns_ref);
86
87                         var source_file = new Vala.SourceFile (
88                                 context, 
89                                 Vala.SourceFileType.SOURCE, 
90                                         this.file
91                         );
92                         source_file.add_using_directive (ns_ref);
93                         context.add_source_file (source_file);
94                         
95                 // add all the files (except the current one) - this.file.path
96                 var pr = ((Project.Gtk)this.file.project);
97                 if (this.file.build_module.length > 0) {
98                                 var cg =  pr.compilegroups.get(this.file.build_module);
99                                 for (var i = 0; i < cg.sources.size; i++) {
100                                         var path = pr.resolve_path(
101                                                         pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
102                                                         
103                                         if (!FileUtils.test(path, FileTest.EXISTS)) {
104                                                 continue;
105                                         }       
106                          
107                                         if (path == this.file.path.replace(".bjs", ".vala")) {
108                                                 valac += " " + path;
109                                                 continue;
110                                         }
111                                         if (FileUtils.test(path, FileTest.IS_DIR)) {
112                                                 continue;
113                                         }
114                                         //print("Add source file %s\n", path);
115                                         
116                                         valac += " " + path;
117                                         
118                                         if (Regex.match_simple("\\.c$", path)) {
119                                                 context.add_c_source_file(path);
120                                                 continue;
121                                         }
122                                         
123                                         
124                                         var xsf = new Vala.SourceFile (
125                                                 context,
126                                                 Vala.SourceFileType.SOURCE, 
127                                                 path
128                                         );
129                                         xsf.add_using_directive (ns_ref);
130                                         context.add_source_file(xsf);
131                                         
132                                 }
133                         }
134                         // default.. packages..
135                         context.add_external_package ("glib-2.0"); 
136                         context.add_external_package ("gobject-2.0");
137                         // user defined ones..
138                         
139                 var dcg = pr.compilegroups.get("_default_");
140                 for (var i = 0; i < dcg.packages.size; i++) {
141                                 valac += " --pkg " + dcg.packages.get(i);
142                                 context.add_external_package (dcg.packages.get(i));
143                         }
144                 
145                          //Vala.Config.PACKAGE_SUFFIX.substring (1)
146                         
147                         // add the modules...
148                         
149                         
150                         
151                         //context.add_external_package ("libvala-0.24");
152                         
153                         this.report.compile_notice("START", "", 0, "");
154
155                 
156                         //add_documented_files (context, settings.source_files);
157                 
158                         Vala.Parser parser = new Vala.Parser ();
159                         parser.parse (context);
160                         //gir_parser.parse (context);
161                         if (context.report.get_errors () > 0) {
162                                 print("parse got errors");
163                                 ((ValaSourceReport)context.report).dump();
164                                 
165                                 Vala.CodeContext.pop ();
166                                 this.report.compile_notice("END", "", 0, "");
167                                 return this.report.line_errors;
168                         }
169
170
171                         
172                         // check context:
173                         context.check ();
174                         if (context.report.get_errors () > 0) {
175                                 print("check got errors");
176                                 ((ValaSourceReport)context.report).dump();
177                                 Vala.CodeContext.pop ();
178                                 this.report.compile_notice("END", "", 0, "");
179                                 return this.report.line_errors;
180                                 
181                         }
182                         
183                         //context.codegen = new Vala.GDBusServerModule ();
184                         
185                          
186                         context.output = "/tmp/testbuild";
187                         valac += " -o " +context.output;
188                         //context.codegen.emit (context);
189                         /*
190                         var ccompiler = new Vala.CCodeCompiler ();
191                         var cc_command = Environment.get_variable ("CC");
192                         var pkg_config_command = Environment.get_variable ("PKG_CONFIG");
193 #if VALA_0_28
194                         ccompiler.compile (context, cc_command, new string[] { }, pkg_config_command);
195 #else
196                         ccompiler.compile (context, cc_command, new string[] { });
197 #endif
198                         */
199  
200                         Vala.CodeContext.pop ();
201                         //(new Vala.CodeNode()).get_error_types().clear();
202                         //(new Vala.NullType()).get_type_arguments().clear();
203                         (new Vala.NullType(null)).get_type_arguments().clear();
204                         parser = null;
205                         this.report.compile_notice("END", "", 0, "");
206                         print("%s\n", valac);
207                         print("ALL OK?\n");
208                         return this.report.line_errors;
209                 }
210         //
211                 // startpoint:
212                 //
213          
214         }
215 }
216 /*
217 int main (string[] args) {
218
219         var a = new ValaSource(file);
220         a.create_valac_tree();
221         return 0;
222 }
223 */
224
225