-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseessentials.cpp
More file actions
76 lines (63 loc) · 3.34 KB
/
Copy pathparseessentials.cpp
File metadata and controls
76 lines (63 loc) · 3.34 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
#include "parseessentials.h"
#include "vartype.h"
std::vector<Statement*> ParseEssentials::toplevel_statements;
Environment ParseEssentials::toplevel_environment;
void ParseEssentials::parseNewStatements()
{
static int parsedStatements = 0;
while(parsedStatements != ParseEssentials::toplevel_statements.size()){
Statement * statement = ParseEssentials::toplevel_statements[parsedStatements++];
parseStatement(statement);
}
} // parse new statements
void ParseEssentials::parseStatement(Statement *statement)
{
ParseEssentials::toplevel_environment.printNewValues(); // for the first time, print system functions
std::cout << getColorCode(GREEN_CODE, true, true) << "---------Parsing Statement-------------" << getColorCode(RESET_CODE) << std::endl;
int statement_good = true;
Environment environment_backup = ParseEssentials::toplevel_environment;
std::cout << statement->print(0) << std::endl;
std::cout << getColorCode(WHITE_CODE, true, true) << "---------Determine Types---------------" << getColorCode(RESET_CODE) << std::endl;
Type type;
try{
type = statement->deduceType(ParseEssentials::toplevel_environment, ParseEssentials::toplevel_environment.getNewPolymorphicType());
//std::cout << "Relations:\n" << ParseEssentials::toplevel_environment.relationsToString();
std::cout << "Deduced type: " << ParseEssentials::toplevel_environment.renumeratedToSmallest(type) << std::endl;
} catch(std::runtime_error ex){
std::cout << getColorCode(RED_CODE, true, true) << "Type deduction for statement failed:\n" << ex.what() << std::endl;
statement_good = false;
ParseEssentials::toplevel_environment = environment_backup;
}
std::cout << getColorCode(WHITE_CODE, true, true) << "--------- Execute ---------------" << getColorCode(RESET_CODE) << std::endl;
if(statement_good){
try{
Value* retVal = statement->execute(ParseEssentials::toplevel_environment);
if(retVal != nullptr){
//std::cout << retVal->printValue() << std::endl;
ParseEssentials::toplevel_environment.addValue(Identifier("<_>"), retVal);
ParseEssentials::toplevel_environment.resetIdentifierType(Identifier("<_>"), type);
}
else{
std::cout << "Some statement, no value to print..." << std::endl;
}
ParseEssentials::toplevel_environment.printNewValues();
}
catch(std::runtime_error ex){
std::cout << getColorCode(RED_CODE, true, true) << "Error during execution: " << ex.what() << std::endl;
ParseEssentials::toplevel_environment = environment_backup;
}
}
else{
std::cout << getColorCode(YELLOW_CODE, true, true) << "Statement not proper. Skipping..." << std::endl;
}
//ParseEssentials::toplevel_environment.cleanupAfterStatement();
std::cout << getColorCode(WHITE_CODE, true, true) << "--------Statement Parsing Done---------" << getColorCode(RESET_CODE) << std::endl;
} // parseStatement
std::string ParseEssentials::getColorCode(int color_id, bool bright, bool bold, bool background)
{
#ifdef COLOR_OUTPUT
return std::string("\x1b[") + std::to_string(color_id+(background?(bright?60:0)+10:color_id==0?0:(bright?60:0))) + (bold?";1":"") + "m";
#else
return std::string();
#endif
}