mojoshader_compiler.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 19 Feb 2010 02:25:51 -0500
changeset 835 743e14b386f3
parent 827 2f955ce29b7b
child 836 d975fa785f1e
permissions -rw-r--r--
Added boilerplate comment to start of file.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
835
743e14b386f3 Added boilerplate comment to start of file.
Ryan C. Gordon <icculus@icculus.org>
parents: 827
diff changeset
     1
/**
743e14b386f3 Added boilerplate comment to start of file.
Ryan C. Gordon <icculus@icculus.org>
parents: 827
diff changeset
     2
 * MojoShader; generate shader programs from bytecode of compiled
743e14b386f3 Added boilerplate comment to start of file.
Ryan C. Gordon <icculus@icculus.org>
parents: 827
diff changeset
     3
 *  Direct3D shaders.
743e14b386f3 Added boilerplate comment to start of file.
Ryan C. Gordon <icculus@icculus.org>
parents: 827
diff changeset
     4
 *
743e14b386f3 Added boilerplate comment to start of file.
Ryan C. Gordon <icculus@icculus.org>
parents: 827
diff changeset
     5
 * Please see the file LICENSE.txt in the source's root directory.
743e14b386f3 Added boilerplate comment to start of file.
Ryan C. Gordon <icculus@icculus.org>
parents: 827
diff changeset
     6
 *
743e14b386f3 Added boilerplate comment to start of file.
Ryan C. Gordon <icculus@icculus.org>
parents: 827
diff changeset
     7
 *  This file written by Ryan C. Gordon.
743e14b386f3 Added boilerplate comment to start of file.
Ryan C. Gordon <icculus@icculus.org>
parents: 827
diff changeset
     8
 */
743e14b386f3 Added boilerplate comment to start of file.
Ryan C. Gordon <icculus@icculus.org>
parents: 827
diff changeset
     9
709
6fbd0e20b40f Removed some ANSI C things from the HLSL grammar that shaders don't do.
Ryan C. Gordon <icculus@icculus.org>
parents: 706
diff changeset
    10
#define __MOJOSHADER_INTERNAL__ 1
6fbd0e20b40f Removed some ANSI C things from the HLSL grammar that shaders don't do.
Ryan C. Gordon <icculus@icculus.org>
parents: 706
diff changeset
    11
#include "mojoshader_internal.h"
706
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
716
94a804b54078 Cleaned up HLSL parser tracing.
Ryan C. Gordon <icculus@icculus.org>
parents: 714
diff changeset
    13
#if DEBUG_COMPILER_PARSER
94a804b54078 Cleaned up HLSL parser tracing.
Ryan C. Gordon <icculus@icculus.org>
parents: 714
diff changeset
    14
#define LEMON_SUPPORT_TRACING 1
94a804b54078 Cleaned up HLSL parser tracing.
Ryan C. Gordon <icculus@icculus.org>
parents: 714
diff changeset
    15
#endif
94a804b54078 Cleaned up HLSL parser tracing.
Ryan C. Gordon <icculus@icculus.org>
parents: 714
diff changeset
    16
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    17
typedef union TokenData
778
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
    18
{
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    19
    int64 i64;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    20
    double dbl;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    21
    const char *string;
778
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
    22
} TokenData;
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
    23
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    24
typedef struct StringBucket
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    25
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    26
    char *string;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    27
    struct StringBucket *next;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    28
} StringBucket;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    29
778
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
    30
