mojoshader_preprocessor.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 13 Feb 2009 02:23:20 -0500
changeset 604 c08b6b14994a
parent 601 bb1484be4e1b
child 605 197c9f2999b7
permissions -rw-r--r--
Patched to compile.
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
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
typedef struct Context
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
    int isfail;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
    int out_of_memory;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
    char failstr[128];
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
    IncludeState *include_stack;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
    DefineHash *define_hashtable[256];
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
    int pushedback;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
    const char *token;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
    unsigned int tokenlen;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
    MOJOSHADER_includeOpen open_callback;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
    MOJOSHADER_includeClose close_callback;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
    MOJOSHADER_malloc malloc;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
    MOJOSHADER_free free;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
    void *malloc_data;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
} Context;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
// Convenience functions for allocators...
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
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
    47
{
598
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 597
diff changeset
    48
    ctx->out_of_memory = 1;
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
} // out_of_memory
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
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
    52
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
    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
    54
    if (retval == NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
        out_of_memory(ctx);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
    return retval;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
} // Malloc
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
static inline void Free(Context *ctx, void *ptr)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
    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
    62
        ctx->free(ptr, ctx->malloc_data);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
} // Free
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 char *StrDup(Context *ctx, const char *str)
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
    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
    68
    if (retval != NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
        strcpy(retval, str);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
    return retval;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
} // StrDup
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
static 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
    74
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
    75
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
    ctx->isfail = 1;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    va_list ap;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
    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
    79
    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
    80
    va_end(ap);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
} // failf
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
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
    84
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
    failf(ctx, "%s", reason);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
} // fail
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
587
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
    89
