-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCNSSemanticActions.cpp
More file actions
176 lines (152 loc) · 4.18 KB
/
Copy pathCNSSemanticActions.cpp
File metadata and controls
176 lines (152 loc) · 4.18 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// CNSSemanticActions.cpp — Semantic-action callbacks invoked by the cns parser.
//
// Part of Sourcy — CNS -> Lua compiler for MUGEN-style characters.
// Licensed under the MIT License. See LICENSE in the project root.
//
// Author: Miguel Angel Exposito Sanchez (radexx), 2012.
#include "CNSSemantics.h"
#include "CNSControllerUsage.h"
extern "C"
{
# include "Helpers.h"
}
// Mutable file-scope state used by ANTLR3 C-style semantic action callbacks.
// ANTLR3 actions are free functions without a context pointer, so per-walk
// state has to live here. Not thread-safe; the compiler runs single-threaded.
cns_ControllerAttribute_t *pCurControllerAttr;
cnsControllerUsage_t *pCurControllerUsage;
cns_ControllerAttribute_t *pCurStateAttribute;
bool isControllernullptr;
typedef enum
{
AC_STATEDEF,
AC_STCONTROLLER
} assignContext_t;
assignContext_t assignCtx = AC_STATEDEF;
extern "C"
{
# include "CNSSemanticActions.h"
ANTLR3_BOOLEAN cnsActions_triggerExist(const char * tName)
{
return (ANTLR3_BOOLEAN)cns_triggerExists(tName);
}
ANTLR3_BOOLEAN cnsActions_getControllerSignature(const char * cName, cnsControllerUsage_t * cu)
{
if(!cns_lookupControllerSignature(cName, cu->pSignature))
{
fprintf(stderr, "The controller %s does not exist.\n", cName);
//return ANTLR3_FALSE;
pCurControllerUsage->pSignature = nullptr;
return ANTLR3_FALSE;
}
isControllernullptr = (!strcmp("NULL", cu->pSignature->UCName));
pCurControllerUsage = cu;
return ANTLR3_TRUE;
}
ANTLR3_BOOLEAN cnsActions_setLHS(char * lhs)
{
if(isControllernullptr)
{
return ANTLR3_TRUE;
}
char_string_toUpperSelf(lhs);
switch(assignCtx)
{
case AC_STATEDEF:
{
int iend = cns_stateDefSignature.syntaxes[0].nAttrs;
for(int i = 0; i < iend; ++i)
{
if(!strcmp(lhs, cns_stateDefSignature.syntaxes[0].attrs[i].UCName))
{
return ANTLR3_TRUE;
}
pCurStateAttribute = &cns_stateDefSignature.syntaxes[0].attrs[i];
}
fprintf(stderr, "ERROR: The attribute %s is not valid for a state definition.!\n", lhs);
return ANTLR3_FALSE;
}
case AC_STCONTROLLER:
{
if(!pCurControllerUsage->pSignature)
{
fprintf(stderr, "ERROR: Controller type not set! (%s)\n", lhs);
return ANTLR3_FALSE;
}
// First find controller argument in the common table
int iend = cns_CommonControllerAttrs.nAttrs;
for(int i = 0; i < iend; ++i)
{
if(!strcmp(lhs, cns_CommonControllerAttrs.attrs[i].UCName))
{
pCurControllerAttr = &(cns_CommonControllerAttrs.attrs[i]);
return ANTLR3_TRUE;
}
}
// Find controller argument in all syntaxes
iend = pCurControllerUsage->pSignature->nSyntaxes;
for(int i = 0; i < iend; ++i)
{
int jend = pCurControllerUsage->pSignature->syntaxes[i].nAttrs;
for(int j = 0; j < jend; ++j)
{
pCurControllerAttr = &(pCurControllerUsage->pSignature->syntaxes[i].attrs[j]);
if(!strcmp(pCurControllerAttr->UCName, lhs))
{
return ANTLR3_TRUE;
}
}
}
// TODO: Report error
fprintf(stderr, "ERROR: The controller attribute %s doesn't exist in any syntax of the controller %s\n", lhs, pCurControllerUsage->pSignature->UCName);
return ANTLR3_FALSE;
}
break;
}
return ANTLR3_FALSE;
}
void cnsActions_setController()
{
assignCtx = AC_STCONTROLLER;
}
void cnsActions_setState()
{
assignCtx = AC_STATEDEF;
}
// Returns ANTLR3_TRUE if an ID was expected, otherwise returns false
ANTLR3_BOOLEAN cnsActions_disambiguateTriggerID(int argIdx)
{
if(isControllernullptr)
{
return ANTLR3_FALSE;
}
switch(assignCtx)
{
case AC_STATEDEF:
{
if(!pCurStateAttribute)
{
return ANTLR3_FALSE;
}
if(argIdx >= (int)pCurStateAttribute->nArgs)
{
return ANTLR3_FALSE;
}
return (pCurStateAttribute->args[argIdx].dataType == CDT_ID) ? ANTLR3_TRUE : ANTLR3_FALSE;
}
case AC_STCONTROLLER:
{
if(!pCurControllerAttr)
{
return ANTLR3_FALSE;
}
if(argIdx >= (int)pCurControllerAttr->nArgs)
{
return ANTLR3_FALSE;
}
return (pCurControllerAttr->args[argIdx].dataType == CDT_ID) ? ANTLR3_TRUE : ANTLR3_FALSE;
}
}
return ANTLR3_FALSE;
}
}