-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageVisitor.cpp
117 lines (108 loc) · 3.96 KB
/
ImageVisitor.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "ImageVisitor.h"
using namespace std;
ImageVisitor::ImageVisitor() {
sketch = new Sketch("test", false);
}
void ImageVisitor::visitFile(ProcessingParser::SketchContext *ctx) {
if (ctx->activeMode()) {
for (auto classBody : ctx->activeMode()->classBodyDeclaration()) {
if (classBody->memberDeclaration()) {
auto method = classBody->memberDeclaration()->methodDeclaration();
if (method) {
string methodName = method->IDENTIFIER()->getText();
ProcessingParser::BlockContext *methodBlock = method->methodBody()->block();
if (methodName == "setup") {
for (auto blockStatement : methodBlock->blockStatement()) {
visitBlockStatement(blockStatement);
}
} else if (methodName == "draw") {
for (auto blockStatement : methodBlock->blockStatement()) {
visitBlockStatement(blockStatement);
}
}
}
}
}
} else if (ctx->staticMode()) {
for (auto blockStatement : ctx->staticMode()->blockStatement()) {
visitBlockStatement(blockStatement);
}
}
}
string ImageVisitor::getExpression(ProcessingParser::ExpressionContext *exp) {
if(exp->mathFunction()) {
auto math = exp->mathFunction();
if (math->mathSinCos()) {
if (math->mathSinCos()->SIN()) {
return to_string(sin(stof(getExpression(math->mathSinCos()->expression()))));
} else {
return to_string(cos(stof(getExpression(math->mathSinCos()->expression()))));
}
} else if(math->ABS_FUNC()) {
return to_string(abs(stof(getExpression(math->expression()))));
}
}
else if(exp->MUL()) {
return to_string(stof(getExpression(exp->expression(0))) * stof(getExpression(exp->expression(1))));
}
else if(exp->DIV()) {
return to_string(stof(getExpression(exp->expression(0))) / stof(getExpression(exp->expression(1))));
}
else if(exp->SUBTRACT()) {
return to_string(stof(getExpression(exp->expression(0))) - stof(getExpression(exp->expression(1))));
}
else if(exp->ADD()) {
return to_string(stof(getExpression(exp->expression(0))) + stof(getExpression(exp->expression(1))));
}
else if(exp->FRAMECOUNT()) {
return to_string(ofGetFrameNum());
} else if (exp->primary()->IDENTIFIER()) {
return memory[exp->getText()];
} else {
return exp->getText();
}
}
void ImageVisitor::visitBlockStatement(ProcessingParser::BlockStatementContext *blockStatement) {
if (blockStatement->localVariableDefinition()) {
string variableName = blockStatement->localVariableDefinition()->IDENTIFIER()->getText();
memory[variableName] = getExpression(blockStatement->localVariableDefinition()->expression());
return;
}
for (auto expression : blockStatement->statement()->expression()) {
if (expression->apiFunction()) {
visitAction(expression->apiFunction());
}
}
}
void ImageVisitor::visitAction(ProcessingParser::ApiFunctionContext *ctx) {
vector<string> arguments;
if (ctx -> apiDraw()) {
ProcessingParser::ApiDrawContext* draw = ctx -> apiDraw();
if (draw -> circleFunction()) {
auto three = draw -> circleFunction();
string funcName = "circle";
sketch->drawThree(funcName, three->expression(0)->getText(),
three->expression(1)->getText(), three->expression(2)->getText());
} else if (draw -> drawFourDecimal()) {
auto four = draw -> drawFourDecimal();
string funcName = four->drawFourDecimalShape()->getText();
sketch->drawFour(funcName, getExpression(four->expression(0)),
getExpression(four->expression(1)), getExpression(four->expression(2)),
getExpression(four->expression(3)));
}
} else if (ctx -> apiColor()) {
ProcessingParser::ApiColorContext* draw = ctx -> apiColor();
sketch->drawOne(draw->colorFunction()->getText(), visitColor(draw -> colorLiteral()));
} else {
cout<<"neither functions satisfied";
}
}
string ImageVisitor::visitColor(ProcessingParser::ColorLiteralContext *ctx) {
if (ctx->hexColorLiteral()) {
return ctx->hexColorLiteral()->hexColorValue()->getText();
}
return "";
}
antlrcpp::Any ImageVisitor::visitShape(ProcessingParser::DrawShapeContext *ctx) {
return ctx->getText();
}