#if DEBUG_TOKENIZER
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
    90
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
    91
                                  const unsigned int tokenlen,
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
    92
                                  const Token tokenval)
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
    93
{
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
    94
    printf("%s TOKEN: \"", subsystem);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
    95
    unsigned int i;
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
    96
    for (i = 0; i < tokenlen; i++)
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
    97
    {
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
    98
        if (token[i] == '\n')
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
    99
            printf("\\n");
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   100
        else
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   101
            printf("%c", token[i]);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   102
    } // for
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   103
    printf("\" (");
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   104
    switch (tokenval)
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   105
    {
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   106
        #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
   107
        TOKENCASE(TOKEN_UNKNOWN);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   108
        TOKENCASE(TOKEN_IDENTIFIER);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   109
        TOKENCASE(TOKEN_INT_LITERAL);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   110
        TOKENCASE(TOKEN_FLOAT_LITERAL);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   111
        TOKENCASE(TOKEN_STRING_LITERAL);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   112
        TOKENCASE(TOKEN_ADDASSIGN);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   113
        TOKENCASE(TOKEN_SUBASSIGN);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   114
        TOKENCASE(TOKEN_MULTASSIGN);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   115
        TOKENCASE(TOKEN_DIVASSIGN);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   116
        TOKENCASE(TOKEN_MODASSIGN);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   117
        TOKENCASE(TOKEN_XORASSIGN);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   118
        TOKENCASE(TOKEN_ANDASSIGN);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   119
        TOKENCASE(TOKEN_ORASSIGN);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   120
        TOKENCASE(TOKEN_INCREMENT);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   121
        TOKENCASE(TOKEN_DECREMENT);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   122
        TOKENCASE(TOKEN_RSHIFT);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   123
        TOKENCASE(TOKEN_LSHIFT);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   124
        TOKENCASE(TOKEN_ANDAND);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   125
        TOKENCASE(TOKEN_OROR);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   126
        TOKENCASE(TOKEN_LEQ);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   127
        TOKENCASE(TOKEN_GEQ);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   128
        TOKENCASE(TOKEN_EQL);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   129
        TOKENCASE(TOKEN_NEQ);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   130
        TOKENCASE(TOKEN_HASHHASH);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   131
        TOKENCASE(TOKEN_PP_INCLUDE);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   132
        TOKENCASE(TOKEN_PP_LINE);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   133
        TOKENCASE(TOKEN_PP_DEFINE);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   134
        TOKENCASE(TOKEN_PP_UNDEF);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   135
        TOKENCASE(TOKEN_PP_IF);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   136
        TOKENCASE(TOKEN_PP_IFDEF);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   137
        TOKENCASE(TOKEN_PP_IFNDEF);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   138
        TOKENCASE(TOKEN_PP_ELSE);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   139
        TOKENCASE(TOKEN_PP_ELIF);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   140
        TOKENCASE(TOKEN_PP_ENDIF);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   141
        TOKENCASE(TOKEN_PP_ERROR);
604
c08b6b14994a Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 601
diff changeset
   142
        TOKENCASE(TOKEN_INCOMPLETE_COMMENT);
c08b6b14994a Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 601
diff changeset
   143
        TOKENCASE(TOKEN_BAD_CHARS);
587
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   144
        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
   145
        TOKENCASE(TOKEN_PREPROCESSING_ERROR);
587
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   146
        #undef TOKENCASE
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   147
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   148
        case ((Token) '\n'):
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   149
            printf("'\\n'");
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   150
            break;
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   151
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   152
        default:
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   153
            assert(((int)tokenval) < 256);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   154
            printf("'%c'", (char) tokenval);
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   155
            break;
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   156
    } // switch
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   157
    printf(")\n");
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   158
} // MOJOSHADER_print_debug_token
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   159
#endif
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   160
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   161
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
// Preprocessor define hashtable stuff...
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
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
   165
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
    unsigned char retval = 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
    while (sym)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
        retval += *(sym++);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
    return retval;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
} // hash_define
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
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
   174
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
    char *identifier = NULL;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
    char *definition = NULL;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
    const unsigned char hash = hash_define(sym);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
    DefineHash *bucket = ctx->define_hashtable[hash];
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
    while (bucket)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
        if (strcmp(bucket->define.identifier, sym) == 0)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
        {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
            failf(ctx, "'%s' already defined", sym);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
            return 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
        } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
        bucket = bucket->next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
    } // while
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
    bucket = (DefineHash *) Malloc(ctx, sizeof (DefineHash));
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
    if (bucket == NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
        return 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
    identifier = (char *) Malloc(ctx, strlen(sym) + 1);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
    definition = (char *) Malloc(ctx, strlen(val) + 1);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
    if ((identifier == NULL) || (definition == NULL))
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
        Free(ctx, identifier);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
        Free(ctx, definition);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
        Free(ctx, bucket);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
        return 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
    } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
    strcpy(identifier, sym);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
    strcpy(definition, val);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
    bucket->define.identifier = identifier;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
    bucket->define.definition = definition;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
    bucket->next = ctx->define_hashtable[hash];
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
    ctx->define_hashtable[hash] = bucket;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
    return 1;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
} // add_define
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
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
   214
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
    const unsigned char hash = hash_define(sym);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
    DefineHash *bucket = ctx->define_hashtable[hash];
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
    DefineHash *prev = NULL;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
    while (bucket)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
        if (strcmp(bucket->define.identifier, sym) == 0)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
        {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
            if (prev == NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
                ctx->define_hashtable[hash] = bucket->next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
            else
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
                prev->next = bucket->next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
            Free(ctx, (void *) bucket->define.identifier);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
            Free(ctx, (void *) bucket->define.definition);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
            Free(ctx, bucket);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
            return 1;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
        } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
        prev = bucket;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
        bucket = bucket->next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
    } // while
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
    failf(ctx, "'%s' not defined", sym);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
    return 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
} // remove_define
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
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
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
   241
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
    const unsigned char hash = hash_define(sym);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
    DefineHash *bucket = ctx->define_hashtable[hash];
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
    while (bucket)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
        if (strcmp(bucket->define.identifier, sym) == 0)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
            return bucket->define.definition;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
        bucket = bucket->next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
    } // while
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
    return NULL;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
} // find_define
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
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
static void free_all_defines(Context *ctx)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
    int i;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
    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
   258
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
        DefineHash *bucket = ctx->define_hashtable[i];
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
        ctx->define_hashtable[i] = NULL;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
        while (bucket)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
        {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
            DefineHash *next = bucket->next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
            Free(ctx, (void *) bucket->define.identifier);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
            Free(ctx, (void *) bucket->define.definition);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
            Free(ctx, bucket);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
            bucket = next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
        } // while
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
    } // for
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
} // find_define
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
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
   274
                       unsigned int srclen, int included)
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
    IncludeState *state = (IncludeState *) Malloc(ctx, sizeof (IncludeState));
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
    if (state == NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
        return 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
    memset(state, '\0', sizeof (IncludeState));
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
560
225d579ae929 Handle NULL file names.
Ryan C. Gordon <icculus@icculus.org>
parents: 559
diff changeset
   281
    if (fname != NULL)
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
    {
560
225d579ae929 Handle NULL file names.
Ryan C. Gordon <icculus@icculus.org>
parents: 559
diff changeset
   283
        state->filename = StrDup(ctx, fname);
225d579ae929 Handle NULL file names.
Ryan C. Gordon <icculus@icculus.org>
parents: 559
diff changeset
   284
        if (state->filename == NULL)
225d579ae929 Handle NULL file names.
Ryan C. Gordon <icculus@icculus.org>
parents: 559
diff changeset
   285
        {
225d579ae929 Handle NULL file names.
Ryan C. Gordon <icculus@icculus.org>
parents: 559
diff changeset
   286
            Free(ctx, state);
225d579ae929 Handle NULL file names.
Ryan C. Gordon <icculus@icculus.org>
parents: 559
diff changeset
   287
            return 0;
225d579ae929 Handle NULL file names.
Ryan C. Gordon <icculus@icculus.org>
parents: 559
diff changeset
   288
        } // if
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
    } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
    state->included = included;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
    state->source_base = source;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   293
    state->source = source;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   294
    state->token = source;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
    state->bytes_left = srclen;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
    state->line = 1;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
    state->next = ctx->include_stack;
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
    ctx->include_stack = state;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
    return 1;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
} // push_source
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
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
static void pop_source(Context *ctx)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
    IncludeState *state = ctx->include_stack;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
    if (state == NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
        return;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
    if (state->included)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
        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
   314
                            ctx->free, ctx->malloc_data);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
    } // if
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
    ctx->include_stack = state->next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
    Free(ctx, state->filename);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
    Free(ctx, state);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   320
} // pop_source
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   321
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
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
   324
                            unsigned int sourcelen,
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
                            MOJOSHADER_includeOpen open_callback,
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   326
                            MOJOSHADER_includeClose close_callback,
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
                            const MOJOSHADER_preprocessorDefine **defines,
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   328
                            unsigned int define_count,
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
                            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
   330
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   331
    int okay = 1;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
    int i = 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
    // 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
   335
    assert(m != NULL);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
    assert(f != NULL);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   337
    assert(open_callback != NULL);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
    assert(close_callback != NULL);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
    Context *ctx = (Context *) m(sizeof (Context), d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
    if (ctx == NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   342
        return 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   343
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
    memset(ctx, '\0', sizeof (Context));
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   345
    ctx->malloc = m;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
    ctx->free = f;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
    ctx->malloc_data = d;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
    ctx->open_callback = open_callback;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   349
    ctx->close_callback = close_callback;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
    for (i = 0; i < define_count; i++)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   352
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
        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
   354
        {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   355
            okay = 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   356
            break;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   357
        } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
    } // for
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
    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
   361
        okay = 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
    if (!okay)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   364
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
        preprocessor_end((Preprocessor *) ctx);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   366
        return NULL;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   367
    } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   368
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   369
    return (Preprocessor *) ctx;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   370
} // preprocessor_start
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   371
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   372
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   373
void preprocessor_end(Preprocessor *_ctx)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   375
    Context *ctx = (Context *) _ctx;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   376
    if (ctx == NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   377
        return;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
    while (ctx->include_stack != NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
        pop_source(ctx);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   381
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   382
    free_all_defines(ctx);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   384
    Free(ctx, ctx);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
} // preprocessor_end
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   386
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
int preprocessor_outofmemory(Preprocessor *_ctx)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   389
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   390
    Context *ctx = (Context *) _ctx;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   391
    return ctx->out_of_memory;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   392
} // preprocessor_outofmemory
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
561
0d2535cc5ac3 Added basic lexer debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 560
diff changeset
   395
