2005-05-01 11:48:36 +00:00
|
|
|
#include "Parser.h"
|
|
|
|
#include "Nil.h"
|
|
|
|
#include "Cons.h"
|
|
|
|
#include "Lambda.h"
|
|
|
|
#include "defs.h"
|
|
|
|
#include "qfile.h"
|
|
|
|
#include "string.h"
|
|
|
|
#include "builtins.h"
|
|
|
|
#include "Compiler.h"
|
|
|
|
#include "Machine.h"
|
|
|
|
#include "CompiledCode.h"
|
|
|
|
|
|
|
|
string readfile (string filename)
|
|
|
|
{
|
|
|
|
local string acc = "", res;
|
|
|
|
local QFile file = Qopen (filename, "r");
|
|
|
|
while (!Qeof (file)) {
|
2005-05-02 04:58:22 +00:00
|
|
|
acc += Qgetline (file);
|
2005-05-01 11:48:36 +00:00
|
|
|
}
|
|
|
|
Qclose (file);
|
|
|
|
res = str_new();
|
|
|
|
str_copy(res, acc);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
integer main (integer argc, string []argv)
|
|
|
|
{
|
|
|
|
local Parser parser;
|
|
|
|
local CompiledCode code;
|
|
|
|
local Compiler comp;
|
|
|
|
local Machine vm;
|
|
|
|
local Lambda lm;
|
2005-05-08 06:38:01 +00:00
|
|
|
local SchemeObject stuff, res;
|
2005-05-01 11:48:36 +00:00
|
|
|
|
|
|
|
if (argc < 1) {
|
|
|
|
return -1;
|
|
|
|
}
|
2005-05-06 23:25:06 +00:00
|
|
|
|
|
|
|
//traceon();
|
2005-05-01 11:48:36 +00:00
|
|
|
|
2005-05-02 04:58:22 +00:00
|
|
|
parser = [Parser newFromSource: readfile(argv[1]) file: argv[1]];
|
2005-05-01 11:48:36 +00:00
|
|
|
vm = [Machine new];
|
2005-05-02 02:33:44 +00:00
|
|
|
[vm makeRootCell];
|
2005-05-06 23:25:06 +00:00
|
|
|
[parser makeRootCell];
|
|
|
|
builtin_addtomachine (vm);
|
2005-05-01 11:48:36 +00:00
|
|
|
while ((stuff = [parser read])) {
|
2005-05-02 04:58:22 +00:00
|
|
|
if ([stuff isError]) {
|
|
|
|
printf(">> %s: %i\n", [stuff source], [stuff line]);
|
|
|
|
printf(">> Error (%s): %s\n", [stuff type], [stuff message]);
|
|
|
|
return -1;
|
|
|
|
}
|
2005-05-01 11:48:36 +00:00
|
|
|
comp = [Compiler newWithLambda: cons ([Symbol forString: "lambda"],
|
|
|
|
cons ([Nil nil],
|
|
|
|
cons(stuff, [Nil nil])))
|
2011-01-14 03:07:40 +00:00
|
|
|
scope: nil];
|
2005-05-02 04:58:22 +00:00
|
|
|
code = (CompiledCode) [comp compile];
|
|
|
|
if ([code isError]) {
|
|
|
|
printf(">> %s: %i\n", [code source], [code line]);
|
|
|
|
printf(">> Error (%s): %s\n", [code type], [code message]);
|
|
|
|
return -1;
|
|
|
|
}
|
2011-01-14 03:07:40 +00:00
|
|
|
lm = [Lambda newWithCode: code environment: nil];
|
2005-05-01 11:48:36 +00:00
|
|
|
[lm invokeOnMachine: vm];
|
2005-05-08 06:38:01 +00:00
|
|
|
res = [vm run];
|
|
|
|
if ([res isError]) {
|
|
|
|
printf(">> %s: %i\n", [res source], [res line]);
|
|
|
|
printf(">> Error (%s): %s\n", [res type], [res message]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
[vm reset];
|
2005-05-01 11:48:36 +00:00
|
|
|
}
|
2005-05-06 23:25:06 +00:00
|
|
|
[SchemeObject finishCollecting];
|
2005-05-01 11:48:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|