Wed, 19 Dec 2012 04:20:02 +0200
Some rework on variables
0 | 1 | /* |
2 | * botc source code | |
3 | * Copyright (C) 2012 Santeri `Dusk` Piippo | |
4 | * All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright notice, | |
10 | * this list of conditions and the following disclaimer. | |
11 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
12 | * this list of conditions and the following disclaimer in the documentation | |
13 | * and/or other materials provided with the distribution. | |
3 | 14 | * 3. Neither the name of the developer nor the names of its contributors may |
15 | * be used to endorse or promote products derived from this software without | |
16 | * specific prior written permission. | |
0 | 17 | * 4. Redistributions in any form must be accompanied by information on how to |
18 | * obtain complete source code for the software and any accompanying | |
19 | * software that uses the software. The source code must either be included | |
20 | * in the distribution or be available for no more than the cost of | |
21 | * distribution plus a nominal fee, and must be freely redistributable | |
22 | * under reasonable conditions. For an executable file, complete source | |
23 | * code means the source code for all modules it contains. It does not | |
24 | * include source code for modules or files that typically accompany the | |
25 | * major components of the operating system on which the executable file | |
26 | * runs. | |
27 | * | |
28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
29 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
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 | ||
41 | #include <stdio.h> | |
42 | #include <stdlib.h> | |
43 | #include "string.h" | |
44 | #include "str.h" | |
45 | #include "common.h" | |
46 | #include "scriptreader.h" | |
47 | ||
63
0557babc8675
- Fixed error display - peeking was pushing recorded `cursor` position out of sync
Teemu Piippo <crimsondusk64@gmail.com>
parents:
54
diff
changeset
|
48 | #define STORE_POSITION \ |
0557babc8675
- Fixed error display - peeking was pushing recorded `cursor` position out of sync
Teemu Piippo <crimsondusk64@gmail.com>
parents:
54
diff
changeset
|
49 | bool _atnewline = atnewline; \ |
0557babc8675
- Fixed error display - peeking was pushing recorded `cursor` position out of sync
Teemu Piippo <crimsondusk64@gmail.com>
parents:
54
diff
changeset
|
50 | unsigned int _curline = curline[fc]; \ |
0557babc8675
- Fixed error display - peeking was pushing recorded `cursor` position out of sync
Teemu Piippo <crimsondusk64@gmail.com>
parents:
54
diff
changeset
|
51 | unsigned int _curchar = curchar[fc]; |
0557babc8675
- Fixed error display - peeking was pushing recorded `cursor` position out of sync
Teemu Piippo <crimsondusk64@gmail.com>
parents:
54
diff
changeset
|
52 | |
0557babc8675
- Fixed error display - peeking was pushing recorded `cursor` position out of sync
Teemu Piippo <crimsondusk64@gmail.com>
parents:
54
diff
changeset
|
53 | #define RESTORE_POSITION \ |
0557babc8675
- Fixed error display - peeking was pushing recorded `cursor` position out of sync
Teemu Piippo <crimsondusk64@gmail.com>
parents:
54
diff
changeset
|
54 | atnewline = _atnewline; \ |
0557babc8675
- Fixed error display - peeking was pushing recorded `cursor` position out of sync
Teemu Piippo <crimsondusk64@gmail.com>
parents:
54
diff
changeset
|
55 | curline[fc] = _curline; \ |
0557babc8675
- Fixed error display - peeking was pushing recorded `cursor` position out of sync
Teemu Piippo <crimsondusk64@gmail.com>
parents:
54
diff
changeset
|
56 | curchar[fc] = _curchar; |
0557babc8675
- Fixed error display - peeking was pushing recorded `cursor` position out of sync
Teemu Piippo <crimsondusk64@gmail.com>
parents:
54
diff
changeset
|
57 | |
38 | 58 | // ============================================================================ |
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
|
59 | 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
|
60 | token = ""; |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
61 | prevtoken = ""; |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
62 | prevpos = 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
|
63 | fc = -1; |
0 | 64 | |
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
|
65 | 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
|
66 | fp[u] = NULL; |
0 | 67 | |
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
|
68 | OpenFile (path); |
28
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
69 | commentmode = 0; |
0 | 70 | } |
71 | ||
38 | 72 | // ============================================================================ |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
73 | ScriptReader::~ScriptReader () { |
36
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
35
diff
changeset
|
74 | // If comment mode is 2 by the time the file ended, the |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
35
diff
changeset
|
75 | // comment was left unterminated. 1 is no problem, since |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
35
diff
changeset
|
76 | // it's terminated by newlines anyway. |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
35
diff
changeset
|
77 | if (commentmode == 2) |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
35
diff
changeset
|
78 | ParserError ("unterminated `/*`-style comment"); |
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
|
79 | |
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 | 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
|
81 | 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
|
82 | 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
|
83 | 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
|
84 | } |
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 | } |
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 | } |
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 | |
38 | 88 | // ============================================================================ |
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
|
89 | // 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
|
90 | 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
|
91 | if (fc+1 >= MAX_FILESTACK) |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
92 | ParserError ("supposed to open file `%s` but file stack is full! do you have recursive `#include` directives?", |
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
|
93 | 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
|
94 | |
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 | // 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
|
96 | 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
|
97 | 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
|
98 | } |
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 | |
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 | 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 | 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
|
103 | 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
|
104 | 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
|
105 | 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
|
106 | } |
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 | |
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 | 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
|
109 | 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
|
110 | 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
|
111 | 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
|
112 | 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
|
113 | 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
|
114 | } |
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 | |
38 | 116 | // ============================================================================ |
117 | // Closes the current file | |
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 | 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
|
119 | if (u >= MAX_FILESTACK) |
38 | 120 | u = fc; |
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
|
121 | |
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
|
122 | 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
|
123 | 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
|
124 | |
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 | 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
|
126 | 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
|
127 | 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
|
128 | |
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
|
129 | 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
|
130 | 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
|
131 | } |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
132 | |
38 | 133 | // ============================================================================ |
0 | 134 | 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
|
135 | 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
|
136 | 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
|
137 | |
50
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
38
diff
changeset
|
138 | char c; |
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
38
diff
changeset
|
139 | if (!fread (&c, 1, 1, fp[fc])) |
0 | 140 | return 0; |
141 | ||
7
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
142 | // 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
|
143 | if (atnewline) { |
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
144 | 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
|
145 | 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
|
146 | curchar[fc] = 0; // gets incremented to 1 |
7
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
147 | } |
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
148 | |
50
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
38
diff
changeset
|
149 | if (c == '\n') { |
7
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
150 | 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
|
151 | |
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 | // 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
|
153 | 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
|
154 | } |
0 | 155 | |
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
|
156 | curchar[fc]++; |
50
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
38
diff
changeset
|
157 | return c; |
0 | 158 | } |
159 | ||
38 | 160 | // ============================================================================ |
161 | // Peeks the next character | |
28
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
162 | char ScriptReader::PeekChar (int offset) { |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
163 | // 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
|
164 | long curpos = ftell (fp[fc]); |
63
0557babc8675
- Fixed error display - peeking was pushing recorded `cursor` position out of sync
Teemu Piippo <crimsondusk64@gmail.com>
parents:
54
diff
changeset
|
165 | STORE_POSITION |
28
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
166 | |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
167 | // 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
|
168 | fseek (fp[fc], offset, SEEK_CUR); |
28
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
169 | |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
170 | // Read the character |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
171 | 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
|
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 | 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
|
174 | fseek (fp[fc], curpos, SEEK_SET); |
28
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
175 | 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
|
176 | } |
28
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
177 | |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
178 | // 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
|
179 | fseek (fp[fc], curpos, SEEK_SET); |
63
0557babc8675
- Fixed error display - peeking was pushing recorded `cursor` position out of sync
Teemu Piippo <crimsondusk64@gmail.com>
parents:
54
diff
changeset
|
180 | RESTORE_POSITION |
28
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
181 | |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
182 | return c[0]; |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
183 | } |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
184 | |
38 | 185 | // ============================================================================ |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
186 | // Read a token from the file buffer. Returns true if token 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
|
187 | bool ScriptReader::Next (bool peek) { |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
188 | prevpos = ftell (fp[fc]); |
0 | 189 | str tmp = ""; |
28
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
190 | |
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
|
191 | 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
|
192 | // 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
|
193 | 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
|
194 | // 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
|
195 | // 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
|
196 | 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
|
197 | 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
|
198 | |
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
|
199 | 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
|
200 | 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
|
201 | 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
|
202 | } |
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
|
203 | |
28
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
204 | // Check if the next token possibly starts a comment. |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
205 | if (PeekChar () == '/' && !tmp.len()) { |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
206 | char c2 = PeekChar (1); |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
207 | // C++-style comment |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
208 | if (c2 == '/') |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
209 | commentmode = 1; |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
210 | else if (c2 == '*') |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
211 | commentmode = 2; |
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 | // We don't need to actually read in the |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
214 | // comment characters, since they will get |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
215 | // ignored due to comment mode anyway. |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
216 | } |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
217 | |
10
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
218 | c = ReadChar (); |
0 | 219 | |
28
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
220 | // 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
|
221 | // gets the comment terminated, otherwise ignore it. |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
222 | if (commentmode > 0) { |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
223 | if (commentmode == 1 && c == '\n') { |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
224 | // C++-style comments are terminated by a newline |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
225 | commentmode = 0; |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
226 | continue; |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
227 | } else if (commentmode == 2 && c == '*') { |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
228 | // C-style comments are terminated by a `*/` |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
229 | if (PeekChar() == '/') { |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
230 | commentmode = 0; |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
231 | ReadChar (); |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
232 | } |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
233 | } |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
234 | |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
235 | // Otherwise, ignore it. |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
236 | continue; |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
237 | } |
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
22
diff
changeset
|
238 | |
11
f08abacb46c9
Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
10
diff
changeset
|
239 | // 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
|
240 | // 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
|
241 | // 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
|
242 | // 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
|
243 | if ((c >= 33 && c <= 47) || |
f08abacb46c9
Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
10
diff
changeset
|
244 | (c >= 58 && c <= 64) || |
f08abacb46c9
Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
10
diff
changeset
|
245 | (c >= 91 && c <= 96 && c != '_') || |
f08abacb46c9
Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
10
diff
changeset
|
246 | (c >= 123 && c <= 126)) { |
f08abacb46c9
Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
10
diff
changeset
|
247 | 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
|
248 | 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
|
249 | else |
f08abacb46c9
Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
10
diff
changeset
|
250 | tmp += c; |
f08abacb46c9
Removed extdelimeters member, said delimeters are always enabled now.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
10
diff
changeset
|
251 | break; |
8
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
7
diff
changeset
|
252 | } |
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
7
diff
changeset
|
253 | |
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
|
254 | if (IsCharWhitespace (c)) { |
0 | 255 | // Don't break if we haven't gathered anything yet. |
256 | if (tmp.len()) | |
257 | break; | |
258 | } else { | |
259 | tmp += c; | |
260 | } | |
261 | } | |
262 | ||
19
66993500719f
Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
12
diff
changeset
|
263 | // If we got nothing here, read failed. This should |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
264 | // only happen in the case of EOF. |
0 | 265 | if (!tmp.len()) { |
266 | token = ""; | |
267 | return false; | |
268 | } | |
269 | ||
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
|
270 | pos[fc]++; |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
271 | prevtoken = token; |
0 | 272 | token = tmp; |
273 | return true; | |
274 | } | |
275 | ||
38 | 276 | // ============================================================================ |
0 | 277 | // Returns the next token without advancing the cursor. |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
278 | str ScriptReader::PeekNext (int offset) { |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
279 | // Store current information |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
280 | str storedtoken = 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
|
281 | int cpos = ftell (fp[fc]); |
63
0557babc8675
- Fixed error display - peeking was pushing recorded `cursor` position out of sync
Teemu Piippo <crimsondusk64@gmail.com>
parents:
54
diff
changeset
|
282 | STORE_POSITION |
0 | 283 | |
284 | // Advance on the token. | |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
285 | while (offset >= 0) { |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
286 | if (!Next (true)) |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
287 | return ""; |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
288 | offset--; |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
289 | } |
0 | 290 | |
291 | str tmp = token; | |
292 | ||
293 | // 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
|
294 | 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
|
295 | pos[fc]--; |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
296 | token = storedtoken; |
63
0557babc8675
- Fixed error display - peeking was pushing recorded `cursor` position out of sync
Teemu Piippo <crimsondusk64@gmail.com>
parents:
54
diff
changeset
|
297 | RESTORE_POSITION |
0 | 298 | return tmp; |
299 | } | |
300 | ||
38 | 301 | // ============================================================================ |
0 | 302 | void ScriptReader::Seek (unsigned int n, int origin) { |
303 | switch (origin) { | |
304 | 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
|
305 | 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
|
306 | pos[fc] = 0; |
0 | 307 | break; |
308 | case SEEK_CUR: | |
309 | break; | |
310 | case SEEK_END: | |
311 | printf ("ScriptReader::Seek: SEEK_END not yet supported.\n"); | |
312 | break; | |
313 | } | |
314 | ||
315 | for (unsigned int i = 0; i < n+1; i++) | |
316 | Next(); | |
317 | } | |
318 | ||
38 | 319 | // ============================================================================ |
0 | 320 | void ScriptReader::MustNext (const char* c) { |
321 | if (!Next()) { | |
322 | if (strlen (c)) | |
7
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
323 | ParserError ("expected `%s`, reached end of file instead\n", c); |
0 | 324 | else |
7
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
325 | ParserError ("expected a token, reached end of file instead\n"); |
0 | 326 | } |
327 | ||
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
328 | if (strlen (c)) |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
329 | MustThis (c); |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
330 | } |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
331 | |
38 | 332 | // ============================================================================ |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
333 | void ScriptReader::MustThis (const char* c) { |
67
0a202714eea4
Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
63
diff
changeset
|
334 | if (token != c) |
7
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
335 | ParserError ("expected `%s`, got `%s` instead", c, token.chars()); |
0 | 336 | } |
337 | ||
38 | 338 | // ============================================================================ |
7
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
339 | void ScriptReader::ParserError (const char* message, ...) { |
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
340 | 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
|
341 | ParserMessage ("\nError: ", outmessage); |
7
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
342 | exit (1); |
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
343 | } |
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
344 | |
38 | 345 | // ============================================================================ |
7
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
346 | void ScriptReader::ParserWarning (const char* message, ...) { |
0 | 347 | PERFORM_FORMAT (message, outmessage); |
7
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
348 | ParserMessage ("Warning: ", outmessage); |
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
349 | } |
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
350 | |
38 | 351 | // ============================================================================ |
7
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
352 | 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
|
353 | if (fc >= 0 && fc < MAX_FILESTACK) |
63
0557babc8675
- Fixed error display - peeking was pushing recorded `cursor` position out of sync
Teemu Piippo <crimsondusk64@gmail.com>
parents:
54
diff
changeset
|
354 | fprintf (stderr, "%s%s:%u:%u: %s\n", |
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
|
355 | 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
|
356 | 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
|
357 | fprintf (stderr, "%s%s\n", header, message); |
9
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
358 | } |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
359 | |
38 | 360 | // ============================================================================ |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
361 | // if gotquote == 1, the current token already holds the quotation mark. |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
362 | void ScriptReader::MustString (bool gotquote) { |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
363 | if (gotquote) |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
364 | MustThis ("\""); |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
365 | else |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
366 | MustNext ("\""); |
9
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
367 | |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
368 | str string; |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
369 | // Keep reading characters until we find a terminating quote. |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
370 | while (1) { |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
371 | // 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
|
372 | if (feof (fp[fc])) |
9
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
373 | ParserError ("unterminated string"); |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
374 | |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
375 | char c = ReadChar (); |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
376 | if (c == '"') |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
377 | break; |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
378 | |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
379 | string += c; |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
380 | } |
d279af9afd6d
Added proper string checking
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
381 | |
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
|
382 | token = string; |
10
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
383 | } |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
384 | |
38 | 385 | // ============================================================================ |
35
3d3f6ed40171
Negative literal integers work properly now..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
386 | void ScriptReader::MustNumber (bool fromthis) { |
3d3f6ed40171
Negative literal integers work properly now..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
387 | if (!fromthis) |
3d3f6ed40171
Negative literal integers work properly now..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
388 | MustNext (); |
30
6c4efed2dbdd
Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents:
28
diff
changeset
|
389 | |
54
8cc91ef94754
Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
51
diff
changeset
|
390 | str num = token; |
67
0a202714eea4
Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
63
diff
changeset
|
391 | if (num == "-") { |
54
8cc91ef94754
Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
51
diff
changeset
|
392 | MustNext (); |
8cc91ef94754
Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
51
diff
changeset
|
393 | num += token; |
8cc91ef94754
Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
51
diff
changeset
|
394 | } |
8cc91ef94754
Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
51
diff
changeset
|
395 | |
38 | 396 | // "true" and "false" are valid numbers |
397 | if (!token.icompare ("true")) | |
398 | token = "1"; | |
399 | else if (!token.icompare ("false")) | |
400 | token = "0"; | |
401 | else { | |
51
2cfa6edbf928
Added <<= and >>= operator support. Zandronum does not provide these, thus they are translated to their longer forms by the compiler. Also aded << and >>.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
50
diff
changeset
|
402 | if (!token.isnumber()) |
54
8cc91ef94754
Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
51
diff
changeset
|
403 | ParserError ("expected a number, got `%s`", num.chars()); |
50
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
38
diff
changeset
|
404 | |
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
38
diff
changeset
|
405 | str check; |
54
8cc91ef94754
Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
51
diff
changeset
|
406 | check.appendformat ("%d", atoi (num)); |
67
0a202714eea4
Some rework on variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
63
diff
changeset
|
407 | if (token != check) |
54
8cc91ef94754
Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
51
diff
changeset
|
408 | ParserWarning ("integer too large: %s -> %s", num.chars(), check.chars()); |
8cc91ef94754
Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
51
diff
changeset
|
409 | |
8cc91ef94754
Added floating point support for the most part. However, decimals are somehow lost during a cast..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
51
diff
changeset
|
410 | token = num; |
22
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
20
diff
changeset
|
411 | } |
0 | 412 | } |