src/variables.cxx

changeset 73
1ee9b312dc18
parent 72
03e4d9db3fd9
child 74
007fbadfa7f9
equal deleted inserted replaced
72:03e4d9db3fd9 73:1ee9b312dc18
1 /* 1 /*
2 * botc source code 2 Copyright (c) 2013-2014, Santeri Piippo
3 * Copyright (C) 2012 Santeri `Dusk` Piippo 3 All rights reserved.
4 * All rights reserved. 4
5 * 5 Redistribution and use in source and binary forms, with or without
6 * Redistribution and use in source and binary forms, with or without 6 modification, are permitted provided that the following conditions are met:
7 * modification, are permitted provided that the following conditions are met: 7
8 * 8 * Redistributions of source code must retain the above copyright
9 * 1. Redistributions of source code must retain the above copyright notice, 9 notice, this list of conditions and the following disclaimer.
10 * this list of conditions and the following disclaimer. 10
11 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * Redistributions in binary form must reproduce the above copyright
12 * this list of conditions and the following disclaimer in the documentation 12 notice, this list of conditions and the following disclaimer in the
13 * and/or other materials provided with the distribution. 13 documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the developer nor the names of its contributors may 14
15 * be used to endorse or promote products derived from this software without 15 * Neither the name of the <organization> nor the
16 * specific prior written permission. 16 names of its contributors may be used to endorse or promote products
17 * 4. Redistributions in any form must be accompanied by information on how to 17 derived from this software without specific prior written permission.
18 * obtain complete source code for the software and any accompanying 18
19 * software that uses the software. The source code must either be included 19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * in the distribution or be available for no more than the cost of 20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * distribution plus a nominal fee, and must be freely redistributable 21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * under reasonable conditions. For an executable file, complete source 22 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
23 * code means the source code for all modules it contains. It does not 23 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * include source code for modules or files that typically accompany the 24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * major components of the operating system on which the executable file 25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * runs. 26 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * 27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 */
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40 30
41 #include <stdio.h> 31 #include <stdio.h>
42 #include <stdlib.h> 32 #include <stdlib.h>
43 #include <string.h> 33 #include <string.h>
44 #include "main.h" 34 #include "main.h"
45 #include "containers.h" 35 #include "containers.h"
46 #include "bots.h" 36 #include "bots.h"
47 #include "botcommands.h" 37 #include "object_writer.h"
48 #include "objwriter.h"
49 #include "stringtable.h" 38 #include "stringtable.h"
50 #include "variables.h" 39 #include "variables.h"
51 #include "scriptreader.h" 40 #include "parser.h"
52 41
53 list<ScriptVar> g_GlobalVariables; 42 list<script_variable> g_GlobalVariables;
54 list<ScriptVar> g_LocalVariables; 43 list<script_variable> g_LocalVariables;
55 44
56 // ============================================================================ 45 // ============================================================================
57 // Tries to declare a new global-scope variable. Returns pointer 46 // Tries to declare a new global-scope variable. Returns pointer
58 // to new global variable, null if declaration failed. 47 // to new global variable, null if declaration failed.
59 ScriptVar* DeclareGlobalVariable (ScriptReader* r, type_e type, string name) { 48 script_variable* DeclareGlobalVariable (botscript_parser* r, type_e type, string name)
49 {
60 // Unfortunately the VM does not support string variables so yeah. 50 // Unfortunately the VM does not support string variables so yeah.
61 if (type == TYPE_STRING) 51 if (type == TYPE_STRING)
62 r->ParserError ("variables cannot be string\n"); 52 error ("variables cannot be string\n");
63 53
64 // Check that the variable is valid 54 // Check that the variable is valid
65 if (FindCommand (name)) 55 if (FindCommand (name))
66 r->ParserError ("name of variable-to-be `%s` conflicts with that of a command", name.chars()); 56 error ("name of variable-to-be `%s` conflicts with that of a command", name.chars());
67 57
68 if (IsKeyword (name)) 58 if (IsKeyword (name))
69 r->ParserError ("name of variable-to-be `%s` is a keyword", name.chars()); 59 error ("name of variable-to-be `%s` is a keyword", name.chars());
70 60
71 if (g_GlobalVariables.size() >= MAX_SCRIPT_VARIABLES) 61 if (g_GlobalVariables.size() >= MAX_SCRIPT_VARIABLES)
72 r->ParserError ("too many global variables!"); 62 error ("too many global variables!");
73 63
74 for (uint i = 0; i < g_GlobalVariables.size(); i++) 64 for (int i = 0; i < g_GlobalVariables.size(); i++)
75 if (g_GlobalVariables[i].name == name) 65 if (g_GlobalVariables[i].name == name)
76 r->ParserError ("attempted redeclaration of global variable `%s`", name.chars()); 66 error ("attempted redeclaration of global variable `%s`", name.chars());
77 67
78 ScriptVar g; 68 script_variable g;
79 g.index = g_GlobalVariables.size(); 69 g.index = g_GlobalVariables.size();
80 g.name = name; 70 g.name = name;
81 g.statename = ""; 71 g.statename = "";
82 g.value = 0; 72 g.value = 0;
83 g.type = type; 73 g.type = type;
84 74
85 g_GlobalVariables << g; 75 g_GlobalVariables << g;
86 return &g_GlobalVariables[g.index]; 76 return &g_GlobalVariables[g.index];
87 } 77 }
88 78
89 // ============================================================================ 79 // ============================================================================
90 // Find a global variable by name 80 // Find a global variable by name
91 ScriptVar* FindGlobalVariable (string name) { 81 script_variable* FindGlobalVariable (string name)
92 for (uint i = 0; i < g_GlobalVariables.size(); i++) { 82 {
93 ScriptVar* g = &g_GlobalVariables[i]; 83 for (int i = 0; i < g_GlobalVariables.size(); i++)
84 {
85 script_variable* g = &g_GlobalVariables[i];
86
94 if (g->name == name) 87 if (g->name == name)
95 return g; 88 return g;
96 } 89 }
97 90
98 return null; 91 return null;
99 } 92 }
100 93
101 // ============================================================================ 94 // ============================================================================
102 // Count all declared global variables 95 // Count all declared global variables
103 uint CountGlobalVars () { 96 int CountGlobalVars ()
97 {
104 return g_GlobalVariables.size(); 98 return g_GlobalVariables.size();
105 } 99 }

mercurial