1 module demo;
2 version (demo){
3 	import utils.misc;
4 	import qscript.qscript;
5 	import std.datetime.stopwatch;
6 	import std.stdio;
7 	/// runs the scripts using QScript
8 	class ScriptExec{
9 	private:
10 		/// where the magic happens
11 		QScript _qscript;
12 		/// writeln function
13 		QData* writeln(QData*[] args){
14 			std.stdio.writeln (args[0].strVal);
15 			return null;
16 		}
17 		/// write function
18 		QData* write(QData*[] args){
19 			std.stdio.write (args[0].strVal);
20 			return null;
21 		}
22 		/// write int
23 		QData* writeInt(QData*[] args){
24 			std.stdio.write(args[0].intVal);
25 			return null;
26 		}
27 		/// write double
28 		QData* writeDbl(QData*[] args){
29 			std.stdio.write(args[0].doubleVal);
30 			return null;
31 		}
32 		/// readln function
33 		QData* readln(QData*[] args){
34 			string s = std.stdio.readln;
35 			s.length--;
36 			return new QData(s);
37 		}
38 	public:
39 		/// constructor
40 		this (){
41 			_qscript = new QScript(
42 				[
43 					Function("writeln", DataType("void"), [DataType("string")]),
44 					Function("write", DataType("void"), [DataType("string")]),
45 					Function("write", DataType("void"), [DataType("int")]),
46 					Function("write", DataType("void"), [DataType("double")]),
47 					Function("readln", DataType("string"), [])
48 				],
49 				[
50 					&writeln,
51 					&write,
52 					&writeInt,
53 					&writeDbl,
54 					&this.readln
55 				]
56 			);
57 		}
58 		/// compiles & returns byte code
59 		string[] compileToByteCode(string[] script, ref CompileError[] errors){
60 			errors = _qscript.compile(script);
61 			return _qscript.byteCodeToString;
62 		}
63 		/// executes the first function in script
64 		QData execute(QData[] args){
65 			return _qscript.execute(0, args);
66 		}
67 	}
68 
69 	void main (string[] args){
70 		debug{
71 			args = args[0]~["sample.qs", "out.bcode"];
72 		}
73 		if (args.length < 2){
74 			writeln("not enough args. Usage:");
75 			writeln("./demo [script] [bytecode, output, optional]");	
76 		}else{
77 			StopWatch sw;
78 			ScriptExec scr = new ScriptExec();
79 			CompileError[] errors;
80 			string[] byteCode = scr.compileToByteCode(fileToArray(args[1]), errors);
81 			if (errors.length > 0){
82 				writeln("Compilation errors:");
83 				foreach (err; errors){
84 					writeln ("line#",err.lineno, ": ",err.msg);
85 				}
86 			}else{
87 				if (args.length > 2){
88 					byteCode.arrayToFile(args[2]);
89 				}
90 				// now execute main
91 				sw.start;
92 				scr.execute([]);
93 				sw.stop;
94 				writeln("execution took: ", sw.peek.total!"msecs", "msecs");
95 			}
96 		}
97 	}
98 }