calculator.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 08 Feb 2010 04:29:05 -0500
branchcalculator-experiment
changeset 820 fd001ff71c65
parent 818 6f893a2224d8
child 821 ecf9f8157ada
permissions -rw-r--r--
Stop parsing if the parser panics.
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
816
dd3e3e03b7ce Fixed literal data getting through the parser in one piece.
Ryan C. Gordon <icculus@icculus.org>
parents: 815
diff changeset
     8
typedef struct TokenData
dd3e3e03b7ce Fixed literal data getting through the parser in one piece.
Ryan C. Gordon <icculus@icculus.org>
parents: 815
diff changeset
     9
{
dd3e3e03b7ce Fixed literal data getting through the parser in one piece.
Ryan C. Gordon <icculus@icculus.org>
parents: 815
diff changeset
    10
    const char *token;
dd3e3e03b7ce Fixed literal data getting through the parser in one piece.
Ryan C. Gordon <icculus@icculus.org>
parents: 815
diff changeset
    11
    unsigned int tokenlen;
dd3e3e03b7ce Fixed literal data getting through the parser in one piece.
Ryan C. Gordon <icculus@icculus.org>
parents: 815
diff changeset
    12
} TokenData;
dd3e3e03b7ce Fixed literal data getting through the parser in one piece.
Ryan C. Gordon <icculus@icculus.org>
parents: 815
diff changeset
    13
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
typedef struct Context
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
    int isfail;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
    int out_of_memory;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
    MOJOSHADER_malloc malloc;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
    MOJOSHADER_free free;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
    void *malloc_data;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
    int error_count;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
    ErrorList *errors;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
    Preprocessor *preprocessor;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
    const char *token;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
    unsigned int tokenlen;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
    Token tokenval;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
    unsigned int parse_errors;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
} Context;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
// Convenience functions for allocators...
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
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
    34
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
    ctx->isfail = ctx->out_of_memory = 1;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
} // out_of_memory
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
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
    39
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
    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
    41
    if (retval == NULL)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
        out_of_memory(ctx);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
    return retval;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
} // Malloc
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
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
    47
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
    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
    49
    if (retval != NULL)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
        strcpy(retval, str);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
    return retval;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
} // StrDup
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
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
    55
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
    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
    57
        ctx->free(ptr, ctx->malloc_data);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
} // Free
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
typedef enum Operator
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
{
815
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
    62
    OP_START_RANGE_UNARY,
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
    OP_POSTINCREMENT,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
    OP_POSTDECREMENT,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
    OP_PREINCREMENT,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
    OP_PREDECREMENT,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
    OP_NEGATE,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
    OP_COMPLEMENT,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
    OP_NOT,
815
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
    70
    OP_END_RANGE_UNARY,
812
ac8e761569da Cleaned up operator enumeration, put it into ranges, fixed data ops.
Ryan C. Gordon <icculus@icculus.org>
parents: 811
diff changeset
    71
815
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
    72
    OP_START_RANGE_BINARY,
812
ac8e761569da Cleaned up operator enumeration, put it into ranges, fixed data ops.
Ryan C. Gordon <icculus@icculus.org>
parents: 811
diff changeset
    73
    OP_DEREF_ARRAY,
ac8e761569da Cleaned up operator enumeration, put it into ranges, fixed data ops.
Ryan C. Gordon <icculus@icculus.org>
parents: 811
diff changeset
    74
    OP_CALLFUNC,
ac8e761569da Cleaned up operator enumeration, put it into ranges, fixed data ops.
Ryan C. Gordon <icculus@icculus.org>
parents: 811
diff changeset
    75
    OP_DEREF_STRUCT,
ac8e761569da Cleaned up operator enumeration, put it into ranges, fixed data ops.
Ryan C. Gordon <icculus@icculus.org>
parents: 811
diff changeset
    76
    OP_COMMA,
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    OP_MULTIPLY,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
    OP_DIVIDE,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
    OP_MODULO,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
    OP_ADD,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
    OP_SUBTRACT,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
    OP_LSHIFT,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
    OP_RSHIFT,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
    OP_LESSTHAN,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
    OP_GREATERTHAN,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
    OP_LESSTHANOREQUAL,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
    OP_GREATERTHANOREQUAL,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
    OP_EQUAL,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
    OP_NOTEQUAL,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
    OP_BINARYAND,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
    OP_BINARYXOR,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
    OP_BINARYOR,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
    OP_LOGICALAND,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
    OP_LOGICALOR,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
    OP_ASSIGN,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
    OP_MULASSIGN,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
    OP_DIVASSIGN,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
    OP_MODASSIGN,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
    OP_ADDASSIGN,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
    OP_SUBASSIGN,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
    OP_LSHIFTASSIGN,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
    OP_RSHIFTASSIGN,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
    OP_ANDASSIGN,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
    OP_XORASSIGN,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
    OP_ORASSIGN,
815
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   106
    OP_END_RANGE_BINARY,
812
ac8e761569da Cleaned up operator enumeration, put it into ranges, fixed data ops.
Ryan C. Gordon <icculus@icculus.org>
parents: 811
diff changeset
   107
815
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   108
    OP_START_RANGE_TERNARY,
812
ac8e761569da Cleaned up operator enumeration, put it into ranges, fixed data ops.
Ryan C. Gordon <icculus@icculus.org>
parents: 811
diff changeset
   109
    OP_CONDITIONAL,
815
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   110
    OP_END_RANGE_TERNARY,
812
ac8e761569da Cleaned up operator enumeration, put it into ranges, fixed data ops.
Ryan C. Gordon <icculus@icculus.org>
parents: 811
diff changeset
   111
ac8e761569da Cleaned up operator enumeration, put it into ranges, fixed data ops.
Ryan C. Gordon <icculus@icculus.org>
parents: 811
diff changeset
   112
    OP_START_RANGE_DATA,
ac8e761569da Cleaned up operator enumeration, put it into ranges, fixed data ops.
Ryan C. Gordon <icculus@icculus.org>
parents: 811
diff changeset
   113
    OP_IDENTIFIER,
ac8e761569da Cleaned up operator enumeration, put it into ranges, fixed data ops.
Ryan C. Gordon <icculus@icculus.org>
parents: 811
diff changeset
   114
    OP_INT_LITERAL,
ac8e761569da Cleaned up operator enumeration, put it into ranges, fixed data ops.
Ryan C. Gordon <icculus@icculus.org>
parents: 811
diff changeset
   115
    OP_FLOAT_LITERAL,
ac8e761569da Cleaned up operator enumeration, put it into ranges, fixed data ops.
Ryan C. Gordon <icculus@icculus.org>
parents: 811
diff changeset
   116
    OP_STRING_LITERAL,
ac8e761569da Cleaned up operator enumeration, put it into ranges, fixed data ops.
Ryan C. Gordon <icculus@icculus.org>
parents: 811
diff changeset
   117
    OP_END_RANGE_DATA,
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
} Operator;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
815
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   120
static inline int operator_is_unary(const Operator op)
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   121
{
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   122
    return ((op > OP_START_RANGE_UNARY) && (op < OP_END_RANGE_UNARY));
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   123
} // operator_is_unary
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   124
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   125
static inline int operator_is_binary(const Operator op)
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   126
{
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   127
    return ((op > OP_START_RANGE_BINARY) && (op < OP_END_RANGE_BINARY));
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   128
} // operator_is_binary
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   129
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   130
static inline int operator_is_ternary(const Operator op)
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   131
{
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   132
    return ((op > OP_START_RANGE_TERNARY) && (op < OP_END_RANGE_TERNARY));
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   133
} // operator_is_ternary
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   134
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   135
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
typedef struct Expression
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
    Operator op;  // operator
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
} Expression;
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
#define NEW_EXPR(cls) \
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
    cls *retval = Malloc(ctx, sizeof (cls)); \
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
    if (retval == NULL) { return NULL; }
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
typedef struct ExpressionUnary
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
    Operator op;  // operator
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
    Expression *operand;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
} ExpressionUnary;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
typedef struct ExpressionBinary
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
    Operator op;  // operator
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
    Expression *left;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
    Expression *right;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
} ExpressionBinary;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
typedef struct ExpressionTernary
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
    Operator op;  // operator
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
    Expression *left;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
    Expression *center;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
    Expression *right;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
} ExpressionTernary;
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
typedef struct ExpressionIdentifier
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
{
812
ac8e761569da Cleaned up operator enumeration, put it into ranges, fixed data ops.
Ryan C. Gordon <icculus@icculus.org>
parents: 811
diff changeset
   168
    Operator op;  // Always OP_IDENTIFIER
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
    const char *identifier;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
} ExpressionIdentifier;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
815
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   172
typedef struct ExpressionIntLiteral
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
{
812
ac8e761569da Cleaned up operator enumeration, put it into ranges, fixed data ops.
Ryan C. Gordon <icculus@icculus.org>
parents: 811
diff changeset
   174
    Operator op;  // Always OP_INT_LITERAL
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
    int64 value;
815
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   176
} ExpressionIntLiteral;
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
815
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   178
typedef struct ExpressionFloatLiteral
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
{
812
ac8e761569da Cleaned up operator enumeration, put it into ranges, fixed data ops.
Ryan C. Gordon <icculus@icculus.org>
parents: 811
diff changeset
   180
    Operator op;  // Always OP_FLOAT_LITERAL
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
    double value;
815
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   182
} ExpressionFloatLiteral;
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
815
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   184
typedef struct ExpressionStringLiteral
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
{
812
ac8e761569da Cleaned up operator enumeration, put it into ranges, fixed data ops.
Ryan C. Gordon <icculus@icculus.org>
parents: 811
diff changeset
   186
    Operator op;  // Always OP_STRING_LITERAL
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
    const char *string;
815
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   188
} ExpressionStringLiteral;
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
816
dd3e3e03b7ce Fixed literal data getting through the parser in one piece.
Ryan C. Gordon <icculus@icculus.org>
parents: 815
diff changeset
   190
