summaryrefslogtreecommitdiff
path: root/postrun.cpp
blob: c8803377f6074c2aacda4f0985a1bc59251db58e (plain)
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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sys/time.h>

#include "runtime.h"

using namespace std;

int g_argc;
char **g_argv;

bool input_is_stdin=false;

string readfile(ifstream stream){
	stream.seekg(0,ios::end);
	size_t len=stream.tellg();
	stream.seekg(0,ios::beg);
	string contents;
	contents.resize(len);
	stream.read(&*contents.begin(),len);
	return contents;
}

int main(int argc,char **argv){
	if(argc<2){
		cerr<<"Call this interpreter with the Postrun source file"<<endl;
		return 1;
	}

	struct timeval tv;
	gettimeofday(&tv,NULL);
	srand(tv.tv_sec*1000000+tv.tv_usec);

	g_argc=argc-2;
	g_argv=argv+2; //skip this executable and the postrun file

	try {
		ifstream srcf;
		vector<string> tokens;
		if(strcmp(argv[1],"-")==0){
			tokens=tokenise(cin);
			input_is_stdin=true;
		} else {
			srcf.open(argv[1]);
			if(!srcf){
				cerr<<"Could not open file '"<<argv[1]<<"'"<<endl;
				return 1;
			}
			tokens=tokenise(srcf);
		}
		run(tokens);
		if(srcf)srcf.close();
	} catch(string e){
		cerr<<"ERROR: "<<e<<endl;
		return 1;
	}
}