added documentation on gm8 .exe format
[gaemu.git] / fvm.d
blob2ccb2c9ae73198f4dcae1db6c5b79371be7e5159
1 module frex is aliced;
3 import std.stdio;
5 import gaem.parser;
6 import gaem.utils;
7 import gaem.runner;
10 // ////////////////////////////////////////////////////////////////////////// //
11 void registerPrims (VM vm) {
12 vm["write"] = (VM self, Real* bp, ubyte argc) {
13 import std.stdio : stdout;
14 foreach (immutable idx; 0..argc) {
15 auto v = bp[vm.Slot.Argument0+idx];
16 if (v.isString) stdout.write(getDynStr(v.getStrId)); else stdout.write(v);
18 stdout.flush();
20 vm["writeln"] = (VM self, Real* bp, ubyte argc) {
21 import std.stdio : stdout;
22 foreach (immutable idx; 0..argc) {
23 auto v = bp[vm.Slot.Argument0+idx];
24 if (v.isString) stdout.write(getDynStr(v.getStrId)); else stdout.write(v);
26 stdout.writeln;
27 stdout.flush();
30 vm["string_length"] = (string s) => s.length;
34 // ////////////////////////////////////////////////////////////////////////// //
35 void main (string[] args) {
36 bool measureTime = false;
38 NodeFunc[] funcs;
40 funcs = cliProcessArgs!(
41 "--time", (fname) { measureTime = true; },
42 )(args);
44 if (funcs.length > 0) {
45 import core.time;
46 auto vm = new VM();
47 vm.registerPrims();
48 writeln(funcs.length, " function", (funcs.length > 1 ? "s" : ""), " parsed");
49 foreach (auto fn; funcs) {
50 vm.compile(fn);
52 if (measureTime) writeln("executing...");
53 auto stt = MonoTime.currTime;
54 auto res = vm.exec("main");
55 auto dur = (MonoTime.currTime-stt).total!"msecs";
56 writeln(res);
57 if (measureTime) writeln("total execution took ", dur, " milliseconds");