typedef struct Context
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
    31
{
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    32
    int isfail;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    33
    int out_of_memory;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    34
    MOJOSHADER_malloc malloc;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    35
    MOJOSHADER_free free;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    36
    void *malloc_data;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    37
    int error_count;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    38
    ErrorList *errors;
778
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
    39
    Preprocessor *preprocessor;
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    40
    StringBucket *string_hashtable[256];
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    41
    const char *usertypes[512];  // !!! FIXME: dynamic allocation
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    42
    int usertype_count;
778
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
    43
} Context;
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
    44
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
    45
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    46
// Convenience functions for allocators...
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    47
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    48
static inline void out_of_memory(Context *ctx)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    49
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    50
    ctx->isfail = ctx->out_of_memory = 1;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    51
} // out_of_memory
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    52
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    53
static inline void *Malloc(Context *ctx, const size_t len)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    54
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    55
    void *retval = ctx->malloc((int) len, ctx->malloc_data);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    56
    if (retval == NULL)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    57
        out_of_memory(ctx);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    58
    return retval;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    59
} // Malloc
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    60
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    61
static inline char *StrDup(Context *ctx, const char *str)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    62
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    63
    char *retval = (char *) Malloc(ctx, strlen(str) + 1);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    64
    if (retval != NULL)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    65
        strcpy(retval, str);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    66
    return retval;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    67
} // StrDup
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    68
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    69
static inline void Free(Context *ctx, void *ptr)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    70
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    71
    if (ptr != NULL)  // check for NULL in case of dumb free() impl.
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    72
        ctx->free(ptr, ctx->malloc_data);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    73
} // Free
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    74
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    75
typedef enum Operator
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    76
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    77
    OP_START_RANGE_UNARY,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    78
    OP_POSTINCREMENT,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    79
    OP_POSTDECREMENT,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    80
    OP_PREINCREMENT,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    81
    OP_PREDECREMENT,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    82
    OP_NEGATE,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    83
    OP_COMPLEMENT,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    84
    OP_NOT,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    85
    OP_END_RANGE_UNARY,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    86
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    87
    OP_START_RANGE_BINARY,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    88
    OP_DEREF_ARRAY,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    89
    OP_CALLFUNC,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    90
    OP_DEREF_STRUCT,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    91
    OP_COMMA,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    92
    OP_MULTIPLY,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    93
    OP_DIVIDE,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    94
    OP_MODULO,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    95
    OP_ADD,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    96
    OP_SUBTRACT,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    97
    OP_LSHIFT,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    98
    OP_RSHIFT,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
    99
    OP_LESSTHAN,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   100
    OP_GREATERTHAN,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   101
    OP_LESSTHANOREQUAL,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   102
    OP_GREATERTHANOREQUAL,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   103
    OP_EQUAL,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   104
    OP_NOTEQUAL,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   105
    OP_BINARYAND,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   106
    OP_BINARYXOR,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   107
    OP_BINARYOR,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   108
    OP_LOGICALAND,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   109
    OP_LOGICALOR,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   110
    OP_ASSIGN,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   111
    OP_MULASSIGN,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   112
    OP_DIVASSIGN,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   113
    OP_MODASSIGN,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   114
    OP_ADDASSIGN,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   115
    OP_SUBASSIGN,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   116
    OP_LSHIFTASSIGN,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   117
    OP_RSHIFTASSIGN,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   118
    OP_ANDASSIGN,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   119
    OP_XORASSIGN,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   120
    OP_ORASSIGN,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   121
    OP_END_RANGE_BINARY,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   122
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   123
    OP_START_RANGE_TERNARY,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   124
    OP_CONDITIONAL,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   125
    OP_END_RANGE_TERNARY,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   126
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   127
    OP_START_RANGE_DATA,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   128
    OP_IDENTIFIER,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   129
    OP_INT_LITERAL,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   130
    OP_FLOAT_LITERAL,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   131
    OP_STRING_LITERAL,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   132
    OP_END_RANGE_DATA,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   133
} Operator;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   134
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   135
static inline int operator_is_unary(const Operator op)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   136
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   137
    return ((op > OP_START_RANGE_UNARY) && (op < OP_END_RANGE_UNARY));
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   138
} // operator_is_unary
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   139
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   140
static inline int operator_is_binary(const Operator op)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   141
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   142
    return ((op > OP_START_RANGE_BINARY) && (op < OP_END_RANGE_BINARY));
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   143
} // operator_is_binary
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   144
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   145
static inline int operator_is_ternary(const Operator op)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   146
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   147
    return ((op > OP_START_RANGE_TERNARY) && (op < OP_END_RANGE_TERNARY));
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   148
} // operator_is_ternary
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   149
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   150
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   151
typedef struct Expression
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   152
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   153
    Operator op;  // operator
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   154
} Expression;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   155
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   156
#define NEW_EXPR(cls) \
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   157
    cls *retval = Malloc(ctx, sizeof (cls)); \
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   158
    if (retval == NULL) { return NULL; }
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   159
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   160
typedef struct ExpressionUnary
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   161
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   162
    Operator op;  // operator
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   163
    Expression *operand;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   164
} ExpressionUnary;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   165
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   166
typedef struct ExpressionBinary
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   167
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   168
    Operator op;  // operator
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   169
    Expression *left;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   170
    Expression *right;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   171
} ExpressionBinary;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   172
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   173
typedef struct ExpressionTernary
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   174
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   175
    Operator op;  // operator
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   176
    Expression *left;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   177
    Expression *center;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   178
    Expression *right;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   179
} ExpressionTernary;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   180
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   181
typedef struct ExpressionIdentifier
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   182
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   183
    Operator op;  // Always OP_IDENTIFIER
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   184
    const char *identifier;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   185
} ExpressionIdentifier;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   186
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   187
typedef struct ExpressionIntLiteral
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   188
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   189
    Operator op;  // Always OP_INT_LITERAL
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   190
    int64 value;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   191
} ExpressionIntLiteral;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   192
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   193
typedef struct ExpressionFloatLiteral
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   194
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   195
    Operator op;  // Always OP_FLOAT_LITERAL
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   196
    double value;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   197
} ExpressionFloatLiteral;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   198
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   199
typedef struct ExpressionStringLiteral
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   200
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   201
    Operator op;  // Always OP_STRING_LITERAL
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   202
    const char *string;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   203
} ExpressionStringLiteral;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   204
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   205
static Expression *new_unary_expr(Context *ctx, const Operator op,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   206
                                  Expression *operand)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   207
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   208
    NEW_EXPR(ExpressionUnary);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   209
    assert(operator_is_unary(op));
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   210
    retval->op = op;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   211
    retval->operand = operand;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   212
    return (Expression *) retval;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   213
} // new_unary_expr
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   214
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   215
static Expression *new_binary_expr(Context *ctx, const Operator op,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   216
                                   Expression *left, Expression *right)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   217
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   218
    NEW_EXPR(ExpressionBinary);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   219
    assert(operator_is_binary(op));
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   220
    retval->op = op;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   221
    retval->left = left;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   222
    retval->right = right;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   223
    return (Expression *) retval;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   224
} // new_binary_expr
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   225
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   226
static Expression *new_ternary_expr(Context *ctx, const Operator op,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   227
                                    Expression *left, Expression *center,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   228
                                    Expression *right)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   229
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   230
    NEW_EXPR(ExpressionTernary);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   231
    assert(operator_is_ternary(op));
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   232
    retval->op = op;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   233
    retval->left = left;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   234
    retval->center = center;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   235
    retval->right = right;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   236
    return (Expression *) retval;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   237
} // new_ternary_expr
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   238
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   239
static Expression *new_identifier_expr(Context *ctx, const char *string)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   240
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   241
    NEW_EXPR(ExpressionIdentifier);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   242
    retval->op = OP_IDENTIFIER;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   243
    retval->identifier = string;  // cached; don't copy string.
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   244
    return (Expression *) retval;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   245
} // new_identifier_expr
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   246
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   247
static inline int64 strtoi64(const char *str, unsigned int len)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   248
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   249
    int64 retval = 0;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   250
    int64 mult = 1;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   251
    int i = 0;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   252
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   253
    while ((len) && (*str == ' '))
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   254
    {
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   255
        str++;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   256
        len--;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   257
    } // while
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   258
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   259
    if ((len) && (*str == '-'))
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   260
    {
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   261
        mult = -1;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   262
        str++;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   263
        len--;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   264
    } // if
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   265
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   266
    while (i < len)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   267
    {
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   268
        const char ch = str[i];
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   269
        if ((ch < '0') || (ch > '9'))
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   270
            break;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   271
        i++;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   272
    } // while
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   273
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   274
    while (--i >= 0)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   275
    {
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   276
        const char ch = str[i];
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   277
        retval += ((int64) (ch - '0')) * mult;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   278
        mult *= 10;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   279
    } // while
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   280
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   281
    return retval;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   282
} // strtoi64
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   283
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   284
static Expression *new_literal_int_expr(Context *ctx, const int64 value)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   285
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   286
    NEW_EXPR(ExpressionIntLiteral);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   287
    retval->op = OP_INT_LITERAL;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   288
    retval->value = value;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   289
    return (Expression *) retval;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   290
} // new_literal_int_expr
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   291
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   292
static inline double strtodouble(const char *_str, unsigned int len)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   293
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   294
    // !!! FIXME: laziness prevails.
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   295
    char *str = (char *) alloca(len+1);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   296
    memcpy(str, _str, len);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   297
    str[len] = '\0';
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   298
    return strtod(str, NULL);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   299
} // strtodouble
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   300
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   301
static Expression *new_literal_float_expr(Context *ctx, const double dbl)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   302
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   303
    NEW_EXPR(ExpressionFloatLiteral);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   304
    retval->op = OP_FLOAT_LITERAL;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   305
    retval->value = dbl;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   306
    return (Expression *) retval;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   307
} // new_literal_float_expr
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   308
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   309
static Expression *new_literal_string_expr(Context *ctx, const char *string)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   310
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   311
    NEW_EXPR(ExpressionStringLiteral);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   312
    retval->op = OP_STRING_LITERAL;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   313
    retval->string = string;  // cached; don't copy string.
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   314
    return (Expression *) retval;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   315
} // new_string_literal_expr
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   316
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   317
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   318
static void add_usertype(Context *ctx, const char *sym)
778
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   319
{
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   320
    // !!! FIXME: error if this is a reserved keyword.
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   321
    // !!! FIXME: dynamic allocation
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   322
    assert(ctx->usertype_count < STATICARRAYLEN(ctx->usertypes));
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   323
    ctx->usertypes[ctx->usertype_count++] = sym;
778
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   324
    ctx->usertype_count++;
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   325
} // add_usertype
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   326
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   327
static int is_usertype(const Context *ctx, const char *token,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   328
                       const unsigned int tokenlen)
