it is now possible to register buildins in VM
[gaemu.git] / fvm.d
blob187a10c7e963803def72d410c0588bdcd7d4ed25
1 module frex is aliced;
3 import std.stdio;
5 import gmlparser;
6 import gmlparser.anal;
8 import ungmk;
9 import loader;
11 import gmlvm;
14 // ////////////////////////////////////////////////////////////////////////// //
15 void registerPrims (VM vm) {
16 vm["write"] = (VM self, Real* bp, ubyte argc) {
17 import std.stdio : stdout;
18 foreach (immutable idx; 0..argc) {
19 auto v = bp[vm.Slot.Argument0+idx];
20 if (v.isString) stdout.write(vm.getDynStr(v.getStrId)); else stdout.write(v);
22 stdout.flush();
24 vm["writeln"] = (VM self, Real* bp, ubyte argc) {
25 import std.stdio : stdout;
26 foreach (immutable idx; 0..argc) {
27 auto v = bp[vm.Slot.Argument0+idx];
28 if (v.isString) stdout.write(vm.getDynStr(v.getStrId)); else stdout.write(v);
30 stdout.writeln;
31 stdout.flush();
34 vm["string_length"] = (string s) => s.length;
38 // ////////////////////////////////////////////////////////////////////////// //
39 void main (string[] args) {
40 bool dumpFileNames = false;
41 bool doScripts = true;
42 bool doActions = true;
43 bool measureTime = false;
45 NodeFunc[] funcs;
47 bool nomore = false;
48 string[] scargs;
49 foreach (string fname; args[1..$]) {
50 import std.file;
51 import std.path;
52 if (nomore) {
53 scargs ~= fname;
54 } else {
55 if (fname.length == 0) continue;
56 if (fname == "--") { nomore = true; continue; }
57 if (fname == "-d") { dumpFileNames = true; continue; }
58 if (fname == "-S") { doScripts = false; continue; }
59 if (fname == "-A") { doActions = false; continue; }
60 if (fname == "--time") { measureTime = true; continue; }
61 if (fname[0] == '@') {
62 if (fname.length < 2) assert(0, "gmk file?");
63 auto gmk = new Gmk(fname[1..$]);
64 funcs ~= gmkLoadScripts(gmk, doScripts:doScripts, doActions:doActions, warnings:false, checkReturns:false);
65 continue;
67 if (isDir(fname)) {
68 foreach (auto de; dirEntries(fname, "*.gm[lx]", SpanMode.breadth)) {
69 bool doit = true;
70 foreach (auto pt; pathSplitter(de.dirName)) {
71 if (pt.length && pt[0] == '_') { doit = false; break; }
73 if (doit) {
74 if (dumpFileNames) { import std.stdio; writeln("loading '", de.name, "'..."); }
75 funcs ~= loadScript(de.name, false);
78 } else {
79 if (dumpFileNames) { import std.stdio; writeln("loading '", fname, "'..."); }
80 funcs ~= loadScript(fname, false);
85 if (funcs.length > 0) {
86 import core.time;
87 auto vm = new VM();
88 vm.registerPrims();
89 writeln(funcs.length, " function", (funcs.length > 1 ? "s" : ""), " parsed");
90 foreach (auto fn; funcs) {
91 vm.compile(fn);
93 if (measureTime) writeln("executing...");
94 auto stt = MonoTime.currTime;
95 auto res = vm.exec("main");
96 auto dur = (MonoTime.currTime-stt).total!"msecs";
97 writeln(res);
98 if (measureTime) writeln("total execution took ", dur, " milliseconds");