calculator.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 08 Feb 2010 02:42:12 -0500
branchcalculator-experiment
changeset 811 307e3ab506fa
child 812 ac8e761569da
permissions -rw-r--r--
Start of experiment with a basic calculator. This is a throwaway so I can debug some of the HLSL parsing code, and get a better idea of what I want to do there. This will eventually merge back into the default branch with the right bits going into the compiler code.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
#define __MOJOSHADER_INTERNAL__ 1
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
#include "mojoshader_internal.h"
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
#if DEBUG_COMPILER_PARSER
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
#define LEMON_SUPPORT_TRACING 1
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
#endif
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
typedef struct Context
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
    int isfail;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
    int out_of_memory;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
    MOJOSHADER_malloc malloc;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
    MOJOSHADER_free free;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
    void *malloc_data;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
    int error_count;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
    ErrorList *errors;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
    Preprocessor *preprocessor;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
    const char *token;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
    unsigned int tokenlen;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
    Token tokenval;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
    unsigned int parse_errors;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
} Context;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
// Convenience functions for allocators...
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
static inline void out_of_memory(Context *ctx)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
    ctx->isfail = ctx->out_of_memory = 1;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
} // out_of_memory
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
static inline void *Malloc(Context *ctx, const size_t len)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
    void *retval = ctx->malloc((int) len, ctx->malloc_data);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
    if (retval == NULL)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
        out_of_memory(ctx);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
    return retval;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
} // Malloc
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
static inline char *StrDup(Context *ctx, const char *str)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
    char *retval = (char *) Malloc(ctx, strlen(str) + 1);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
    if (retval != NULL)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
        strcpy(retval, str);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
    return retval;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
} // StrDup
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
static inline void Free(Context *ctx, void *ptr)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
    if (ptr != NULL)  // check for NULL in case of dumb free() impl.
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
        ctx->free(ptr, ctx->malloc_data);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
} // Free
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
typedef enum Operator
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
    OP_DEREF_ARRAY,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
    OP_CALLFUNC,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
    OP_DEREF_STRUCT,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
    OP_POSTINCREMENT,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
    OP_POSTDECREMENT,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
    OP_COMMA,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
    OP_PREINCREMENT,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
    OP_PREDECREMENT,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
    OP_NEGATE,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
    OP_COMPLEMENT,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
    OP_NOT,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
    OP_MULTIPLY,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
    OP_DIVIDE,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
    OP_MODULO,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
    OP_ADD,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
    OP_SUBTRACT,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
    OP_LSHIFT,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
    OP_RSHIFT,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
    OP_LESSTHAN,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
    OP_GREATERTHAN,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
    OP_LESSTHANOREQUAL,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    OP_GREATERTHANOREQUAL,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
    OP_EQUAL,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
    OP_NOTEQUAL,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
    OP_BINARYAND,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
    OP_BINARYXOR,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
    OP_BINARYOR,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
    OP_LOGICALAND,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
    OP_LOGICALOR,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
    OP_CONDITIONAL,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
    OP_ASSIGN,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
    OP_MULASSIGN,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
    OP_DIVASSIGN,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
    OP_MODASSIGN,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
    OP_ADDASSIGN,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
    OP_SUBASSIGN,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
    OP_LSHIFTASSIGN,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
    OP_RSHIFTASSIGN,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
    OP_ANDASSIGN,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
    OP_XORASSIGN,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
    OP_ORASSIGN,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
} Operator;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
typedef struct Expression
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
    Operator op;  // operator
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
} Expression;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
#define NEW_EXPR(cls) \
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
    cls *retval = Malloc(ctx, sizeof (cls)); \
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
    if (retval == NULL) { return NULL; }
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
typedef struct ExpressionUnary
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
    Operator op;  // operator
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
    Expression *operand;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
} ExpressionUnary;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
typedef struct ExpressionBinary
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
    Operator op;  // operator
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
    Expression *left;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
    Expression *right;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
} ExpressionBinary;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
typedef struct ExpressionTernary
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
    Operator op;  // operator
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
    Expression *left;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
    Expression *center;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
    Expression *right;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
} ExpressionTernary;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
typedef struct ExpressionIdentifier
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
    Operator op;  // Always TOKEN_CALC_IDENTIFIER
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
    const char *identifier;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
} ExpressionIdentifier;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
typedef struct ExpressionLiteralInt
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
    Operator op;  // Always TOKEN_CALC_INT_CONSTANT
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
    int64 value;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
} ExpressionLiteralInt;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
typedef struct ExpressionLiteralFloat
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
    Operator op;  // Always TOKEN_CALC_FLOAT_CONSTANT
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
    double value;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
} ExpressionLiteralFloat;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
typedef struct ExpressionLiteralString
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
    Operator op;  // Always TOKEN_CALC_STRING_LITERAL
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
    const char *string;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
} ExpressionLiteralString;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
static const char *new_identifier(Context *);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
static Expression *new_unary_expr(Context *, const Operator, Expression *);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
static Expression *new_binary_expr(Context *, const Operator, Expression *, Expression *);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
static Expression *new_ternary_expr(Context *, const Operator, Expression *, Expression *, Expression *);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
static Expression *new_identifier_expr(Context *, const char *);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
static Expression *new_literal_int_expr(Context *);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
static Expression *new_literal_float_expr(Context *);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
static Expression *new_literal_string_expr(Context *);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
static void parse_complete(const Expression *expr)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
    printf("parse complete!\n");
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
    
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
} // parse_complete
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
// This is where the actual parsing happens. It's Lemon-generated!
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
#define __MOJOSHADER_CALC_COMPILER__ 1
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
#include "calculator.h"
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
static const char *new_identifier(Context *ctx)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
    // !!! FIXME: this needs to cache strings.
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
    const unsigned int len = ctx->tokenlen;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
    char *retval = Malloc(ctx, len + 1);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
    if (retval == NULL)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
        return NULL;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
    memcpy(retval, ctx->token, len);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
    retval[len] = '\0';
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
    return retval;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
} // new_identifier
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
static Expression *new_unary_expr(Context *ctx, const Operator op,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
                                  Expression *operand)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
    NEW_EXPR(ExpressionUnary);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
    retval->op = op;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
    retval->operand = operand;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
    return (Expression *) retval;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
} // new_unary_expr
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
static Expression *new_binary_expr(Context *ctx, const Operator op,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
                                   Expression *left, Expression *right)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
    NEW_EXPR(ExpressionBinary);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
    retval->op = op;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
    retval->left = left;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
    retval->right = right;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
    return (Expression *) retval;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
} // new_binary_expr
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
static Expression *new_ternary_expr(Context *ctx, const Operator op,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
                                    Expression *left, Expression *center,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
                                    Expression *right)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
    NEW_EXPR(ExpressionTernary);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
    retval->op = op;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
    retval->left = left;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
    retval->center = center;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
    retval->right = right;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
    return (Expression *) retval;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
} // new_ternary_expr
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
static Expression *new_identifier_expr(Context *ctx, const char *identifier)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
    NEW_EXPR(ExpressionIdentifier);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
    retval->op = TOKEN_CALC_IDENTIFIER;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
    retval->identifier = identifier;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
    return (Expression *) retval;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
} // new_identifier_expr
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
static inline int64 strtoi64(const char *str, unsigned int len)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
    int64 retval = 0;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
    int64 mult = 1;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
    int i = 0;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
    while ((len) && (*str == ' '))
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
    {
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
        str++;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
        len--;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
    } // while
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
    if ((len) && (*str == '-'))
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
    {
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
        mult = -1;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
        str++;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
        len--;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
    } // if
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
    while (i < len)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
    {
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
        const char ch = str[i];
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
        if ((ch < '0') || (ch > '9'))
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
            break;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
        i++;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
    } // while
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
    while (--i >= 0)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
    {
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
        const char ch = str[i];
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
        retval += ((int64) (ch - '0')) * mult;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
        mult *= 10;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
    } // while
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
    return retval;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
} // strtoi64
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
static Expression *new_literal_int_expr(Context *ctx)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
    NEW_EXPR(ExpressionLiteralInt);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
    retval->op = TOKEN_CALC_INT_CONSTANT;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
    retval->value = strtoi64(ctx->token, ctx->tokenlen);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
    return (Expression *) retval;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
} // new_literal_int_expr
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
static inline double strtodouble(const char *_str, unsigned int len)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
    // !!! FIXME: laziness prevails.
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
    char *str = (char *) alloca(len+1);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
    memcpy(str, _str, len);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
    str[len] = '\0';
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
    return strtod(str, NULL);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
} // strtodouble
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
static Expression *new_literal_float_expr(Context *ctx)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
    NEW_EXPR(ExpressionLiteralFloat);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
    retval->op = TOKEN_CALC_FLOAT_CONSTANT;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
    retval->value = strtodouble(ctx->token, ctx->tokenlen);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
    return (Expression *) retval;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
} // new_literal_float_expr
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
static Expression *new_literal_string_expr(Context *ctx)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
    NEW_EXPR(ExpressionLiteralString);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
    retval->op = TOKEN_CALC_STRING_LITERAL;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
    retval->string = new_identifier(ctx);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
    return (Expression *) retval;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
} // new_string_literal_expr
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   293
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   294
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
static int convert_to_lemon_token(const Context *ctx)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
    switch (ctx->tokenval)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
    {
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
        case ((Token) ','): return TOKEN_CALC_COMMA;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
        case ((Token) '='): return TOKEN_CALC_ASSIGN;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
        case ((Token) TOKEN_ADDASSIGN): return TOKEN_CALC_ADDASSIGN;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
        case ((Token) TOKEN_SUBASSIGN): return TOKEN_CALC_SUBASSIGN;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
        case ((Token) TOKEN_MULTASSIGN): return TOKEN_CALC_MULASSIGN;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
        case ((Token) TOKEN_DIVASSIGN): return TOKEN_CALC_DIVASSIGN;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
        case ((Token) TOKEN_MODASSIGN): return TOKEN_CALC_MODASSIGN;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
        case ((Token) TOKEN_LSHIFTASSIGN): return TOKEN_CALC_LSHIFTASSIGN;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
        case ((Token) TOKEN_RSHIFTASSIGN): return TOKEN_CALC_RSHIFTASSIGN;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
        case ((Token) TOKEN_ANDASSIGN): return TOKEN_CALC_ANDASSIGN;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
        case ((Token) TOKEN_ORASSIGN): return TOKEN_CALC_ORASSIGN;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
        case ((Token) TOKEN_XORASSIGN): return TOKEN_CALC_XORASSIGN;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
        case ((Token) '?'): return TOKEN_CALC_QUESTION;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
        case ((Token) TOKEN_OROR): return TOKEN_CALC_OROR;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
        case ((Token) TOKEN_ANDAND): return TOKEN_CALC_ANDAND;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
        case ((Token) '|'): return TOKEN_CALC_OR;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
        case ((Token) '^'): return TOKEN_CALC_XOR;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   316
        case ((Token) '&'): return TOKEN_CALC_AND;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   317
        case ((Token) TOKEN_EQL): return TOKEN_CALC_EQL;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
        case ((Token) TOKEN_NEQ): return TOKEN_CALC_NEQ;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
        case ((Token) '<'): return TOKEN_CALC_LT;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   320
        case ((Token) TOKEN_LEQ): return TOKEN_CALC_LEQ;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   321
        case ((Token) '>'): return TOKEN_CALC_GT;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
        case ((Token) TOKEN_GEQ): return TOKEN_CALC_GEQ;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
        case ((Token) TOKEN_LSHIFT): return TOKEN_CALC_LSHIFT;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
        case ((Token) TOKEN_RSHIFT): return TOKEN_CALC_RSHIFT;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
        case ((Token) '+'): return TOKEN_CALC_PLUS;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   326
        case ((Token) '-'): return TOKEN_CALC_MINUS;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
        case ((Token) '*'): return TOKEN_CALC_STAR;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   328
        case ((Token) '/'): return TOKEN_CALC_SLASH;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
        case ((Token) '%'): return TOKEN_CALC_PERCENT;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
        case ((Token) '!'): return TOKEN_CALC_EXCLAMATION;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   331
        case ((Token) '~'): return TOKEN_CALC_COMPLEMENT;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
        case ((Token) TOKEN_DECREMENT): return TOKEN_CALC_MINUSMINUS;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
        case ((Token) TOKEN_INCREMENT): return TOKEN_CALC_PLUSPLUS;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
        case ((Token) '.'): return TOKEN_CALC_DOT;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
        case ((Token) '['): return TOKEN_CALC_LBRACKET;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
        case ((Token) ']'): return TOKEN_CALC_RBRACKET;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   337
        case ((Token) '('): return TOKEN_CALC_LPAREN;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
        case ((Token) ')'): return TOKEN_CALC_RPAREN;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
        case ((Token) TOKEN_INT_LITERAL): return TOKEN_CALC_INT_CONSTANT;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
        case ((Token) TOKEN_FLOAT_LITERAL): return TOKEN_CALC_FLOAT_CONSTANT;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
        case ((Token) TOKEN_STRING_LITERAL): return TOKEN_CALC_STRING_LITERAL;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   342
        case ((Token) ':'): return TOKEN_CALC_COLON;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   343
        //case ((Token) ';'): return TOKEN_CALC_SEMICOLON;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
        //case ((Token) '{'): return TOKEN_CALC_LBRACE;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   345
        //case ((Token) '}'): return TOKEN_CALC_RBRACE;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
        case ((Token) TOKEN_IDENTIFIER): return TOKEN_CALC_IDENTIFIER;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
        case TOKEN_EOI: return 0;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
        case TOKEN_BAD_CHARS: printf("bad chars from lexer\n"); return 0;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   349
        case TOKEN_PREPROCESSING_ERROR: printf("error from lexer\n"); return 0;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
        default: assert(0 && "unexpected token from lexer\n"); return 0;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
    } // switch
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   352
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
    return 0;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   354
} // convert_to_lemon_token
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   355
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   356
static void MOJOSHADER_compile(const char *filename,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   357
                             const char *source, unsigned int sourcelen,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
                             const MOJOSHADER_preprocessorDefine *defines,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
                             unsigned int define_count,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
                             MOJOSHADER_includeOpen include_open,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
                             MOJOSHADER_includeClose include_close,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
                             MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   364
    Context ctx;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
    if (m == NULL) m = MOJOSHADER_internal_malloc;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   366
    if (f == NULL) f = MOJOSHADER_internal_free;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   367
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   368
    memset(&ctx, '\0', sizeof (Context));
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   369
    ctx.malloc = m;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   370
    ctx.free = f;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   371
    ctx.malloc_data = d;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   372
    ctx.preprocessor = preprocessor_start(filename, source, sourcelen,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   373
                                           include_open, include_close,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
                                           defines, define_count, 0, m, f, d);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   375
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   376
    void *pParser = ParseCalculatorAlloc(m, d);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   377
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
    #if DEBUG_COMPILER_PARSER
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
    ParseCalculatorTrace(stdout, "COMPILER: ");
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
    #endif
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   381
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   382
    do {
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
        ctx.token = preprocessor_nexttoken(ctx.preprocessor,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   384
                                                &ctx.tokenlen,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
                                                &ctx.tokenval);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   386
        ParseCalculator(pParser, convert_to_lemon_token(&ctx), 0, &ctx);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   387
    } while (ctx.tokenval != TOKEN_EOI);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   388
    ParseCalculatorFree(pParser, f, d);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   389
}
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   390
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   391
int main(int argc, char **argv)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   392
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
    const char *ln;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
    size_t len = 0;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   395
    FILE *io = stdin;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   396
    const char *filename = "<stdin>";
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   397
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   398
    while ((ln = fgetln(io, &len)) != NULL)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   399
    {
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   400
        if ((len == 5) && (memcmp(ln, "quit\n", 5) == 0))
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   401
            break;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   402
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   403
        MOJOSHADER_compile(filename, ln, (unsigned int) len,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   404
                           NULL, 0, NULL, NULL, NULL, NULL, NULL);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   405
    } // while
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   406
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   407
    fclose(io);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   408
    return 0;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   409
} // main
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   410
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   411
// end of calculator.c ...
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   412