static const char *new_identifier(Context *ctx, const TokenData *data)
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
    // !!! FIXME: this needs to cache strings.
816
dd3e3e03b7ce Fixed literal data getting through the parser in one piece.
Ryan C. Gordon <icculus@icculus.org>
parents: 815
diff changeset
   193
    const unsigned int len = data->tokenlen;
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
    char *retval = Malloc(ctx, len + 1);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
    if (retval == NULL)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
        return NULL;
816
dd3e3e03b7ce Fixed literal data getting through the parser in one piece.
Ryan C. Gordon <icculus@icculus.org>
parents: 815
diff changeset
   197
    memcpy(retval, data->token, len);
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
    retval[len] = '\0';
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
    return retval;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
} // new_identifier
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
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
   203
                                  Expression *operand)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
    NEW_EXPR(ExpressionUnary);
815
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   206
    assert(operator_is_unary(op));
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
    retval->op = op;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
    retval->operand = operand;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
    return (Expression *) retval;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
} // new_unary_expr
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
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
   213
                                   Expression *left, Expression *right)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
    NEW_EXPR(ExpressionBinary);
815
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   216
    assert(operator_is_binary(op));
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
    retval->op = op;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
    retval->left = left;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
    retval->right = right;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
    return (Expression *) retval;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
} // new_binary_expr
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
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
   224
                                    Expression *left, Expression *center,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
                                    Expression *right)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
    NEW_EXPR(ExpressionTernary);
