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

#include "runtime.h"

using namespace std;

int g_argc;
char **g_argv;

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;
	}
	g_argc=argc-2;
	g_argv=argv+2; //skip this executable and the postrun file
	ifstream srcf(argv[1]);
	if(!srcf){
		cerr<<"Could not open file '"<<argv[1]<<"'"<<endl;
		return 1;
	}

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

	//string source=readfile(srcf);
	try {
		const vector<string> tokens=tokenise(srcf);
		//for(const string &tok : tokens)cerr<<'['<<tok<<"] "; cerr<<endl;
		run(tokens);
		srcf.close();
	} catch(string e){
		cerr<<"ERROR: "<<e<<endl;
		return 1;
	}
}