-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBattler.cpp
97 lines (75 loc) · 2.41 KB
/
Battler.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
#include "Parser.h"
#include "expression.h"
#include "vm/game.h"
#include "Compiler.h"
using namespace Battler;
std::string GetErrorString(std::string errorTypeString, std::string reason, Token t, vector<string> lines) {
std::stringstream ss;
ss << "Error: " << errorTypeString << " on line " << t.l << std::endl;
ss << lines[t.l-1] << std::endl;
for(int i=0; i<t.c; i++) {
ss << "_";
}
ss << "^" << std::endl << std::endl;
ss << "Reason: " << reason << std::endl;
return ss.str();
}
int main(int argc, char* argv[]) {
srand(time(NULL));
if (argc < 2) {
std::cout<< "Please call like this: \"battler.exe path/to/main/game/file.battler\"" << std::endl;
return 1;
}
std::ifstream is;
is.open(argv[1]);
if (!is.is_open()) {
std::cout << "Could not find file " << argv[1] << std::endl;
return 1;
}
std::vector<std::string> lines;
std::string line;
while (std::getline(is, line))
{
lines.push_back(line);
}
Program program;
try {
std::cout << "Compiling game file" << std::endl;
program.Compile(lines);
std::cout << "Loading Compiled Game" << std::endl;
program.Run(true);
std::cout << "Running game setup" << std::endl;
program.RunSetup();
while (program.game().winner == -1)
{
cout << "running player " << program.game().currentPlayerIndex << "'s turn" << endl;
program.RunTurn();
}
cout << "The winner is " << program.game().winner << endl;
} catch (VMError e) {
std::cout << "VM Error" << e.reason << endl;
return 1;
} catch (UnexpectedTokenException e) {
std::cout << GetErrorString("unexpected token", e.reason, e.t, lines) << endl;
return 1;
} catch (NameRedeclaredException e) {
std::cout << "Error: redeclaration of name: " << e.name << std::endl;
return 1;
} catch (NoNameException e) {
std::cout << "Error: " << e.name << " is not declared" << std::endl;
return 1;
} catch (OperationError e) {
std::cout << e.reason << endl;
return 1;
}
catch (CompileError e) {
std::cout << GetErrorString("compiler error", e.reason, e.t, lines) << endl;
}
return 0;
}