815
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   228
    assert(operator_is_ternary(op));
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
    retval->op = op;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
    retval->left = left;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
    retval->center = center;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
    retval->right = right;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
    return (Expression *) retval;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
} // new_ternary_expr
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
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
   237
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
    NEW_EXPR(ExpressionIdentifier);
815
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   239
    retval->op = OP_IDENTIFIER;
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
    retval->identifier = identifier;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
    return (Expression *) retval;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
} // new_identifier_expr
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
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
   245
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
    int64 retval = 0;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
    int64 mult = 1;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
    int i = 0;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
    while ((len) && (*str == ' '))
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
    {
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
        str++;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
        len--;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
    } // while
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
    if ((len) && (*str == '-'))
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
        mult = -1;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
        str++;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
        len--;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
    } // if
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
    while (i < len)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
    {
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
        const char ch = str[i];
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
        if ((ch < '0') || (ch > '9'))
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
            break;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
        i++;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
    } // while
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
    while (--i >= 0)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
    {
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
        const char ch = str[i];
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
        retval += ((int64) (ch - '0')) * mult;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
        mult *= 10;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
    } // while
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
    return retval;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
} // strtoi64
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
816
dd3e3e03b7ce Fixed literal data getting through the parser in one piece.
Ryan C. Gordon <icculus@icculus.org>
parents: 815
diff changeset
   281
