aboutsummaryrefslogtreecommitdiff
path: root/main.cpp
blob: 4b4fc460e4f7326702ca508a8e2ac9a615736bd3 (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 <istream>

#include "Parser.h"
#include "Circuit.h"
#include "Exception.h"

using std::cout;
using std::endl;
using std::ifstream;
using std::istream;

istream * open_input(int argc, char** argv) {
	if (argc > 1) {
		ifstream * file = new ifstream(argv[1]);
		if (!file->is_open()) return nullptr;
		return file;
	}

	return &std::cin;
}

int main(int argc, char** argv) {
	istream * input = open_input(argc, argv);
	if (input == nullptr) {
		cout << "Could not open file" << endl;
		return EXIT_FAILURE;
	}

	Circuit circuit;
	Parser parser(circuit);

	try {
		*input >> parser;
	} catch (ParserException & e) {
		cout << "Parser error: " << e.what() << endl;
		return EXIT_FAILURE;
	}

	try {
	  circuit.sim();
	} catch (CircuitException & e) {
	  cout << "Circuit error: " << e.what() << endl;
	  return EXIT_FAILURE;
	}

	cout << circuit.result();
	return EXIT_SUCCESS;
}