1 /++
2 To create a executable which serves as a front end to just the compiler.  
3 
4 This can be used to:
5 * convert script to AST, and output the AST in JSON
6 * convert script to byte code, and output the byte code (to be added)
7 * optimize existing byte code, and output new optimized byte code (to be added, when optimizer is added)
8 * convert script to byte code, optimize it, and output optimized byte code (to be added, when optimizer is added)
9 +/
10 module compiler;
11 
12 import std.stdio;
13 import std.file;
14 
15 import qscript.compiler.compiler;
16 import qscript.compiler.misc : CompileError;
17 import qscript.compiler.misc : Function;
18 
19 import utils.misc;
20 
21 version(compiler){
22 	void main(string[] args){
23 		if (args.length < 3){
24 			writeln ("not enough args. Usage:\n./compiler CompilationType path/to/script");
25 			writeln ("CompilationType can be:\n* ast - output AST in JSON\n* bytecode - output Byte Code");
26 		}else if (!["ast", "bytecode"].hasElement(args[1])){
27 			writeln ("invalid CompilationType");
28 		}else if (!exists(args[2])){
29 			writeln ("file "~args[2]~" doesn't exist");
30 		}else{
31 			string[] script;
32 			try{
33 				script = fileToArray(args[2]);
34 			}catch (Exception e){
35 				.destroy(e);
36 				writeln ("Error reading file");
37 			}
38 			CompileError[] errors;
39 			if (args[1] == "ast"){
40 				string json = compileScriptAST(script,[],errors);
41 				if (errors.length > 0){
42 					stderr.writeln ("There are errors:");
43 					foreach (error; errors){
44 						stderr.writeln("Line#",error.lineno,": ",error.msg);
45 					}
46 				}
47 				writeln (json);
48 			}else if (args[1] == "bytecode"){
49 				Function[] fMap;
50 				string[] byteCode = compileScript(script, [], errors, fMap);
51 				if (errors.length > 0){
52 					stderr.writeln ("There are errors:");
53 					foreach (error; errors){
54 						stderr.writeln("Line#",error.lineno,": ",error.msg);
55 					}
56 				}
57 				foreach (line; byteCode){
58 					writeln(line);
59 				}
60 			}
61 		}
62 	}
63 }