-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
162 lines (143 loc) · 4.8 KB
/
Copy pathmain.cpp
File metadata and controls
162 lines (143 loc) · 4.8 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <string>
#include <print>
#include <optional>
#include <fstream>
#include <string_view>
#include <memory>
#include <format>
#include <map>
#include <vector>
#include "globals.h"
#include "proof.h"
#include "expression.h"
#include "statement.h"
#include "syntax_error.h"
#include "parser.h"
#include "evaluate_proof.h"
using std::string_view_literals::operator""sv;
using std::vector;
using std::map;
using std::pair;
using std::shared_ptr;
using std::string_view;
[[nodiscard]] std::string readFile(const std::string& path)
{
std::ifstream f(path, std::ios::binary | std::ios::ate);
if (!f) throw "Cannot open file " + path;
auto size = f.tellg();
if (size <= 0) return {};
std::string s(size, '\0');
f.seekg(0);
f.read(s.data(), size);
return s;
}
int main(int nargs, char** args) {
if (nargs < 2) {
std::println("Specify filename");
exit(1);
return 1;
}
try {
std::vector<Syntax> syntaxes;
{
auto leftId = Id::make("a"sv);
auto rightId = Id::make("b"sv);
syntaxes.emplace_back(Syntax{
.name_to_syntaxAndExpr = {},
.syntax = {leftId, rightId},
.expr = std::make_shared<Expression_Apply>(
FileRange::none(),
std::make_shared<Expression_Id>(FileRange::none(), leftId),
std::make_shared<Expression_Id>(FileRange::none(), rightId)
),
.precedence = 10000,
.associativity = LEFT,
});
}
std::vector<ProofSyntax> proofSyntaxes;
Namespace rootNamespace(syntaxes, proofSyntaxes);
ATOM_IMPLIES = std::make_shared<Expression_Id>(FileRange::none(), rootNamespace.make("IMPLIES"sv, FileRange::none()));
std::string filename = args[1];
std::string code = readFile(filename);
/*unsigned int line = 1;
unsigned int col = 1;
{
bool prevWasCR = false;
for (char c : code) {
if (c == '\r') {
line++;
col = 1;
prevWasCR = true;
} else {
if (c == '\n') {
if (!prevWasCR) {
line++;
col = 1;
}
} else {
col++;
}
prevWasCR = false;
}
}
}
FilePos codeStart = FilePos{.index = 0, .line = 1, .col = 1};
FilePos codeEnd = FilePos{.index = (unsigned int)code.size(), .line = line, .col = col};*/
Parser parser(code/*, {TextSource{.range = FileRange::startEnd(codeStart, codeEnd), .source = FileRange::startEnd(codeStart, codeEnd)}}*/);
vector<shared_ptr<const Statement>> statements;
shared_ptr<const Proof> finalProofExpr;
try {
std::println("Parsing...");
finalProofExpr = parser.readStatementsAndMaybeOneProof(rootNamespace, statements, false);
parser.skipWhitespace();
std::println("Parsed successfully, {} chars left unparsed!", parser.str.size());
} catch (const SyntaxError& se) {
if (!se.fileRange2.has_value()) {
std::print("\n\n\nSyntax error at {}:{}:{}:\n {}\n\n\n", filename, se.fileRange1.start.line, se.fileRange1.start.col, se.message);
} else {
std::print("\n\n\nSyntax error at {}:{}:{} and {}:{}:{}:\n {}\n\n\n", filename, se.fileRange1.start.line, se.fileRange1.start.col, filename, se.fileRange2->start.line, se.fileRange2->start.col, se.message);
}
exit(1);
return 1;
}
map<Id, shared_ptr<const Expression>> proofId_to_provenProp;
try {
vector<Id> proofIdsAdded;
vector<Id> forAnyVarsIntroduced;
vector<shared_ptr<const Expression>> assumptionsIntroduced;
std::println("Evaluating...");
runStatements(statements, proofId_to_provenProp, proofIdsAdded, forAnyVarsIntroduced, assumptionsIntroduced, true);
} catch (const ProofError& pe) {
if (pe.fileRange.length != 0 && pe.fileRange2.length != 0 && pe.fileRange3.length != 0) {
std::print("\n\n\nProof error at {}:{}:{}, {}:{}:{}, {}:{}:{}:\n {}\n\n\n",
filename, pe.fileRange.start.line, pe.fileRange.start.col, filename, pe.fileRange2.start.line, pe.fileRange2.start.col, filename, pe.fileRange3.start.line, pe.fileRange3.start.col, pe.message);
} else if (pe.fileRange.length != 0 && pe.fileRange2.length != 0) {
std::print("\n\n\nProof error at {}:{}:{}, {}:{}:{}:\n {}\n\n\n", filename, pe.fileRange.start.line, pe.fileRange.start.col, filename, pe.fileRange2.start.line, pe.fileRange2.start.col, pe.message);
} else if (pe.fileRange.length != 0) {
std::print("\n\n\nProof error at {}:{}:{}:\n {}\n\n\n", filename, pe.fileRange.start.line, pe.fileRange.start.col, pe.message);
} else {
std::print("\n\n\nProof error in {}:\n {}\n\n\n", filename, pe.message);
}
}
} catch (int i) {
std::print("\n\nERROR: {}\n\n", i);
exit(1);
return 1;
} catch (long i) {
std::print("\n\nERROR: {}\n\n", i);
exit(1);
return 1;
} catch (const char* msg) {
std::print("\n\nERROR: {}\n\n", msg);
exit(1);
return 1;
} catch (const std::string& msg) {
std::print("\n\nERROR: {}\n\n", msg);
exit(1);
return 1;
} catch (std::string_view msg) {
std::print("\n\nERROR: {}\n\n", msg);
exit(1);
return 1;
}
}