author | Ryan C. Gordon <icculus@icculus.org> |
Sun, 29 May 2016 03:03:15 -0400 | |
changeset 1176 | 1356c2c32ef6 |
parent 1142 | 7b554e065321 |
child 1182 | 179ffe99c57f |
permissions | -rw-r--r-- |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
1 |
/** |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
2 |
* MojoShader; generate shader programs from bytecode of compiled |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
3 |
* Direct3D shaders. |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
4 |
* |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
5 |
* Please see the file LICENSE.txt in the source's root directory. |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
6 |
* |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
7 |
* This file written by Ryan C. Gordon. |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
8 |
*/ |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
9 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
10 |
#define __MOJOSHADER_INTERNAL__ 1 |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
11 |
#include "mojoshader_internal.h" |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
12 |
|
587
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
13 |
#if DEBUG_PREPROCESSOR |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
14 |
#define print_debug_token(token, len, val) \ |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
15 |
MOJOSHADER_print_debug_token("PREPROCESSOR", token, len, val) |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
16 |
#else |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
17 |
#define print_debug_token(token, len, val) |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
18 |
#endif |
561
0d2535cc5ac3
Added basic lexer debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
560
diff
changeset
|
19 |
|
630
e6bd53dd1f91
Allow debug output directly from the lexer, before preprocessor eats.
Ryan C. Gordon <icculus@icculus.org>
parents:
629
diff
changeset
|
20 |
#if DEBUG_LEXER |
649
527cfe6f2d45
Renamed preprocessor_internal_lexer to preprocessor_lexer.
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
21 |
static Token debug_preprocessor_lexer(IncludeState *s) |
630
e6bd53dd1f91
Allow debug output directly from the lexer, before preprocessor eats.
Ryan C. Gordon <icculus@icculus.org>
parents:
629
diff
changeset
|
22 |
{ |
649
527cfe6f2d45
Renamed preprocessor_internal_lexer to preprocessor_lexer.
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
23 |
const Token retval = preprocessor_lexer(s); |
648
243e35876142
Have lexer calculate token size now.
Ryan C. Gordon <icculus@icculus.org>
parents:
647
diff
changeset
|
24 |
MOJOSHADER_print_debug_token("LEXER", s->token, s->tokenlen, retval); |
630
e6bd53dd1f91
Allow debug output directly from the lexer, before preprocessor eats.
Ryan C. Gordon <icculus@icculus.org>
parents:
629
diff
changeset
|
25 |
return retval; |
649
527cfe6f2d45
Renamed preprocessor_internal_lexer to preprocessor_lexer.
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
26 |
} // debug_preprocessor_lexer |
527cfe6f2d45
Renamed preprocessor_internal_lexer to preprocessor_lexer.
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
27 |
#define preprocessor_lexer(s) debug_preprocessor_lexer(s) |
630
e6bd53dd1f91
Allow debug output directly from the lexer, before preprocessor eats.
Ryan C. Gordon <icculus@icculus.org>
parents:
629
diff
changeset
|
28 |
#endif |
e6bd53dd1f91
Allow debug output directly from the lexer, before preprocessor eats.
Ryan C. Gordon <icculus@icculus.org>
parents:
629
diff
changeset
|
29 |
|
723
6d58b504cc6e
Print out current source/line number when debugging any parser/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents:
701
diff
changeset
|
30 |
#if DEBUG_TOKENIZER |
6d58b504cc6e
Print out current source/line number when debugging any parser/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents:
701
diff
changeset
|
31 |
static void print_debug_lexing_position(IncludeState *s) |
6d58b504cc6e
Print out current source/line number when debugging any parser/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents:
701
diff
changeset
|
32 |
{ |
727
6978fac0fa31
Fixed NULL dereference in debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
723
diff
changeset
|
33 |
if (s != NULL) |
6978fac0fa31
Fixed NULL dereference in debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
723
diff
changeset
|
34 |
printf("NOW LEXING %s:%d ...\n", s->filename, s->line); |
723
6d58b504cc6e
Print out current source/line number when debugging any parser/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents:
701
diff
changeset
|
35 |
} // print_debug_lexing_position |
6d58b504cc6e
Print out current source/line number when debugging any parser/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents:
701
diff
changeset
|
36 |
#else |
6d58b504cc6e
Print out current source/line number when debugging any parser/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents:
701
diff
changeset
|
37 |
#define print_debug_lexing_position(s) |
6d58b504cc6e
Print out current source/line number when debugging any parser/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents:
701
diff
changeset
|
38 |
#endif |
6d58b504cc6e
Print out current source/line number when debugging any parser/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents:
701
diff
changeset
|
39 |
|
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
40 |
typedef struct Context |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
41 |
{ |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
42 |
int isfail; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
43 |
int out_of_memory; |
606
63e7a66ac320
Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
605
diff
changeset
|
44 |
char failstr[256]; |
689
5872092cbf95
Fixed crash when macros are recursing.
Ryan C. Gordon <icculus@icculus.org>
parents:
687
diff
changeset
|
45 |
int recursion_count; |
690
4710df464f13
Moved asm comment processing into the lexer.
Ryan C. Gordon <icculus@icculus.org>
parents:
689
diff
changeset
|
46 |
int asm_comments; |
910
3ee487d85ecb
Handle #pragma (or at least, don't fail on them).
Ryan C. Gordon <icculus@icculus.org>
parents:
898
diff
changeset
|
47 |
int parsing_pragma; |
620
1c4cf996004e
Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents:
618
diff
changeset
|
48 |
Conditional *conditional_pool; |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
49 |
IncludeState *include_stack; |
646
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
50 |
IncludeState *include_pool; |
676
29447167fc68
Renamed DefineHash to Define.
Ryan C. Gordon <icculus@icculus.org>
parents:
675
diff
changeset
|
51 |
Define *define_hashtable[256]; |
29447167fc68
Renamed DefineHash to Define.
Ryan C. Gordon <icculus@icculus.org>
parents:
675
diff
changeset
|
52 |
Define *define_pool; |
896
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
53 |
Define *file_macro; |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
54 |
Define *line_macro; |
858
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
841
diff
changeset
|
55 |
StringCache *filename_cache; |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
56 |
MOJOSHADER_includeOpen open_callback; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
57 |
MOJOSHADER_includeClose close_callback; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
58 |
MOJOSHADER_malloc malloc; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
59 |
MOJOSHADER_free free; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
60 |
void *malloc_data; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
61 |
} Context; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
62 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
63 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
64 |
// Convenience functions for allocators... |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
65 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
66 |
static inline void out_of_memory(Context *ctx) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
67 |
{ |
598
decc32dc03a7
Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents:
597
diff
changeset
|
68 |
ctx->out_of_memory = 1; |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
69 |
} // out_of_memory |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
70 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
71 |
static inline void *Malloc(Context *ctx, const size_t len) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
72 |
{ |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
73 |
void *retval = ctx->malloc((int) len, ctx->malloc_data); |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
74 |
if (retval == NULL) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
75 |
out_of_memory(ctx); |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
76 |
return retval; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
77 |
} // Malloc |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
78 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
79 |
static inline void Free(Context *ctx, void *ptr) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
80 |
{ |
939
64cc93ee5a56
Cut-and-paste cleanup: unified the ErrorList functionality.
Ryan C. Gordon <icculus@icculus.org>
parents:
913
diff
changeset
|
81 |
ctx->free(ptr, ctx->malloc_data); |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
82 |
} // Free |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
83 |
|
944
9f9fa9650772
Paying off more technical debt: unified growable buffers into one place.
Ryan C. Gordon <icculus@icculus.org>
parents:
941
diff
changeset
|
84 |
static void *MallocBridge(int bytes, void *data) |
9f9fa9650772
Paying off more technical debt: unified growable buffers into one place.
Ryan C. Gordon <icculus@icculus.org>
parents:
941
diff
changeset
|
85 |
{ |
9f9fa9650772
Paying off more technical debt: unified growable buffers into one place.
Ryan C. Gordon <icculus@icculus.org>
parents:
941
diff
changeset
|
86 |
return Malloc((Context *) data, (size_t) bytes); |
9f9fa9650772
Paying off more technical debt: unified growable buffers into one place.
Ryan C. Gordon <icculus@icculus.org>
parents:
941
diff
changeset
|
87 |
} // MallocBridge |
9f9fa9650772
Paying off more technical debt: unified growable buffers into one place.
Ryan C. Gordon <icculus@icculus.org>
parents:
941
diff
changeset
|
88 |
|
9f9fa9650772
Paying off more technical debt: unified growable buffers into one place.
Ryan C. Gordon <icculus@icculus.org>
parents:
941
diff
changeset
|
89 |
static void FreeBridge(void *ptr, void *data) |
9f9fa9650772
Paying off more technical debt: unified growable buffers into one place.
Ryan C. Gordon <icculus@icculus.org>
parents:
941
diff
changeset
|
90 |
{ |
9f9fa9650772
Paying off more technical debt: unified growable buffers into one place.
Ryan C. Gordon <icculus@icculus.org>
parents:
941
diff
changeset
|
91 |
Free((Context *) data, ptr); |
9f9fa9650772
Paying off more technical debt: unified growable buffers into one place.
Ryan C. Gordon <icculus@icculus.org>
parents:
941
diff
changeset
|
92 |
} // FreeBridge |
9f9fa9650772
Paying off more technical debt: unified growable buffers into one place.
Ryan C. Gordon <icculus@icculus.org>
parents:
941
diff
changeset
|
93 |
|
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
94 |
static inline char *StrDup(Context *ctx, const char *str) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
95 |
{ |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
96 |
char *retval = (char *) Malloc(ctx, strlen(str) + 1); |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
97 |
if (retval != NULL) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
98 |
strcpy(retval, str); |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
99 |
return retval; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
100 |
} // StrDup |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
101 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
102 |
static void failf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3); |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
103 |
static void failf(Context *ctx, const char *fmt, ...) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
104 |
{ |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
105 |
ctx->isfail = 1; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
106 |
va_list ap; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
107 |
va_start(ap, fmt); |
598
decc32dc03a7
Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents:
597
diff
changeset
|
108 |
vsnprintf(ctx->failstr, sizeof (ctx->failstr), fmt, ap); |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
109 |
va_end(ap); |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
110 |
} // failf |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
111 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
112 |
static inline void fail(Context *ctx, const char *reason) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
113 |
{ |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
114 |
failf(ctx, "%s", reason); |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
115 |
} // fail |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
116 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
117 |
|
587
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
118 |
#if DEBUG_TOKENIZER |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
119 |
void MOJOSHADER_print_debug_token(const char *subsystem, const char *token, |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
120 |
const unsigned int tokenlen, |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
121 |
const Token tokenval) |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
122 |
{ |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
123 |
printf("%s TOKEN: \"", subsystem); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
124 |
unsigned int i; |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
125 |
for (i = 0; i < tokenlen; i++) |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
126 |
{ |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
127 |
if (token[i] == '\n') |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
128 |
printf("\\n"); |
629
d886403c6609
Better debug token output.
Ryan C. Gordon <icculus@icculus.org>
parents:
628
diff
changeset
|
129 |
else if (token[i] == '\\') |
d886403c6609
Better debug token output.
Ryan C. Gordon <icculus@icculus.org>
parents:
628
diff
changeset
|
130 |
printf("\\\\"); |
587
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
131 |
else |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
132 |
printf("%c", token[i]); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
133 |
} // for |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
134 |
printf("\" ("); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
135 |
switch (tokenval) |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
136 |
{ |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
137 |
#define TOKENCASE(x) case x: printf("%s", #x); break |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
138 |
TOKENCASE(TOKEN_UNKNOWN); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
139 |
TOKENCASE(TOKEN_IDENTIFIER); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
140 |
TOKENCASE(TOKEN_INT_LITERAL); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
141 |
TOKENCASE(TOKEN_FLOAT_LITERAL); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
142 |
TOKENCASE(TOKEN_STRING_LITERAL); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
143 |
TOKENCASE(TOKEN_ADDASSIGN); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
144 |
TOKENCASE(TOKEN_SUBASSIGN); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
145 |
TOKENCASE(TOKEN_MULTASSIGN); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
146 |
TOKENCASE(TOKEN_DIVASSIGN); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
147 |
TOKENCASE(TOKEN_MODASSIGN); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
148 |
TOKENCASE(TOKEN_XORASSIGN); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
149 |
TOKENCASE(TOKEN_ANDASSIGN); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
150 |
TOKENCASE(TOKEN_ORASSIGN); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
151 |
TOKENCASE(TOKEN_INCREMENT); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
152 |
TOKENCASE(TOKEN_DECREMENT); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
153 |
TOKENCASE(TOKEN_RSHIFT); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
154 |
TOKENCASE(TOKEN_LSHIFT); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
155 |
TOKENCASE(TOKEN_ANDAND); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
156 |
TOKENCASE(TOKEN_OROR); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
157 |
TOKENCASE(TOKEN_LEQ); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
158 |
TOKENCASE(TOKEN_GEQ); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
159 |
TOKENCASE(TOKEN_EQL); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
160 |
TOKENCASE(TOKEN_NEQ); |
865
4eb06464212d
Added stringify operator ("#") to preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
864
diff
changeset
|
161 |
TOKENCASE(TOKEN_HASH); |
587
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
162 |
TOKENCASE(TOKEN_HASHHASH); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
163 |
TOKENCASE(TOKEN_PP_INCLUDE); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
164 |
TOKENCASE(TOKEN_PP_LINE); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
165 |
TOKENCASE(TOKEN_PP_DEFINE); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
166 |
TOKENCASE(TOKEN_PP_UNDEF); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
167 |
TOKENCASE(TOKEN_PP_IF); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
168 |
TOKENCASE(TOKEN_PP_IFDEF); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
169 |
TOKENCASE(TOKEN_PP_IFNDEF); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
170 |
TOKENCASE(TOKEN_PP_ELSE); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
171 |
TOKENCASE(TOKEN_PP_ELIF); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
172 |
TOKENCASE(TOKEN_PP_ENDIF); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
173 |
TOKENCASE(TOKEN_PP_ERROR); |
910
3ee487d85ecb
Handle #pragma (or at least, don't fail on them).
Ryan C. Gordon <icculus@icculus.org>
parents:
898
diff
changeset
|
174 |
TOKENCASE(TOKEN_PP_PRAGMA); |
604 | 175 |
TOKENCASE(TOKEN_INCOMPLETE_COMMENT); |
176 |
TOKENCASE(TOKEN_BAD_CHARS); |
|
1121
4142681f9fda
Allow preprocessor to pass through comments, like GNU cpp does.
Ryan C. Gordon <icculus@icculus.org>
parents:
1120
diff
changeset
|
177 |
TOKENCASE(TOKEN_SINGLE_COMMENT); |
4142681f9fda
Allow preprocessor to pass through comments, like GNU cpp does.
Ryan C. Gordon <icculus@icculus.org>
parents:
1120
diff
changeset
|
178 |
TOKENCASE(TOKEN_MULTI_COMMENT); |
587
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
179 |
TOKENCASE(TOKEN_EOI); |
598
decc32dc03a7
Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents:
597
diff
changeset
|
180 |
TOKENCASE(TOKEN_PREPROCESSING_ERROR); |
587
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
181 |
#undef TOKENCASE |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
182 |
|
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
183 |
case ((Token) '\n'): |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
184 |
printf("'\\n'"); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
185 |
break; |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
186 |
|
629
d886403c6609
Better debug token output.
Ryan C. Gordon <icculus@icculus.org>
parents:
628
diff
changeset
|
187 |
case ((Token) '\\'): |
d886403c6609
Better debug token output.
Ryan C. Gordon <icculus@icculus.org>
parents:
628
diff
changeset
|
188 |
printf("'\\\\'"); |
d886403c6609
Better debug token output.
Ryan C. Gordon <icculus@icculus.org>
parents:
628
diff
changeset
|
189 |
break; |
d886403c6609
Better debug token output.
Ryan C. Gordon <icculus@icculus.org>
parents:
628
diff
changeset
|
190 |
|
587
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
191 |
default: |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
192 |
assert(((int)tokenval) < 256); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
193 |
printf("'%c'", (char) tokenval); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
194 |
break; |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
195 |
} // switch |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
196 |
printf(")\n"); |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
197 |
} // MOJOSHADER_print_debug_token |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
198 |
#endif |
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
199 |
|
202354e004fc
Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents:
586
diff
changeset
|
200 |
|
611
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
201 |
|
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
202 |
#if !MOJOSHADER_FORCE_INCLUDE_CALLBACKS |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
203 |
|
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
204 |
// !!! FIXME: most of these _MSC_VER should probably be _WINDOWS? |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
205 |
#ifdef _MSC_VER |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
206 |
#define WIN32_LEAN_AND_MEAN 1 |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
207 |
#include <windows.h> // GL headers need this for WINGDIAPI definition. |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
208 |
#else |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
209 |
#include <sys/stat.h> |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
210 |
#include <fcntl.h> |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
211 |
#include <unistd.h> |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
212 |
#endif |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
213 |
|
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
214 |
int MOJOSHADER_internal_include_open(MOJOSHADER_includeType inctype, |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
215 |
const char *fname, const char *parent, |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
216 |
const char **outdata, |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
217 |
unsigned int *outbytes, |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
218 |
MOJOSHADER_malloc m, MOJOSHADER_free f, |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
219 |
void *d) |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
220 |
{ |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
221 |
#ifdef _MSC_VER |
882
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
222 |
WCHAR wpath[MAX_PATH]; |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
223 |
if (!MultiByteToWideChar(CP_UTF8, 0, fname, -1, wpath, MAX_PATH)) |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
224 |
return 0; |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
225 |
|
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
226 |
const DWORD share = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
227 |
const HANDLE handle = CreateFileW(wpath, FILE_GENERIC_READ, share, |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
228 |
NULL, OPEN_EXISTING, NULL, NULL); |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
229 |
if (handle == INVALID_HANDLE_VALUE) |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
230 |
return 0; |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
231 |
|
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
232 |
const DWORD fileSize = GetFileSize(handle, NULL); |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
233 |
if (fileSize == INVALID_FILE_SIZE) |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
234 |
{ |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
235 |
CloseHandle(handle); |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
236 |
return 0; |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
237 |
} // if |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
238 |
|
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
239 |
char *data = (char *) m(fileSize, d); |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
240 |
if (data == NULL) |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
241 |
{ |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
242 |
CloseHandle(handle); |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
243 |
return 0; |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
244 |
} // if |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
245 |
|
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
246 |
DWORD readLength = 0; |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
247 |
if (!ReadFile(handle, data, fileSize, &readLength, NULL)) |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
248 |
{ |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
249 |
CloseHandle(handle); |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
250 |
f(data, d); |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
251 |
return 0; |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
252 |
} // if |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
253 |
|
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
254 |
CloseHandle(handle); |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
255 |
|
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
256 |
if (readLength != fileSize) |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
257 |
{ |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
258 |
f(data, d); |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
259 |
return 0; |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
260 |
} // if |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
261 |
*outdata = data; |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
262 |
*outbytes = fileSize; |
dd526bab5a51
Did some Ryanification of Aras's Windows #include handling code.
Ryan C. Gordon <icculus@icculus.org>
parents:
881
diff
changeset
|
263 |
return 1; |
611
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
264 |
#else |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
265 |
struct stat statbuf; |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
266 |
if (stat(fname, &statbuf) == -1) |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
267 |
return 0; |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
268 |
char *data = (char *) m(statbuf.st_size, d); |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
269 |
if (data == NULL) |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
270 |
return 0; |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
271 |
const int fd = open(fname, O_RDONLY); |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
272 |
if (fd == -1) |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
273 |
{ |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
274 |
f(data, d); |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
275 |
return 0; |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
276 |
} // if |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
277 |
if (read(fd, data, statbuf.st_size) != statbuf.st_size) |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
278 |
{ |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
279 |
f(data, d); |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
280 |
close(fd); |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
281 |
return 0; |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
282 |
} // if |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
283 |
close(fd); |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
284 |
*outdata = data; |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
285 |
*outbytes = (unsigned int) statbuf.st_size; |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
286 |
return 1; |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
287 |
#endif |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
288 |
} // MOJOSHADER_internal_include_open |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
289 |
|
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
290 |
|
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
291 |
void MOJOSHADER_internal_include_close(const char *data, MOJOSHADER_malloc m, |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
292 |
MOJOSHADER_free f, void *d) |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
293 |
{ |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
294 |
f((void *) data, d); |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
295 |
} // MOJOSHADER_internal_include_close |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
296 |
#endif // !MOJOSHADER_FORCE_INCLUDE_CALLBACKS |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
297 |
|
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
298 |
|
647 | 299 |
// !!! FIXME: maybe use these pool magic elsewhere? |
940
bc2a5efade5e
Added a bunch of FIXMEs to accurately portray current technical debt.
Ryan C. Gordon <icculus@icculus.org>
parents:
939
diff
changeset
|
300 |
// !!! FIXME: maybe just get rid of this? (maybe the fragmentation isn't a big deal?) |
632
a5f63a10ed91
Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents:
631
diff
changeset
|
301 |
|
646
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
302 |
// Pool stuff... |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
303 |
// ugh, I hate this macro salsa. |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
304 |
#define FREE_POOL(type, poolname) \ |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
305 |
static void free_##poolname##_pool(Context *ctx) { \ |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
306 |
type *item = ctx->poolname##_pool; \ |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
307 |
while (item != NULL) { \ |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
308 |
type *next = item->next; \ |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
309 |
Free(ctx, item); \ |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
310 |
item = next; \ |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
311 |
} \ |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
312 |
} |
620
1c4cf996004e
Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents:
618
diff
changeset
|
313 |
|
646
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
314 |
#define GET_POOL(type, poolname) \ |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
315 |
static type *get_##poolname(Context *ctx) { \ |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
316 |
type *retval = ctx->poolname##_pool; \ |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
317 |
if (retval != NULL) \ |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
318 |
ctx->poolname##_pool = retval->next; \ |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
319 |
else \ |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
320 |
retval = (type *) Malloc(ctx, sizeof (type)); \ |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
321 |
if (retval != NULL) \ |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
322 |
memset(retval, '\0', sizeof (type)); \ |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
323 |
return retval; \ |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
324 |
} |
620
1c4cf996004e
Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents:
618
diff
changeset
|
325 |
|
646
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
326 |
#define PUT_POOL(type, poolname) \ |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
327 |
static void put_##poolname(Context *ctx, type *item) { \ |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
328 |
item->next = ctx->poolname##_pool; \ |
672
bcca8b3ae152
Fixed memory pool code.
Ryan C. Gordon <icculus@icculus.org>
parents:
658
diff
changeset
|
329 |
ctx->poolname##_pool = item; \ |
646
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
330 |
} |
620
1c4cf996004e
Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents:
618
diff
changeset
|
331 |
|
646
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
332 |
#define IMPLEMENT_POOL(type, poolname) \ |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
333 |
FREE_POOL(type, poolname) \ |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
334 |
GET_POOL(type, poolname) \ |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
335 |
PUT_POOL(type, poolname) |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
336 |
|
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
337 |
IMPLEMENT_POOL(Conditional, conditional) |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
338 |
IMPLEMENT_POOL(IncludeState, include) |
676
29447167fc68
Renamed DefineHash to Define.
Ryan C. Gordon <icculus@icculus.org>
parents:
675
diff
changeset
|
339 |
IMPLEMENT_POOL(Define, define) |
620
1c4cf996004e
Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents:
618
diff
changeset
|
340 |
|
611
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
341 |
|
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
342 |
// Preprocessor define hashtable stuff... |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
343 |
|
841 | 344 |
// !!! FIXME: why isn't this using mojoshader_common.c's code? |
345 |
||
729
92cc053fbf75
Put in a much better string hashing algorithm.
Ryan C. Gordon <icculus@icculus.org>
parents:
727
diff
changeset
|
346 |
// this is djb's xor hashing function. |
92cc053fbf75
Put in a much better string hashing algorithm.
Ryan C. Gordon <icculus@icculus.org>
parents:
727
diff
changeset
|
347 |
static inline uint32 hash_string_djbxor(const char *sym) |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
348 |
{ |
729
92cc053fbf75
Put in a much better string hashing algorithm.
Ryan C. Gordon <icculus@icculus.org>
parents:
727
diff
changeset
|
349 |
register uint32 hash = 5381; |
622
59b3003f6494
Fixed logic bug in string loop.
Ryan C. Gordon <icculus@icculus.org>
parents:
621
diff
changeset
|
350 |
while (*sym) |
729
92cc053fbf75
Put in a much better string hashing algorithm.
Ryan C. Gordon <icculus@icculus.org>
parents:
727
diff
changeset
|
351 |
hash = ((hash << 5) + hash) ^ *(sym++); |
92cc053fbf75
Put in a much better string hashing algorithm.
Ryan C. Gordon <icculus@icculus.org>
parents:
727
diff
changeset
|
352 |
return hash; |
92cc053fbf75
Put in a much better string hashing algorithm.
Ryan C. Gordon <icculus@icculus.org>
parents:
727
diff
changeset
|
353 |
} // hash_string_djbxor |
92cc053fbf75
Put in a much better string hashing algorithm.
Ryan C. Gordon <icculus@icculus.org>
parents:
727
diff
changeset
|
354 |
|
92cc053fbf75
Put in a much better string hashing algorithm.
Ryan C. Gordon <icculus@icculus.org>
parents:
727
diff
changeset
|
355 |
static inline uint8 hash_define(const char *sym) |
92cc053fbf75
Put in a much better string hashing algorithm.
Ryan C. Gordon <icculus@icculus.org>
parents:
727
diff
changeset
|
356 |
{ |
92cc053fbf75
Put in a much better string hashing algorithm.
Ryan C. Gordon <icculus@icculus.org>
parents:
727
diff
changeset
|
357 |
return (uint8) hash_string_djbxor(sym); |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
358 |
} // hash_define |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
359 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
360 |
|
678
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
361 |
static int add_define(Context *ctx, const char *sym, const char *val, |
755
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
362 |
char **parameters, int paramcount) |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
363 |
{ |
729
92cc053fbf75
Put in a much better string hashing algorithm.
Ryan C. Gordon <icculus@icculus.org>
parents:
727
diff
changeset
|
364 |
const uint8 hash = hash_define(sym); |
676
29447167fc68
Renamed DefineHash to Define.
Ryan C. Gordon <icculus@icculus.org>
parents:
675
diff
changeset
|
365 |
Define *bucket = ctx->define_hashtable[hash]; |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
366 |
while (bucket) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
367 |
{ |
655
0b82fdeebf86
Changed DefineHash definition.
Ryan C. Gordon <icculus@icculus.org>
parents:
654
diff
changeset
|
368 |
if (strcmp(bucket->identifier, sym) == 0) |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
369 |
{ |
875
dc28354d576e
Added support for __FILE__ and __LINE__ to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
873
diff
changeset
|
370 |
failf(ctx, "'%s' already defined", sym); // !!! FIXME: warning? |
dc28354d576e
Added support for __FILE__ and __LINE__ to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
873
diff
changeset
|
371 |
// !!! FIXME: gcc reports the location of previous #define here. |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
372 |
return 0; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
373 |
} // if |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
374 |
bucket = bucket->next; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
375 |
} // while |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
376 |
|
646
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
377 |
bucket = get_define(ctx); |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
378 |
if (bucket == NULL) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
379 |
return 0; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
380 |
|
678
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
381 |
bucket->definition = val; |
894
1847405a16c6
Serious reworking of preprocessor macro replacement code.
Ryan C. Gordon <icculus@icculus.org>
parents:
882
diff
changeset
|
382 |
bucket->original = NULL; |
678
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
383 |
bucket->identifier = sym; |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
384 |
bucket->parameters = (const char **) parameters; |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
385 |
bucket->paramcount = paramcount; |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
386 |
bucket->next = ctx->define_hashtable[hash]; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
387 |
ctx->define_hashtable[hash] = bucket; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
388 |
return 1; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
389 |
} // add_define |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
390 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
391 |
|
678
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
392 |
static void free_define(Context *ctx, Define *def) |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
393 |
{ |
896
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
394 |
if (def != NULL) |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
395 |
{ |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
396 |
int i; |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
397 |
for (i = 0; i < def->paramcount; i++) |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
398 |
Free(ctx, (void *) def->parameters[i]); |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
399 |
Free(ctx, (void *) def->parameters); |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
400 |
Free(ctx, (void *) def->identifier); |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
401 |
Free(ctx, (void *) def->definition); |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
402 |
Free(ctx, (void *) def->original); |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
403 |
put_define(ctx, def); |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
404 |
} // if |
678
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
405 |
} // free_define |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
406 |
|
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
407 |
|
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
408 |
static int remove_define(Context *ctx, const char *sym) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
409 |
{ |
729
92cc053fbf75
Put in a much better string hashing algorithm.
Ryan C. Gordon <icculus@icculus.org>
parents:
727
diff
changeset
|
410 |
const uint8 hash = hash_define(sym); |
676
29447167fc68
Renamed DefineHash to Define.
Ryan C. Gordon <icculus@icculus.org>
parents:
675
diff
changeset
|
411 |
Define *bucket = ctx->define_hashtable[hash]; |
29447167fc68
Renamed DefineHash to Define.
Ryan C. Gordon <icculus@icculus.org>
parents:
675
diff
changeset
|
412 |
Define *prev = NULL; |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
413 |
while (bucket) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
414 |
{ |
655
0b82fdeebf86
Changed DefineHash definition.
Ryan C. Gordon <icculus@icculus.org>
parents:
654
diff
changeset
|
415 |
if (strcmp(bucket->identifier, sym) == 0) |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
416 |
{ |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
417 |
if (prev == NULL) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
418 |
ctx->define_hashtable[hash] = bucket->next; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
419 |
else |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
420 |
prev->next = bucket->next; |
678
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
421 |
free_define(ctx, bucket); |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
422 |
return 1; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
423 |
} // if |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
424 |
prev = bucket; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
425 |
bucket = bucket->next; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
426 |
} // while |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
427 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
428 |
return 0; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
429 |
} // remove_define |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
430 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
431 |
|
678
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
432 |
static const Define *find_define(Context *ctx, const char *sym) |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
433 |
{ |
1128
a66416e507d1
Optimizations to preprocessor's find_define().
Ryan C. Gordon <icculus@icculus.org>
parents:
1126
diff
changeset
|
434 |
const uint8 hash = hash_define(sym); |
a66416e507d1
Optimizations to preprocessor's find_define().
Ryan C. Gordon <icculus@icculus.org>
parents:
1126
diff
changeset
|
435 |
Define *bucket = ctx->define_hashtable[hash]; |
a66416e507d1
Optimizations to preprocessor's find_define().
Ryan C. Gordon <icculus@icculus.org>
parents:
1126
diff
changeset
|
436 |
while (bucket) |
a66416e507d1
Optimizations to preprocessor's find_define().
Ryan C. Gordon <icculus@icculus.org>
parents:
1126
diff
changeset
|
437 |
{ |
a66416e507d1
Optimizations to preprocessor's find_define().
Ryan C. Gordon <icculus@icculus.org>
parents:
1126
diff
changeset
|
438 |
if (strcmp(bucket->identifier, sym) == 0) |
a66416e507d1
Optimizations to preprocessor's find_define().
Ryan C. Gordon <icculus@icculus.org>
parents:
1126
diff
changeset
|
439 |
return bucket; |
a66416e507d1
Optimizations to preprocessor's find_define().
Ryan C. Gordon <icculus@icculus.org>
parents:
1126
diff
changeset
|
440 |
bucket = bucket->next; |
a66416e507d1
Optimizations to preprocessor's find_define().
Ryan C. Gordon <icculus@icculus.org>
parents:
1126
diff
changeset
|
441 |
} // while |
a66416e507d1
Optimizations to preprocessor's find_define().
Ryan C. Gordon <icculus@icculus.org>
parents:
1126
diff
changeset
|
442 |
|
a66416e507d1
Optimizations to preprocessor's find_define().
Ryan C. Gordon <icculus@icculus.org>
parents:
1126
diff
changeset
|
443 |
const uint8 filestrhash = 67; |
a66416e507d1
Optimizations to preprocessor's find_define().
Ryan C. Gordon <icculus@icculus.org>
parents:
1126
diff
changeset
|
444 |
assert(hash_define("__FILE__") == filestrhash); |
a66416e507d1
Optimizations to preprocessor's find_define().
Ryan C. Gordon <icculus@icculus.org>
parents:
1126
diff
changeset
|
445 |
|
a66416e507d1
Optimizations to preprocessor's find_define().
Ryan C. Gordon <icculus@icculus.org>
parents:
1126
diff
changeset
|
446 |
const uint8 linestrhash = 75; |
a66416e507d1
Optimizations to preprocessor's find_define().
Ryan C. Gordon <icculus@icculus.org>
parents:
1126
diff
changeset
|
447 |
assert(hash_define("__LINE__") == linestrhash); |
a66416e507d1
Optimizations to preprocessor's find_define().
Ryan C. Gordon <icculus@icculus.org>
parents:
1126
diff
changeset
|
448 |
|
a66416e507d1
Optimizations to preprocessor's find_define().
Ryan C. Gordon <icculus@icculus.org>
parents:
1126
diff
changeset
|
449 |
if ( (hash == filestrhash) && (ctx->file_macro) && (strcmp(sym, "__FILE__") == 0) ) |
896
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
450 |
{ |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
451 |
Free(ctx, (char *) ctx->file_macro->definition); |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
452 |
const IncludeState *state = ctx->include_stack; |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
453 |
const char *fname = state ? state->filename : ""; |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
454 |
const size_t len = strlen(fname) + 2; |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
455 |
char *str = (char *) Malloc(ctx, len); |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
456 |
if (!str) |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
457 |
return NULL; |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
458 |
str[0] = '\"'; |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
459 |
memcpy(str + 1, fname, len - 2); |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
460 |
str[len - 1] = '\"'; |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
461 |
ctx->file_macro->definition = str; |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
462 |
return ctx->file_macro; |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
463 |
} // if |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
464 |
|
1128
a66416e507d1
Optimizations to preprocessor's find_define().
Ryan C. Gordon <icculus@icculus.org>
parents:
1126
diff
changeset
|
465 |
else if ( (hash == linestrhash) && (ctx->line_macro) && (strcmp(sym, "__LINE__") == 0) ) |
896
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
466 |
{ |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
467 |
Free(ctx, (char *) ctx->line_macro->definition); |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
468 |
const IncludeState *state = ctx->include_stack; |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
469 |
const size_t bufsize = 32; |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
470 |
char *str = (char *) Malloc(ctx, bufsize); |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
471 |
if (!str) |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
472 |
return 0; |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
473 |
|
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
474 |
const size_t len = snprintf(str, bufsize, "%u", state->line); |
1129
908551a4334c
Fixed compiler warning.
Ryan C. Gordon <icculus@icculus.org>
parents:
1128
diff
changeset
|
475 |
assert(len < bufsize); (void) len; |
896
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
476 |
ctx->line_macro->definition = str; |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
477 |
return ctx->line_macro; |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
478 |
} // else |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
479 |
|
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
480 |
return NULL; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
481 |
} // find_define |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
482 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
483 |
|
699
c2f50b391d96
Support for "#if defined" in preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
698
diff
changeset
|
484 |
static const Define *find_define_by_token(Context *ctx) |
c2f50b391d96
Support for "#if defined" in preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
698
diff
changeset
|
485 |
{ |
c2f50b391d96
Support for "#if defined" in preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
698
diff
changeset
|
486 |
IncludeState *state = ctx->include_stack; |
c2f50b391d96
Support for "#if defined" in preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
698
diff
changeset
|
487 |
assert(state->tokenval == TOKEN_IDENTIFIER); |
c2f50b391d96
Support for "#if defined" in preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
698
diff
changeset
|
488 |
char *sym = (char *) alloca(state->tokenlen+1); |
c2f50b391d96
Support for "#if defined" in preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
698
diff
changeset
|
489 |
memcpy(sym, state->token, state->tokenlen); |
c2f50b391d96
Support for "#if defined" in preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
698
diff
changeset
|
490 |
sym[state->tokenlen] = '\0'; |
c2f50b391d96
Support for "#if defined" in preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
698
diff
changeset
|
491 |
return find_define(ctx, sym); |
c2f50b391d96
Support for "#if defined" in preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
698
diff
changeset
|
492 |
} // find_define_by_token |
c2f50b391d96
Support for "#if defined" in preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
698
diff
changeset
|
493 |
|
c2f50b391d96
Support for "#if defined" in preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
698
diff
changeset
|
494 |
|
894
1847405a16c6
Serious reworking of preprocessor macro replacement code.
Ryan C. Gordon <icculus@icculus.org>
parents:
882
diff
changeset
|
495 |
static const Define *find_macro_arg(const IncludeState *state, |
1847405a16c6
Serious reworking of preprocessor macro replacement code.
Ryan C. Gordon <icculus@icculus.org>
parents:
882
diff
changeset
|
496 |
const Define *defines) |
869
5bd359b047f3
Added concat operator ("##") to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
867
diff
changeset
|
497 |
{ |
5bd359b047f3
Added concat operator ("##") to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
867
diff
changeset
|
498 |
const Define *def = NULL; |
5bd359b047f3
Added concat operator ("##") to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
867
diff
changeset
|
499 |
char *sym = (char *) alloca(state->tokenlen + 1); |
5bd359b047f3
Added concat operator ("##") to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
867
diff
changeset
|
500 |
memcpy(sym, state->token, state->tokenlen); |
5bd359b047f3
Added concat operator ("##") to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
867
diff
changeset
|
501 |
sym[state->tokenlen] = '\0'; |
5bd359b047f3
Added concat operator ("##") to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
867
diff
changeset
|
502 |
|
894
1847405a16c6
Serious reworking of preprocessor macro replacement code.
Ryan C. Gordon <icculus@icculus.org>
parents:
882
diff
changeset
|
503 |
for (def = defines; def != NULL; def = def->next) |
869
5bd359b047f3
Added concat operator ("##") to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
867
diff
changeset
|
504 |
{ |
873
bdffabea8e40
Macro args in macro args need to be replaced inline.
Ryan C. Gordon <icculus@icculus.org>
parents:
871
diff
changeset
|
505 |
assert(def->parameters == NULL); // args can't have args! |
869
5bd359b047f3
Added concat operator ("##") to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
867
diff
changeset
|
506 |
assert(def->paramcount == 0); // args can't have args! |
5bd359b047f3
Added concat operator ("##") to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
867
diff
changeset
|
507 |
if (strcmp(def->identifier, sym) == 0) |
5bd359b047f3
Added concat operator ("##") to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
867
diff
changeset
|
508 |
break; |
5bd359b047f3
Added concat operator ("##") to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
867
diff
changeset
|
509 |
} // while |
5bd359b047f3
Added concat operator ("##") to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
867
diff
changeset
|
510 |
|
5bd359b047f3
Added concat operator ("##") to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
867
diff
changeset
|
511 |
return def; |
5bd359b047f3
Added concat operator ("##") to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
867
diff
changeset
|
512 |
} // find_macro_arg |
5bd359b047f3
Added concat operator ("##") to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
867
diff
changeset
|
513 |
|
5bd359b047f3
Added concat operator ("##") to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
867
diff
changeset
|
514 |
|
646
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
515 |
static void put_all_defines(Context *ctx) |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
516 |
{ |
798
5dd67cc04cf9
Bunch of small tweaks to make this compile as C++ code without errors/warnings.
Ryan C. Gordon <icculus@icculus.org>
parents:
755
diff
changeset
|
517 |
size_t i; |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
518 |
for (i = 0; i < STATICARRAYLEN(ctx->define_hashtable); i++) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
519 |
{ |
676
29447167fc68
Renamed DefineHash to Define.
Ryan C. Gordon <icculus@icculus.org>
parents:
675
diff
changeset
|
520 |
Define *bucket = ctx->define_hashtable[i]; |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
521 |
ctx->define_hashtable[i] = NULL; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
522 |
while (bucket) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
523 |
{ |
676
29447167fc68
Renamed DefineHash to Define.
Ryan C. Gordon <icculus@icculus.org>
parents:
675
diff
changeset
|
524 |
Define *next = bucket->next; |
678
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
525 |
free_define(ctx, bucket); |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
526 |
bucket = next; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
527 |
} // while |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
528 |
} // for |
675 | 529 |
} // put_all_defines |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
530 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
531 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
532 |
static int push_source(Context *ctx, const char *fname, const char *source, |
656
ddb1a0b4c443
Let there be different close callbacks per IncludeState.
Ryan C. Gordon <icculus@icculus.org>
parents:
655
diff
changeset
|
533 |
unsigned int srclen, unsigned int linenum, |
894
1847405a16c6
Serious reworking of preprocessor macro replacement code.
Ryan C. Gordon <icculus@icculus.org>
parents:
882
diff
changeset
|
534 |
MOJOSHADER_includeClose close_callback) |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
535 |
{ |
646
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
536 |
IncludeState *state = get_include(ctx); |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
537 |
if (state == NULL) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
538 |
return 0; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
539 |
|
560
225d579ae929
Handle NULL file names.
Ryan C. Gordon <icculus@icculus.org>
parents:
559
diff
changeset
|
540 |
if (fname != NULL) |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
541 |
{ |
858
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
841
diff
changeset
|
542 |
state->filename = stringcache(ctx->filename_cache, fname); |
560
225d579ae929
Handle NULL file names.
Ryan C. Gordon <icculus@icculus.org>
parents:
559
diff
changeset
|
543 |
if (state->filename == NULL) |
225d579ae929
Handle NULL file names.
Ryan C. Gordon <icculus@icculus.org>
parents:
559
diff
changeset
|
544 |
{ |
646
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
545 |
put_include(ctx, state); |
560
225d579ae929
Handle NULL file names.
Ryan C. Gordon <icculus@icculus.org>
parents:
559
diff
changeset
|
546 |
return 0; |
225d579ae929
Handle NULL file names.
Ryan C. Gordon <icculus@icculus.org>
parents:
559
diff
changeset
|
547 |
} // if |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
548 |
} // if |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
549 |
|
656
ddb1a0b4c443
Let there be different close callbacks per IncludeState.
Ryan C. Gordon <icculus@icculus.org>
parents:
655
diff
changeset
|
550 |
state->close_callback = close_callback; |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
551 |
state->source_base = source; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
552 |
state->source = source; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
553 |
state->token = source; |
679
3892ebde7d99
Fixed lexer to only accept preprocessor directives at start of a line.
Ryan C. Gordon <icculus@icculus.org>
parents:
678
diff
changeset
|
554 |
state->tokenval = ((Token) '\n'); |
642
624ab8696f9b
Enormous amount of tapdancing to handle EOI better in the lexer.
Ryan C. Gordon <icculus@icculus.org>
parents:
641
diff
changeset
|
555 |
state->orig_length = srclen; |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
556 |
state->bytes_left = srclen; |
640
813a139a4a5a
push_source() now specifies a line number.
Ryan C. Gordon <icculus@icculus.org>
parents:
639
diff
changeset
|
557 |
state->line = linenum; |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
558 |
state->next = ctx->include_stack; |
690
4710df464f13
Moved asm comment processing into the lexer.
Ryan C. Gordon <icculus@icculus.org>
parents:
689
diff
changeset
|
559 |
state->asm_comments = ctx->asm_comments; |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
560 |
|
723
6d58b504cc6e
Print out current source/line number when debugging any parser/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents:
701
diff
changeset
|
561 |
print_debug_lexing_position(state); |
6d58b504cc6e
Print out current source/line number when debugging any parser/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents:
701
diff
changeset
|
562 |
|
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
563 |
ctx->include_stack = state; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
564 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
565 |
return 1; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
566 |
} // push_source |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
567 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
568 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
569 |
static void pop_source(Context *ctx) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
570 |
{ |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
571 |
IncludeState *state = ctx->include_stack; |
913
483458775ec5
Fixed crash when pushing an empty string on the include_stack.
Ryan C. Gordon <icculus@icculus.org>
parents:
910
diff
changeset
|
572 |
assert(state != NULL); // more pops than pushes! |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
573 |
if (state == NULL) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
574 |
return; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
575 |
|
656
ddb1a0b4c443
Let there be different close callbacks per IncludeState.
Ryan C. Gordon <icculus@icculus.org>
parents:
655
diff
changeset
|
576 |
if (state->close_callback) |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
577 |
{ |
656
ddb1a0b4c443
Let there be different close callbacks per IncludeState.
Ryan C. Gordon <icculus@icculus.org>
parents:
655
diff
changeset
|
578 |
state->close_callback(state->source_base, ctx->malloc, |
ddb1a0b4c443
Let there be different close callbacks per IncludeState.
Ryan C. Gordon <icculus@icculus.org>
parents:
655
diff
changeset
|
579 |
ctx->free, ctx->malloc_data); |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
580 |
} // if |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
581 |
|
612
72ccfe69eaf1
Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
611
diff
changeset
|
582 |
// state->filename is a pointer to the filename cache; don't free it here! |
72ccfe69eaf1
Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
611
diff
changeset
|
583 |
|
677
75e28bb57b2d
Fixed cleanup of Conditionals in pop_source().
Ryan C. Gordon <icculus@icculus.org>
parents:
676
diff
changeset
|
584 |
Conditional *cond = state->conditional_stack; |
75e28bb57b2d
Fixed cleanup of Conditionals in pop_source().
Ryan C. Gordon <icculus@icculus.org>
parents:
676
diff
changeset
|
585 |
while (cond) |
75e28bb57b2d
Fixed cleanup of Conditionals in pop_source().
Ryan C. Gordon <icculus@icculus.org>
parents:
676
diff
changeset
|
586 |
{ |
75e28bb57b2d
Fixed cleanup of Conditionals in pop_source().
Ryan C. Gordon <icculus@icculus.org>
parents:
676
diff
changeset
|
587 |
Conditional *next = cond->next; |
75e28bb57b2d
Fixed cleanup of Conditionals in pop_source().
Ryan C. Gordon <icculus@icculus.org>
parents:
676
diff
changeset
|
588 |
put_conditional(ctx, cond); |
75e28bb57b2d
Fixed cleanup of Conditionals in pop_source().
Ryan C. Gordon <icculus@icculus.org>
parents:
676
diff
changeset
|
589 |
cond = next; |
75e28bb57b2d
Fixed cleanup of Conditionals in pop_source().
Ryan C. Gordon <icculus@icculus.org>
parents:
676
diff
changeset
|
590 |
} // while |
620
1c4cf996004e
Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents:
618
diff
changeset
|
591 |
|
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
592 |
ctx->include_stack = state->next; |
723
6d58b504cc6e
Print out current source/line number when debugging any parser/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents:
701
diff
changeset
|
593 |
|
6d58b504cc6e
Print out current source/line number when debugging any parser/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents:
701
diff
changeset
|
594 |
print_debug_lexing_position(ctx->include_stack); |
6d58b504cc6e
Print out current source/line number when debugging any parser/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents:
701
diff
changeset
|
595 |
|
646
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
596 |
put_include(ctx, state); |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
597 |
} // pop_source |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
598 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
599 |
|
657
1268d95644f0
Fold predefined macros into a virtual #include at the start of input.
Ryan C. Gordon <icculus@icculus.org>
parents:
656
diff
changeset
|
600 |
static void close_define_include(const char *data, MOJOSHADER_malloc m, |
1268d95644f0
Fold predefined macros into a virtual #include at the start of input.
Ryan C. Gordon <icculus@icculus.org>
parents:
656
diff
changeset
|
601 |
MOJOSHADER_free f, void *d) |
1268d95644f0
Fold predefined macros into a virtual #include at the start of input.
Ryan C. Gordon <icculus@icculus.org>
parents:
656
diff
changeset
|
602 |
{ |
1268d95644f0
Fold predefined macros into a virtual #include at the start of input.
Ryan C. Gordon <icculus@icculus.org>
parents:
656
diff
changeset
|
603 |
f((void *) data, d); |
1268d95644f0
Fold predefined macros into a virtual #include at the start of input.
Ryan C. Gordon <icculus@icculus.org>
parents:
656
diff
changeset
|
604 |
} // close_define_include |
1268d95644f0
Fold predefined macros into a virtual #include at the start of input.
Ryan C. Gordon <icculus@icculus.org>
parents:
656
diff
changeset
|
605 |
|
1268d95644f0
Fold predefined macros into a virtual #include at the start of input.
Ryan C. Gordon <icculus@icculus.org>
parents:
656
diff
changeset
|
606 |
|
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
607 |
Preprocessor *preprocessor_start(const char *fname, const char *source, |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
608 |
unsigned int sourcelen, |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
609 |
MOJOSHADER_includeOpen open_callback, |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
610 |
MOJOSHADER_includeClose close_callback, |
658
22695d3d6b98
Should be an array of structs, not an array of pointers to structs.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
611 |
const MOJOSHADER_preprocessorDefine *defines, |
690
4710df464f13
Moved asm comment processing into the lexer.
Ryan C. Gordon <icculus@icculus.org>
parents:
689
diff
changeset
|
612 |
unsigned int define_count, int asm_comments, |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
613 |
MOJOSHADER_malloc m, MOJOSHADER_free f, void *d) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
614 |
{ |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
615 |
int okay = 1; |
798
5dd67cc04cf9
Bunch of small tweaks to make this compile as C++ code without errors/warnings.
Ryan C. Gordon <icculus@icculus.org>
parents:
755
diff
changeset
|
616 |
unsigned int i = 0; |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
617 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
618 |
// the preprocessor is internal-only, so we verify all these are != NULL. |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
619 |
assert(m != NULL); |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
620 |
assert(f != NULL); |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
621 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
622 |
Context *ctx = (Context *) m(sizeof (Context), d); |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
623 |
if (ctx == NULL) |
858
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
841
diff
changeset
|
624 |
return NULL; |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
625 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
626 |
memset(ctx, '\0', sizeof (Context)); |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
627 |
ctx->malloc = m; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
628 |
ctx->free = f; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
629 |
ctx->malloc_data = d; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
630 |
ctx->open_callback = open_callback; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
631 |
ctx->close_callback = close_callback; |
690
4710df464f13
Moved asm comment processing into the lexer.
Ryan C. Gordon <icculus@icculus.org>
parents:
689
diff
changeset
|
632 |
ctx->asm_comments = asm_comments; |
896
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
633 |
|
949
356f04114580
Hook up preprocessor string cache to MallocBridge().
Ryan C. Gordon <icculus@icculus.org>
parents:
945
diff
changeset
|
634 |
ctx->filename_cache = stringcache_create(MallocBridge, FreeBridge, ctx); |
896
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
635 |
okay = ((okay) && (ctx->filename_cache != NULL)); |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
636 |
|
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
637 |
ctx->file_macro = get_define(ctx); |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
638 |
okay = ((okay) && (ctx->file_macro != NULL)); |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
639 |
if ((okay) && (ctx->file_macro)) |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
640 |
okay = ((ctx->file_macro->identifier = StrDup(ctx, "__FILE__")) != 0); |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
641 |
|
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
642 |
ctx->line_macro = get_define(ctx); |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
643 |
okay = ((okay) && (ctx->line_macro != NULL)); |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
644 |
if ((okay) && (ctx->line_macro)) |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
645 |
okay = ((ctx->line_macro->identifier = StrDup(ctx, "__LINE__")) != 0); |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
646 |
|
657
1268d95644f0
Fold predefined macros into a virtual #include at the start of input.
Ryan C. Gordon <icculus@icculus.org>
parents:
656
diff
changeset
|
647 |
// let the usual preprocessor parser sort these out. |
1268d95644f0
Fold predefined macros into a virtual #include at the start of input.
Ryan C. Gordon <icculus@icculus.org>
parents:
656
diff
changeset
|
648 |
char *define_include = NULL; |
1268d95644f0
Fold predefined macros into a virtual #include at the start of input.
Ryan C. Gordon <icculus@icculus.org>
parents:
656
diff
changeset
|
649 |
unsigned int define_include_len = 0; |
896
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
650 |
if ((okay) && (define_count > 0)) |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
651 |
{ |
951
6ef73d06ecb0
Preprocessor: Moved pushing of predefined macros over to Buffer.
Ryan C. Gordon <icculus@icculus.org>
parents:
950
diff
changeset
|
652 |
Buffer *predefbuf = buffer_create(256, MallocBridge, FreeBridge, ctx); |
6ef73d06ecb0
Preprocessor: Moved pushing of predefined macros over to Buffer.
Ryan C. Gordon <icculus@icculus.org>
parents:
950
diff
changeset
|
653 |
okay = okay && (predefbuf != NULL); |
6ef73d06ecb0
Preprocessor: Moved pushing of predefined macros over to Buffer.
Ryan C. Gordon <icculus@icculus.org>
parents:
950
diff
changeset
|
654 |
for (i = 0; okay && (i < define_count); i++) |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
655 |
{ |
951
6ef73d06ecb0
Preprocessor: Moved pushing of predefined macros over to Buffer.
Ryan C. Gordon <icculus@icculus.org>
parents:
950
diff
changeset
|
656 |
okay = okay && buffer_append_fmt(predefbuf, "#define %s %s\n", |
6ef73d06ecb0
Preprocessor: Moved pushing of predefined macros over to Buffer.
Ryan C. Gordon <icculus@icculus.org>
parents:
950
diff
changeset
|
657 |
defines[i].identifier, defines[i].definition); |
657
1268d95644f0
Fold predefined macros into a virtual #include at the start of input.
Ryan C. Gordon <icculus@icculus.org>
parents:
656
diff
changeset
|
658 |
} // for |
1268d95644f0
Fold predefined macros into a virtual #include at the start of input.
Ryan C. Gordon <icculus@icculus.org>
parents:
656
diff
changeset
|
659 |
|
951
6ef73d06ecb0
Preprocessor: Moved pushing of predefined macros over to Buffer.
Ryan C. Gordon <icculus@icculus.org>
parents:
950
diff
changeset
|
660 |
define_include_len = buffer_size(predefbuf); |
6ef73d06ecb0
Preprocessor: Moved pushing of predefined macros over to Buffer.
Ryan C. Gordon <icculus@icculus.org>
parents:
950
diff
changeset
|
661 |
if (define_include_len > 0) |
657
1268d95644f0
Fold predefined macros into a virtual #include at the start of input.
Ryan C. Gordon <icculus@icculus.org>
parents:
656
diff
changeset
|
662 |
{ |
951
6ef73d06ecb0
Preprocessor: Moved pushing of predefined macros over to Buffer.
Ryan C. Gordon <icculus@icculus.org>
parents:
950
diff
changeset
|
663 |
define_include = buffer_flatten(predefbuf); |
6ef73d06ecb0
Preprocessor: Moved pushing of predefined macros over to Buffer.
Ryan C. Gordon <icculus@icculus.org>
parents:
950
diff
changeset
|
664 |
okay = okay && (define_include != NULL); |
6ef73d06ecb0
Preprocessor: Moved pushing of predefined macros over to Buffer.
Ryan C. Gordon <icculus@icculus.org>
parents:
950
diff
changeset
|
665 |
} // if |
6ef73d06ecb0
Preprocessor: Moved pushing of predefined macros over to Buffer.
Ryan C. Gordon <icculus@icculus.org>
parents:
950
diff
changeset
|
666 |
buffer_destroy(predefbuf); |
657
1268d95644f0
Fold predefined macros into a virtual #include at the start of input.
Ryan C. Gordon <icculus@icculus.org>
parents:
656
diff
changeset
|
667 |
} // if |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
668 |
|
894
1847405a16c6
Serious reworking of preprocessor macro replacement code.
Ryan C. Gordon <icculus@icculus.org>
parents:
882
diff
changeset
|
669 |
if ((okay) && (!push_source(ctx,fname,source,sourcelen,1,NULL))) |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
670 |
okay = 0; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
671 |
|
950
dcbf30c07fc9
Preprocessor: Don't push predefined macro null terminator to the include stack.
Ryan C. Gordon <icculus@icculus.org>
parents:
949
diff
changeset
|
672 |
if ((okay) && (define_include_len > 0)) |
657
1268d95644f0
Fold predefined macros into a virtual #include at the start of input.
Ryan C. Gordon <icculus@icculus.org>
parents:
656
diff
changeset
|
673 |
{ |
950
dcbf30c07fc9
Preprocessor: Don't push predefined macro null terminator to the include stack.
Ryan C. Gordon <icculus@icculus.org>
parents:
949
diff
changeset
|
674 |
assert(define_include != NULL); |
657
1268d95644f0
Fold predefined macros into a virtual #include at the start of input.
Ryan C. Gordon <icculus@icculus.org>
parents:
656
diff
changeset
|
675 |
okay = push_source(ctx, "<predefined macros>", define_include, |
951
6ef73d06ecb0
Preprocessor: Moved pushing of predefined macros over to Buffer.
Ryan C. Gordon <icculus@icculus.org>
parents:
950
diff
changeset
|
676 |
define_include_len, 1, close_define_include); |
657
1268d95644f0
Fold predefined macros into a virtual #include at the start of input.
Ryan C. Gordon <icculus@icculus.org>
parents:
656
diff
changeset
|
677 |
} // if |
1268d95644f0
Fold predefined macros into a virtual #include at the start of input.
Ryan C. Gordon <icculus@icculus.org>
parents:
656
diff
changeset
|
678 |
|
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
679 |
if (!okay) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
680 |
{ |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
681 |
preprocessor_end((Preprocessor *) ctx); |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
682 |
return NULL; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
683 |
} // if |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
684 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
685 |
return (Preprocessor *) ctx; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
686 |
} // preprocessor_start |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
687 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
688 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
689 |
void preprocessor_end(Preprocessor *_ctx) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
690 |
{ |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
691 |
Context *ctx = (Context *) _ctx; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
692 |
if (ctx == NULL) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
693 |
return; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
694 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
695 |
while (ctx->include_stack != NULL) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
696 |
pop_source(ctx); |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
697 |
|
646
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
698 |
put_all_defines(ctx); |
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
699 |
|
858
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
841
diff
changeset
|
700 |
if (ctx->filename_cache != NULL) |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
841
diff
changeset
|
701 |
stringcache_destroy(ctx->filename_cache); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
841
diff
changeset
|
702 |
|
896
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
703 |
free_define(ctx, ctx->file_macro); |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
704 |
free_define(ctx, ctx->line_macro); |
646
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
705 |
free_define_pool(ctx); |
620
1c4cf996004e
Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents:
618
diff
changeset
|
706 |
free_conditional_pool(ctx); |
646
60911555980a
Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents:
645
diff
changeset
|
707 |
free_include_pool(ctx); |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
708 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
709 |
Free(ctx, ctx); |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
710 |
} // preprocessor_end |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
711 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
712 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
713 |
int preprocessor_outofmemory(Preprocessor *_ctx) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
714 |
{ |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
715 |
Context *ctx = (Context *) _ctx; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
716 |
return ctx->out_of_memory; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
717 |
} // preprocessor_outofmemory |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
718 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
719 |
|
652
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
720 |
static inline void pushback(IncludeState *state) |
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
721 |
{ |
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
722 |
#if DEBUG_PREPROCESSOR |
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
723 |
printf("PREPROCESSOR PUSHBACK\n"); |
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
724 |
#endif |
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
725 |
assert(!state->pushedback); |
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
726 |
state->pushedback = 1; |
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
727 |
} // pushback |
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
728 |
|
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
729 |
|
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
730 |
static Token lexer(IncludeState *state) |
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
731 |
{ |
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
732 |
if (!state->pushedback) |
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
733 |
return preprocessor_lexer(state); |
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
734 |
state->pushedback = 0; |
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
735 |
return state->tokenval; |
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
736 |
} // lexer |
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
737 |
|
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
738 |
|
638 | 739 |
// !!! FIXME: parsing fails on preprocessor directives should skip rest of line. |
614
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
740 |
static int require_newline(IncludeState *state) |
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
741 |
{ |
652
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
742 |
const Token token = lexer(state); |
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
743 |
pushback(state); // rewind no matter what. |
654 | 744 |
return ( (token == TOKEN_INCOMPLETE_COMMENT) || // call it an eol. |
652
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
745 |
(token == ((Token) '\n')) || (token == TOKEN_EOI) ); |
614
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
746 |
} // require_newline |
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
747 |
|
940
bc2a5efade5e
Added a bunch of FIXMEs to accurately portray current technical debt.
Ryan C. Gordon <icculus@icculus.org>
parents:
939
diff
changeset
|
748 |
// !!! FIXME: didn't we implement this by hand elsewhere? |
682
ad75eb06ddce
First work on #if directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
681
diff
changeset
|
749 |
static int token_to_int(IncludeState *state) |
ad75eb06ddce
First work on #if directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
681
diff
changeset
|
750 |
{ |
ad75eb06ddce
First work on #if directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
681
diff
changeset
|
751 |
assert(state->tokenval == TOKEN_INT_LITERAL); |
ad75eb06ddce
First work on #if directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
681
diff
changeset
|
752 |
char *buf = (char *) alloca(state->tokenlen+1); |
ad75eb06ddce
First work on #if directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
681
diff
changeset
|
753 |
memcpy(buf, state->token, state->tokenlen); |
ad75eb06ddce
First work on #if directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
681
diff
changeset
|
754 |
buf[state->tokenlen] = '\0'; |
ad75eb06ddce
First work on #if directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
681
diff
changeset
|
755 |
return atoi(buf); |
ad75eb06ddce
First work on #if directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
681
diff
changeset
|
756 |
} // token_to_int |
ad75eb06ddce
First work on #if directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
681
diff
changeset
|
757 |
|
ad75eb06ddce
First work on #if directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
681
diff
changeset
|
758 |
|
611
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
759 |
static void handle_pp_include(Context *ctx) |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
760 |
{ |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
761 |
IncludeState *state = ctx->include_stack; |
652
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
762 |
Token token = lexer(state); |
611
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
763 |
MOJOSHADER_includeType incltype; |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
764 |
char *filename = NULL; |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
765 |
int bogus = 0; |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
766 |
|
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
767 |
if (token == TOKEN_STRING_LITERAL) |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
768 |
incltype = MOJOSHADER_INCLUDETYPE_LOCAL; |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
769 |
else if (token == ((Token) '<')) |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
770 |
{ |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
771 |
incltype = MOJOSHADER_INCLUDETYPE_SYSTEM; |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
772 |
// can't use lexer, since every byte between the < > pair is |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
773 |
// considered part of the filename. :/ |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
774 |
while (!bogus) |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
775 |
{ |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
776 |
if ( !(bogus = (state->bytes_left == 0)) ) |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
777 |
{ |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
778 |
const char ch = *state->source; |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
779 |
if ( !(bogus = ((ch == '\r') || (ch == '\n'))) ) |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
780 |
{ |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
781 |
state->source++; |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
782 |
state->bytes_left--; |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
783 |
|
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
784 |
if (ch == '>') |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
785 |
break; |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
786 |
} // if |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
787 |
} // if |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
788 |
} // while |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
789 |
} // else if |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
790 |
else |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
791 |
{ |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
792 |
bogus = 1; |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
793 |
} // else |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
794 |
|
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
795 |
if (!bogus) |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
796 |
{ |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
797 |
state->token++; // skip '<' or '\"'... |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
798 |
const unsigned int len = ((unsigned int) (state->source-state->token)); |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
799 |
filename = (char *) alloca(len); |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
800 |
memcpy(filename, state->token, len-1); |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
801 |
filename[len-1] = '\0'; |
614
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
802 |
bogus = !require_newline(state); |
611
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
803 |
} // if |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
804 |
|
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
805 |
if (bogus) |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
806 |
{ |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
807 |
fail(ctx, "Invalid #include directive"); |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
808 |
return; |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
809 |
} // else |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
810 |
|
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
811 |
const char *newdata = NULL; |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
812 |
unsigned int newbytes = 0; |
813
a8ec84003cd2
Make #include callbacks optional.
Ryan C. Gordon <icculus@icculus.org>
parents:
798
diff
changeset
|
813 |
if ((ctx->open_callback == NULL) || (ctx->close_callback == NULL)) |
a8ec84003cd2
Make #include callbacks optional.
Ryan C. Gordon <icculus@icculus.org>
parents:
798
diff
changeset
|
814 |
{ |
a8ec84003cd2
Make #include callbacks optional.
Ryan C. Gordon <icculus@icculus.org>
parents:
798
diff
changeset
|
815 |
fail(ctx, "Saw #include, but no include callbacks defined"); |
a8ec84003cd2
Make #include callbacks optional.
Ryan C. Gordon <icculus@icculus.org>
parents:
798
diff
changeset
|
816 |
return; |
a8ec84003cd2
Make #include callbacks optional.
Ryan C. Gordon <icculus@icculus.org>
parents:
798
diff
changeset
|
817 |
} // if |
a8ec84003cd2
Make #include callbacks optional.
Ryan C. Gordon <icculus@icculus.org>
parents:
798
diff
changeset
|
818 |
|
611
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
819 |
if (!ctx->open_callback(incltype, filename, state->source_base, |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
820 |
&newdata, &newbytes, ctx->malloc, |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
821 |
ctx->free, ctx->malloc_data)) |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
822 |
{ |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
823 |
fail(ctx, "Include callback failed"); // !!! FIXME: better error |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
824 |
return; |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
825 |
} // if |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
826 |
|
678
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
827 |
MOJOSHADER_includeClose callback = ctx->close_callback; |
894
1847405a16c6
Serious reworking of preprocessor macro replacement code.
Ryan C. Gordon <icculus@icculus.org>
parents:
882
diff
changeset
|
828 |
if (!push_source(ctx, filename, newdata, newbytes, 1, callback)) |
611
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
829 |
{ |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
830 |
assert(ctx->out_of_memory); |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
831 |
ctx->close_callback(newdata, ctx->malloc, ctx->free, ctx->malloc_data); |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
832 |
} // if |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
833 |
} // handle_pp_include |
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
834 |
|
8c2ee1a97ee1
Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
607
diff
changeset
|
835 |
|
614
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
836 |
static void handle_pp_line(Context *ctx) |
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
837 |
{ |
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
838 |
IncludeState *state = ctx->include_stack; |
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
839 |
char *filename = NULL; |
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
840 |
int linenum = 0; |
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
841 |
int bogus = 0; |
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
842 |
|
652
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
843 |
if (lexer(state) != TOKEN_INT_LITERAL) |
614
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
844 |
bogus = 1; |
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
845 |
else |
682
ad75eb06ddce
First work on #if directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
681
diff
changeset
|
846 |
linenum = token_to_int(state); |
614
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
847 |
|
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
848 |
if (!bogus) |
871
e72ec737ed74
filename in #line is optional
Aras Pranckevicius <aras@unity3d.com>
parents:
869
diff
changeset
|
849 |
{ |
e72ec737ed74
filename in #line is optional
Aras Pranckevicius <aras@unity3d.com>
parents:
869
diff
changeset
|
850 |
Token t = lexer(state); |
e72ec737ed74
filename in #line is optional
Aras Pranckevicius <aras@unity3d.com>
parents:
869
diff
changeset
|
851 |
if (t == ((Token) '\n')) |
e72ec737ed74
filename in #line is optional
Aras Pranckevicius <aras@unity3d.com>
parents:
869
diff
changeset
|
852 |
{ |
e72ec737ed74
filename in #line is optional
Aras Pranckevicius <aras@unity3d.com>
parents:
869
diff
changeset
|
853 |
state->line = linenum; |
e72ec737ed74
filename in #line is optional
Aras Pranckevicius <aras@unity3d.com>
parents:
869
diff
changeset
|
854 |
return; |
e72ec737ed74
filename in #line is optional
Aras Pranckevicius <aras@unity3d.com>
parents:
869
diff
changeset
|
855 |
} |
e72ec737ed74
filename in #line is optional
Aras Pranckevicius <aras@unity3d.com>
parents:
869
diff
changeset
|
856 |
bogus = (t != TOKEN_STRING_LITERAL); |
e72ec737ed74
filename in #line is optional
Aras Pranckevicius <aras@unity3d.com>
parents:
869
diff
changeset
|
857 |
} |
614
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
858 |
|
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
859 |
if (!bogus) |
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
860 |
{ |
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
861 |
state->token++; // skip '\"'... |
648
243e35876142
Have lexer calculate token size now.
Ryan C. Gordon <icculus@icculus.org>
parents:
647
diff
changeset
|
862 |
filename = (char *) alloca(state->tokenlen); |
243e35876142
Have lexer calculate token size now.
Ryan C. Gordon <icculus@icculus.org>
parents:
647
diff
changeset
|
863 |
memcpy(filename, state->token, state->tokenlen-1); |
243e35876142
Have lexer calculate token size now.
Ryan C. Gordon <icculus@icculus.org>
parents:
647
diff
changeset
|
864 |
filename[state->tokenlen-1] = '\0'; |
614
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
865 |
bogus = !require_newline(state); |
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
866 |
} // if |
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
867 |
|
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
868 |
if (bogus) |
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
869 |
{ |
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
870 |
fail(ctx, "Invalid #line directive"); |
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
871 |
return; |
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
872 |
} // if |
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
873 |
|
858
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
841
diff
changeset
|
874 |
const char *cached = stringcache(ctx->filename_cache, filename); |
949
356f04114580
Hook up preprocessor string cache to MallocBridge().
Ryan C. Gordon <icculus@icculus.org>
parents:
945
diff
changeset
|
875 |
state->filename = cached; // may be NULL if stringcache() failed. |
614
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
876 |
state->line = linenum; |
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
877 |
} // handle_pp_line |
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
878 |
|
0f2f298003ae
Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
612
diff
changeset
|
879 |
|
606
63e7a66ac320
Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
605
diff
changeset
|
880 |
static void handle_pp_error(Context *ctx) |
63e7a66ac320
Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
605
diff
changeset
|
881 |
{ |
63e7a66ac320
Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
605
diff
changeset
|
882 |
IncludeState *state = ctx->include_stack; |
635
be3b428802a1
Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents:
634
diff
changeset
|
883 |
char *ptr = ctx->failstr; |
be3b428802a1
Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents:
634
diff
changeset
|
884 |
int avail = sizeof (ctx->failstr) - 1; |
be3b428802a1
Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents:
634
diff
changeset
|
885 |
int cpy = 0; |
606
63e7a66ac320
Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
605
diff
changeset
|
886 |
int done = 0; |
63e7a66ac320
Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
605
diff
changeset
|
887 |
|
635
be3b428802a1
Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents:
634
diff
changeset
|
888 |
const char *prefix = "#error"; |
be3b428802a1
Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents:
634
diff
changeset
|
889 |
const size_t prefixlen = strlen(prefix); |
be3b428802a1
Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents:
634
diff
changeset
|
890 |
strcpy(ctx->failstr, prefix); |
be3b428802a1
Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents:
634
diff
changeset
|
891 |
avail -= prefixlen; |
be3b428802a1
Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents:
634
diff
changeset
|
892 |
ptr += prefixlen; |
be3b428802a1
Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents:
634
diff
changeset
|
893 |
|
be3b428802a1
Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents:
634
diff
changeset
|
894 |
state->report_whitespace = 1; |
606
63e7a66ac320
Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
605
diff
changeset
|
895 |
while (!done) |
63e7a66ac320
Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
605
diff
changeset
|
896 |
{ |
652
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
897 |
const Token token = lexer(state); |
606
63e7a66ac320
Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
605
diff
changeset
|
898 |
switch (token) |
63e7a66ac320
Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
605
diff
changeset
|
899 |
{ |
615
5467d19b4d7d
Fix line numbers in #error preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents:
614
diff
changeset
|
900 |
case ((Token) '\n'): |
5467d19b4d7d
Fix line numbers in #error preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents:
614
diff
changeset
|
901 |
state->line--; // make sure error is on the right line. |
5467d19b4d7d
Fix line numbers in #error preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents:
614
diff
changeset
|
902 |
// fall through! |
606
63e7a66ac320
Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
605
diff
changeset
|
903 |
case TOKEN_INCOMPLETE_COMMENT: |
63e7a66ac320
Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
605
diff
changeset
|
904 |
case TOKEN_EOI: |
652
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
905 |
pushback(state); // move back so we catch this later. |
606
63e7a66ac320
Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
605
diff
changeset
|
906 |
done = 1; |
63e7a66ac320
Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
605
diff
changeset
|
907 |
break; |
63e7a66ac320
Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
605
diff
changeset
|
908 |
|
894
1847405a16c6
Serious reworking of preprocessor macro replacement code.
Ryan C. Gordon <icculus@icculus.org>
parents:
882
diff
changeset
|
909 |
case ((Token) ' '): |
635
be3b428802a1
Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents:
634
diff
changeset
|
910 |
if (!avail) |
be3b428802a1
Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents:
634
diff
changeset
|
911 |
break; |
be3b428802a1
Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents:
634
diff
changeset
|
912 |
*(ptr++) = ' '; |
be3b428802a1
Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents:
634
diff
changeset
|
913 |
avail--; |
be3b428802a1
Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents:
634
diff
changeset
|
914 |
break; |
be3b428802a1
Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents:
634
diff
changeset
|
915 |
|
606
63e7a66ac320
Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
605
diff
changeset
|
916 |
default: |
650 | 917 |
cpy = Min(avail, (int) state->tokenlen); |
635
be3b428802a1
Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents:
634
diff
changeset
|
918 |
if (cpy) |
be3b428802a1
Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents:
634
diff
changeset
|
919 |
memcpy(ptr, state->token, cpy); |
be3b428802a1
Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents:
634
diff
changeset
|
920 |
ptr += cpy; |
be3b428802a1
Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents:
634
diff
changeset
|
921 |
avail -= cpy; |
606
63e7a66ac320
Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
605
diff
changeset
|
922 |
break; |
63e7a66ac320
Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
605
diff
changeset
|
923 |
} // switch |
63e7a66ac320
Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
605
diff
changeset
|
924 |
} // while |
63e7a66ac320
Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
605
diff
changeset
|
925 |
|
635
be3b428802a1
Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents:
634
diff
changeset
|
926 |
*ptr = '\0'; |
be3b428802a1
Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents:
634
diff
changeset
|
927 |
state->report_whitespace = 0; |
606
63e7a66ac320
Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
605
diff
changeset
|
928 |
ctx->isfail = 1; |
63e7a66ac320
Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
605
diff
changeset
|
929 |
} // handle_pp_error |
63e7a66ac320
Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
605
diff
changeset
|
930 |
|
63e7a66ac320
Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
605
diff
changeset
|
931 |
|
639
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
932 |
static void handle_pp_define(Context *ctx) |
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
933 |
{ |
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
934 |
IncludeState *state = ctx->include_stack; |
798
5dd67cc04cf9
Bunch of small tweaks to make this compile as C++ code without errors/warnings.
Ryan C. Gordon <icculus@icculus.org>
parents:
755
diff
changeset
|
935 |
int done = 0; |
639
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
936 |
|
652
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
937 |
if (lexer(state) != TOKEN_IDENTIFIER) |
639
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
938 |
{ |
754 | 939 |
fail(ctx, "Macro names must be identifiers"); |
639
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
940 |
return; |
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
941 |
} // if |
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
942 |
|
678
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
943 |
char *definition = NULL; |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
944 |
char *sym = (char *) Malloc(ctx, state->tokenlen+1); |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
945 |
if (sym == NULL) |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
946 |
return; |
648
243e35876142
Have lexer calculate token size now.
Ryan C. Gordon <icculus@icculus.org>
parents:
647
diff
changeset
|
947 |
memcpy(sym, state->token, state->tokenlen); |
243e35876142
Have lexer calculate token size now.
Ryan C. Gordon <icculus@icculus.org>
parents:
647
diff
changeset
|
948 |
sym[state->tokenlen] = '\0'; |
639
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
949 |
|
699
c2f50b391d96
Support for "#if defined" in preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
698
diff
changeset
|
950 |
if (strcmp(sym, "defined") == 0) |
c2f50b391d96
Support for "#if defined" in preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
698
diff
changeset
|
951 |
{ |
c2f50b391d96
Support for "#if defined" in preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
698
diff
changeset
|
952 |
Free(ctx, sym); |
c2f50b391d96
Support for "#if defined" in preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
698
diff
changeset
|
953 |
fail(ctx, "'defined' cannot be used as a macro name"); |
c2f50b391d96
Support for "#if defined" in preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
698
diff
changeset
|
954 |
return; |
c2f50b391d96
Support for "#if defined" in preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
698
diff
changeset
|
955 |
} // if |
c2f50b391d96
Support for "#if defined" in preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
698
diff
changeset
|
956 |
|
875
dc28354d576e
Added support for __FILE__ and __LINE__ to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
873
diff
changeset
|
957 |
// Don't treat these symbols as special anymore if they get (re)#defined. |
dc28354d576e
Added support for __FILE__ and __LINE__ to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
873
diff
changeset
|
958 |
if (strcmp(sym, "__FILE__") == 0) |
dc28354d576e
Added support for __FILE__ and __LINE__ to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
873
diff
changeset
|
959 |
{ |
896
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
960 |
if (ctx->file_macro) |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
961 |
{ |
875
dc28354d576e
Added support for __FILE__ and __LINE__ to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
873
diff
changeset
|
962 |
failf(ctx, "'%s' already defined", sym); // !!! FIXME: warning? |
896
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
963 |
free_define(ctx, ctx->file_macro); |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
964 |
ctx->file_macro = NULL; |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
965 |
} // if |
875
dc28354d576e
Added support for __FILE__ and __LINE__ to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
873
diff
changeset
|
966 |
} // if |
dc28354d576e
Added support for __FILE__ and __LINE__ to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
873
diff
changeset
|
967 |
else if (strcmp(sym, "__LINE__") == 0) |
dc28354d576e
Added support for __FILE__ and __LINE__ to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
873
diff
changeset
|
968 |
{ |
896
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
969 |
if (ctx->line_macro) |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
970 |
{ |
875
dc28354d576e
Added support for __FILE__ and __LINE__ to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
873
diff
changeset
|
971 |
failf(ctx, "'%s' already defined", sym); // !!! FIXME: warning? |
896
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
972 |
free_define(ctx, ctx->line_macro); |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
973 |
ctx->line_macro = NULL; |
7a7989b18117
Handle __FILE__ and __LINE__ better.
Ryan C. Gordon <icculus@icculus.org>
parents:
895
diff
changeset
|
974 |
} // if |
875
dc28354d576e
Added support for __FILE__ and __LINE__ to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
873
diff
changeset
|
975 |
} // else if |
dc28354d576e
Added support for __FILE__ and __LINE__ to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
873
diff
changeset
|
976 |
|
678
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
977 |
// #define a(b) is different than #define a (b) :( |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
978 |
state->report_whitespace = 1; |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
979 |
lexer(state); |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
980 |
state->report_whitespace = 0; |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
981 |
|
755
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
982 |
int params = 0; |
678
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
983 |
char **idents = NULL; |
798
5dd67cc04cf9
Bunch of small tweaks to make this compile as C++ code without errors/warnings.
Ryan C. Gordon <icculus@icculus.org>
parents:
755
diff
changeset
|
984 |
static const char space = ' '; |
678
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
985 |
|
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
986 |
if (state->tokenval == ((Token) ' ')) |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
987 |
lexer(state); // skip it. |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
988 |
else if (state->tokenval == ((Token) '(')) |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
989 |
{ |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
990 |
IncludeState saved; |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
991 |
memcpy(&saved, state, sizeof (IncludeState)); |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
992 |
while (1) |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
993 |
{ |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
994 |
if (lexer(state) != TOKEN_IDENTIFIER) |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
995 |
break; |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
996 |
params++; |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
997 |
if (lexer(state) != ((Token) ',')) |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
998 |
break; |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
999 |
} // while |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
1000 |
|
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
1001 |
if (state->tokenval != ((Token) ')')) |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
1002 |
{ |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
1003 |
fail(ctx, "syntax error in macro parameter list"); |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
1004 |
goto handle_pp_define_failed; |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
1005 |
} // if |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
1006 |
|
755
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1007 |
if (params == 0) // special case for void args: "#define a() b" |
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1008 |
params = -1; |
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1009 |
else |
748
818ae6582f24
Fixed assertion failure when defining a void-argument macro: "#define a() b"
Ryan C. Gordon <icculus@icculus.org>
parents:
746
diff
changeset
|
1010 |
{ |
818ae6582f24
Fixed assertion failure when defining a void-argument macro: "#define a() b"
Ryan C. Gordon <icculus@icculus.org>
parents:
746
diff
changeset
|
1011 |
idents = (char **) Malloc(ctx, sizeof (char *) * params); |
818ae6582f24
Fixed assertion failure when defining a void-argument macro: "#define a() b"
Ryan C. Gordon <icculus@icculus.org>
parents:
746
diff
changeset
|
1012 |
if (idents == NULL) |
818ae6582f24
Fixed assertion failure when defining a void-argument macro: "#define a() b"
Ryan C. Gordon <icculus@icculus.org>
parents:
746
diff
changeset
|
1013 |
goto handle_pp_define_failed; |
678
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
1014 |
|
755
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1015 |
// roll all the way back, do it again. |
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1016 |
memcpy(state, &saved, sizeof (IncludeState)); |
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1017 |
memset(idents, '\0', sizeof (char *) * params); |
678
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
1018 |
|
755
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1019 |
int i; |
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1020 |
for (i = 0; i < params; i++) |
748
818ae6582f24
Fixed assertion failure when defining a void-argument macro: "#define a() b"
Ryan C. Gordon <icculus@icculus.org>
parents:
746
diff
changeset
|
1021 |
{ |
818ae6582f24
Fixed assertion failure when defining a void-argument macro: "#define a() b"
Ryan C. Gordon <icculus@icculus.org>
parents:
746
diff
changeset
|
1022 |
lexer(state); |
755
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1023 |
assert(state->tokenval == TOKEN_IDENTIFIER); |
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1024 |
|
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1025 |
char *dst = (char *) Malloc(ctx, state->tokenlen+1); |
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1026 |
if (dst == NULL) |
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1027 |
break; |
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1028 |
|
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1029 |
memcpy(dst, state->token, state->tokenlen); |
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1030 |
dst[state->tokenlen] = '\0'; |
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1031 |
idents[i] = dst; |
678
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
1032 |
|
755
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1033 |
if (i < (params-1)) |
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1034 |
{ |
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1035 |
lexer(state); |
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1036 |
assert(state->tokenval == ((Token) ',')); |
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1037 |
} // if |
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1038 |
} // for |
678
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
1039 |
|
755
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1040 |
if (i != params) |
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1041 |
{ |
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1042 |
assert(ctx->out_of_memory); |
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1043 |
goto handle_pp_define_failed; |
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1044 |
} // if |
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1045 |
|
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1046 |
lexer(state); |
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1047 |
assert(state->tokenval == ((Token) ')')); |
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1048 |
} // else |
6a7aa96c51c7
Reworked preprocessor macro argument parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
754
diff
changeset
|
1049 |
|
678
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
1050 |
lexer(state); |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
1051 |
} // else if |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
1052 |
|
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
1053 |
pushback(state); |
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
1054 |
|
944
9f9fa9650772
Paying off more technical debt: unified growable buffers into one place.
Ryan C. Gordon <icculus@icculus.org>
parents:
941
diff
changeset
|
1055 |
Buffer *buffer = buffer_create(128, MallocBridge, FreeBridge, ctx); |
639
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
1056 |
|
678
9fa2eb7d7871
Preprocessor now supports macros with parameters.
Ryan C. Gordon <icculus@icculus.org>
parents:
677
diff
changeset
|
1057 |
state->report_whitespace = 1; |
639
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
1058 |
while ((!done) && (!ctx->out_of_memory)) |
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
1059 |
{ |
652
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
1060 |
const Token token = lexer(state); |
639
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
1061 |
switch (token) |
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
1062 |
{ |
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
1063 |
case TOKEN_INCOMPLETE_COMMENT: |
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
1064 |
case TOKEN_EOI: |
652
3333a0774ccc
Added basic pushback facility to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
1065 |
pushback(state); // move back so we catch this later. |
864
aa3bec973a21
Check for "##" at the start and end of macro definitions.
Ryan C. Gordon <icculus@icculus.org>
parents:
861
diff
changeset
|
1066 |
done = 1; |
aa3bec973a21
Check for "##" at the start and end of macro definitions.
Ryan C. Gordon <icculus@icculus.org>
parents:
861
diff
changeset
|
1067 |
break; |
aa3bec973a21
Check for "##" at the start and end of macro definitions.
Ryan C. Gordon <icculus@icculus.org>
parents:
861
diff
changeset
|
1068 |
|
639
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
1069 |
case ((Token) '\n'): |
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
1070 |
done = 1; |
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
1071 |
break; |
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
1072 |
|
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
1073 |
case ((Token) ' '): // may not actually point to ' '. |
944
9f9fa9650772
Paying off more technical debt: unified growable buffers into one place.
Ryan C. Gordon <icculus@icculus.org>
parents:
941
diff
changeset
|
1074 |
assert(buffer_size(buffer) > 0); |
9f9fa9650772
Paying off more technical debt: unified growable buffers into one place.
Ryan C. Gordon <icculus@icculus.org>
parents:
941
diff
changeset
|
1075 |
buffer_append(buffer, &space, 1); |
639
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
1076 |
break; |
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
1077 |
|
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
1078 |
default: |
944
9f9fa9650772
Paying off more technical debt: unified growable buffers into one place.
Ryan C. Gordon <icculus@icculus.org>
parents:
941
diff
changeset
|
1079 |
buffer_append(buffer, state->token, state->tokenlen); |
639
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
1080 |
break; |
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
1081 |
} // switch |
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
1082 |
} // while |
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
1083 |
state->report_whitespace = 0; |
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
1084 |
|
944
9f9fa9650772
Paying off more technical debt: unified growable buffers into one place.
Ryan C. Gordon <icculus@icculus.org>
parents:
941
diff
changeset
|
1085 |
size_t buflen = buffer_size(buffer) + 1; |
639
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
1086 |
if (!ctx->out_of_memory) |
944
9f9fa9650772
Paying off more technical debt: unified growable buffers into one place.
Ryan C. Gordon <icculus@icculus.org>
parents:
941
diff
changeset
|
1087 |
definition = buffer_flatten(buffer); |
639
1b549be9ab59
Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents:
638
diff
changeset
|
1088 |
|
944
9f9fa9650772
Paying off more technical debt: unified growable buffers into one place.
Ryan C. Gordon <icculus@icculus.org& |