calculator.c
branchcalculator-experiment
changeset 812 ac8e761569da
parent 811 307e3ab506fa
child 814 a2cf3f36ed1c
equal deleted inserted replaced
811:307e3ab506fa 812:ac8e761569da
    51         ctx->free(ptr, ctx->malloc_data);
    51         ctx->free(ptr, ctx->malloc_data);
    52 } // Free
    52 } // Free
    53 
    53 
    54 typedef enum Operator
    54 typedef enum Operator
    55 {
    55 {
    56     OP_DEREF_ARRAY,
    56     OP_START_RANGE_UNARY_OPERATORS,
    57     OP_CALLFUNC,
       
    58     OP_DEREF_STRUCT,
       
    59     OP_POSTINCREMENT,
    57     OP_POSTINCREMENT,
    60     OP_POSTDECREMENT,
    58     OP_POSTDECREMENT,
    61     OP_COMMA,
       
    62     OP_PREINCREMENT,
    59     OP_PREINCREMENT,
    63     OP_PREDECREMENT,
    60     OP_PREDECREMENT,
    64     OP_NEGATE,
    61     OP_NEGATE,
    65     OP_COMPLEMENT,
    62     OP_COMPLEMENT,
    66     OP_NOT,
    63     OP_NOT,
       
    64     OP_END_RANGE_UNARY_OPERATORS,
       
    65 
       
    66     OP_START_RANGE_BINARY_OPERATORS,
       
    67     OP_DEREF_ARRAY,
       
    68     OP_CALLFUNC,
       
    69     OP_DEREF_STRUCT,
       
    70     OP_COMMA,
    67     OP_MULTIPLY,
    71     OP_MULTIPLY,
    68     OP_DIVIDE,
    72     OP_DIVIDE,
    69     OP_MODULO,
    73     OP_MODULO,
    70     OP_ADD,
    74     OP_ADD,
    71     OP_SUBTRACT,
    75     OP_SUBTRACT,
    80     OP_BINARYAND,
    84     OP_BINARYAND,
    81     OP_BINARYXOR,
    85     OP_BINARYXOR,
    82     OP_BINARYOR,
    86     OP_BINARYOR,
    83     OP_LOGICALAND,
    87     OP_LOGICALAND,
    84     OP_LOGICALOR,
    88     OP_LOGICALOR,
    85     OP_CONDITIONAL,
       
    86     OP_ASSIGN,
    89     OP_ASSIGN,
    87     OP_MULASSIGN,
    90     OP_MULASSIGN,
    88     OP_DIVASSIGN,
    91     OP_DIVASSIGN,
    89     OP_MODASSIGN,
    92     OP_MODASSIGN,
    90     OP_ADDASSIGN,
    93     OP_ADDASSIGN,
    92     OP_LSHIFTASSIGN,
    95     OP_LSHIFTASSIGN,
    93     OP_RSHIFTASSIGN,
    96     OP_RSHIFTASSIGN,
    94     OP_ANDASSIGN,
    97     OP_ANDASSIGN,
    95     OP_XORASSIGN,
    98     OP_XORASSIGN,
    96     OP_ORASSIGN,
    99     OP_ORASSIGN,
       
   100     OP_END_RANGE_BINARY_OPERATORS,
       
   101 
       
   102     OP_START_RANGE_TERNARY_OPERATORS,
       
   103     OP_CONDITIONAL,
       
   104     OP_END_RANGE_TERNARY_OPERATORS,
       
   105 
       
   106     OP_START_RANGE_DATA,
       
   107     OP_IDENTIFIER,
       
   108     OP_INT_LITERAL,
       
   109     OP_FLOAT_LITERAL,
       
   110     OP_STRING_LITERAL,
       
   111     OP_END_RANGE_DATA,
    97 } Operator;
   112 } Operator;
    98 
   113 
    99 typedef struct Expression
   114 typedef struct Expression
   100 {
   115 {
   101     Operator op;  // operator
   116     Operator op;  // operator
   126     Expression *right;
   141     Expression *right;
   127 } ExpressionTernary;
   142 } ExpressionTernary;
   128 
   143 
   129 typedef struct ExpressionIdentifier
   144 typedef struct ExpressionIdentifier
   130 {
   145 {
   131     Operator op;  // Always TOKEN_CALC_IDENTIFIER
   146     Operator op;  // Always OP_IDENTIFIER
   132     const char *identifier;
   147     const char *identifier;
   133 } ExpressionIdentifier;
   148 } ExpressionIdentifier;
   134 
   149 
   135 typedef struct ExpressionLiteralInt
   150 typedef struct ExpressionLiteralInt
   136 {
   151 {
   137     Operator op;  // Always TOKEN_CALC_INT_CONSTANT
   152     Operator op;  // Always OP_INT_LITERAL
   138     int64 value;
   153     int64 value;
   139 } ExpressionLiteralInt;
   154 } ExpressionLiteralInt;
   140 
   155 
   141 typedef struct ExpressionLiteralFloat
   156 typedef struct ExpressionLiteralFloat
   142 {
   157 {
   143     Operator op;  // Always TOKEN_CALC_FLOAT_CONSTANT
   158     Operator op;  // Always OP_FLOAT_LITERAL
   144     double value;
   159     double value;
   145 } ExpressionLiteralFloat;
   160 } ExpressionLiteralFloat;
   146 
   161 
   147 typedef struct ExpressionLiteralString
   162 typedef struct ExpressionLiteralString
   148 {
   163 {
   149     Operator op;  // Always TOKEN_CALC_STRING_LITERAL
   164     Operator op;  // Always OP_STRING_LITERAL
   150     const char *string;
   165     const char *string;
   151 } ExpressionLiteralString;
   166 } ExpressionLiteralString;
   152 
   167 
   153 static const char *new_identifier(Context *);
   168 static const char *new_identifier(Context *);
   154 static Expression *new_unary_expr(Context *, const Operator, Expression *);
   169 static Expression *new_unary_expr(Context *, const Operator, Expression *);