scriptreader.cxx

Tue, 17 Jul 2012 20:35:43 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Tue, 17 Jul 2012 20:35:43 +0300
changeset 33
fd35f6cb5f28
parent 30
6c4efed2dbdd
child 34
0a9a5902beaa
permissions
-rw-r--r--

Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..

0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * botc source code
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012 Santeri `Dusk` Piippo
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 * All rights reserved.
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 *
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 *
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright notice,
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * this list of conditions and the following disclaimer.
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * this list of conditions and the following disclaimer in the documentation
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * and/or other materials provided with the distribution.
3
08cab2b67dce oh blerb.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 2
diff changeset
14 * 3. Neither the name of the developer nor the names of its contributors may
08cab2b67dce oh blerb.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 2
diff changeset
15 * be used to endorse or promote products derived from this software without
08cab2b67dce oh blerb.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 2
diff changeset
16 * specific prior written permission.
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 * 4. Redistributions in any form must be accompanied by information on how to
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18 * obtain complete source code for the software and any accompanying
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 * software that uses the software. The source code must either be included
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 * in the distribution or be available for no more than the cost of
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 * distribution plus a nominal fee, and must be freely redistributable
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 * under reasonable conditions. For an executable file, complete source
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 * code means the source code for all modules it contains. It does not
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 * include source code for modules or files that typically accompany the
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 * major components of the operating system on which the executable file
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 * runs.
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 *
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 * POSSIBILITY OF SUCH DAMAGE.
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 */
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 #include <stdio.h>
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 #include <stdlib.h>
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 #include "string.h"
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 #include "str.h"
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 #include "common.h"
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 #include "scriptreader.h"
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
48 ScriptReader::ScriptReader (str path) {
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
49 token = "";
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
50 fc = -1;
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
52 for (unsigned int u = 0; u < MAX_FILESTACK; u++)
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
53 fp[u] = NULL;
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
55 OpenFile (path);
28
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
56 commentmode = 0;
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 }
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58
1
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
59 ScriptReader::~ScriptReader () {
28
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
60 FinalChecks ();
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
61
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
62 for (unsigned int u = 0; u < MAX_FILESTACK; u++) {
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
63 if (fp[u]) {
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
64 ParserWarning ("file idx %u remained open after parsing", u);
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
65 CloseFile (u);
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
66 }
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
67 }
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
68 }
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
69
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
70 // Opens a file and pushes its pointer to stack
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
71 void ScriptReader::OpenFile (str path) {
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
72 if (fc+1 >= MAX_FILESTACK)
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
73 ParserError ("supposed to open file `%s` but file stack is full! \
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
74 do you have recursive `#include` directives?",
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
75 path.chars());
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
76
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
77 // Save the position first.
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
78 if (fc != -1) {
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
79 savedpos[fc] = ftell (fp[fc]);
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
80 }
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
81
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
82 fc++;
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
83
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
84 fp[fc] = fopen (path, "r");
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
85 if (!fp[fc]) {
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
86 ParserError ("couldn't open %s for reading!\n", path.chars ());
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
87 exit (1);
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
88 }
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
89
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
90 fseek (fp[fc], 0, SEEK_SET);
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
91 filepath[fc] = path.chars();
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
92 curline[fc] = 1;
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
93 curchar[fc] = 1;
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
94 pos[fc] = 0;
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
95 atnewline = 0;
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
96 }
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
97
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
98 void ScriptReader::CloseFile (unsigned int u) {
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
99 if (u >= MAX_FILESTACK)
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
100 u = fc;
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
101
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
102 if (!fp[u])
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
103 return;
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
104
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
105 fclose (fp[u]);
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
106 fp[u] = NULL;
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
107 fc--;
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
108
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
109 if (fc != -1)
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
110 fseek (fp[fc], savedpos[fc], SEEK_SET);
1
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
111 }
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
112
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113 char ScriptReader::ReadChar () {
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
114 if (feof (fp[fc]))
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
115 return 0;
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
116
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117 char* c = (char*)malloc (sizeof (char));
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
118 if (!fread (c, sizeof (char), 1, fp[fc]))
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119 return 0;
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120
7
118d3d5db64f Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
121 // We're at a newline, thus next char read will begin the next line
118d3d5db64f Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
122 if (atnewline) {
118d3d5db64f Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
123 atnewline = false;
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
124 curline[fc]++;
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
125 curchar[fc] = 0; // gets incremented to 1
7
118d3d5db64f Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
126 }
118d3d5db64f Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
127
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
128 if (c[0] == '\n') {
7
118d3d5db64f Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
129 atnewline = true;
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
130
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
131 // Check for pre-processor directives
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
132 PreprocessDirectives ();
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
133 }
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
134
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
135 curchar[fc]++;
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136 return c[0];
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
137 }
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
138
28
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
139 char ScriptReader::PeekChar (int offset) {
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
140 // Store current position
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
141 long curpos = ftell (fp[fc]);
28
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
142
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
143 // Forward by offset
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
144 fseek (fp[fc], offset, SEEK_CUR);
28
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
145
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
146 // Read the character
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
147 char* c = (char*)malloc (sizeof (char));
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
148
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
149 if (!fread (c, sizeof (char), 1, fp[fc])) {
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
150 fseek (fp[fc], curpos, SEEK_SET);
28
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
151 return 0;
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
152 }
28
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
153
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
154 // Rewind back
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
155 fseek (fp[fc], curpos, SEEK_SET);
28
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
156
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
157 return c[0];
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
158 }
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
159
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
160 // true if was found, false if not.
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
161 bool ScriptReader::Next (bool peek) {
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
162 str tmp = "";
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
163 // printf ("begin token\n");
28
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
164
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
165 while (1) {
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
166 // Check end-of-file
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
167 if (feof (fp[fc])) {
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
168 // If we're just peeking, we shouldn't
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
169 // actually close anything..
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
170 if (peek)
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
171 break;
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
172
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
173 CloseFile ();
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
174 if (fc == -1)
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
175 break;
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
176 }
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
177
28
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
178 // Check if the next token possibly starts a comment.
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
179 if (PeekChar () == '/' && !tmp.len()) {
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
180 char c2 = PeekChar (1);
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
181 // C++-style comment
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
182 if (c2 == '/')
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
183 commentmode = 1;
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
184 else if (c2 == '*')
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
185 commentmode = 2;
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
186
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
187 // We don't need to actually read in the
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
188 // comment characters, since they will get
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
189 // ignored due to comment mode anyway.
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
190 }
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
191
10
2c0f76090372 Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
192 c = ReadChar ();
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
193 // printf ("add char [%d] `%c`\n", c, c);
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
194
28
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
195 // If this is a comment we're reading, check if this character
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
196 // gets the comment terminated, otherwise ignore it.
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
197 if (commentmode > 0) {
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
198 if (commentmode == 1 && c == '\n') {
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
199 // C++-style comments are terminated by a newline
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
200 commentmode = 0;
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
201 continue;
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
202 } else if (commentmode == 2 && c == '*') {
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
203 // C-style comments are terminated by a `*/`
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
204 if (PeekChar() == '/') {
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
205 commentmode = 0;
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
206 ReadChar ();
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
207 }
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
208 }
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
209
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
210 // Otherwise, ignore it.
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
211 continue;
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
212 }
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
213
11
f08abacb46c9 Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
214 // Non-alphanumber characters (sans underscore) break the word too.
f08abacb46c9 Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
215 // If there was prior data, the delimeter pushes the cursor back so
f08abacb46c9 Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
216 // that the next character will be the same delimeter. If there isn't,
f08abacb46c9 Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
217 // the delimeter itself is included (and thus becomes a token itself.)
f08abacb46c9 Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
218 if ((c >= 33 && c <= 47) ||
f08abacb46c9 Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
219 (c >= 58 && c <= 64) ||
f08abacb46c9 Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
220 (c >= 91 && c <= 96 && c != '_') ||
f08abacb46c9 Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
221 (c >= 123 && c <= 126)) {
f08abacb46c9 Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
222 if (tmp.len())
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
223 fseek (fp[fc], ftell (fp[fc]) - 1, SEEK_SET);
11
f08abacb46c9 Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
224 else
f08abacb46c9 Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
225 tmp += c;
f08abacb46c9 Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
226 break;
8
c8bfa7e6ae1b Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 7
diff changeset
227 }
c8bfa7e6ae1b Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 7
diff changeset
228
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
229 if (IsCharWhitespace (c)) {
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
230 // Don't break if we haven't gathered anything yet.
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
231 if (tmp.len())
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
232 break;
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
233 } else {
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
234 tmp += c;
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
235 }
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
236 }
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
237
19
66993500719f Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
238 // If we got nothing here, read failed. This should
66993500719f Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
239 // only hapen in the case of EOF.
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
240 if (!tmp.len()) {
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
241 token = "";
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
242 return false;
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
243 }
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
244
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
245 pos[fc]++;
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
246 token = tmp;
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
247 return true;
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
248 }
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
249
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
250 // Returns the next token without advancing the cursor.
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
251 str ScriptReader::PeekNext () {
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
252 // Store current position
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
253 int cpos = ftell (fp[fc]);
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
254
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
255 // Advance on the token.
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
256 if (!Next (true))
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
257 return "";
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
258
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
259 str tmp = token;
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
260
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
261 // Restore position
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
262 fseek (fp[fc], cpos, SEEK_SET);
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
263 pos[fc]--;
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
264
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
265 return tmp;
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
266 }
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
267
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
268 void ScriptReader::Seek (unsigned int n, int origin) {
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
269 switch (origin) {
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
270 case SEEK_SET:
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
271 fseek (fp[fc], 0, SEEK_SET);
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
272 pos[fc] = 0;
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
273 break;
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
274 case SEEK_CUR:
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
275 break;
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
276 case SEEK_END:
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
277 printf ("ScriptReader::Seek: SEEK_END not yet supported.\n");
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
278 break;
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
279 }
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
280
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
281 for (unsigned int i = 0; i < n+1; i++)
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
282 Next();
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
283 }
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
284
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
285 void ScriptReader::MustNext (const char* c) {
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
286 if (!Next()) {
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
287 if (strlen (c))
7
118d3d5db64f Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
288 ParserError ("expected `%s`, reached end of file instead\n", c);
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
289 else
7
118d3d5db64f Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
290 ParserError ("expected a token, reached end of file instead\n");
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
291 }
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
292
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
293 if (strlen (c) && token.compare (c) != 0) {
7
118d3d5db64f Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
294 ParserError ("expected `%s`, got `%s` instead", c, token.chars());
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
295 }
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
296 }
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
297
7
118d3d5db64f Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
298 void ScriptReader::ParserError (const char* message, ...) {
118d3d5db64f Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
299 PERFORM_FORMAT (message, outmessage);
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
300 ParserMessage ("\nError: ", outmessage);
7
118d3d5db64f Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
301 exit (1);
118d3d5db64f Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
302 }
118d3d5db64f Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
303
118d3d5db64f Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
304 void ScriptReader::ParserWarning (const char* message, ...) {
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
305 PERFORM_FORMAT (message, outmessage);
7
118d3d5db64f Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
306 ParserMessage ("Warning: ", outmessage);
118d3d5db64f Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
307 }
118d3d5db64f Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
308
118d3d5db64f Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
309 void ScriptReader::ParserMessage (const char* header, char* message) {
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
310 if (fc >= 0 && fc < MAX_FILESTACK)
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
311 fprintf (stderr, "%sIn file %s, at line %u, col %u: %s\n",
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
312 header, filepath[fc], curline[fc], curchar[fc], message);
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
313 else
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
314 fprintf (stderr, "%s%s\n", header, message);
9
d279af9afd6d Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
315 }
d279af9afd6d Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
316
12
1bdbfcca2fc6 MustString now behaves more like its siblings - sets token to result rather than returning it
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
317 void ScriptReader::MustString () {
9
d279af9afd6d Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
318 MustNext ("\"");
d279af9afd6d Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
319
d279af9afd6d Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
320 str string;
d279af9afd6d Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
321 // Keep reading characters until we find a terminating quote.
d279af9afd6d Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
322 while (1) {
d279af9afd6d Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
323 // can't end here!
33
fd35f6cb5f28 Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 30
diff changeset
324 if (feof (fp[fc]))
9
d279af9afd6d Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
325 ParserError ("unterminated string");
d279af9afd6d Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
326
d279af9afd6d Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
327 char c = ReadChar ();
d279af9afd6d Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
328 if (c == '"')
d279af9afd6d Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
329 break;
d279af9afd6d Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
330
d279af9afd6d Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
331 string += c;
d279af9afd6d Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
332 }
d279af9afd6d Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
333
12
1bdbfcca2fc6 MustString now behaves more like its siblings - sets token to result rather than returning it
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
334 token = string;
10
2c0f76090372 Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
335 }
2c0f76090372 Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
336
2c0f76090372 Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
337 void ScriptReader::MustNumber () {
30
6c4efed2dbdd Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents: 28
diff changeset
338 str num;
10
2c0f76090372 Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
339 MustNext ();
30
6c4efed2dbdd Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents: 28
diff changeset
340 num += token;
6c4efed2dbdd Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents: 28
diff changeset
341
6c4efed2dbdd Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents: 28
diff changeset
342 // Cater for a possible minus sign, since it
6c4efed2dbdd Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents: 28
diff changeset
343 // breaks the token, read a new one with the number.
6c4efed2dbdd Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents: 28
diff changeset
344 if (!token.compare ("-")) {
6c4efed2dbdd Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents: 28
diff changeset
345 MustNext ();
6c4efed2dbdd Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents: 28
diff changeset
346 num += token;
6c4efed2dbdd Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents: 28
diff changeset
347 }
6c4efed2dbdd Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents: 28
diff changeset
348
6c4efed2dbdd Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents: 28
diff changeset
349 if (!num.isnumber())
6c4efed2dbdd Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents: 28
diff changeset
350 ParserError ("expected a number, got `%s`", num.chars());
10
2c0f76090372 Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
351 }
2c0f76090372 Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
352
2c0f76090372 Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
353 void ScriptReader::MustBool () {
2c0f76090372 Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
354 MustNext();
2c0f76090372 Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
355 if (!token.compare ("0") || !token.compare ("1") ||
2c0f76090372 Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
356 !token.compare ("true") || !token.compare ("false") ||
2c0f76090372 Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
357 !token.compare ("yes") || !token.compare ("no")) {
2c0f76090372 Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
358 return;
2c0f76090372 Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
359 }
2c0f76090372 Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
360
2c0f76090372 Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
361 ParserError ("expected a boolean value, got `%s`", token.chars());
20
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
362 }
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
363
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
364 bool ScriptReader::BoolValue () {
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
365 return (!token.compare ("1") || !token.compare ("true") || !token.compare ("yes"));
22
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
366 }
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
367
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
368 void ScriptReader::MustValue (int type) {
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
369 switch (type) {
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
370 case RETURNVAL_INT: MustNumber (); break;
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
371 case RETURNVAL_STRING: MustString (); break;
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
372 case RETURNVAL_BOOLEAN: MustBool (); break;
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
373 }
28
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
374 }
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
375
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
376 // Checks to be performed at the end of file
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
377 void ScriptReader::FinalChecks () {
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
378 // If comment mode is 2 by the time the file ended, the
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
379 // comment was left unterminated. 1 is no problem, since
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
380 // it's terminated by newlines anyway.
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
381 if (commentmode == 2)
fb46d3d40064 Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
382 ParserError ("unterminated `/*`-style comment");
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
383 }

mercurial