-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSemantics.cpp
More file actions
162 lines (151 loc) · 4.38 KB
/
Semantics.cpp
File metadata and controls
162 lines (151 loc) · 4.38 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 "Semantics.h"
double Parameter = 0.0;
double Origin_x = 0.0, Origin_y = 0.0;
double Rot_angle = 0.0;
double Scale_x = 1.0, Scale_y = 1.0;
double forStart, forEnd, forStep;
double GetExprValue(struct ExprNode *root)
{
if(root == NULL)
{
return 0.0;
}
int opCode = root->OpCode;
//printf("[DEBUG] GetExprValue %d\n", opCode);
if(opCode >= 14 && opCode <= 18)
{
double left = GetExprValue(root->Content.CaseOperator.Left);
double right = GetExprValue(root->Content.CaseOperator.Right);
switch(root->OpCode)
{
case PLUS:
return left + right;
case MINUS:
return left - right;
case MUL:
return left * right;
case DIV:
return left / right;
case POWER:
return pow(left, right);
}
}else if(opCode == FUNC)
{
return (*root->Content.CaseFunc.MathFuncPtr)(GetExprValue(root->Content.CaseFunc.Child));
}else if(opCode == CONST_ID)
{
return root->Content.CaseConst;
}else if(opCode == T)
{
return Parameter;
}else return 0.0;
}
void CalcCoordinate(struct ExprNode *xRoot, struct ExprNode *yRoot, double *x_val, double *y_val)
{
double local_x, local_y, temp;
//printf("[DEBUG] CalcCoordinate \n");
local_x = GetExprValue(xRoot);
local_y = GetExprValue(yRoot);
//printf("[DEBUG] CalcCoordinate %f %f\n", local_x, local_y);
local_x *= Scale_x;
local_y *= Scale_y;
temp = local_x * cos(Rot_angle) + local_y * sin(Rot_angle);
local_y = local_y * cos(Rot_angle) - local_x * sin(Rot_angle);
local_x = temp;
local_x += Origin_x;
local_y += Origin_y;
*x_val = local_x;
*y_val = local_y;
}
/*
void DrawPixel(unsigned long x, unsigned long y)
{
#ifdef _VC_COMPILER
SetPixel(hDC, x, y, red);
SetPixel(hDC, x+1, y, red);
SetPixel(hDC, x, y+1, red);
SetPixel(hDC, x+1, y+1, red);
#endif
#ifdef _BC_COMPILER
putpixel(x, y, white);
putpixel(x+1, y, white);
putpixel(x, y+1, white);
putpixel(x+1, y+1, white);
#endif
}*/
void DrawLoop(double start, double end, double step, struct ExprNode *HorPtr, struct ExprNode *VerPtr)
{
double x = 0.0, y = 0.0;
//printf("[DEBUG] DrawLoop %d %d\n", HorPtr->OpCode, VerPtr->OpCode);
for(Parameter = start; Parameter <= end; Parameter += step)
{
CalcCoordinate(HorPtr, VerPtr, &x, &y);
printf("(%f, %f) ", x, y);
//DrawPixel((unsigned long)x, (unsigned long)y);
}
}
//analyse the whole statement
struct ExprNode* Statement(struct Token *tokenChain, int lenStatement)
{
struct ExprNode* exprRoot;
if(tokenChain[0].type == ORIGIN)
{
OriginStatement(tokenChain, lenStatement);
//printf("[DEBUG] Statement Origin %d %d\n", xRoot->OpCode, yRoot->OpCode);
Origin_x = GetExprValue(xRoot);
Origin_y = GetExprValue(yRoot);
//printf("[DEBUG] Statement Origin\n");
}
else if(tokenChain[0].type == ROT)
{
RotStatement(tokenChain, lenStatement);
Rot_angle = GetExprValue(rotRoot);
}
else if(tokenChain[0].type == SCALE)
{
ScaleStatement(tokenChain, lenStatement);
Scale_x = GetExprValue(xRoot);
Scale_y = GetExprValue(yRoot);
}
else if(tokenChain[0].type == FOR)
{
ForStatement(tokenChain, lenStatement);
forStart = GetExprValue(startRoot);
forEnd = GetExprValue(endRoot);
forStep = GetExprValue(stepRoot);
//printf("[DEBUG] Statement For\n");
DrawLoop(forStart, forEnd, forStep, xRootFor, yRootFor);
}
else{
SyntaxError(tokenChain[0].type);
return NULL;
}
return exprRoot;
}
void Program(Token *tokenChain)
{
struct ExprNode** ExprRootChain = (struct ExprNode**)malloc(sizeof(struct ExprNode) * 100);
int countExprRoot = 0;
struct Token *tokenPoint = tokenChain;
int head = 0;
for(int i = 0; i < lenTokenChain; i++)
{
if(tokenChain[i].type == SEMICO)
{
int lenStatement = i - head;
ExprRootChain[countExprRoot++] = Statement(tokenPoint, lenStatement);
head = i;
tokenPoint = &tokenChain[i + 1];
}
}
}
void Parser(char*filename)
{
struct Token *tokenChain = Scanner(filename);
Program(tokenChain);
}
int main(int argc, char* argv[])
{
Parser(argv[1]);
return 0;
}