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