variables.cxx

changeset 36
a8838b5f1213
parent 32
d11a034aabfd
child 67
0a202714eea4
equal deleted inserted replaced
35:3d3f6ed40171 36:a8838b5f1213
57 unsigned int u; 57 unsigned int u;
58 ITERATE_GLOBAL_VARS (u) 58 ITERATE_GLOBAL_VARS (u)
59 g_GlobalVariables[u] = NULL; 59 g_GlobalVariables[u] = NULL;
60 } 60 }
61 61
62 // ============================================================================
62 // Tries to declare a new global-scope variable. Returns pointer 63 // Tries to declare a new global-scope variable. Returns pointer
63 // to new global variable, NULL if declaration failed. 64 // to new global variable, NULL if declaration failed.
64 ScriptVar* DeclareGlobalVariable (ScriptReader* r, str name) { 65 ScriptVar* DeclareGlobalVariable (ScriptReader* r, str name) {
66 // Check that the name is valid
67 if (FindCommand (name))
68 r->ParserError ("name of variable-to-be `%s` conflicts with that of a command", name.chars());
69
70 if (IsKeyword (name))
71 r->ParserError ("name of variable-to-be `%s` is a keyword", name.chars());
72
65 // Find a NULL pointer to a global variable 73 // Find a NULL pointer to a global variable
66 ScriptVar* g; 74 ScriptVar* g;
67 unsigned int u = 0; 75 unsigned int u = 0;
68 ITERATE_GLOBAL_VARS (u) { 76 ITERATE_GLOBAL_VARS (u) {
69 // Check that it doesn't exist already 77 // Check that it doesn't exist already
70 if (!g_GlobalVariables[u]) 78 if (!g_GlobalVariables[u])
71 break; 79 break;
72 80
73 if (!g_GlobalVariables[u]->name.icompare (name)) 81 if (!g_GlobalVariables[u]->name.icompare (name))
74 r->ParserError ("tried to declare global scope variable %s twice", name.chars()); 82 r->ParserError ("attempted redeclaration of variable `%s`", name.chars());
75 } 83 }
76 84
77 if (u == MAX_SCRIPT_VARIABLES) 85 if (u == MAX_SCRIPT_VARIABLES)
78 r->ParserError ("too many global variables!"); 86 r->ParserError ("too many global variables!");
79 87
94 if (!g) 102 if (!g)
95 r->ParserError ("global variable %s not declared", name.chars()); 103 r->ParserError ("global variable %s not declared", name.chars());
96 } 104 }
97 */ 105 */
98 106
107 // ============================================================================
99 // Find a global variable by name 108 // Find a global variable by name
100 ScriptVar* FindGlobalVariable (str name) { 109 ScriptVar* FindGlobalVariable (str name) {
101 unsigned int u = 0; 110 unsigned int u = 0;
102 ITERATE_GLOBAL_VARS (u) { 111 ITERATE_GLOBAL_VARS (u) {
103 ScriptVar* g = g_GlobalVariables[u]; 112 ScriptVar* g = g_GlobalVariables[u];
109 } 118 }
110 119
111 return NULL; 120 return NULL;
112 } 121 }
113 122
123 // ============================================================================
124 // Count all declared global variables
114 unsigned int CountGlobalVars () { 125 unsigned int CountGlobalVars () {
115 unsigned int count = 0; 126 unsigned int count = 0;
116 unsigned int u = 0; 127 unsigned int u = 0;
117 ITERATE_GLOBAL_VARS (u) { 128 ITERATE_GLOBAL_VARS (u) {
118 if (g_GlobalVariables[u] != NULL) 129 if (g_GlobalVariables[u] != NULL)

mercurial