static inline const char *_preprocessor_nexttoken(Preprocessor *_ctx,
0d2535cc5ac3 Added basic lexer debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 560
diff changeset
   396
                                             unsigned int *_len, Token *_token)
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   397
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   398
    Context *ctx = (Context *) _ctx;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   399
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   400
    while (1)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   401
    {
598
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 597
diff changeset
   402
        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
   403
        {
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 597
diff changeset
   404
            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
   405
            *_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
   406
            *_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
   407
            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
   408
        } // if
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 597
diff changeset
   409
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   410
        IncludeState *state = ctx->include_stack;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   411
        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
   412
        {
d34ac77da171 Preprocessor should return TOKEN_EOI when we finish the whole include stack.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   413
            *_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
   414
            *_len = 0;
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   415
            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
   416
        } // if
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   417
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   418
        Token token = preprocessor_internal_lexer(state);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   419
        if (token == TOKEN_EOI)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   420
        {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   421
            assert(state->bytes_left == 0);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   422
            pop_source(ctx);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   423
            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
   424
        } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   425
601
bb1484be4e1b Reworked and documented preprocessor tokens.
Ryan C. Gordon <icculus@icculus.org>
parents: 598
diff changeset
   426
        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
   427
        {
c0718e4595e5 Set an error in the preprocessor if there's an incomplete multiline comment.
Ryan C. Gordon <icculus@icculus.org>
parents: 564
diff changeset
   428
            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
   429
            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
   430
        } // 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
   431
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   432
        *_token = token;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   433
        *_len = (unsigned int) (state->source - state->token);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   434
        return state->token;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   435
    } // while
