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