1 module standalone;
2 version (qscriptstandalone){
3 	import utils.misc;
4 	import qscript.qscript;
5 	import qscript.compiler.compiler;
6 	import std.datetime.stopwatch;
7 	import std.stdio;
8 
9 	void main (string[] args){
10 		if (args.length < 2){
11 			stderr.writeln("not enough args. Usage:");
12 			stderr.writeln("execute script:\n qscript script/path");
13 			stderr.writeln("print compiled bytecode:\n qscript --bcode script/path");
14 			stderr.writeln("pretty print generated AST:\n qscript --ast script/path");
15 			return;
16 		}
17 		StopWatch sw;
18 		// create an instance of qscript, script name is "demo", not auto imported if used as library, no additional libraries, and enable builtin libraries
19 		QScript scr = new QScript("demo", false, [], true);
20 		string[] script = fileToArray(args[$-1]);
21 		CompileError[] errors;
22 		if (args.length > 2 && args[1] == "--ast"){
23 			QSCompiler compiler = new QSCompiler([],[]); // no need to provide instruction table, we wont be using codeGen
24 			compiler.loadScript(script);
25 			compiler.scriptExports = scr;
26 			if (!compiler.generateTokens || !compiler.generateAST || !compiler.checkAST){
27 				stderr.writeln("There are errors:");
28 				errors = compiler.errors;
29 				foreach (error; errors)
30 					stderr.writeln("Line#",error.lineno,": ",error.msg);
31 			}
32 			writeln(compiler.prettyAST);
33 			.destroy(scr);
34 			.destroy(compiler);
35 			return;
36 		}
37 		QScriptBytecode code = scr.compileScript(script, errors);
38 		if (errors.length > 0 || code is null){
39 			stderr.writeln("Compilation errors:");
40 			foreach (err; errors)
41 				stderr.writeln ("line#",err.lineno, ": ",err.msg);
42 			return;
43 		}
44 		if (args.length > 2 && args[1] == "--bcode"){
45 			foreach (i, line; code.getBytecodePretty)
46 				writeln(cast(integer)i-2, '\t', line);
47 			return;
48 		}
49 		// now execute main
50 		sw.start;
51 		scr.execute(0, []);
52 		sw.stop;
53 		stderr.writeln("execution took: ", sw.peek.total!"msecs", "msecs");
54 		.destroy(scr);
55 	}
56 }