mojoshader_preprocessor.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 18 Feb 2009 00:35:42 -0500
changeset 647 a09899ba5e8c
parent 646 60911555980a
child 648 243e35876142
permissions -rw-r--r--
Added some FIXMEs.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
e6bd53dd1f91 Allow debug output directly from the lexer, before preprocessor eats.
Ryan C. Gordon <icculus@icculus.org>
parents: 629
diff changeset
    21
static Token debug_preprocessor_internal_lexer(IncludeState *s)
e6bd53dd1f91 Allow debug output directly from the lexer, before preprocessor eats.
Ryan C. Gordon <icculus@icculus.org>
parents: 629
diff changeset
    22
{
e6bd53dd1f91 Allow debug output directly from the lexer, before preprocessor eats.
Ryan C. Gordon <icculus@icculus.org>
parents: 629
diff changeset
    23
    const Token retval = preprocessor_internal_lexer(s);
e6bd53dd1f91 Allow debug output directly from the lexer, before preprocessor eats.
Ryan C. Gordon <icculus@icculus.org>
parents: 629
diff changeset
    24
    MOJOSHADER_print_debug_token("LEXER", s->token,
e6bd53dd1f91 Allow debug output directly from the lexer, before preprocessor eats.
Ryan C. Gordon <icculus@icculus.org>
parents: 629
diff changeset
    25
                                 (unsigned int) (s->source - s->token),
e6bd53dd1f91 Allow debug output directly from the lexer, before preprocessor eats.
Ryan C. Gordon <icculus@icculus.org>
parents: 629
diff changeset
    26
                                 retval);
e6bd53dd1f91 Allow debug output directly from the lexer, before preprocessor eats.
Ryan C. Gordon <icculus@icculus.org>
parents: 629
diff changeset
    27
    return retval;
e6bd53dd1f91 Allow debug output directly from the lexer, before preprocessor eats.
Ryan C. Gordon <icculus@icculus.org>
parents: 629
diff changeset
    28
} // debug_preprocessor_internal_lexer
e6bd53dd1f91 Allow debug output directly from the lexer, before preprocessor eats.
Ryan C. Gordon <icculus@icculus.org>
parents: 629
diff changeset
    29
#define preprocessor_internal_lexer(s) debug_preprocessor_internal_lexer(s)
e6bd53dd1f91 Allow debug output directly from the lexer, before preprocessor eats.
Ryan C. Gordon <icculus@icculus.org>
parents: 629
diff changeset
    30
#endif
e6bd53dd1f91 Allow debug output directly from the lexer, before preprocessor eats.
Ryan C. Gordon <icculus@icculus.org>
parents: 629
diff changeset
    31
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
typedef struct DefineHash
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
    MOJOSHADER_preprocessorDefine define;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
    struct DefineHash *next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
} DefineHash;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
620
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
    38
612
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
    39
// Simple linked list to cache source filenames, so we don't have to copy
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
    40
//  the same string over and over for each opcode.
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
    41
typedef struct FilenameCache
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
    42
{
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
    43
    char *filename;
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
    44
    struct FilenameCache *next;
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
    45
} FilenameCache;
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
    46
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
typedef struct Context
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
    int isfail;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
    int out_of_memory;
606
63e7a66ac320 Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 605
diff changeset
    51
    char failstr[256];
620
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
    52
    Conditional *conditional_pool;
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
    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
    54
    IncludeState *include_pool;
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
    DefineHash *define_hashtable[256];
646
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
    56
    DefineHash *define_pool;
612
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
    57
    FilenameCache *filename_cache;
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
    MOJOSHADER_includeOpen open_callback;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
    MOJOSHADER_includeClose close_callback;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
    MOJOSHADER_malloc malloc;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
    MOJOSHADER_free free;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
    void *malloc_data;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
} Context;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
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
// Convenience functions for allocators...
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
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
    69
{
598
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 597
diff changeset
    70
    ctx->out_of_memory = 1;
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
} // out_of_memory
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
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
    74
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
    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
    76
    if (retval == NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
        out_of_memory(ctx);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
    return retval;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
} // Malloc
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
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
    82
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
    if (ptr != NULL)  // check for NULL in case of dumb free() impl.
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
        ctx->free(ptr, ctx->malloc_data);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
} // Free
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
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
    88
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
    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
    90
    if (retval != NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
        strcpy(retval, str);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
    return retval;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
} // StrDup
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
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
    96
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
    97
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
    ctx->isfail = 1;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
    va_list ap;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
    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
   101
    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
   102
    va_end(ap);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
} // failf
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
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
   106
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
    failf(ctx, "%s", reason);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
} // fail
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
587
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   111
#if DEBUG_TOKENIZER
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   112
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
   113
                                  const unsigned int tokenlen,
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   114
                                  const Token tokenval)
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   115
{
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   116
    printf("%s TOKEN: \"", subsystem);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   117
    unsigned int i;
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   118
    for (i = 0; i < tokenlen; i++)
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   119
    {
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   120
        if (token[i] == '\n')
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   121
            printf("\\n");
629
d886403c6609 Better debug token output.
Ryan C. Gordon <icculus@icculus.org>
parents: 628
diff changeset
   122
        else if (token[i] == '\\')
d886403c6609 Better debug token output.
Ryan C. Gordon <icculus@icculus.org>
parents: 628
diff changeset
   123
            printf("\\\\");
587
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   124
        else
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   125
            printf("%c", token[i]);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   126
    } // for
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   127
    printf("\" (");
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   128
    switch (tokenval)
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   129
    {
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   130
        #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
   131
        TOKENCASE(TOKEN_UNKNOWN);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   132
        TOKENCASE(TOKEN_IDENTIFIER);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   133
        TOKENCASE(TOKEN_INT_LITERAL);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   134
        TOKENCASE(TOKEN_FLOAT_LITERAL);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   135
        TOKENCASE(TOKEN_STRING_LITERAL);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   136
        TOKENCASE(TOKEN_ADDASSIGN);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   137
        TOKENCASE(TOKEN_SUBASSIGN);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   138
        TOKENCASE(TOKEN_MULTASSIGN);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   139
        TOKENCASE(TOKEN_DIVASSIGN);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   140
        TOKENCASE(TOKEN_MODASSIGN);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   141
        TOKENCASE(TOKEN_XORASSIGN);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   142
        TOKENCASE(TOKEN_ANDASSIGN);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   143
        TOKENCASE(TOKEN_ORASSIGN);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   144
        TOKENCASE(TOKEN_INCREMENT);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   145
        TOKENCASE(TOKEN_DECREMENT);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   146
        TOKENCASE(TOKEN_RSHIFT);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   147
        TOKENCASE(TOKEN_LSHIFT);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   148
        TOKENCASE(TOKEN_ANDAND);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   149
        TOKENCASE(TOKEN_OROR);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   150
        TOKENCASE(TOKEN_LEQ);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   151
        TOKENCASE(TOKEN_GEQ);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   152
        TOKENCASE(TOKEN_EQL);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   153
        TOKENCASE(TOKEN_NEQ);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   154
        TOKENCASE(TOKEN_HASHHASH);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   155
        TOKENCASE(TOKEN_PP_INCLUDE);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   156
        TOKENCASE(TOKEN_PP_LINE);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   157
        TOKENCASE(TOKEN_PP_DEFINE);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   158
        TOKENCASE(TOKEN_PP_UNDEF);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   159
        TOKENCASE(TOKEN_PP_IF);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   160
        TOKENCASE(TOKEN_PP_IFDEF);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   161
        TOKENCASE(TOKEN_PP_IFNDEF);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   162
        TOKENCASE(TOKEN_PP_ELSE);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   163
        TOKENCASE(TOKEN_PP_ELIF);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   164
        TOKENCASE(TOKEN_PP_ENDIF);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   165
        TOKENCASE(TOKEN_PP_ERROR);
604
c08b6b14994a Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 601
diff changeset
   166
        TOKENCASE(TOKEN_INCOMPLETE_COMMENT);
c08b6b14994a Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 601
diff changeset
   167
        TOKENCASE(TOKEN_BAD_CHARS);
587
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   168
        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
   169
        TOKENCASE(TOKEN_PREPROCESSING_ERROR);
587
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   170
        #undef TOKENCASE
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   171
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   172
        case ((Token) '\n'):
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   173
            printf("'\\n'");
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   174
            break;
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   175
629
d886403c6609 Better debug token output.
Ryan C. Gordon <icculus@icculus.org>
parents: 628
diff changeset
   176
        case ((Token) '\\'):
d886403c6609 Better debug token output.
Ryan C. Gordon <icculus@icculus.org>
parents: 628
diff changeset
   177
            printf("'\\\\'");
d886403c6609 Better debug token output.
Ryan C. Gordon <icculus@icculus.org>
parents: 628
diff changeset
   178
            break;
d886403c6609 Better debug token output.
Ryan C. Gordon <icculus@icculus.org>
parents: 628
diff changeset
   179
587
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   180
        default:
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   181
            assert(((int)tokenval) < 256);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   182
            printf("'%c'", (char) tokenval);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   183
            break;
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   184
    } // switch
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   185
    printf(")\n");
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   186
} // MOJOSHADER_print_debug_token
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   187
#endif
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   188
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   189
611
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   190
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   191
#if !MOJOSHADER_FORCE_INCLUDE_CALLBACKS
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   192
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   193
// !!! 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
   194