561
0d2535cc5ac3 Added basic lexer debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 560
diff changeset
   436
0d2535cc5ac3 Added basic lexer debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 560
diff changeset
   437
    assert(0 && "shouldn't hit this code");
0d2535cc5ac3 Added basic lexer debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 560
diff changeset
   438
    *_token = TOKEN_UNKNOWN;
0d2535cc5ac3 Added basic lexer debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 560
diff changeset
   439
    *_len = 0;
0d2535cc5ac3 Added basic lexer debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 560
diff changeset
   440
    return NULL;
0d2535cc5ac3 Added basic lexer debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 560
diff changeset
   441
} // _preprocessor_nexttoken
0d2535cc5ac3 Added basic lexer debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 560
diff changeset
   442
0d2535cc5ac3 Added basic lexer debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 560
diff changeset
   443
0d2535cc5ac3 Added basic lexer debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 560
diff changeset
   444
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
   445
                                   Token *token)
0d2535cc5ac3 Added basic lexer debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 560
diff changeset
   446
{
0d2535cc5ac3 Added basic lexer debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 560
diff changeset
   447
    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
   448
    print_debug_token(retval, *len, *token);
561
0d2535cc5ac3 Added basic lexer debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 560
diff changeset
   449
    return retval;
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   450
} // preprocessor_nexttoken
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   451
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   452
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   453
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
   454
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   455
    Context *ctx = (Context *) _ctx;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   456
    if (ctx->include_stack == NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   457
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   458
        *pos = 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   459
        return NULL;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   460
    } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   461
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   462
    *pos = ctx->include_stack->line;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   463
    return ctx->include_stack->filename;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   464
} // preprocessor_sourcepos
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
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   467
// public API...
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   468
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   469
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
   470
    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
   471
};
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
#define BUFFER_LEN (64 * 1024)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   474
typedef struct BufferList
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
    char buffer[BUFFER_LEN];
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   477
    size_t bytes;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   478
    struct BufferList *next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   479
} BufferList;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   480
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   481
typedef struct Buffer
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   482
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   483
    size_t total_bytes;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   484
    BufferList head;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   485
    BufferList *tail;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   486
} Buffer;
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
static void buffer_init(Buffer *buffer)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   489
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   490
    buffer->total_bytes = 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   491
    buffer->head.bytes = 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   492
    buffer->head.next = NULL;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   493
    buffer->tail = &buffer->head;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   494
} // buffer_init
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   495
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   496
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   497
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
   498
                         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
   499
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   500
    buffer->total_bytes += len;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   501
    while (len > 0)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   502
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   503
        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
   504
        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
   505
        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
   506
        len -= cpy;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   507
        data += cpy;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   508
        buffer->tail->bytes += cpy;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   509
        assert(buffer->tail->bytes <= BUFFER_LEN);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   510
        if (buffer->tail->bytes == BUFFER_LEN)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   511
        {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   512
            BufferList *item = (BufferList *) m(sizeof (BufferList), d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   513
            if (item == NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   514
                return 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   515
            item->bytes = 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   516
            item->next = NULL;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   517
            buffer->tail->next = item;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   518
            buffer->tail = item;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   519
        } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   520
    } // while
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   521
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   522
    return 1;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   523
} // add_to_buffer
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   524
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   525
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   526
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
   527
                         MOJOSHADER_malloc m, void *d)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   528
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   529
    static char spaces[4] = { ' ', ' ', ' ', ' ' };
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   530
    if (newline)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   531
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   532
        while (n--)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   533
        {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   534
            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
   535
                return 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   536
        } // while
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   537
    } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   538
    else
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   539
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   540
        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
   541
            return 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   542
    } // else
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   543
    return 1;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   544
} // indent_buffer
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   545
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   546
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   547
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
   548
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   549
    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
   550
    if (retval == NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   551
        return NULL;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   552
    BufferList *item = &buffer->head;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   553
    char *ptr = retval;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   554
    while (item != NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   555
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   556
        BufferList *next = item->next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   557
        memcpy(ptr, item->buffer, item->bytes);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   558
        ptr += item->bytes;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   559
        item = next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   560
    } // while
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   561
    *ptr = '\0';
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   562
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   563
    assert(ptr == (retval + buffer->total_bytes));
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   564
    return retval;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   565
} // flatten_buffer
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   566
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   567
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   568
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
   569
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   570
    // 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
   571
    BufferList *item = buffer->head.next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   572
    while (item != NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   573
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   574
        BufferList *next = item->next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   575
        f(item, d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   576
        item = next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   577
    } // while
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   578
    buffer_init(buffer);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   579
} // free_buffer
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   580
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   581
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   582
// !!! FIXME: cut and paste.
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   583
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
   584
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   585
    while (item != NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   586
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   587
        ErrorList *next = item->next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   588
        f((void *) item->error.error, d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   589
        f((void *) item->error.filename, d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   590
        f(item, d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   591
        item = next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   592
    } // while
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   593
} // free_error_list
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   594
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   595
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   596
// !!! FIXME: cut and paste.
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   597
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
   598
                         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
   599
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   600
    int total = 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   601
    MOJOSHADER_error *retval = (MOJOSHADER_error *)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   602
                                m(sizeof (MOJOSHADER_error) * count, d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   603
    if (retval == NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   604
        return NULL;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   605
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   606
    ErrorList *item = *errors;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   607
    while (item != NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   608
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   609
        ErrorList *next = item->next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   610
        // reuse the string allocations
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   611
        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
   612
        f(item, d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   613
        item = next;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   614
        total++;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   615
    } // while
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   616
    *errors = NULL;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   617
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   618
    assert(total == count);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   619
    return retval;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   620
} // build_errors
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   621
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   622
578
6c8f73c845e7 Allow app to specify a base filename for error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 568
diff changeset
   623
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
   624
                             const char *source, unsigned int sourcelen,
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   625
                             const MOJOSHADER_preprocessorDefine **defines,
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   626
                             unsigned int define_count,
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   627
                             MOJOSHADER_includeOpen include_open,
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   628
                             MOJOSHADER_includeClose include_close,
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   629
                             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
   630
{
564
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   631
    #ifdef _WINDOWS
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   632
    static const char endline[] = { '\r', '\n' };
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   633
    #else
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   634
    static const char endline[] = { '\n' };
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   635
    #endif
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   636
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   637
    ErrorList *errors = NULL;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   638
    int error_count = 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   639
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   640
    if (m == NULL) m = MOJOSHADER_internal_malloc;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   641
    if (f == NULL) f = MOJOSHADER_internal_free;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   642
582
637dcfff28d8 Added another FIXME.
Ryan C. Gordon <icculus@icculus.org>
parents: 578
diff changeset
   643
// !!! FIXME
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   644
include_open = (MOJOSHADER_includeOpen) 0x1;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   645
include_close = (MOJOSHADER_includeClose) 0x1;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   646
578
6c8f73c845e7 Allow app to specify a base filename for error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 568
diff changeset
   647
    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
   648
                                          include_open, include_close,
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   649
                                          defines, define_count, m, f, d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   650
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   651
    if (pp == NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   652
        return &out_of_mem_data_preprocessor;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   653
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   654
    Token token = TOKEN_UNKNOWN;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   655
    const char *tokstr = NULL;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   656
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   657
    Buffer buffer;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   658
    buffer_init(&buffer);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   659
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   660
    int nl = 1;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   661
    int indent = 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   662
    unsigned int len = 0;
564
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   663
    int out_of_memory = 0;
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   664
    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
   665
    {
564
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   666
        int isnewline = 0;
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   667
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   668
        assert(token != TOKEN_EOI);
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   669
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   670
        if (!out_of_memory)
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   671
            out_of_memory = preprocessor_outofmemory(pp);
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   672
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   673
        // Microsoft's preprocessor is weird.
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   674
        // 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
   675
        //  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
   676
        //  be mostly readable, instead of a stream of tokens.
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   677
        if (token == ((Token) '\n'))
583
9c966b751fd6 Fixed preprocessor indent and endline output.
Ryan C. Gordon <icculus@icculus.org>
parents: 582
diff changeset
   678
            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
   679
564
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   680
        else if ( (token == ((Token) '}')) || (token == ((Token) ';')) )
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   681
        {
564
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   682
            if (!out_of_memory)
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   683
            {
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   684
                if ( (token == ((Token) '}')) && (indent > 0) )
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   685
                    indent--;
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   686
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   687
                out_of_memory =
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   688
                    (!indent_buffer(&buffer, indent, nl, m, d)) ||
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   689
                    (!add_to_buffer(&buffer, tokstr, len, m, d)) ||
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   690
                    (!add_to_buffer(&buffer, endline, sizeof (endline), m, d));
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   691
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   692
                isnewline = 1;
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   693
            } // if
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   694
        } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   695
564
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   696
        else if (token == ((Token) '{'))
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   697
        {
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   698
            if (!out_of_memory)
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   699
            {
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   700
                out_of_memory =
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   701
                    (!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
   702
                    (!indent_buffer(&buffer, indent, 1, m, d)) ||
564
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   703
                    (!add_to_buffer(&buffer, "{", 1, m, d)) ||
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   704
                    (!add_to_buffer(&buffer,endline,sizeof (endline),m,d));
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   705
                indent++;
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   706
                isnewline = 1;
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   707
            } // if
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   708
        } // else if
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   709
598
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 597
diff changeset
   710
        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
   711
        {
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 597
diff changeset
   712
            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
   713
            {
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 597
diff changeset
   714
                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
   715
                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
   716
                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
   717
                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
   718
                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
   719
                {
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 597
diff changeset
   720
                    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
   721
                    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
   722
                        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
   723
                } // if
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 597
diff changeset
   724
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 597
diff changeset
   725
                // !!! 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
   726
                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
   727
                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
   728
                    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
   729
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 597
diff changeset
   730
                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
   731
                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
   732
                {
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 597
diff changeset
   733
                    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
   734
                    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
   735
                    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
   736
                } // if
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 597
diff changeset
   737
                else
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 597
diff changeset
   738
                {
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 597
diff changeset
   739
                    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
   740
                    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
   741
                    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
   742
                    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
   743
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 597
diff changeset
   744
                    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
   745
                    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
   746
                    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
   747
                    {
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 597
diff changeset
   748
                        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
   749
                        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
   750
                    } // while
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 597
diff changeset
   751
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 597
diff changeset
   752
                    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
   753
                        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
   754
                    else
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 597
diff changeset
   755
                        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
   756
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 597
diff changeset
   757
                    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
   758
                } // else
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 597
diff changeset
   759
            } // if
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 597
diff changeset
   760
        } // 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
   761
564
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   762
        else
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   763
        {
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   764
            if (!out_of_memory)
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   765
            {
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   766
                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
   767
                                (!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
   768
564
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   769
            } // if
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   770
        } // else
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   771
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   772
        nl = isnewline;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   773
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   774
        if (out_of_memory)
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
            preprocessor_end(pp);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   777
            free_buffer(&buffer, f, d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   778
            free_error_list(errors, f, d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   779
            return &out_of_mem_data_preprocessor;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   780
        } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   781
    } // while
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   782
    
564
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   783
    assert((token == TOKEN_EOI) || (out_of_memory));
c669568326fb Reworked preprocessor newline management.
Ryan C. Gordon <icculus@icculus.org>
parents: 561
diff changeset
   784
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   785
    preprocessor_end(pp);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   786
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   787
    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
   788
    char *output = flatten_buffer(&buffer, m, d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   789
    free_buffer(&buffer, f, d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   790
    if (output == NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   791
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   792
        free_error_list(errors, f, d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   793
        return &out_of_mem_data_preprocessor;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   794
    } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   795
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   796
    MOJOSHADER_preprocessData *retval = (MOJOSHADER_preprocessData *)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   797
                                    m(sizeof (MOJOSHADER_preprocessData), d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   798
    if (retval == NULL)
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
        free_error_list(errors, f, d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   801
        f(output, d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   802
        return &out_of_mem_data_preprocessor;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   803
    } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   804
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   805
    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
   806
    if (retval->errors == NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   807
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   808
        free_error_list(errors, f, d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   809
        f(retval, d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   810
        f(output, d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   811
        return &out_of_mem_data_preprocessor;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   812
    } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   813
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   814
    retval->error_count = error_count;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   815
    retval->output = output;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   816
    retval->output_len = total_bytes;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   817
    retval->malloc = m;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   818
    retval->free = f;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   819
    retval->malloc_data = d;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   820
    return retval;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   821
} // MOJOSHADER_preprocess
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   822
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
void MOJOSHADER_freePreprocessData(const MOJOSHADER_preprocessData *_data)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   825
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   826
    MOJOSHADER_preprocessData *data = (MOJOSHADER_preprocessData *) _data;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   827
    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
   828
        return;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   829
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   830
    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
   831
    void *d = data->malloc_data;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   832
    int i;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   833
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   834
    if (data->output != NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   835
        f((void *) data->output, d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   836
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   837
    if (data->errors != NULL)
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
        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
   840
        {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   841
            if (data->errors[i].error != NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   842
                f((void *) data->errors[i].error, d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   843
            if (data->errors[i].filename != NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   844
                f((void *) data->errors[i].filename, d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   845
        } // for
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   846
        f(data->errors, d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   847
    } // if
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
    f(data, d);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   850
} // MOJOSHADER_freePreprocessData
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   851
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   852
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   853
// end of mojoshader_preprocessor.c ...
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   854