-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFacts.h
105 lines (92 loc) · 2.2 KB
/
Facts.h
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
#pragma once
#include <string>
#include <list>
#include <vector>
#include <set>
#include <algorithm>
#include "Lexer.h"
#include "Parameter.h"
#include "Utilities.h"
#include "Predicates.h"
#include "Rules.h"
using namespace std;
class Fact {
public:
Id* id;
vector<DLString*>* listOfStrings;
~Fact() {
delete id;
for (unsigned int i = 0; i < listOfStrings->size(); i++) {
delete listOfStrings->at(i);
}
delete listOfStrings;
}
Fact(Lexer& lex) {
try {
id = nullptr;
listOfStrings = nullptr;
id = new Id(lex);
checkFor(lex, "LEFT_PAREN");
listOfStrings = new vector<DLString*>();
while (true) {
listOfStrings->push_back(new DLString(lex));
if (lex.getCurrentToken().getTokenType() != "COMMA") break;
lex.advanceTokens();
}
checkFor(lex, "RIGHT_PAREN");
checkFor(lex, "PERIOD");
}
catch (const std::runtime_error&) {
if (id) {
delete id;
}
if (listOfStrings) {
for (unsigned int i = 0; i < listOfStrings->size(); i++) {
delete listOfStrings->at(i);
}
delete listOfStrings;
}
throw std::runtime_error("Deleted");
}
}
};
class Facts {
public:
vector<Fact*>* factList;
~Facts() {
for (unsigned int i = 0; i < factList->size(); i++) {
delete factList->at(i);
}
delete factList;
}
Facts(Lexer& lex) {
checkFor(lex, "FACTS");
checkFor(lex, "COLON");
try {
factList = nullptr;
factList = new vector<Fact* >();
while (lex.getCurrentToken().getTokenType() == "ID") {
factList->push_back(new Fact(lex));
}
}
catch (const std::runtime_error&) {
if (factList) {
for (unsigned int i = 0; i < factList->size(); i++) {
delete factList->at(i);
}
delete factList;
}
throw std::runtime_error("Deleted");
}
}
void toString() {
cout << "Facts(" + to_string(factList->size()) + "):" << endl;
for (unsigned int i = 0; i < factList->size(); i++) {
cout << " " << factList->at(i)->id->value << "(";
for (unsigned int j = 0; j < factList->at(i)->listOfStrings->size() - 1; j++) {
cout << factList->at(i)->listOfStrings->at(j)->toString() << ",";
}
cout << factList->at(i)->listOfStrings->at(factList->at(i)->listOfStrings->size() - 1)->toString() << ")." << endl;
}
}
};