#ifdef _MSC_VER
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   195
#define WIN32_LEAN_AND_MEAN 1
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   196
#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
   197
#else
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   198
#include <sys/stat.h>
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   199
#include <fcntl.h>
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   200
#include <unistd.h>
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   201
#endif
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   202
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   203
int MOJOSHADER_internal_include_open(MOJOSHADER_includeType inctype,
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   204
                                     const char *fname, const char *parent,
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   205
                                     const char **outdata,
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   206
                                     unsigned int *outbytes,
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   207
                                     MOJOSHADER_malloc m, MOJOSHADER_free f,
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   208
                                     void *d)
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   209
{
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   210
#ifdef _MSC_VER
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   211
#error Write me.
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   212
#else
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   213
    struct stat statbuf;
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   214
    if (stat(fname, &statbuf) == -1)
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   215
        return 0;
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   216
    char *data = (char *) m(statbuf.st_size, d);
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   217
    if (data == NULL)
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   218
        return 0;
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   219
    const int fd = open(fname, O_RDONLY);
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   220
    if (fd == -1)
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   221
    {
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   222
        f(data, d);
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   223
        return 0;
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   224
    } // if
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   225
    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
   226
    {
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   227
        f(data, d);
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   228
        close(fd);
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   229
        return 0;
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   230
    } // if
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   231
    close(fd);
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   232
    *outdata = data;
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   233
    *outbytes = (unsigned int) statbuf.st_size;
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   234
    return 1;
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   235
#endif
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   236
} // MOJOSHADER_internal_include_open
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   237
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   238
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   239
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
   240
                                       MOJOSHADER_free f, void *d)
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   241
{
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   242
    f((void *) data, d);
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   243
} // MOJOSHADER_internal_include_close
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   244
#endif  // !MOJOSHADER_FORCE_INCLUDE_CALLBACKS
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   245
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   246
632
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   247
// data buffer stuff...
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   248
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   249
#define BUFFER_LEN (64 * 1024)
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   250
typedef struct BufferList
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   251
{
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   252
    char buffer[BUFFER_LEN];
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   253
    size_t bytes;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   254
    struct BufferList *next;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   255
} BufferList;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   256
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   257
typedef struct Buffer
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   258
{
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   259
    size_t total_bytes;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   260
    BufferList head;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   261
    BufferList *tail;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   262
} Buffer;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   263
633
9cd2376b9aa2 Renamed buffer_init() to init_buffer() to match rest of function names.
Ryan C. Gordon <icculus@icculus.org>
parents: 632
diff changeset
   264
static void init_buffer(Buffer *buffer)
632
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   265
{
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   266
    buffer->total_bytes = 0;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   267
    buffer->head.bytes = 0;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   268
    buffer->head.next = NULL;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   269
    buffer->tail = &buffer->head;
633
9cd2376b9aa2 Renamed buffer_init() to init_buffer() to match rest of function names.
Ryan C. Gordon <icculus@icculus.org>
parents: 632
diff changeset
   270
} // init_buffer
632
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   271
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   272
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   273
static int add_to_buffer(Buffer *buffer, const char *data,
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   274
                         size_t len, MOJOSHADER_malloc m, void *d)
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   275
{
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   276
    buffer->total_bytes += len;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   277
    while (len > 0)
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   278
    {
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   279
        const size_t avail = BUFFER_LEN - buffer->tail->bytes;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   280
        const size_t cpy = (avail > len) ? len : avail;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   281
        memcpy(buffer->tail->buffer + buffer->tail->bytes, data, cpy);
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   282
        len -= cpy;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   283
        data += cpy;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   284
        buffer->tail->bytes += cpy;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   285
        assert(buffer->tail->bytes <= BUFFER_LEN);
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   286
        if (buffer->tail->bytes == BUFFER_LEN)
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   287
        {
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   288
            BufferList *item = (BufferList *) m(sizeof (BufferList), d);
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   289
            if (item == NULL)
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   290
                return 0;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   291
            item->bytes = 0;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   292
            item->next = NULL;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   293
            buffer->tail->next = item;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   294
            buffer->tail = item;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   295
        } // if
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   296
    } // while
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   297
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   298
    return 1;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   299
} // add_to_buffer
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   300
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   301
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   302
static char *flatten_buffer(Buffer *buffer, MOJOSHADER_malloc m, void *d)
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   303
{
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   304
    char *retval = m(buffer->total_bytes + 1, d);
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   305
    if (retval == NULL)
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   306
        return NULL;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   307
    BufferList *item = &buffer->head;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   308
    char *ptr = retval;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   309
    while (item != NULL)
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   310
    {
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   311
        BufferList *next = item->next;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   312
        memcpy(ptr, item->buffer, item->bytes);
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   313
        ptr += item->bytes;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   314
        item = next;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   315
    } // while
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   316
    *ptr = '\0';
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   317
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   318
    assert(ptr == (retval + buffer->total_bytes));
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   319
    return retval;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   320
} // flatten_buffer
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   321
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   322
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   323
static void free_buffer(Buffer *buffer, MOJOSHADER_free f, void *d)
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   324
{
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   325
    // head is statically allocated, so start with head.next...
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   326
    BufferList *item = buffer->head.next;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   327
    while (item != NULL)
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   328
    {
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   329
        BufferList *next = item->next;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   330
        f(item, d);
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   331
        item = next;
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   332
    } // while
633
9cd2376b9aa2 Renamed buffer_init() to init_buffer() to match rest of function names.
Ryan C. Gordon <icculus@icculus.org>
parents: 632
diff changeset
   333
    init_buffer(buffer);
632
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   334
} // free_buffer
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   335
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   336
647
a09899ba5e8c Added some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 646
diff changeset
   337
