Menu

[30a0cb]: / mvm.d  Maximize  Restore  History

Download this file

209 lines (165 with data), 4.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
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;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.