static Expression *new_literal_int_expr(Context *ctx, const TokenData *data)
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
{
815
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   283
    NEW_EXPR(ExpressionIntLiteral);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   284
    retval->op = OP_INT_LITERAL;
816
dd3e3e03b7ce Fixed literal data getting through the parser in one piece.
Ryan C. Gordon <icculus@icculus.org>
parents: 815
diff changeset
   285
    retval->value = strtoi64(data->token, data->tokenlen);
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
    return (Expression *) retval;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
} // new_literal_int_expr
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
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
   290
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
    // !!! FIXME: laziness prevails.
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
    char *str = (char *) alloca(len+1);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   293
    memcpy(str, _str, len);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   294
    str[len] = '\0';
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
    return strtod(str, NULL);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
} // strtodouble
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
816
dd3e3e03b7ce Fixed literal data getting through the parser in one piece.
Ryan C. Gordon <icculus@icculus.org>
parents: 815
diff changeset
   298
static Expression *new_literal_float_expr(Context *ctx, const TokenData *data)
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
{
815
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   300
    NEW_EXPR(ExpressionFloatLiteral);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   301
    retval->op = OP_FLOAT_LITERAL;
816
dd3e3e03b7ce Fixed literal data getting through the parser in one piece.
Ryan C. Gordon <icculus@icculus.org>
parents: 815
diff changeset
   302
    retval->value = strtodouble(data->token, data->tokenlen);
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
    return (Expression *) retval;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
} // new_literal_float_expr
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
816
dd3e3e03b7ce Fixed literal data getting through the parser in one piece.
Ryan C. Gordon <icculus@icculus.org>
parents: 815
diff changeset
   306