// !!! FIXME: maybe use these pool magic elsewhere?
632
a5f63a10ed91 Moved Buffer stuff higher up, so internal routines can use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   338
646
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   339
// Pool stuff...
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   340
// 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
   341
#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
   342
    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
   343
        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
   344
        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
   345
            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
   346
            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
   347
            item = next; \
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   348
        } \
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   349
    }
620
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
   350
646
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   351
#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
   352
    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
   353
        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
   354
        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
   355
            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
   356
        else \
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   357
            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
   358
        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
   359
            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
   360
        return retval; \
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   361
    }
620
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
   362
646
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   363
#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
   364
    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
   365
        item->next = 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
   366
    }
620
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
   367
646
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   368
#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
   369
    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
   370
    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
   371
    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
   372
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   373
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
   374
IMPLEMENT_POOL(IncludeState, include)
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   375
IMPLEMENT_POOL(DefineHash, define)
620
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
   376
611
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   377
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
// Preprocessor define hashtable stuff...
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
static unsigned char hash_define(const char *sym)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   381
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   382
    unsigned char retval = 0;
622
59b3003f6494 Fixed logic bug in string loop.
Ryan C. Gordon <icculus@icculus.org>
parents: 621
diff changeset
   383
    while (*sym)
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   384
        retval += *(sym++);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
    return retval;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   386
} // hash_define
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   387
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   388
637
2e6202cde135 add_define() can take a string we allocated elsewhere, to avoid extra StrDup().
Ryan C. Gordon <icculus@icculus.org>
parents: 636
diff changeset
   389
