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