static Expression *new_literal_string_expr(Context *ctx, const TokenData *data)
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
{
815
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   308
    NEW_EXPR(ExpressionStringLiteral);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   309
    retval->op = OP_STRING_LITERAL;
816
dd3e3e03b7ce Fixed literal data getting through the parser in one piece.
Ryan C. Gordon <icculus@icculus.org>
parents: 815
diff changeset
   310
    retval->string = new_identifier(ctx, data);
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
    return (Expression *) retval;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
} // new_string_literal_expr
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
815
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   314
static void print_expr(const Expression *expr, const int depth)
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   315
{
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   316
    int i;
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   317
    for (i = 0; i < depth; i++)
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   318
        printf("    ");
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   319
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   320
    printf("Expression ");
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   321
    switch (expr->op)
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   322
    {
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   323
        #define PRINT_OP(op) case op: printf("%s\n", #op); break;
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   324
        PRINT_OP(OP_DEREF_ARRAY);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   325
        PRINT_OP(OP_CALLFUNC);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   326
        PRINT_OP(OP_DEREF_STRUCT);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   327
        PRINT_OP(OP_POSTINCREMENT);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   328
        PRINT_OP(OP_POSTDECREMENT);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   329
        PRINT_OP(OP_COMMA);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   330
        PRINT_OP(OP_PREINCREMENT);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   331
        PRINT_OP(OP_PREDECREMENT);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   332
        PRINT_OP(OP_NEGATE);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   333
        PRINT_OP(OP_COMPLEMENT);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   334
        PRINT_OP(OP_NOT);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   335
        PRINT_OP(OP_MULTIPLY);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   336
        PRINT_OP(OP_DIVIDE);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   337
        PRINT_OP(OP_MODULO);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   338
        PRINT_OP(OP_ADD);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   339
        PRINT_OP(OP_SUBTRACT);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   340
        PRINT_OP(OP_LSHIFT);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   341
        PRINT_OP(OP_RSHIFT);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   342
        PRINT_OP(OP_LESSTHAN);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   343
        PRINT_OP(OP_GREATERTHAN);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   344
        PRINT_OP(OP_LESSTHANOREQUAL);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   345
        PRINT_OP(OP_GREATERTHANOREQUAL);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   346
        PRINT_OP(OP_EQUAL);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   347
        PRINT_OP(OP_NOTEQUAL);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   348
        PRINT_OP(OP_BINARYAND);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   349
        PRINT_OP(OP_BINARYXOR);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   350
        PRINT_OP(OP_BINARYOR);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   351
        PRINT_OP(OP_LOGICALAND);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   352
        PRINT_OP(OP_LOGICALOR);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   353
        PRINT_OP(OP_CONDITIONAL);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   354
        PRINT_OP(OP_ASSIGN);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   355
        PRINT_OP(OP_MULASSIGN);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   356
        PRINT_OP(OP_DIVASSIGN);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   357
        PRINT_OP(OP_MODASSIGN);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   358
        PRINT_OP(OP_ADDASSIGN);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   359
        PRINT_OP(OP_SUBASSIGN);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   360
        PRINT_OP(OP_LSHIFTASSIGN);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   361
        PRINT_OP(OP_RSHIFTASSIGN);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   362
        PRINT_OP(OP_ANDASSIGN);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   363
        PRINT_OP(OP_XORASSIGN);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   364
        PRINT_OP(OP_ORASSIGN);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   365
        PRINT_OP(OP_INT_LITERAL);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   366
        PRINT_OP(OP_FLOAT_LITERAL);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   367
        PRINT_OP(OP_STRING_LITERAL);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   368
        PRINT_OP(OP_IDENTIFIER);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   369
        default: printf("---UNKNOWN!---\n"); return;
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   370
    } // switch
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   371
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   372
    if (operator_is_unary(expr->op))
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   373
    {
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   374
        const ExpressionUnary *unary = (const ExpressionUnary *) expr;
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   375
        print_expr(unary->operand, depth + 1);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   376
    } // if
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   377
    else if (operator_is_binary(expr->op))
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   378
    {
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   379
        const ExpressionBinary *binary = (const ExpressionBinary *) expr;
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   380
        print_expr(binary->left, depth + 1);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   381
        print_expr(binary->right, depth + 1);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   382
    } // else if
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   383
    else if (operator_is_ternary(expr->op))
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   384
    {
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   385
        const ExpressionTernary *ternary = (const ExpressionTernary *) expr;
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   386
        print_expr(ternary->left, depth + 1);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   387
        print_expr(ternary->center, depth + 1);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   388
        print_expr(ternary->right, depth + 1);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   389
    } // else if
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   390
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   391
    else
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   392
    {
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   393
        for (i = 0; i < (depth + 1); i++)
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   394
            printf("    ");
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   395
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   396
        if (expr->op == OP_IDENTIFIER)
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   397
        {
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   398
            const ExpressionIdentifier *ident = (const ExpressionIdentifier *) expr;
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   399
            printf("(%s)\n", ident->identifier);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   400
        } // if
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   401
        else if (expr->op == OP_INT_LITERAL)
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   402
        {
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   403
            const ExpressionIntLiteral *lit = (const ExpressionIntLiteral *) expr;
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   404
            printf("(%lld)\n", (long long) lit->value);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   405
        } // if
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   406
        else if (expr->op == OP_FLOAT_LITERAL)
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   407
        {
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   408
            const ExpressionFloatLiteral *lit = (const ExpressionFloatLiteral *) expr;
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   409
            printf("(%lf)\n", lit->value);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   410
        } // if
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   411
        else if (expr->op == OP_STRING_LITERAL)
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   412
        {
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   413
            const ExpressionStringLiteral *lit = (const ExpressionStringLiteral *) expr;
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   414
            printf("(\"%s\")\n", lit->string);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   415
        } // if
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   416
        else
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   417
        {
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   418
            assert(0 && "Shouldn't hit this.");
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   419
        } // else
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   420
    } // else
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   421
} // print_expr
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   422
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   423
static void parse_complete(const Expression *expr)
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   424
{
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   425
    print_expr(expr, 0);
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   426
} // parse_complete
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   427
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   428
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   429
// This is where the actual parsing happens. It's Lemon-generated!
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   430
#define __MOJOSHADER_CALC_COMPILER__ 1
c999c015cdc0 Initial dumping of parse tree once parsing finishes.
Ryan C. Gordon <icculus@icculus.org>
parents: 814
diff changeset
   431