static int add_define(Context *ctx, const char *sym, const char *val, int copy)
555
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
    char *identifier = NULL;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   392
    char *definition = NULL;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
    const unsigned char hash = hash_define(sym);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
    DefineHash *bucket = ctx->define_hashtable[hash];
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   395
    while (bucket)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   396
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   397
        if (strcmp(bucket->define.identifier, sym) == 0)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   398
        {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   399
            failf(ctx, "'%s' already defined", sym);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   400
            return 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   401
        } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   402
        bucket = bucket->next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   403
    } // while
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   404
646
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   405
    bucket = get_define(ctx);
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   406
    if (bucket == NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   407
        return 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   408
646
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   409
    identifier = StrDup(ctx, sym);
637
2e6202cde135 add_define() can take a string we allocated elsewhere, to avoid extra StrDup().
Ryan C. Gordon <icculus@icculus.org>
parents: 636
diff changeset
   410
    if (identifier == NULL)
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   411
    {
646
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   412
        put_define(ctx, bucket);
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   413
        return 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   414
    } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   415
637
2e6202cde135 add_define() can take a string we allocated elsewhere, to avoid extra StrDup().
Ryan C. Gordon <icculus@icculus.org>
parents: 636
diff changeset
   416
    if (!copy)
2e6202cde135 add_define() can take a string we allocated elsewhere, to avoid extra StrDup().
Ryan C. Gordon <icculus@icculus.org>
parents: 636
diff changeset
   417
        bucket->define.definition = val;
2e6202cde135 add_define() can take a string we allocated elsewhere, to avoid extra StrDup().
Ryan C. Gordon <icculus@icculus.org>
parents: 636
diff changeset
   418
    else
2e6202cde135 add_define() can take a string we allocated elsewhere, to avoid extra StrDup().
Ryan C. Gordon <icculus@icculus.org>
parents: 636
diff changeset
   419
    {
646
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   420
        definition = StrDup(ctx, val);
637
2e6202cde135 add_define() can take a string we allocated elsewhere, to avoid extra StrDup().
Ryan C. Gordon <icculus@icculus.org>
parents: 636
diff changeset
   421
        if (definition == NULL)
2e6202cde135 add_define() can take a string we allocated elsewhere, to avoid extra StrDup().
Ryan C. Gordon <icculus@icculus.org>
parents: 636
diff changeset
   422
        {
2e6202cde135 add_define() can take a string we allocated elsewhere, to avoid extra StrDup().
Ryan C. Gordon <icculus@icculus.org>
parents: 636
diff changeset
   423
            Free(ctx, identifier);
646
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   424
            put_define(ctx, bucket);
637
2e6202cde135 add_define() can take a string we allocated elsewhere, to avoid extra StrDup().
Ryan C. Gordon <icculus@icculus.org>
parents: 636
diff changeset
   425
            return 0;
2e6202cde135 add_define() can take a string we allocated elsewhere, to avoid extra StrDup().
Ryan C. Gordon <icculus@icculus.org>
parents: 636
diff changeset
   426
        } // if
2e6202cde135 add_define() can take a string we allocated elsewhere, to avoid extra StrDup().
Ryan C. Gordon <icculus@icculus.org>
parents: 636
diff changeset
   427
        bucket->define.definition = definition;
2e6202cde135 add_define() can take a string we allocated elsewhere, to avoid extra StrDup().
Ryan C. Gordon <icculus@icculus.org>
parents: 636
diff changeset
   428
    } // if
2e6202cde135 add_define() can take a string we allocated elsewhere, to avoid extra StrDup().
Ryan C. Gordon <icculus@icculus.org>
parents: 636
diff changeset
   429
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   430
    bucket->define.identifier = identifier;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   431
    bucket->next = ctx->define_hashtable[hash];
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   432
    ctx->define_hashtable[hash] = bucket;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   433
    return 1;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   434
} // add_define
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   435
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   436
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   437
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
   438
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   439
    const unsigned char hash = hash_define(sym);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   440
    DefineHash *bucket = ctx->define_hashtable[hash];
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   441
    DefineHash *prev = NULL;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   442
    while (bucket)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   443
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   444
        if (strcmp(bucket->define.identifier, sym) == 0)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   445
        {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   446
            if (prev == NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   447
                ctx->define_hashtable[hash] = bucket->next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   448
            else
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   449
                prev->next = bucket->next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   450
            Free(ctx, (void *) bucket->define.identifier);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   451
            Free(ctx, (void *) bucket->define.definition);
646
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   452
            put_define(ctx, bucket);
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   453
            return 1;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   454
        } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   455
        prev = bucket;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   456
        bucket = bucket->next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   457
    } // while
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   458
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   459
    return 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   460
} // remove_define
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   461
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   462
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   463
static const char *find_define(Context *ctx, const char *sym)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   464
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   465
    const unsigned char hash = hash_define(sym);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   466
    DefineHash *bucket = ctx->define_hashtable[hash];
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   467
    while (bucket)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   468
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   469
        if (strcmp(bucket->define.identifier, sym) == 0)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   470
            return bucket->define.definition;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   471
        bucket = bucket->next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   472
    } // while
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   473
    return NULL;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   474
} // find_define
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   475
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   476
646
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   477
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
   478
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   479
    int i;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   480
    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
   481
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   482
        DefineHash *bucket = ctx->define_hashtable[i];
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   483
        ctx->define_hashtable[i] = NULL;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   484
        while (bucket)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   485
        {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   486
            DefineHash *next = bucket->next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   487
            Free(ctx, (void *) bucket->define.identifier);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   488
            Free(ctx, (void *) bucket->define.definition);
646
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   489
            put_define(ctx, bucket);
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   490
            bucket = next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   491
        } // while
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   492
    } // for
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   493
} // find_define
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   494
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   495
612
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   496
// filename cache stuff...
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   497
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   498
static const char *cache_filename(Context *ctx, const char *fname)
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   499
{
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   500
    if (fname == NULL)
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   501
        return NULL;
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   502
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   503
    // !!! FIXME: this could be optimized into a hash table, but oh well.
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   504
    FilenameCache *item = ctx->filename_cache;
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   505
    while (item != NULL)
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   506
    {
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   507
        if (strcmp(item->filename, fname) == 0)
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   508
            return item->filename;
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   509
        item = item->next;
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   510
    } // while
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   511
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   512
    // new cache item.
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   513
    item = (FilenameCache *) Malloc(ctx, sizeof (FilenameCache));
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   514
    if (item == NULL)
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   515
        return NULL;
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   516
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   517
    item->filename = StrDup(ctx, fname);
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   518
    if (item->filename == NULL)
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   519
    {
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   520
        Free(ctx, item);
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   521
        return NULL;
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   522
    } // if
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   523
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   524
    item->next = ctx->filename_cache;
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   525
    ctx->filename_cache = item;
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   526
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   527
    return item->filename;
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   528
} // cache_filename
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   529
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   530
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   531
static void free_filename_cache(Context *ctx)
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   532
{
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   533
    FilenameCache *item = ctx->filename_cache;
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   534
    while (item != NULL)
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   535
    {
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   536
        FilenameCache *next = item->next;
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   537
        Free(ctx, item->filename);
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   538
        Free(ctx, item);
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   539
        item = next;
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   540
    } // while
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   541
} // free_filename_cache
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   542
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   543
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   544
static int push_source(Context *ctx, const char *fname, const char *source,
640
813a139a4a5a push_source() now specifies a line number.
Ryan C. Gordon <icculus@icculus.org>
parents: 639
diff changeset
   545
                       unsigned int srclen, unsigned int linenum, int included)
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   546
{
646
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   547
    IncludeState *state = get_include(ctx);
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   548
    if (state == NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   549
        return 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   550
560
225d579ae929 Handle NULL file names.
Ryan C. Gordon <icculus@icculus.org>
parents: 559
diff changeset
   551
    if (fname != NULL)
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   552
    {
612
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   553
        state->filename = cache_filename(ctx, fname);
560
225d579ae929 Handle NULL file names.
Ryan C. Gordon <icculus@icculus.org>
parents: 559
diff changeset
   554
        if (state->filename == NULL)
225d579ae929 Handle NULL file names.
Ryan C. Gordon <icculus@icculus.org>
parents: 559
diff changeset
   555
        {
646
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   556
            put_include(ctx, state);
560
225d579ae929 Handle NULL file names.
Ryan C. Gordon <icculus@icculus.org>
parents: 559
diff changeset
   557
            return 0;
225d579ae929 Handle NULL file names.
Ryan C. Gordon <icculus@icculus.org>
parents: 559
diff changeset
   558
        } // if
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   559
    } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   560
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   561
    state->included = included;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   562
    state->source_base = source;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   563
    state->source = source;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   564
    state->token = source;
642
624ab8696f9b Enormous amount of tapdancing to handle EOI better in the lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 641
diff changeset
   565
    state->orig_length = srclen;
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   566
    state->bytes_left = srclen;
640
813a139a4a5a push_source() now specifies a line number.
Ryan C. Gordon <icculus@icculus.org>
parents: 639
diff changeset
   567
    state->line = linenum;
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   568
    state->next = ctx->include_stack;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   569
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   570
    ctx->include_stack = state;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   571
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   572
    return 1;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   573
} // push_source
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   574
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   575
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   576
static void pop_source(Context *ctx)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   577
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   578
    IncludeState *state = ctx->include_stack;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   579
    if (state == NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   580
        return;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   581
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   582
    if (state->included)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   583
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   584
        ctx->close_callback(state->source_base, ctx->malloc,
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   585
                            ctx->free, ctx->malloc_data);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   586
    } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   587
612
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   588
    // 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
   589
646
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   590
    while (state->conditional_stack)
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   591
        put_conditional(ctx, state->conditional_stack);
620
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
   592
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   593
    ctx->include_stack = state->next;
646
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   594
    put_include(ctx, state);
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   595
} // pop_source
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   596
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   597
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   598
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
   599
                            unsigned int sourcelen,
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   600
                            MOJOSHADER_includeOpen open_callback,
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   601
                            MOJOSHADER_includeClose close_callback,
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   602
                            const MOJOSHADER_preprocessorDefine **defines,
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   603
                            unsigned int define_count,
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   604
                            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
   605
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   606
    int okay = 1;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   607
    int i = 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   608
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   609
    // 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
   610
    assert(m != NULL);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   611
    assert(f != NULL);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   612
    assert(open_callback != NULL);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   613
    assert(close_callback != NULL);
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
    Context *ctx = (Context *) m(sizeof (Context), d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   616
    if (ctx == NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   617
        return 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   618
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   619
    memset(ctx, '\0', sizeof (Context));
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   620
    ctx->malloc = m;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   621
    ctx->free = f;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   622
    ctx->malloc_data = d;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   623
    ctx->open_callback = open_callback;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   624
    ctx->close_callback = close_callback;
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
    for (i = 0; i < define_count; i++)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   627
    {
637
2e6202cde135 add_define() can take a string we allocated elsewhere, to avoid extra StrDup().
Ryan C. Gordon <icculus@icculus.org>
parents: 636
diff changeset
   628
        if (!add_define(ctx, defines[i]->identifier, defines[i]->definition, 1))
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   629
        {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   630
            okay = 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   631
            break;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   632
        } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   633
    } // for
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   634
640
813a139a4a5a push_source() now specifies a line number.
Ryan C. Gordon <icculus@icculus.org>
parents: 639
diff changeset
   635
    if ((okay) && (!push_source(ctx, fname, source, sourcelen, 1, 0)))
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   636
        okay = 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   637
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   638
    if (!okay)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   639
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   640
        preprocessor_end((Preprocessor *) ctx);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   641
        return NULL;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   642
    } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   643
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   644
    return (Preprocessor *) ctx;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   645
} // preprocessor_start
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   646
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   647
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   648
void preprocessor_end(Preprocessor *_ctx)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   649
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   650
    Context *ctx = (Context *) _ctx;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   651
    if (ctx == NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   652
        return;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   653
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   654
    while (ctx->include_stack != NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   655
        pop_source(ctx);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   656
646
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   657
    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
   658
612
72ccfe69eaf1 Moved filename caching into the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 611
diff changeset
   659
    free_filename_cache(ctx);
646
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
   660
    free_define_pool(ctx);
620
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
   661
    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
   662
    free_include_pool(ctx);
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   663
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   664
    Free(ctx, ctx);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   665
} // preprocessor_end
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   666
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   667
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   668
int preprocessor_outofmemory(Preprocessor *_ctx)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   669
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   670
    Context *ctx = (Context *) _ctx;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   671
    return ctx->out_of_memory;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   672
} // preprocessor_outofmemory
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   673
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   674
638
e910619c61f4 Added a FIXME.
Ryan C. Gordon <icculus@icculus.org>
parents: 637
diff changeset
   675
