123 // ============================================================================ |
123 // ============================================================================ |
124 char ScriptReader::ReadChar () { |
124 char ScriptReader::ReadChar () { |
125 if (feof (fp[fc])) |
125 if (feof (fp[fc])) |
126 return 0; |
126 return 0; |
127 |
127 |
128 char* c = (char*)malloc (sizeof (char)); |
128 char c; |
129 if (!fread (c, sizeof (char), 1, fp[fc])) |
129 if (!fread (&c, 1, 1, fp[fc])) |
130 return 0; |
130 return 0; |
131 |
131 |
132 // We're at a newline, thus next char read will begin the next line |
132 // We're at a newline, thus next char read will begin the next line |
133 if (atnewline) { |
133 if (atnewline) { |
134 atnewline = false; |
134 atnewline = false; |
135 curline[fc]++; |
135 curline[fc]++; |
136 curchar[fc] = 0; // gets incremented to 1 |
136 curchar[fc] = 0; // gets incremented to 1 |
137 } |
137 } |
138 |
138 |
139 if (c[0] == '\n') { |
139 if (c == '\n') { |
140 atnewline = true; |
140 atnewline = true; |
141 |
141 |
142 // Check for pre-processor directives |
142 // Check for pre-processor directives |
143 PreprocessDirectives (); |
143 PreprocessDirectives (); |
144 } |
144 } |
145 |
145 |
146 curchar[fc]++; |
146 curchar[fc]++; |
147 return c[0]; |
147 return c; |
148 } |
148 } |
149 |
149 |
150 // ============================================================================ |
150 // ============================================================================ |
151 // Peeks the next character |
151 // Peeks the next character |
152 char ScriptReader::PeekChar (int offset) { |
152 char ScriptReader::PeekChar (int offset) { |
373 str num; |
373 str num; |
374 if (!fromthis) |
374 if (!fromthis) |
375 MustNext (); |
375 MustNext (); |
376 num += token; |
376 num += token; |
377 |
377 |
378 // The number can possibly start off with a minus sign, which |
|
379 // also breaks the token. If we encounter it, read another token |
|
380 // and merge it to this one. |
|
381 if (!token.compare ("-")) { |
|
382 MustNext (); |
|
383 num += token; |
|
384 } |
|
385 |
|
386 // "true" and "false" are valid numbers |
378 // "true" and "false" are valid numbers |
387 if (!token.icompare ("true")) |
379 if (!token.icompare ("true")) |
388 token = "1"; |
380 token = "1"; |
389 else if (!token.icompare ("false")) |
381 else if (!token.icompare ("false")) |
390 token = "0"; |
382 token = "0"; |
391 else { |
383 else { |
392 if (!num.isnumber()) |
384 if (!num.isnumber()) |
393 ParserError ("expected a number, got `%s`", num.chars()); |
385 ParserError ("expected a number, got `%s`", num.chars()); |
394 token = num; |
386 token = num; |
395 } |
387 |
396 } |
388 // Overflow check |
|
389 str check; |
|
390 check.appendformat ("%d", atoi (num)); |
|
391 if (token.compare (check) != 0) |
|
392 ParserWarning ("integer too large: %s -> %s", token.chars(), check.chars()); |
|
393 } |
|
394 } |