/*
Monster - an advanced game scripting language
Copyright (C) 2007-2009 Nicolay Korslund
Email: <korslund@gmail.com>
WWW: http://monster.snaptoad.com/
This file (mvm.d) is part of the Monster script language
package.
Monster is distributed as free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License
version 3, as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
version 3 along with this program. If not, see
http://www.gnu.org/licenses/ .
*/
module monster.mvm;
import monster.minibos.stdio;
import monster.minibos.c.stdio;
import monster.minibos.stream;
import monster.minibos.cstream;
import monster.vm.dbg;
import monster.compiler.tokenizer;
import monster.compiler.functions;
import monster.monster;
import monster.modules.all;
import monster.modules.console;
import monster.util.string;
const char[] internal =
"class MVM;
import io, timer;
final native exit(int status = 0);
main() {}
"; //"// This is here so emacs doesn't get confused
extern(C) void exit(int status);
TokenArray tokenize(char[] file)
{
// Check if the file exists
if(!vm.vfs.has(file))
fail("File not found: " ~ file);
// Create the stream
auto bf = vm.vfs.open(file);
// Get the BOM and tokenize the stream
auto ef = new EndianStream(bf);
int bom = ef.readBOM();
TokenArray tokens = tokenizeStream(file, ef, bom);
delete bf;
return tokens;
}
import monster.vm.thread;
import monster.vm.fstack;
char[] getLine()
{
const char[] nl = "\n";
char[] res = readln();
if(res.ends(nl))
res = res[0..$-nl.length];
return res;
}
int main(char[][] args)
{
// Create the internal MVM class first.
auto mc_int = vm.loadString(internal, "MVM");
// Add an exit function
mc_int.bind("exit", { exit(stack.popInt()); });
if(args.length < 2)
{
// If no file was given, start in interactive mode.
writefln("Monster interactive mode. Type 'exit' to quit.");
writefln("Use mvm -h for command line options\n");
Console cn = new Console(mc_int.createObject);
// Set the modules imported into the console by default
cn.addImports("io");
// Since we're not sharing the stack with anyone, we can allow
// variables.
cn.allowVar = true;
// Print the prompt first
cn.prompt();
writef(cn.output());
char[] inp = getLine();
while(true)
{
cn.input(inp);
cn.prompt();
writef(cn.output());
inp = getLine();
}
assert(0);
return 0;
}
if(args[1] == "-h")
{
writefln(
"Usage:
%s [options] [script-file]", args[0]);
writefln("
Options:
-t tokenize script and exit
-h show this help
-d debug mode
");
return 0;
}
if(args[1] == "-t")
{
if(args.length < 3)
{
writefln("Missing file argument");
return 1;
}
auto toks = tokenize(args[2]);
foreach(t; toks)
writefln("%s: %s", t.loc.line, t.str);
return 0;
}
char[] file = args[1];
if(args[1] == "-d")
{
if(args.length < 3)
{
writefln("Missing file argument");
return 1;
}
dbg.enableStdout();
file = args[2];
}
// Tokenize the file manually, since we have to determine if it's a
// function or a class
auto toks = tokenize(file);
// Which is it?
if(!MonsterClass.canParse(toks))
{
// Function. Call it in the context of the MVM class.
Function fn;
fn.compile(args[1], toks, mc_int);
fn.call(mc_int.createObject);
}
else
{
// It's a class. Load it.
auto mc = vm.load(toks, args[1]);
if(!mc.childOf(mc_int))
{
writefln("Error: Class '" ~ mc.getName ~
"' must inherit from 'MVM'");
return 1;
}
auto mo = mc.createObject;
// Call the 'main' function.
mo.callVoid("main");
}
// If there are no scheduled objects, then we are done. Exit without
// any messages.
if(scheduler.numTotal == 0)
return 0;
// Continue to run the program until there are no scheduled objects
// left.
while(scheduler.numTotal != 0)
vm.frame(0);
// Leave a message about why we are quitting
writefln("\nNo more scheduled states or idle functions. MVM exiting.");
return 0;
}