// !!! 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
   676
static int require_newline(IncludeState *state)
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   677
{
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   678
    const char *source = state->source;
634
b7fd8aa02bf4 Fix state when rewinding stream.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   679
    const unsigned int bytes_left = state->bytes_left;
626
95d97e613112 Fixed line numbers when a preprocessor directive fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 625
diff changeset
   680
    const unsigned int linenum = state->line;
614
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   681
    const Token token = preprocessor_internal_lexer(state);
626
95d97e613112 Fixed line numbers when a preprocessor directive fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 625
diff changeset
   682
    state->source = source;  // rewind no matter what.
634
b7fd8aa02bf4 Fix state when rewinding stream.
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   683
    state->bytes_left = bytes_left;
626
95d97e613112 Fixed line numbers when a preprocessor directive fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 625
diff changeset
   684
    state->line = linenum;
614
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   685
    if (token == TOKEN_INCOMPLETE_COMMENT)
626
95d97e613112 Fixed line numbers when a preprocessor directive fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 625
diff changeset
   686
        return 1; // call it an eol.
614
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   687
    return ( (token == ((Token) '\n')) || (token == TOKEN_EOI) );
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   688
} // require_newline
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   689
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   690
611
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   691
static void handle_pp_include(Context *ctx)
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   692
{
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   693
    IncludeState *state = ctx->include_stack;
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   694
    Token token = preprocessor_internal_lexer(state);
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   695
    MOJOSHADER_includeType incltype;
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   696
    char *filename = NULL;
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   697
    int bogus = 0;
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   698
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   699
    if (token == TOKEN_STRING_LITERAL)
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   700
        incltype = MOJOSHADER_INCLUDETYPE_LOCAL;
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   701
    else if (token == ((Token) '<'))
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   702
    {
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   703
        incltype = MOJOSHADER_INCLUDETYPE_SYSTEM;
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   704
        // 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
   705
        //  considered part of the filename.  :/
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   706
        while (!bogus)
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   707
        {
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   708
            if ( !(bogus = (state->bytes_left == 0)) )
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   709
            {
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   710
                const char ch = *state->source;
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   711
                if ( !(bogus = ((ch == '\r') || (ch == '\n'))) )
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   712
                {
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   713
                    state->source++;
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   714
                    state->bytes_left--;
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   715
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   716
                    if (ch == '>')
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   717
                        break;
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   718
                } // if
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   719
            } // if
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   720
        } // while
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   721
    } // else if
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   722
    else
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   723
    {
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   724
        bogus = 1;
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   725
    } // else
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   726
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   727
    if (!bogus)
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   728
    {
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   729
        state->token++;  // skip '<' or '\"'...
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   730
        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
   731
        filename = (char *) alloca(len);
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   732
        memcpy(filename, state->token, len-1);
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   733
        filename[len-1] = '\0';
614
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   734
        bogus = !require_newline(state);
611
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   735
    } // if
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   736
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   737
    if (bogus)
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   738
    {
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   739
        fail(ctx, "Invalid #include directive");
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   740
        return;
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   741
    } // else
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   742
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   743
    const char *newdata = NULL;
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   744
    unsigned int newbytes = 0;
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   745
    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
   746
                            &newdata, &newbytes, ctx->malloc,
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   747
                            ctx->free, ctx->malloc_data))
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   748
    {
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   749
        fail(ctx, "Include callback failed");  // !!! FIXME: better error
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   750
        return;
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   751
    } // if
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   752
640
813a139a4a5a push_source() now specifies a line number.
Ryan C. Gordon <icculus@icculus.org>
parents: 639
diff changeset
   753
    if (!push_source(ctx, filename, newdata, newbytes, 1, 1))
611
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   754
    {
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   755
        assert(ctx->out_of_memory);
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   756
        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
   757
    } // if
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   758
} // handle_pp_include
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   759
8c2ee1a97ee1 Implemented #include in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 607
diff changeset
   760
614
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   761
static void handle_pp_line(Context *ctx)
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   762
{
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   763
    IncludeState *state = ctx->include_stack;
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   764
    char *filename = NULL;
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   765
    int linenum = 0;
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   766
    int bogus = 0;
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   767
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   768
    if (preprocessor_internal_lexer(state) != TOKEN_INT_LITERAL)
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   769
        bogus = 1;
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   770
    else
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   771
    {
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   772
        const unsigned int len = ((unsigned int) (state->source-state->token));
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   773
        char *buf = (char *) alloca(len+1);
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   774
        memcpy(buf, state->token, len);
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   775
        buf[len] = '\0';
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   776
        linenum = atoi(buf);
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   777
    } // else
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   778
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   779
    if (!bogus)
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   780
        bogus = (preprocessor_internal_lexer(state) != TOKEN_STRING_LITERAL);
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   781
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   782
    if (!bogus)
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   783
    {
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   784
        state->token++;  // skip '\"'...
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   785
        const unsigned int len = ((unsigned int) (state->source-state->token));
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   786
        filename = (char *) alloca(len);
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   787
        memcpy(filename, state->token, len-1);
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   788
        filename[len-1] = '\0';
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   789
        bogus = !require_newline(state);
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   790
    } // if
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   791
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   792
    if (bogus)
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   793
    {
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   794
        fail(ctx, "Invalid #line directive");
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   795
        return;
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   796
    } // if
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   797
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   798
    const char *cached = cache_filename(ctx, filename);
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   799
    assert((cached != NULL) || (ctx->out_of_memory));
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   800
    state->filename = cached;
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   801
    state->line = linenum;
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   802
} // handle_pp_line
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   803
0f2f298003ae Implemented #line preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 612
diff changeset
   804