778
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   329
{
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   330
    // !!! FIXME: dynamic allocation
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   331
    // !!! FIXME: should probably redesign this anyhow.
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   332
    int i;
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   333
    for (i = 0; i < ctx->usertype_count; i++)
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   334
    {
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   335
        const char *type = ctx->usertypes[i];
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   336
        if (strncmp(type, token, tokenlen) == 0)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   337
            return type[tokenlen] == '\0';
778
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   338
    } // for
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   339
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   340
    return 0;
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   341
} // is_usertype
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   342
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   343
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   344
// This is where the actual parsing happens. It's Lemon-generated!
709
6fbd0e20b40f Removed some ANSI C things from the HLSL grammar that shaders don't do.
Ryan C. Gordon <icculus@icculus.org>
parents: 706
diff changeset
   345
#define __MOJOSHADER_HLSL_COMPILER__ 1
714
e8ed66bfff18 Make parser generate a header, to avoid external project build confusion.
Ryan C. Gordon <icculus@icculus.org>
parents: 713
diff changeset
   346
#include "mojoshader_parser_hlsl.h"
706
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   348
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   349
static void free_expr(Context *ctx, Expression *expr)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   350
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   351
    if (operator_is_unary(expr->op))
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   352
    {
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   353
        const ExpressionUnary *unary = (const ExpressionUnary *) expr;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   354
        free_expr(ctx, unary->operand);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   355
    } // if
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   356
    else if (operator_is_binary(expr->op))
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   357
    {
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   358
        const ExpressionBinary *binary = (const ExpressionBinary *) expr;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   359
        free_expr(ctx, binary->left);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   360
        free_expr(ctx, binary->right);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   361
    } // else if
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   362
    else if (operator_is_ternary(expr->op))
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   363
    {
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   364
        const ExpressionTernary *ternary = (const ExpressionTernary *) expr;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   365
        free_expr(ctx, ternary->left);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   366
        free_expr(ctx, ternary->center);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   367
        free_expr(ctx, ternary->right);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   368
    } // else if
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   369
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   370
    // don't need to free extra fields in other types at the moment.
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   371
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   372
    Free(ctx, expr);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   373
} // free_expr
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   374
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   375
// !!! FIXME: sort of cut-and-paste from the preprocessor...
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   376
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   377
// this is djb's xor hashing function.
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   378
static inline uint32 hash_string_djbxor(const char *str, unsigned int len)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   379
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   380
    register uint32 hash = 5381;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   381
    while (len--)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   382
        hash = ((hash << 5) + hash) ^ *(str++);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   383
    return hash;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   384
} // hash_string_djbxor
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   385
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   386
static inline uint8 hash_string(const char *str, const unsigned int len)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   387
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   388
    return (uint8) hash_string_djbxor(str, len);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   389
} // hash_string
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   390
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   391
static const char *cache_string(Context *ctx, const char *str,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   392
                                const unsigned int len)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   393
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   394
    const uint8 hash = hash_string(str, len);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   395
    StringBucket *bucket = ctx->string_hashtable[hash];
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   396
    StringBucket *prev = NULL;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   397
    while (bucket)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   398
    {
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   399
        const char *bstr = bucket->string;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   400
        if ((strncmp(bstr, str, len) == 0) && (bstr[len] == 0))
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   401
        {
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   402
            // Matched! Move this to the front of the list.
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   403
            if (prev != NULL)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   404
            {
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   405
                assert(prev->next == bucket);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   406
                prev->next = bucket->next;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   407
                bucket->next = ctx->string_hashtable[hash];
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   408
                ctx->string_hashtable[hash] = bucket;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   409
            } // if
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   410
            return bstr; // already cached
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   411
        } // if
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   412
        prev = bucket;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   413
        bucket = bucket->next;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   414
    } // while
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   415
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   416
    // no match, add to the table.
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   417
    bucket = (StringBucket *) Malloc(ctx, sizeof (StringBucket));
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   418
    if (bucket == NULL)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   419
        return NULL;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   420
    bucket->string = (char *) Malloc(ctx, len + 1);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   421
    if (bucket->string == NULL)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   422
    {
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   423
        Free(ctx, bucket);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   424
        return NULL;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   425
    } // if
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   426
    memcpy(bucket->string, str, len);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   427
    bucket->string[len] = '\0';
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   428
    bucket->next = ctx->string_hashtable[hash];
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   429
    ctx->string_hashtable[hash] = bucket;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   430
    return bucket->string;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   431
} // cache_string
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   432
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   433
static void free_string_cache(Context *ctx)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   434
{
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   435
    size_t i;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   436
    for (i = 0; i < STATICARRAYLEN(ctx->string_hashtable); i++)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   437
    {
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   438
        StringBucket *bucket = ctx->string_hashtable[i];
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   439
        ctx->string_hashtable[i] = NULL;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   440
        while (bucket)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   441
        {
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   442
            StringBucket *next = bucket->next;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   443
            Free(ctx, bucket->string);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   444
            Free(ctx, bucket);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   445
            bucket = next;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   446
        } // while
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   447
    } // for
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   448
} // free_string_cache
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   449
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   450
730
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   451
// This does not check correctness (POSITIONT993842 passes, etc).
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   452
static int is_semantic(const Context *ctx, const char *token,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   453
                       const unsigned int tokenlen)
