summaryrefslogtreecommitdiff
path: root/main.cpp
blob: 98dbec9b494aef31607b0505d967fd348c14f1db (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 <cstring>
#include "evaluate.h"
#include "parser.h"

using namespace std;


string readstream(istream &in){
	string res;
	char buf[4096];
	while(true){
		in.read(buf,sizeof(buf));
		if(in.gcount()==0)break;
		res.append(buf,in.gcount());
	}
	return res;
}

int main(int argc,char **argv){
	string source,filename;
	bool fromstdin=false;
	if(argc==1)fromstdin=true;
	else if(argc==2){
		if(strcmp(argv[1],"-")==0)fromstdin=true;
		else filename=argv[1];
	} else {
		cerr<<"Pass source on stdin, or filename as argument (or '-' for stdin)"<<endl;
		return 1;
	}
	if(fromstdin){
		source=readstream(cin);
		filename="<stdin>";
	} else {
		ifstream f(filename);
		source=readstream(f);
	}

	StatementList stl;
	try {
		stl=parse(source,filename);
	} catch(ParseError e){
		cerr<<e.what()<<endl;
		return 1;
	}
	// cout<<stl<<endl;
	Assembly as(stl);
	cout<<as<<flush;
	return 0;
}