606
63e7a66ac320 Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 605
diff changeset
   805
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
   806
{
63e7a66ac320 Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 605
diff changeset
   807
    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
   808
    unsigned int bytes_left = 0;
be3b428802a1 Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents: 634
diff changeset
   809
    char *ptr = ctx->failstr;
be3b428802a1 Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents: 634
diff changeset
   810
    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
   811
    int cpy = 0;
606
63e7a66ac320 Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 605
diff changeset
   812
    int done = 0;
63e7a66ac320 Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 605
diff changeset
   813
635
be3b428802a1 Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents: 634
diff changeset
   814
    const char *prefix = "#error";
be3b428802a1 Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents: 634
diff changeset
   815
    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
   816
    strcpy(ctx->failstr, prefix);
be3b428802a1 Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents: 634
diff changeset
   817
    avail -= prefixlen;
be3b428802a1 Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents: 634
diff changeset
   818
    ptr += prefixlen;
be3b428802a1 Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents: 634
diff changeset
   819
be3b428802a1 Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents: 634
diff changeset
   820
    state->report_whitespace = 1;
615
5467d19b4d7d Fix line numbers in #error preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 614
diff changeset
   821
    const char *source = NULL;
606
63e7a66ac320 Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 605
diff changeset
   822
    while (!done)
63e7a66ac320 Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 605
diff changeset
   823
    {
635
be3b428802a1 Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents: 634
diff changeset
   824
        bytes_left = state->bytes_left;
615
5467d19b4d7d Fix line numbers in #error preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 614
diff changeset
   825
        source = state->source;
606
63e7a66ac320 Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 605
diff changeset
   826
        const Token token = preprocessor_internal_lexer(state);
63e7a66ac320 Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 605
diff changeset
   827
        switch (token)
63e7a66ac320 Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 605
diff changeset
   828
        {
615
5467d19b4d7d Fix line numbers in #error preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 614
diff changeset
   829
            case ((Token) '\n'):
5467d19b4d7d Fix line numbers in #error preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 614
diff changeset
   830
                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
   831
                // fall through!
606
63e7a66ac320 Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 605
diff changeset
   832
            case TOKEN_INCOMPLETE_COMMENT:
63e7a66ac320 Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 605
diff changeset
   833
            case TOKEN_EOI:
635
be3b428802a1 Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents: 634
diff changeset
   834
                state->bytes_left = bytes_left;
be3b428802a1 Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents: 634
diff changeset
   835
                state->source = source;  // 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
   836
                done = 1;
63e7a66ac320 Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 605
diff changeset
   837
                break;
63e7a66ac320 Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 605
diff changeset
   838
635
be3b428802a1 Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents: 634
diff changeset
   839
            case ' ':
be3b428802a1 Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents: 634
diff changeset
   840
                if (!avail)
be3b428802a1 Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents: 634
diff changeset
   841
                    break;
be3b428802a1 Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents: 634
diff changeset
   842
                *(ptr++) = ' ';
be3b428802a1 Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents: 634
diff changeset
   843
                avail--;
be3b428802a1 Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents: 634
diff changeset
   844
                break;
be3b428802a1 Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents: 634
diff changeset
   845
606
63e7a66ac320 Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 605
diff changeset
   846
            default:
635
be3b428802a1 Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents: 634
diff changeset
   847
                cpy = Min(avail, (int) (state->source-state->token));
be3b428802a1 Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents: 634
diff changeset
   848
                if (cpy)
be3b428802a1 Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents: 634
diff changeset
   849
                    memcpy(ptr, state->token, cpy);
be3b428802a1 Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents: 634
diff changeset
   850
                ptr += cpy;
be3b428802a1 Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents: 634
diff changeset
   851
                avail -= cpy;
606
63e7a66ac320 Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 605
diff changeset
   852
                break;
63e7a66ac320 Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 605
diff changeset
   853
        } // switch
63e7a66ac320 Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 605
diff changeset
   854
    } // while
63e7a66ac320 Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 605
diff changeset
   855
635
be3b428802a1 Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents: 634
diff changeset
   856
    *ptr = '\0';
be3b428802a1 Try to make #error lexing match gcc's.
Ryan C. Gordon <icculus@icculus.org>
parents: 634
diff changeset
   857
    state->report_whitespace = 0;
606
63e7a66ac320 Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 605
diff changeset
   858
    ctx->isfail = 1;
63e7a66ac320 Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 605
diff changeset
   859
} // handle_pp_error
63e7a66ac320 Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 605
diff changeset
   860
63e7a66ac320 Added support for #error directive to the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 605
diff changeset
   861
639
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   862
static void handle_pp_define(Context *ctx)
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   863
{
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   864
    IncludeState *state = ctx->include_stack;
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   865
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   866
    if (preprocessor_internal_lexer(state) != TOKEN_IDENTIFIER)
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   867
    {
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   868
        fail(ctx, "Macro names must be indentifiers");
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   869
        return;
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   870
    } // if
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   871
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   872
    MOJOSHADER_malloc m = ctx->malloc;
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   873
    void *d = ctx->malloc_data;
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   874
    const char space = ' ';
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   875
    unsigned int len = ((unsigned int) (state->source-state->token));
641
2002d0399ef2 Fixed some off-by-one nonsense.
Ryan C. Gordon <icculus@icculus.org>
parents: 640
diff changeset
   876
    char *sym = (char *) alloca(len+1);
2002d0399ef2 Fixed some off-by-one nonsense.
Ryan C. Gordon <icculus@icculus.org>
parents: 640
diff changeset
   877
    memcpy(sym, state->token, len);
2002d0399ef2 Fixed some off-by-one nonsense.
Ryan C. Gordon <icculus@icculus.org>
parents: 640
diff changeset
   878
    sym[len] = '\0';
639
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   879
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   880
    Buffer buffer;
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   881
    init_buffer(&buffer);
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   882
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   883
    int done = 0;
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   884
    const char *source = NULL;
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   885
    unsigned int bytes_left = 0;
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   886
    state->report_whitespace = 1;
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   887
    while ((!done) && (!ctx->out_of_memory))
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   888
    {
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   889
        bytes_left = state->bytes_left;
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   890
        source = state->source;
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   891
        const Token token = preprocessor_internal_lexer(state);
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   892
        switch (token)
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   893
        {
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   894
            case TOKEN_INCOMPLETE_COMMENT:
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   895
            case TOKEN_EOI:
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   896
                state->bytes_left = bytes_left;
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   897
                state->source = source;  // move back so we catch this later.
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   898
                // fall through!
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   899
            case ((Token) '\n'):
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   900
                done = 1;
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   901
                break;
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   902
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   903
            case ((Token) ' '):  // may not actually point to ' '.
643
20403ca72f54 Don't add spaces to the start of #define data.
Ryan C. Gordon <icculus@icculus.org>
parents: 642
diff changeset
   904
                if (buffer.total_bytes > 0)
20403ca72f54 Don't add spaces to the start of #define data.
Ryan C. Gordon <icculus@icculus.org>
parents: 642
diff changeset
   905
                {
20403ca72f54 Don't add spaces to the start of #define data.
Ryan C. Gordon <icculus@icculus.org>
parents: 642
diff changeset
   906
                    if (!add_to_buffer(&buffer, &space, 1, m, d))
20403ca72f54 Don't add spaces to the start of #define data.
Ryan C. Gordon <icculus@icculus.org>
parents: 642
diff changeset
   907
                        ctx->out_of_memory = 1;
20403ca72f54 Don't add spaces to the start of #define data.
Ryan C. Gordon <icculus@icculus.org>
parents: 642
diff changeset
   908
                } // if
639
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   909
                break;
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   910
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   911
            default:
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   912
                len = ((unsigned int) (state->source-state->token));
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   913
                if (!add_to_buffer(&buffer, state->token, len, m, d))
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   914
                    ctx->out_of_memory = 1;
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   915
                break;
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   916
        } // switch
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   917
    } // while
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   918
    state->report_whitespace = 0;
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   919
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   920
    char *definition = NULL;
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   921
    if (!ctx->out_of_memory)
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   922
    {
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   923
        definition = flatten_buffer(&buffer, m, d);
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   924
        ctx->out_of_memory = (definition == NULL);
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   925
    } // if
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   926
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   927
    free_buffer(&buffer, ctx->free, d);
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   928
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   929
    if (ctx->out_of_memory)
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   930
        return;
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   931
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   932
    assert(done);
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   933
    add_define(ctx, sym, definition, 0);
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   934
} // handle_pp_define
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   935
1b549be9ab59 Added #define preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 638
diff changeset
   936
618
83302ac1b9f4 Added #undef directive to preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 617
diff changeset
   937