730
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   454
{
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   455
    static const char *names[] = {
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   456
        "BINORMAL", "BLENDINDICES", "BLENDWEIGHT",
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   457
        "COLOR", "NORMAL", "POSITION", "POSITIONT", "PSIZE", "TANGENT",
792
e0c5fc7d71c3 Forgot to add DEPTH to the list of valid semantics.
Ryan C. Gordon <icculus@icculus.org>
parents: 787
diff changeset
   458
        "TEXCOORD", "FOG", "TESSFACTOR", "TEXCOORD", "VFACE", "VPOS",
e0c5fc7d71c3 Forgot to add DEPTH to the list of valid semantics.
Ryan C. Gordon <icculus@icculus.org>
parents: 787
diff changeset
   459
        "DEPTH", NULL
730
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   460
    };
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   461
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   462
    // !!! FIXME: DX10 has SV_* ("System Value Semantics").
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   463
    const char **i;
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   464
    for (i = names; *i; i++)
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   465
    {
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   466
        const char *name = *i;
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   467
        const size_t namelen = strlen(name);
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   468
        if (tokenlen < namelen)
730
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   469
            continue;
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   470
        else if (memcmp(token, name, namelen) != 0)
730
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   471
            continue;
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   472
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   473
        for (name += namelen; *name; name++)
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   474
        {
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   475
            if ((*name < '0') || (*name > '9'))
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   476
                break;
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   477
        } // for
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   478
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   479
        if (*name == '\0')
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   480
            return 1;
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   481
    } // for
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   482
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   483
    return 0;
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   484
} // is_semantic
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   485
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   486
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   487
static int convert_to_lemon_token(const Context *ctx, const char *token,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   488
                                  unsigned int tokenlen, const Token tokenval)