#include "calculator.h"
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   432
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   433
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
   434
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   435
    switch (ctx->tokenval)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   436
    {
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   437
        case ((Token) ','): return TOKEN_CALC_COMMA;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   438
        case ((Token) '='): return TOKEN_CALC_ASSIGN;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   439
        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
   440
        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
   441
        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
   442
        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
   443
        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
   444
        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
   445
        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
   446
        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
   447
        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
   448
        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
   449
        case ((Token) '?'): return TOKEN_CALC_QUESTION;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   450
        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
   451
        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
   452
        case ((Token) '|'): return TOKEN_CALC_OR;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   453
        case ((Token) '^'): return TOKEN_CALC_XOR;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   454
        case ((Token) '&'): return TOKEN_CALC_AND;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   455
        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
   456
        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
   457
        case ((Token) '<'): return TOKEN_CALC_LT;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   458
        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
   459
        case ((Token) '>'): return TOKEN_CALC_GT;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   460
        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
   461
        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
   462
        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
   463
        case ((Token) '+'): return TOKEN_CALC_PLUS;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   464
        case ((Token) '-'): return TOKEN_CALC_MINUS;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   465
        case ((Token) '*'): return TOKEN_CALC_STAR;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   466
        case ((Token) '/'): return TOKEN_CALC_SLASH;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   467
        case ((Token) '%'): return TOKEN_CALC_PERCENT;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   468
        case ((Token) '!'): return TOKEN_CALC_EXCLAMATION;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   469
        case ((Token) '~'): return TOKEN_CALC_COMPLEMENT;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   470
        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
   471
        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
   472
        case ((Token) '.'): return TOKEN_CALC_DOT;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   473
        case ((Token) '['): return TOKEN_CALC_LBRACKET;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   474
        case ((Token) ']'): return TOKEN_CALC_RBRACKET;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   475
        case ((Token) '('): return TOKEN_CALC_LPAREN;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   476
        case ((Token) ')'): return TOKEN_CALC_RPAREN;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   477
        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
   478
        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
   479
        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
   480
        case ((Token) ':'): return TOKEN_CALC_COLON;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   481
        //case ((Token) ';'): return TOKEN_CALC_SEMICOLON;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   482
        //case ((Token) '{'): return TOKEN_CALC_LBRACE;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   483
        //case ((Token) '}'): return TOKEN_CALC_RBRACE;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   484
        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
   485
        case TOKEN_EOI: return 0;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   486
        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
   487
        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
   488
        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
   489
    } // switch
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   490
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   491
    return 0;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   492
} // convert_to_lemon_token
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   493
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   494
static void MOJOSHADER_compile(const char *filename,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   495
                             const char *source, unsigned int sourcelen,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   496
                             const MOJOSHADER_preprocessorDefine *defines,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   497
                             unsigned int define_count,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   498
                             MOJOSHADER_includeOpen include_open,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   499
                             MOJOSHADER_includeClose include_close,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   500
                             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
   501
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   502
    Context ctx;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   503
    if (m == NULL) m = MOJOSHADER_internal_malloc;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   504
    if (f == NULL) f = MOJOSHADER_internal_free;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   505
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   506
    memset(&ctx, '\0', sizeof (Context));
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   507
    ctx.malloc = m;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   508
    ctx.free = f;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   509
    ctx.malloc_data = d;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   510
    ctx.preprocessor = preprocessor_start(filename, source, sourcelen,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   511
                                           include_open, include_close,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   512
                                           defines, define_count, 0, m, f, d);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   513
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   514
    void *pParser = ParseCalculatorAlloc(m, d);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   515
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   516
    #if DEBUG_COMPILER_PARSER
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   517
    ParseCalculatorTrace(stdout, "COMPILER: ");
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   518
    #endif
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   519
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   520
    do {
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   521
        ctx.token = preprocessor_nexttoken(ctx.preprocessor,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   522
                                                &ctx.tokenlen,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   523
                                                &ctx.tokenval);
816
dd3e3e03b7ce Fixed literal data getting through the parser in one piece.
Ryan C. Gordon <icculus@icculus.org>
parents: 815
diff changeset
   524
        // !!! FIXME: this can't refer directly to pointers in the stream,
dd3e3e03b7ce Fixed literal data getting through the parser in one piece.
Ryan C. Gordon <icculus@icculus.org>
parents: 815
diff changeset
   525
        // !!! FIXME:  as they can be free()'d before we actually use them
dd3e3e03b7ce Fixed literal data getting through the parser in one piece.
Ryan C. Gordon <icculus@icculus.org>
parents: 815
diff changeset
   526
        // !!! FIXME:  when a rule reduces down later.
dd3e3e03b7ce Fixed literal data getting through the parser in one piece.
Ryan C. Gordon <icculus@icculus.org>
parents: 815
diff changeset
   527
        TokenData token = { ctx.token, ctx.tokenlen };
dd3e3e03b7ce Fixed literal data getting through the parser in one piece.
Ryan C. Gordon <icculus@icculus.org>
parents: 815
diff changeset
   528
        ParseCalculator(pParser, convert_to_lemon_token(&ctx), token, &ctx);
820
fd001ff71c65 Stop parsing if the parser panics.
Ryan C. Gordon <icculus@icculus.org>
parents: 818
diff changeset
   529
    } while ((!ctx.isfail) && (ctx.tokenval != TOKEN_EOI));
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   530
    ParseCalculatorFree(pParser, f, d);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   531
}
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   532
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   533
int main(int argc, char **argv)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   534
{
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   535
    const char *ln;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   536
    size_t len = 0;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   537
    FILE *io = stdin;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   538
    const char *filename = "<stdin>";
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   539
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   540
    while ((ln = fgetln(io, &len)) != NULL)
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   541
    {
818
6f893a2224d8 Ignore blank lines.
Ryan C. Gordon <icculus@icculus.org>
parents: 817
diff changeset
   542
        if (len == 1)
6f893a2224d8 Ignore blank lines.
Ryan C. Gordon <icculus@icculus.org>
parents: 817
diff changeset
   543
            continue;
6f893a2224d8 Ignore blank lines.
Ryan C. Gordon <icculus@icculus.org>
parents: 817
diff changeset
   544
        else if ((len == 5) && (memcmp(ln, "quit\n", 5) == 0))
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   545
            break;
814
a2cf3f36ed1c Allow "q" to quit calculator, too.
Ryan C. Gordon <icculus@icculus.org>
parents: 812
diff changeset
   546
        else if ((len == 2) && (memcmp(ln, "q\n", 2) == 0))
a2cf3f36ed1c Allow "q" to quit calculator, too.
Ryan C. Gordon <icculus@icculus.org>
parents: 812
diff changeset
   547
            break;
811
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   548
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   549
        MOJOSHADER_compile(filename, ln, (unsigned int) len,
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   550
                           NULL, 0, NULL, NULL, NULL, NULL, NULL);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   551
    } // while
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   552
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   553
    fclose(io);
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   554
    return 0;
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   555
} // main
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   556
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   557
// end of calculator.c ...
307e3ab506fa Start of experiment with a basic calculator.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   558