static void handle_pp_undef(Context *ctx)
83302ac1b9f4 Added #undef directive to preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 617
diff changeset
   938
{
83302ac1b9f4 Added #undef directive to preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 617
diff changeset
   939
    IncludeState *state = ctx->include_stack;
83302ac1b9f4 Added #undef directive to preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 617
diff changeset
   940
83302ac1b9f4 Added #undef directive to preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 617
diff changeset
   941
    if (preprocessor_internal_lexer(state) != TOKEN_IDENTIFIER)
83302ac1b9f4 Added #undef directive to preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 617
diff changeset
   942
    {
83302ac1b9f4 Added #undef directive to preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 617
diff changeset
   943
        fail(ctx, "Macro names must be indentifiers");
83302ac1b9f4 Added #undef directive to preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 617
diff changeset
   944
        return;
83302ac1b9f4 Added #undef directive to preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 617
diff changeset
   945
    } // if
83302ac1b9f4 Added #undef directive to preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 617
diff changeset
   946
83302ac1b9f4 Added #undef directive to preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 617
diff changeset
   947
    const unsigned int len = ((unsigned int) (state->source-state->token));
641
2002d0399ef2 Fixed some off-by-one nonsense.
Ryan C. Gordon <icculus@icculus.org>
parents: 640
diff changeset
   948
    char *sym = (char *) alloca(len+1);
2002d0399ef2 Fixed some off-by-one nonsense.
Ryan C. Gordon <icculus@icculus.org>
parents: 640
diff changeset
   949
    memcpy(sym, state->token, len);
2002d0399ef2 Fixed some off-by-one nonsense.
Ryan C. Gordon <icculus@icculus.org>
parents: 640
diff changeset
   950
    sym[len] = '\0';
618
83302ac1b9f4 Added #undef directive to preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 617
diff changeset
   951
83302ac1b9f4 Added #undef directive to preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 617
diff changeset
   952
    if (!require_newline(state))
83302ac1b9f4 Added #undef directive to preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 617
diff changeset
   953
    {
83302ac1b9f4 Added #undef directive to preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 617
diff changeset
   954
        fail(ctx, "Invalid #undef directive");
83302ac1b9f4 Added #undef directive to preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 617
diff changeset
   955
        return;
83302ac1b9f4 Added #undef directive to preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 617
diff changeset
   956
    } // if
83302ac1b9f4 Added #undef directive to preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 617
diff changeset
   957
83302ac1b9f4 Added #undef directive to preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 617
diff changeset
   958
    remove_define(ctx, sym);
83302ac1b9f4 Added #undef directive to preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 617
diff changeset
   959
} // handle_pp_undef
83302ac1b9f4 Added #undef directive to preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 617
diff changeset
   960
83302ac1b9f4 Added #undef directive to preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 617
diff changeset
   961
623
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   962
static Conditional *_handle_pp_ifdef(Context *ctx, const Token type)
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   963
{
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   964
    IncludeState *state = ctx->include_stack;
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   965
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   966
    assert((type == TOKEN_PP_IFDEF) || (type == TOKEN_PP_IFNDEF));
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   967
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   968
    if (preprocessor_internal_lexer(state) != TOKEN_IDENTIFIER)
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   969
    {
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   970
        fail(ctx, "Macro names must be indentifiers");
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   971
        return NULL;
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   972
    } // if
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   973
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   974
    const unsigned int len = ((unsigned int) (state->source-state->token));
641
2002d0399ef2 Fixed some off-by-one nonsense.
Ryan C. Gordon <icculus@icculus.org>
parents: 640
diff changeset
   975
    char *sym = (char *) alloca(len+1);
2002d0399ef2 Fixed some off-by-one nonsense.
Ryan C. Gordon <icculus@icculus.org>
parents: 640
diff changeset
   976
    memcpy(sym, state->token, len);
2002d0399ef2 Fixed some off-by-one nonsense.
Ryan C. Gordon <icculus@icculus.org>
parents: 640
diff changeset
   977
    sym[len] = '\0';
623
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   978
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   979
    if (!require_newline(state))
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   980
    {
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   981
        if (type == TOKEN_PP_IFDEF)
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   982
            fail(ctx, "Invalid #ifdef directive");
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   983
        else
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   984
            fail(ctx, "Invalid #ifndef directive");
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   985
        return NULL;
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   986
    } // if
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   987
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   988
    Conditional *conditional = get_conditional(ctx);
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   989
    assert((conditional != NULL) || (ctx->out_of_memory));
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   990
    if (conditional == NULL)
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   991
        return NULL;
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   992
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   993
    Conditional *prev = state->conditional_stack;
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   994
    int skipping = ((prev != NULL) && (prev->skipping));
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   995
    if (!skipping)
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   996
    {
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   997
        const int found = (find_define(ctx, sym) != NULL);
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   998
        if (type == TOKEN_PP_IFDEF)
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   999
            skipping = !found;
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
  1000
        else
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
  1001
            skipping = found;
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
  1002
    } // if
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
  1003
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
  1004
    conditional->type = type;
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
  1005
    conditional->linenum = state->line - 1;
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
  1006
    conditional->skipping = skipping;
625
bfb4016d9404 Fixed #else preprocessor directive so it'll work with #elif.
Ryan C. Gordon <icculus@icculus.org>
parents: 624
diff changeset
  1007
    conditional->chosen = !skipping;
623
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
  1008
    conditional->next = prev;
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
  1009
    state->conditional_stack = conditional;
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
  1010
    return conditional;
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
  1011
} // _handle_pp_ifdef
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
  1012
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
  1013
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
  1014
static inline void handle_pp_ifdef(Context *ctx)
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
  1015
{
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
  1016
    _handle_pp_ifdef(ctx, TOKEN_PP_IFDEF);
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
  1017
} // handle_pp_ifdef
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
  1018
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
  1019
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
  1020
static inline void handle_pp_ifndef(Context *ctx)
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
  1021
{
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
  1022
    _handle_pp_ifdef(ctx, TOKEN_PP_IFNDEF);
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
  1023
} // handle_pp_ifndef
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
  1024
899d7618efef Implemented #ifdef and #ifndef preprocessor directives.
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
  1025
624
99d1cf8a18a3 Implemented #else preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 623
diff changeset
  1026
static inline void handle_pp_else(Context *ctx)
99d1cf8a18a3 Implemented #else preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 623
diff changeset
  1027
{
99d1cf8a18a3 Implemented #else preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 623
diff changeset
  1028
    IncludeState *state = ctx->include_stack;
99d1cf8a18a3 Implemented #else preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 623
diff changeset
  1029
    Conditional *cond = state->conditional_stack;
99d1cf8a18a3 Implemented #else preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 623
diff changeset
  1030
99d1cf8a18a3 Implemented #else preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 623
diff changeset
  1031
    if (!require_newline(state))
99d1cf8a18a3 Implemented #else preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 623
diff changeset
  1032
        fail(ctx, "Invalid #else directive");
99d1cf8a18a3 Implemented #else preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 623
diff changeset
  1033
    else if (cond == NULL)
99d1cf8a18a3 Implemented #else preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 623
diff changeset
  1034
        fail(ctx, "#else without #if");
99d1cf8a18a3 Implemented #else preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 623
diff changeset
  1035
    else if (cond->type == TOKEN_PP_ELSE)
99d1cf8a18a3 Implemented #else preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 623
diff changeset
  1036
        fail(ctx, "#else after #else");
99d1cf8a18a3 Implemented #else preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 623
diff changeset
  1037
    else
99d1cf8a18a3 Implemented #else preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 623
diff changeset
  1038
    {
99d1cf8a18a3 Implemented #else preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 623
diff changeset
  1039
        cond->type = TOKEN_PP_ELSE;
625
bfb4016d9404 Fixed #else preprocessor directive so it'll work with #elif.
Ryan C. Gordon <icculus@icculus.org>
parents: 624
diff changeset
  1040
        cond->skipping = cond->chosen;
bfb4016d9404 Fixed #else preprocessor directive so it'll work with #elif.
Ryan C. Gordon <icculus@icculus.org>
parents: 624
diff changeset
  1041
        if (!cond->chosen)
bfb4016d9404 Fixed #else preprocessor directive so it'll work with #elif.
Ryan C. Gordon <icculus@icculus.org>
parents: 624
diff changeset
  1042
            cond->chosen = 1;
624
99d1cf8a18a3 Implemented #else preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 623
diff changeset
  1043
    } // else
99d1cf8a18a3 Implemented #else preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 623
diff changeset
  1044
} // handle_pp_else
99d1cf8a18a3 Implemented #else preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 623
diff changeset
  1045