706
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   489
{
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   490
    switch (tokenval)
706
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   491
    {
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   492
        case ((Token) ','): return TOKEN_HLSL_COMMA;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   493
        case ((Token) '='): return TOKEN_HLSL_ASSIGN;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   494
        case ((Token) TOKEN_ADDASSIGN): return TOKEN_HLSL_ADDASSIGN;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   495
        case ((Token) TOKEN_SUBASSIGN): return TOKEN_HLSL_SUBASSIGN;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   496
        case ((Token) TOKEN_MULTASSIGN): return TOKEN_HLSL_MULASSIGN;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   497
        case ((Token) TOKEN_DIVASSIGN): return TOKEN_HLSL_DIVASSIGN;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   498
        case ((Token) TOKEN_MODASSIGN): return TOKEN_HLSL_MODASSIGN;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   499
        case ((Token) TOKEN_LSHIFTASSIGN): return TOKEN_HLSL_LSHIFTASSIGN;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   500
        case ((Token) TOKEN_RSHIFTASSIGN): return TOKEN_HLSL_RSHIFTASSIGN;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   501
        case ((Token) TOKEN_ANDASSIGN): return TOKEN_HLSL_ANDASSIGN;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   502
        case ((Token) TOKEN_ORASSIGN): return TOKEN_HLSL_ORASSIGN;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   503
        case ((Token) TOKEN_XORASSIGN): return TOKEN_HLSL_XORASSIGN;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   504
        case ((Token) '?'): return TOKEN_HLSL_QUESTION;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   505
        case ((Token) TOKEN_OROR): return TOKEN_HLSL_OROR;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   506
        case ((Token) TOKEN_ANDAND): return TOKEN_HLSL_ANDAND;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   507
        case ((Token) '|'): return TOKEN_HLSL_OR;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   508
        case ((Token) '^'): return TOKEN_HLSL_XOR;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   509
        case ((Token) '&'): return TOKEN_HLSL_AND;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   510
        case ((Token) TOKEN_EQL): return TOKEN_HLSL_EQL;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   511
        case ((Token) TOKEN_NEQ): return TOKEN_HLSL_NEQ;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   512
        case ((Token) '<'): return TOKEN_HLSL_LT;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   513
        case ((Token) TOKEN_LEQ): return TOKEN_HLSL_LEQ;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   514
        case ((Token) '>'): return TOKEN_HLSL_GT;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   515
        case ((Token) TOKEN_GEQ): return TOKEN_HLSL_GEQ;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   516
        case ((Token) TOKEN_LSHIFT): return TOKEN_HLSL_LSHIFT;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   517
        case ((Token) TOKEN_RSHIFT): return TOKEN_HLSL_RSHIFT;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   518
        case ((Token) '+'): return TOKEN_HLSL_PLUS;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   519
        case ((Token) '-'): return TOKEN_HLSL_MINUS;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   520
        case ((Token) '*'): return TOKEN_HLSL_STAR;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   521
        case ((Token) '/'): return TOKEN_HLSL_SLASH;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   522
        case ((Token) '%'): return TOKEN_HLSL_PERCENT;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   523
        case ((Token) '!'): return TOKEN_HLSL_EXCLAMATION;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   524
        case ((Token) '~'): return TOKEN_HLSL_COMPLEMENT;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   525
        case ((Token) TOKEN_DECREMENT): return TOKEN_HLSL_MINUSMINUS;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   526
        case ((Token) TOKEN_INCREMENT): return TOKEN_HLSL_PLUSPLUS;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   527
        case ((Token) '.'): return TOKEN_HLSL_DOT;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   528
        case ((Token) '['): return TOKEN_HLSL_LBRACKET;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   529
        case ((Token) ']'): return TOKEN_HLSL_RBRACKET;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   530
        case ((Token) '('): return TOKEN_HLSL_LPAREN;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   531
        case ((Token) ')'): return TOKEN_HLSL_RPAREN;
721
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   532
        case ((Token) TOKEN_INT_LITERAL): return TOKEN_HLSL_INT_CONSTANT;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   533
        case ((Token) TOKEN_FLOAT_LITERAL): return TOKEN_HLSL_FLOAT_CONSTANT;
706
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   534
        case ((Token) TOKEN_STRING_LITERAL): return TOKEN_HLSL_STRING_LITERAL;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   535
        case ((Token) ':'): return TOKEN_HLSL_COLON;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   536
        case ((Token) ';'): return TOKEN_HLSL_SEMICOLON;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   537
        case ((Token) '{'): return TOKEN_HLSL_LBRACE;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   538
        case ((Token) '}'): return TOKEN_HLSL_RBRACE;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   539
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   540
        case ((Token) TOKEN_IDENTIFIER):
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   541
            #define tokencmp(t) ((tokenlen == strlen(t)) && (memcmp(token, t, tokenlen) == 0))
706
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   542
            //case ((Token) ''): return TOKEN_HLSL_TYPECAST
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   543
            //if (tokencmp("")) return TOKEN_HLSL_TYPE_NAME
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   544
            //if (tokencmp("...")) return TOKEN_HLSL_ELIPSIS
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   545
            if (tokencmp("else")) return TOKEN_HLSL_ELSE;
721
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   546
            if (tokencmp("inline")) return TOKEN_HLSL_INLINE;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   547
            if (tokencmp("void")) return TOKEN_HLSL_VOID;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   548
            if (tokencmp("in")) return TOKEN_HLSL_IN;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   549
            if (tokencmp("inout")) return TOKEN_HLSL_INOUT;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   550
            if (tokencmp("out")) return TOKEN_HLSL_OUT;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   551
            if (tokencmp("uniform")) return TOKEN_HLSL_UNIFORM;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   552
            if (tokencmp("linear")) return TOKEN_HLSL_LINEAR;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   553
            if (tokencmp("centroid")) return TOKEN_HLSL_CENTROID;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   554
            if (tokencmp("nointerpolation")) return TOKEN_HLSL_NOINTERPOLATION;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   555
            if (tokencmp("noperspective")) return TOKEN_HLSL_NOPERSPECTIVE;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   556
            if (tokencmp("sample")) return TOKEN_HLSL_SAMPLE;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   557
            if (tokencmp("struct")) return TOKEN_HLSL_STRUCT;
706
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   558
            if (tokencmp("typedef")) return TOKEN_HLSL_TYPEDEF;
721
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   559
            if (tokencmp("const")) return TOKEN_HLSL_CONST;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   560
            if (tokencmp("packoffset")) return TOKEN_HLSL_PACKOFFSET;
706
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   561
            if (tokencmp("register")) return TOKEN_HLSL_REGISTER;
721
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   562
            if (tokencmp("extern")) return TOKEN_HLSL_EXTERN;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   563
            if (tokencmp("shared")) return TOKEN_HLSL_SHARED;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   564
            if (tokencmp("static")) return TOKEN_HLSL_STATIC;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   565
            if (tokencmp("volatile")) return TOKEN_HLSL_VOLATILE;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   566
            if (tokencmp("row_major")) return TOKEN_HLSL_ROWMAJOR;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   567
            if (tokencmp("column_major")) return TOKEN_HLSL_COLUMNMAJOR;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   568
            if (tokencmp("bool")) return TOKEN_HLSL_BOOL;
706
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   569
            if (tokencmp("int")) return TOKEN_HLSL_INT;
721
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   570
            if (tokencmp("uint")) return TOKEN_HLSL_UINT;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   571
            if (tokencmp("half")) return TOKEN_HLSL_HALF;
706
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   572
            if (tokencmp("float")) return TOKEN_HLSL_FLOAT;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   573
            if (tokencmp("double")) return TOKEN_HLSL_DOUBLE;
721
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   574
            if (tokencmp("string")) return TOKEN_HLSL_STRING;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   575
            if (tokencmp("snorm")) return TOKEN_HLSL_SNORM;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   576
            if (tokencmp("unorm")) return TOKEN_HLSL_UNORM;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   577
            if (tokencmp("buffer")) return TOKEN_HLSL_BUFFER;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   578
            if (tokencmp("vector")) return TOKEN_HLSL_VECTOR;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   579
            if (tokencmp("bool1")) return TOKEN_HLSL_BOOL1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   580
            if (tokencmp("bool2")) return TOKEN_HLSL_BOOL2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   581
            if (tokencmp("bool3")) return TOKEN_HLSL_BOOL3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   582
            if (tokencmp("bool4")) return TOKEN_HLSL_BOOL4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   583
            if (tokencmp("int1")) return TOKEN_HLSL_INT1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   584
            if (tokencmp("int2")) return TOKEN_HLSL_INT2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   585
            if (tokencmp("int3")) return TOKEN_HLSL_INT3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   586
            if (tokencmp("int4")) return TOKEN_HLSL_INT4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   587
            if (tokencmp("uint1")) return TOKEN_HLSL_UINT1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   588
            if (tokencmp("uint2")) return TOKEN_HLSL_UINT2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   589
            if (tokencmp("uint3")) return TOKEN_HLSL_UINT3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   590
            if (tokencmp("uint4")) return TOKEN_HLSL_UINT4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   591
            if (tokencmp("half1")) return TOKEN_HLSL_HALF1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   592
            if (tokencmp("half2")) return TOKEN_HLSL_HALF2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   593
            if (tokencmp("half3")) return TOKEN_HLSL_HALF3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   594
            if (tokencmp("half4")) return TOKEN_HLSL_HALF4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   595
            if (tokencmp("float1")) return TOKEN_HLSL_FLOAT1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   596
            if (tokencmp("float2")) return TOKEN_HLSL_FLOAT2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   597
            if (tokencmp("float3")) return TOKEN_HLSL_FLOAT3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   598
            if (tokencmp("float4")) return TOKEN_HLSL_FLOAT4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   599
            if (tokencmp("double1")) return TOKEN_HLSL_DOUBLE1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   600
            if (tokencmp("double2")) return TOKEN_HLSL_DOUBLE2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   601
            if (tokencmp("double3")) return TOKEN_HLSL_DOUBLE3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   602
            if (tokencmp("double4")) return TOKEN_HLSL_DOUBLE4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   603
            if (tokencmp("matrix")) return TOKEN_HLSL_MATRIX;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   604
            if (tokencmp("bool1x1")) return TOKEN_HLSL_BOOL1X1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   605
            if (tokencmp("bool1x2")) return TOKEN_HLSL_BOOL1X2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   606
            if (tokencmp("bool1x3")) return TOKEN_HLSL_BOOL1X3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   607
            if (tokencmp("bool1x4")) return TOKEN_HLSL_BOOL1X4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   608
            if (tokencmp("bool2x1")) return TOKEN_HLSL_BOOL2X1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   609
            if (tokencmp("bool2x2")) return TOKEN_HLSL_BOOL2X2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   610
            if (tokencmp("bool2x3")) return TOKEN_HLSL_BOOL2X3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   611
            if (tokencmp("bool2x4")) return TOKEN_HLSL_BOOL2X4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   612
            if (tokencmp("bool3x1")) return TOKEN_HLSL_BOOL3X1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   613
            if (tokencmp("bool3x2")) return TOKEN_HLSL_BOOL3X2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   614
            if (tokencmp("bool3x3")) return TOKEN_HLSL_BOOL3X3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   615
            if (tokencmp("bool3x4")) return TOKEN_HLSL_BOOL3X4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   616
            if (tokencmp("bool4x1")) return TOKEN_HLSL_BOOL4X1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   617
            if (tokencmp("bool4x2")) return TOKEN_HLSL_BOOL4X2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   618
            if (tokencmp("bool4x3")) return TOKEN_HLSL_BOOL4X3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   619
            if (tokencmp("bool4x4")) return TOKEN_HLSL_BOOL4X4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   620
            if (tokencmp("int1x1")) return TOKEN_HLSL_INT1X1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   621
            if (tokencmp("int1x2")) return TOKEN_HLSL_INT1X2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   622
            if (tokencmp("int1x3")) return TOKEN_HLSL_INT1X3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   623
            if (tokencmp("int1x4")) return TOKEN_HLSL_INT1X4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   624
            if (tokencmp("int2x1")) return TOKEN_HLSL_INT2X1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   625
            if (tokencmp("int2x2")) return TOKEN_HLSL_INT2X2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   626
            if (tokencmp("int2x3")) return TOKEN_HLSL_INT2X3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   627
            if (tokencmp("int2x4")) return TOKEN_HLSL_INT2X4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   628
            if (tokencmp("int3x1")) return TOKEN_HLSL_INT3X1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   629
            if (tokencmp("int3x2")) return TOKEN_HLSL_INT3X2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   630
            if (tokencmp("int3x3")) return TOKEN_HLSL_INT3X3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   631
            if (tokencmp("int3x4")) return TOKEN_HLSL_INT3X4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   632
            if (tokencmp("int4x1")) return TOKEN_HLSL_INT4X1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   633
            if (tokencmp("int4x2")) return TOKEN_HLSL_INT4X2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   634
            if (tokencmp("int4x3")) return TOKEN_HLSL_INT4X3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   635
            if (tokencmp("int4x4")) return TOKEN_HLSL_INT4X4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   636
            if (tokencmp("uint1x1")) return TOKEN_HLSL_UINT1X1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   637
            if (tokencmp("uint1x2")) return TOKEN_HLSL_UINT1X2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   638
            if (tokencmp("uint1x3")) return TOKEN_HLSL_UINT1X3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   639
            if (tokencmp("uint1x4")) return TOKEN_HLSL_UINT1X4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   640
            if (tokencmp("uint2x1")) return TOKEN_HLSL_UINT2X1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   641
            if (tokencmp("uint2x2")) return TOKEN_HLSL_UINT2X2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   642
            if (tokencmp("uint2x3")) return TOKEN_HLSL_UINT2X3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   643
            if (tokencmp("uint2x4")) return TOKEN_HLSL_UINT2X4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   644
            if (tokencmp("uint3x1")) return TOKEN_HLSL_UINT3X1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   645
            if (tokencmp("uint3x2")) return TOKEN_HLSL_UINT3X2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   646
            if (tokencmp("uint3x3")) return TOKEN_HLSL_UINT3X3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   647
            if (tokencmp("uint3x4")) return TOKEN_HLSL_UINT3X4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   648
            if (tokencmp("uint4x1")) return TOKEN_HLSL_UINT4X1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   649
            if (tokencmp("uint4x2")) return TOKEN_HLSL_UINT4X2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   650
            if (tokencmp("uint4x3")) return TOKEN_HLSL_UINT4X3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   651
            if (tokencmp("uint4x4")) return TOKEN_HLSL_UINT4X4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   652
            if (tokencmp("half1x1")) return TOKEN_HLSL_HALF1X1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   653
            if (tokencmp("half1x2")) return TOKEN_HLSL_HALF1X2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   654
            if (tokencmp("half1x3")) return TOKEN_HLSL_HALF1X3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   655
            if (tokencmp("half1x4")) return TOKEN_HLSL_HALF1X4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   656
            if (tokencmp("half2x1")) return TOKEN_HLSL_HALF2X1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   657
            if (tokencmp("half2x2")) return TOKEN_HLSL_HALF2X2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   658
            if (tokencmp("half2x3")) return TOKEN_HLSL_HALF2X3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   659
            if (tokencmp("half2x4")) return TOKEN_HLSL_HALF2X4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   660
            if (tokencmp("half3x1")) return TOKEN_HLSL_HALF3X1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   661
            if (tokencmp("half3x2")) return TOKEN_HLSL_HALF3X2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   662
            if (tokencmp("half3x3")) return TOKEN_HLSL_HALF3X3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   663
            if (tokencmp("half3x4")) return TOKEN_HLSL_HALF3X4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   664
            if (tokencmp("half4x1")) return TOKEN_HLSL_HALF4X1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   665
            if (tokencmp("half4x2")) return TOKEN_HLSL_HALF4X2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   666
            if (tokencmp("half4x3")) return TOKEN_HLSL_HALF4X3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   667
            if (tokencmp("half4x4")) return TOKEN_HLSL_HALF4X4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   668
            if (tokencmp("float1x1")) return TOKEN_HLSL_FLOAT1X1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   669
            if (tokencmp("float1x2")) return TOKEN_HLSL_FLOAT1X2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   670
            if (tokencmp("float1x3")) return TOKEN_HLSL_FLOAT1X3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   671
            if (tokencmp("float1x4")) return TOKEN_HLSL_FLOAT1X4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   672
            if (tokencmp("float2x1")) return TOKEN_HLSL_FLOAT2X1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   673
            if (tokencmp("float2x2")) return TOKEN_HLSL_FLOAT2X2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   674
            if (tokencmp("float2x3")) return TOKEN_HLSL_FLOAT2X3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   675
            if (tokencmp("float2x4")) return TOKEN_HLSL_FLOAT2X4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   676
            if (tokencmp("float3x1")) return TOKEN_HLSL_FLOAT3X1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   677
            if (tokencmp("float3x2")) return TOKEN_HLSL_FLOAT3X2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   678
            if (tokencmp("float3x3")) return TOKEN_HLSL_FLOAT3X3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   679
            if (tokencmp("float3x4")) return TOKEN_HLSL_FLOAT3X4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   680
            if (tokencmp("float4x1")) return TOKEN_HLSL_FLOAT4X1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   681
            if (tokencmp("float4x2")) return TOKEN_HLSL_FLOAT4X2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   682
            if (tokencmp("float4x3")) return TOKEN_HLSL_FLOAT4X3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   683
            if (tokencmp("float4x4")) return TOKEN_HLSL_FLOAT4X4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   684
            if (tokencmp("double1x1")) return TOKEN_HLSL_DOUBLE1X1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   685
            if (tokencmp("double1x2")) return TOKEN_HLSL_DOUBLE1X2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   686
            if (tokencmp("double1x3")) return TOKEN_HLSL_DOUBLE1X3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   687
            if (tokencmp("double1x4")) return TOKEN_HLSL_DOUBLE1X4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   688
            if (tokencmp("double2x1")) return TOKEN_HLSL_DOUBLE2X1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   689
            if (tokencmp("double2x2")) return TOKEN_HLSL_DOUBLE2X2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   690
            if (tokencmp("double2x3")) return TOKEN_HLSL_DOUBLE2X3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   691
            if (tokencmp("double2x4")) return TOKEN_HLSL_DOUBLE2X4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   692
            if (tokencmp("double3x1")) return TOKEN_HLSL_DOUBLE3X1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   693
            if (tokencmp("double3x2")) return TOKEN_HLSL_DOUBLE3X2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   694
            if (tokencmp("double3x3")) return TOKEN_HLSL_DOUBLE3X3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   695
            if (tokencmp("double3x4")) return TOKEN_HLSL_DOUBLE3X4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   696
            if (tokencmp("double4x1")) return TOKEN_HLSL_DOUBLE4X1;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   697
            if (tokencmp("double4x2")) return TOKEN_HLSL_DOUBLE4X2;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   698
            if (tokencmp("double4x3")) return TOKEN_HLSL_DOUBLE4X3;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   699
            if (tokencmp("double4x4")) return TOKEN_HLSL_DOUBLE4X4;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   700
            if (tokencmp("break")) return TOKEN_HLSL_BREAK;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   701
            if (tokencmp("continue")) return TOKEN_HLSL_CONTINUE;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   702
            if (tokencmp("discard")) return TOKEN_HLSL_DISCARD;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   703
            if (tokencmp("return")) return TOKEN_HLSL_RETURN;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   704
            if (tokencmp("while")) return TOKEN_HLSL_WHILE;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   705
            if (tokencmp("for")) return TOKEN_HLSL_FOR;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   706
            if (tokencmp("unroll")) return TOKEN_HLSL_UNROLL;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   707
            if (tokencmp("loop")) return TOKEN_HLSL_LOOP;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   708
            if (tokencmp("do")) return TOKEN_HLSL_DO;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   709
            if (tokencmp("if")) return TOKEN_HLSL_IF;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   710
            if (tokencmp("branch")) return TOKEN_HLSL_BRANCH;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   711
            if (tokencmp("flatten")) return TOKEN_HLSL_FLATTEN;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   712
            if (tokencmp("switch")) return TOKEN_HLSL_SWITCH;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   713
            if (tokencmp("forcecase")) return TOKEN_HLSL_FORCECASE;
82d1aec6b549 Rewrote HLSL grammar, mostly from scratch.
Ryan C. Gordon <icculus@icculus.org>
parents: 716
diff changeset
   714
            if (tokencmp("call")) return TOKEN_HLSL_CALL;
706
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   715
            if (tokencmp("case")) return TOKEN_HLSL_CASE;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   716
            if (tokencmp("default")) return TOKEN_HLSL_DEFAULT;
724
e96f140736f0 Initial work on parsing sampler declarations.
Ryan C. Gordon <icculus@icculus.org>
parents: 721
diff changeset
   717
            if (tokencmp("sampler")) return TOKEN_HLSL_SAMPLER;
e96f140736f0 Initial work on parsing sampler declarations.
Ryan C. Gordon <icculus@icculus.org>
parents: 721
diff changeset
   718
            if (tokencmp("sampler1D")) return TOKEN_HLSL_SAMPLER1D;
e96f140736f0 Initial work on parsing sampler declarations.
Ryan C. Gordon <icculus@icculus.org>
parents: 721
diff changeset
   719
            if (tokencmp("sampler2D")) return TOKEN_HLSL_SAMPLER2D;
e96f140736f0 Initial work on parsing sampler declarations.
Ryan C. Gordon <icculus@icculus.org>
parents: 721
diff changeset
   720
            if (tokencmp("sampler3D")) return TOKEN_HLSL_SAMPLER3D;
e96f140736f0 Initial work on parsing sampler declarations.
Ryan C. Gordon <icculus@icculus.org>
parents: 721
diff changeset
   721
            if (tokencmp("samplerCUBE")) return TOKEN_HLSL_SAMPLERCUBE;
e96f140736f0 Initial work on parsing sampler declarations.
Ryan C. Gordon <icculus@icculus.org>
parents: 721
diff changeset
   722
            if (tokencmp("sampler_state")) return TOKEN_HLSL_SAMPLER_STATE;
e96f140736f0 Initial work on parsing sampler declarations.
Ryan C. Gordon <icculus@icculus.org>
parents: 721
diff changeset
   723
            if (tokencmp("SamplerState")) return TOKEN_HLSL_SAMPLERSTATE;
e96f140736f0 Initial work on parsing sampler declarations.
Ryan C. Gordon <icculus@icculus.org>
parents: 721
diff changeset
   724
            if (tokencmp("SamplerComparisonState")) return TOKEN_HLSL_SAMPLERCOMPARISONSTATE;
787
04d5c34d8b40 Added statement block attributes to the HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 784
diff changeset
   725
            if (tokencmp("isolate")) return TOKEN_HLSL_ISOLATE;
04d5c34d8b40 Added statement block attributes to the HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 784
diff changeset
   726
            if (tokencmp("maxInstructionCount")) return TOKEN_HLSL_MAXINSTRUCTIONCOUNT;
04d5c34d8b40 Added statement block attributes to the HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 784
diff changeset
   727
            if (tokencmp("noExpressionOptimizations")) return TOKEN_HLSL_NOEXPRESSIONOPTIMIZATIONS;
04d5c34d8b40 Added statement block attributes to the HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 784
diff changeset
   728
            if (tokencmp("unused")) return TOKEN_HLSL_UNUSED;
04d5c34d8b40 Added statement block attributes to the HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 784
diff changeset
   729
            if (tokencmp("xps")) return TOKEN_HLSL_XPS;
724
e96f140736f0 Initial work on parsing sampler declarations.
Ryan C. Gordon <icculus@icculus.org>
parents: 721
diff changeset
   730
706
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   731
            #undef tokencmp
730
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   732
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   733
            if (is_semantic(ctx, token, tokenlen))
730
d4fe68d04624 Fixed semantic name parsing in HLSL grammar.
Ryan C. Gordon <icculus@icculus.org>
parents: 724
diff changeset
   734
                return TOKEN_HLSL_SEMANTIC;
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   735
            else if (is_usertype(ctx, token, tokenlen))
778
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   736
                return TOKEN_HLSL_USERTYPE;
706
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   737
            return TOKEN_HLSL_IDENTIFIER;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   738
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   739
        case TOKEN_EOI: return 0;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   740
        case TOKEN_BAD_CHARS: printf("bad chars from lexer\n"); return 0;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   741
        case TOKEN_PREPROCESSING_ERROR: printf("error from lexer\n"); return 0;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   742
        default: assert(0 && "unexpected token from lexer\n"); return 0;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   743
    } // switch
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   744
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   745
    return 0;
731
a3ce99bdc376 Corrected function name coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 730
diff changeset
   746
} // convert_to_lemon_token
706
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   747
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   748
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   749
void MOJOSHADER_compile(const char *filename,
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   750
                        const char *source, unsigned int sourcelen,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   751
                        const MOJOSHADER_preprocessorDefine *defines,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   752
                        unsigned int define_count,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   753
                        MOJOSHADER_includeOpen include_open,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   754
                        MOJOSHADER_includeClose include_close,
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   755
                        MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
706
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   756
{
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   757
    Context ctx;
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   758
    TokenData data;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   759
    unsigned int tokenlen;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   760
    Token tokenval;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   761
    const char *token;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   762
    int lemon_token;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   763
706
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   764
    if (m == NULL) m = MOJOSHADER_internal_malloc;
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   765
    if (f == NULL) f = MOJOSHADER_internal_free;
778
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   766
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   767
    memset(&ctx, '\0', sizeof (Context));
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   768
    ctx.malloc = m;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   769
    ctx.free = f;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   770
    ctx.malloc_data = d;
706
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   771
    ctx.preprocessor = preprocessor_start(filename, source, sourcelen,
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   772
                                           include_open, include_close,
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   773
                                           defines, define_count, 0, m, f, d);
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   774
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   775
    void *pParser = ParseHLSLAlloc(m, d);
716
94a804b54078 Cleaned up HLSL parser tracing.
Ryan C. Gordon <icculus@icculus.org>
parents: 714
diff changeset
   776
94a804b54078 Cleaned up HLSL parser tracing.
Ryan C. Gordon <icculus@icculus.org>
parents: 714
diff changeset
   777
    #if DEBUG_COMPILER_PARSER
94a804b54078 Cleaned up HLSL parser tracing.
Ryan C. Gordon <icculus@icculus.org>
parents: 714
diff changeset
   778
    ParseHLSLTrace(stdout, "COMPILER: ");
94a804b54078 Cleaned up HLSL parser tracing.
Ryan C. Gordon <icculus@icculus.org>
parents: 714
diff changeset
   779
    #endif
706
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   780
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   781
    do {
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   782
        token = preprocessor_nexttoken(ctx.preprocessor, &tokenlen, &tokenval);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   783
        lemon_token = convert_to_lemon_token(&ctx, token, tokenlen, tokenval);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   784
        switch (lemon_token)
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   785
        {
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   786
            case TOKEN_HLSL_INT_CONSTANT:
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   787
                data.i64 = strtoi64(token, tokenlen);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   788
                break;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   789
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   790
            case TOKEN_HLSL_FLOAT_CONSTANT:
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   791
                data.dbl = strtodouble(token, tokenlen);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   792
                break;
778
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   793
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   794
            case TOKEN_HLSL_SEMANTIC:
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   795
            case TOKEN_HLSL_USERTYPE:
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   796
            case TOKEN_HLSL_STRING_LITERAL:
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   797
            case TOKEN_HLSL_IDENTIFIER:
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   798
                data.string = cache_string(&ctx, token, tokenlen);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   799
                break;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   800
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   801
            default:
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   802
                data.i64 = 0;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   803
                break;
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   804
        } // switch
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   805
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   806
        ParseHLSL(pParser, lemon_token, data, &ctx);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   807
    } while ((!ctx.isfail) && (tokenval != TOKEN_EOI));
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   808
706
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   809
    ParseHLSLFree(pParser, f, d);
827
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   810
    // !!! FIXME: destruct (ctx) here.
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   811
    free_string_cache(&ctx);
2f955ce29b7b Moved the calculator experiment work back into the compiler.
Ryan C. Gordon <icculus@icculus.org>
parents: 792
diff changeset
   812
} // MOJOSHADER_compile
706
01a92f30b84f Added some basic compiler stub stuff, just to get this building at all.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   813
778
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   814
// end of mojoshader_compiler.c ...
a43d07e5da68 Added some structure for user types (struct at the moment).
Ryan C. Gordon <icculus@icculus.org>
parents: 731
diff changeset
   815