-
Notifications
You must be signed in to change notification settings - Fork 0
/
semanticActions.c
182 lines (152 loc) · 4.82 KB
/
semanticActions.c
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* @file semanticActions.c
* @author Jan Brudný ([email protected])
* @author Antonín Jarolím ([email protected])
* @brief Semantic action definitions
* Implementation IFJ22 compiler
*/
#include "semanticActions.h"
i3Table_t program;
void callSemanticAction(rule *pravidlo, semanticActionInfo info) {
if (!pravidlo) {
loging("Pravidlo je NULL");
return;
}
if (pravidlo->semanticAction == NULL) {
loging("Pravidlo nema semantickou akci. nt: %s, t: %s",
getNonTerminalName(pravidlo->from),
getTerminalName(info.lastToken.type));
return;
}
pravidlo->semanticAction(info);
}
void SA_Statement(semanticActionInfo info) {
newStatement(program, info.lastToken);
}
void SA_FceCall(semanticActionInfo info) {
startFunctionCall(program, info.lastToken);
}
void SA_FceCallEnd(semanticActionInfo info) {
endFunctionCall(program, info.lastToken);
}
void SA_FceDefine(semanticActionInfo info) {
enterFunc(program, dstrGet(info.lastToken.data.valueString));
functionDefBegin(dstrGet(info.lastToken.data.valueString));
}
void SA_FceDefParam(semanticActionInfo info) {
functionDefParam(dstrGet(info.lastToken.data.valueString), info.lastToken);
}
void SA_FceDefType(semanticActionInfo info) {
functionDefParamRememberType(info.lastToken.type);
}
void SA_FceDefRet(semanticActionInfo info) {
functionDefRet(info.lastToken);
}
void SA_DeclareNewVariable(semanticActionInfo info) {
newVariable(program, info.lastToken);
}
void SA_ExpressionAction(semanticActionInfo info) {
switch (info.action) {
case APlus:
actionPlus(program);
break;
case AMinus:
actionSubtraction(program);
break;
case ADivision:
actionDivision(program);
break;
case AMultiplication:
actionMultiplication(program);
break;
case AConcatenation:
actionConcat(program);
break;
case AGreaterThen:
actionGTS(program);
break;
case ALowerThen:
actionLTS(program);
break;
case AEq:
actionEQS(program);
break;
case AGreaterThenEq:
actionGTSEQ(program);
break;
case ALowerThenEq:
actionLTSEQ(program);
break;
case ANotEq:
actionNEQS(program);
break;
case ANotAnAction:
InternalError("Expression action called at very wrong time.");
}
}
void SA_Return(semanticActionInfo info) {
prepareReturn(program);
}
void SA_NewSymtableFrame(semanticActionInfo info) {
createNewSymtableFrame();
}
void SA_ifKey(semanticActionInfo info) {
SA_NewSymtableFrame(info);
ifStart();
}
void SA_elseKey(semanticActionInfo info) {
SA_NewSymtableFrame(info);
elseStart();
}
void SA_Destroy_NULL(semanticActionInfo info) {
printlog("NULL nelze přiřadit přímo do proměnné!");
PrettyExit(ERR_TYPES);
}
void SA_NewCommand() {
checkIfHaveElseBranch(program);
}
void SA_whileKey(semanticActionInfo info) {
SA_NewSymtableFrame(info);
whilestarts(program);
}
void semanticActionsInit() {
initIgen(program);
initializeProgram(&program);
// Function calls
setSemanticAction(FceCall, identifierFunc, &SA_FceCall);
setSemanticAction(Statement, stringLiteral, &SA_Statement);
setSemanticAction(Statement, integerLiteral, &SA_Statement);
setSemanticAction(Statement, floatLiteral, &SA_Statement);
setSemanticAction(Statement, identifierVar, &SA_Statement);
setSemanticAction(CommaOrEpsParam, rightPar, &SA_FceCallEnd);
setSemanticAction(FirstFceParam, rightPar, &SA_FceCallEnd);
// Variables assignment
setSemanticAction(DeclareVariable, identifierVar, &SA_DeclareNewVariable);
setSemanticAction(DefVarAss, nullKey, &SA_Destroy_NULL);
// Function definition
setSemanticAction(FceHeader, identifierFunc, &SA_FceDefine);
setSemanticActionRow(DataType, &SA_FceDefType, 0);
setSemanticAction(DeclareParam, identifierVar, &SA_FceDefParam);
setSemanticActionRow(FuncReturnColonType, &SA_FceDefRet, 0);
// Expression action(s)
setSemanticActionAllRules(Exp, &SA_ExpressionAction);
// IF
setSemanticAction(Condition, ifKey, &SA_ifKey);
setSemanticAction(ElseCond, elseKey, &SA_elseKey);
setSemanticActionRow(Command, &SA_NewCommand, 0);
// while
setSemanticAction(While, whileKey, &SA_whileKey);
// Return
setSemanticAction(Return, returnKey, &SA_Return);
// Creating new symtable frame
// setSemanticAction(While, whileKey, &SA_NewSymtableFrame);
}
void SA_EndOfCommand() {
flushCommand(program);
}
void SA_EndOfBlock() {
exitCodeBlock(program);
}
void SA_EndOfParsing() {
finalGeneration(program);
}