99d1cf8a18a3 Implemented #else preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 623
diff changeset
  1046
621
bbeb13128ddb Implemented #endif preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 620
diff changeset
  1047
static void handle_pp_endif(Context *ctx)
bbeb13128ddb Implemented #endif preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 620
diff changeset
  1048
{
bbeb13128ddb Implemented #endif preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 620
diff changeset
  1049
    IncludeState *state = ctx->include_stack;
bbeb13128ddb Implemented #endif preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 620
diff changeset
  1050
    Conditional *cond = state->conditional_stack;
bbeb13128ddb Implemented #endif preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 620
diff changeset
  1051
bbeb13128ddb Implemented #endif preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 620
diff changeset
  1052
    if (!require_newline(state))
bbeb13128ddb Implemented #endif preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 620
diff changeset
  1053
        fail(ctx, "Invalid #endif directive");
bbeb13128ddb Implemented #endif preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 620
diff changeset
  1054
    else if (cond == NULL)
bbeb13128ddb Implemented #endif preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 620
diff changeset
  1055
        fail(ctx, "Unmatched #endif");
bbeb13128ddb Implemented #endif preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 620
diff changeset
  1056
    else
bbeb13128ddb Implemented #endif preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 620
diff changeset
  1057
    {
bbeb13128ddb Implemented #endif preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 620
diff changeset
  1058
        state->conditional_stack = cond->next;  // pop it.
646
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
  1059
        put_conditional(ctx, cond);
621
bbeb13128ddb Implemented #endif preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 620
diff changeset
  1060
    } // else
bbeb13128ddb Implemented #endif preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 620
diff changeset
  1061
} // handle_pp_endif
bbeb13128ddb Implemented #endif preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 620
diff changeset
  1062
bbeb13128ddb Implemented #endif preprocessor directive.
Ryan C. Gordon <icculus@icculus.org>
parents: 620
diff changeset
  1063
644
ea8b50a4edaf Handle basic macro replacement in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 643
diff changeset
  1064
static int handle_pp_identifier(Context *ctx)
ea8b50a4edaf Handle basic macro replacement in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 643
diff changeset
  1065
{
ea8b50a4edaf Handle basic macro replacement in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 643
diff changeset
  1066
    IncludeState *state = ctx->include_stack;
ea8b50a4edaf Handle basic macro replacement in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 643
diff changeset
  1067
    const unsigned int len = ((unsigned int) (state->source-state->token));
ea8b50a4edaf Handle basic macro replacement in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 643
diff changeset
  1068
    char *sym = (char *) alloca(len+1);
ea8b50a4edaf Handle basic macro replacement in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 643
diff changeset
  1069
    memcpy(sym, state->token, len);
ea8b50a4edaf Handle basic macro replacement in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 643
diff changeset
  1070
    sym[len] = '\0';
ea8b50a4edaf Handle basic macro replacement in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 643
diff changeset
  1071
ea8b50a4edaf Handle basic macro replacement in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 643
diff changeset
  1072
    const char *def = find_define(ctx, sym);
ea8b50a4edaf Handle basic macro replacement in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 643
diff changeset
  1073
    if (def == NULL)
ea8b50a4edaf Handle basic macro replacement in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 643
diff changeset
  1074
        return 0;   // just send the token through unchanged.
ea8b50a4edaf Handle basic macro replacement in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 643
diff changeset
  1075
ea8b50a4edaf Handle basic macro replacement in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 643
diff changeset
  1076
    if (!push_source(ctx, state->filename, def, strlen(def), state->line, 0))
ea8b50a4edaf Handle basic macro replacement in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 643
diff changeset
  1077
    {
ea8b50a4edaf Handle basic macro replacement in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 643
diff changeset
  1078
        assert(ctx->out_of_memory);
ea8b50a4edaf Handle basic macro replacement in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 643
diff changeset
  1079
        return 0;
ea8b50a4edaf Handle basic macro replacement in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 643
diff changeset
  1080
    } // if
ea8b50a4edaf Handle basic macro replacement in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 643
diff changeset
  1081
ea8b50a4edaf Handle basic macro replacement in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 643
diff changeset
  1082
    return 1;
ea8b50a4edaf Handle basic macro replacement in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 643
diff changeset
  1083
} // handle_pp_identifier
ea8b50a4edaf Handle basic macro replacement in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 643
diff changeset
  1084
ea8b50a4edaf Handle basic macro replacement in the preprocessor.
Ryan C. Gordon <icculus@icculus.org>
parents: 643
diff changeset
  1085
620
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
  1086
static void unterminated_pp_condition(Context *ctx)
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
  1087
{
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
  1088
    IncludeState *state = ctx->include_stack;
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
  1089
    Conditional *cond = state->conditional_stack;
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
  1090
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
  1091
    // !!! FIXME: report the line number where the #if is, not the EOI.
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
  1092
    switch (cond->type)
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
  1093
    {
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
  1094
        case TOKEN_PP_IF: fail(ctx, "Unterminated #if"); break;
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
  1095
        case TOKEN_PP_IFDEF: fail(ctx, "Unterminated #ifdef"); break;
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
  1096
        case TOKEN_PP_IFNDEF: fail(ctx, "Unterminated #ifndef"); break;
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
  1097
        case TOKEN_PP_ELSE: fail(ctx, "Unterminated #else"); break;
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
  1098
        case TOKEN_PP_ELIF: fail(ctx, "Unterminated #elif"); break;
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
  1099
        default: assert(0 && "Shouldn't hit this case"); break;
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
  1100
    } // switch
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
  1101
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
  1102
    // pop this conditional, we'll report the next error next time...
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
  1103
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
  1104
    state->conditional_stack = cond->next;  // pop it.
646
60911555980a Use memory pools when allocating a bunch of short-lived structures.
Ryan C. Gordon <icculus@icculus.org>
parents: 645
diff changeset
  1105
    put_conditional(ctx, cond);
620
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
  1106
} // unterminated_pp_condition
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
  1107
1c4cf996004e Initial work on preprocessor conditionals.
Ryan C. Gordon <icculus@icculus.org>
parents: 618
diff changeset
  1108
561
0d2535cc5ac3 Added basic lexer debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 560
diff changeset
  1109
static inline const char *_preprocessor_nexttoken(Preprocessor *_ctx,
0d2535cc5ac3 Added basic lexer debug output.
Ryan C. Gordon <icculus@icculu