Skip to content

Latest commit

 

History

History
758 lines (675 loc) · 23.2 KB

calculator.c

File metadata and controls

758 lines (675 loc) · 23.2 KB
 
1
2
3
4
5
6
7
#define __MOJOSHADER_INTERNAL__ 1
#include "mojoshader_internal.h"
#if DEBUG_COMPILER_PARSER
#define LEMON_SUPPORT_TRACING 1
#endif
Feb 9, 2010
Feb 9, 2010
8
typedef union TokenData
Feb 8, 2010
Feb 8, 2010
9
{
Feb 9, 2010
Feb 9, 2010
10
11
12
int64 i64;
double dbl;
const char *string;
Feb 8, 2010
Feb 8, 2010
13
14
} TokenData;
Feb 9, 2010
Feb 9, 2010
15
16
17
18
19
20
typedef struct StringBucket
{
char *string;
struct StringBucket *next;
} StringBucket;
21
22
23
24
25
26
27
28
29
30
typedef struct Context
{
int isfail;
int out_of_memory;
MOJOSHADER_malloc malloc;
MOJOSHADER_free free;
void *malloc_data;
int error_count;
ErrorList *errors;
Preprocessor *preprocessor;
Feb 9, 2010
Feb 9, 2010
31
32
StringBucket *string_hashtable[256];
// !!! FIXME: do these really need to be in here?
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const char *token;
unsigned int tokenlen;
Token tokenval;
unsigned int parse_errors;
} Context;
// Convenience functions for allocators...
static inline void out_of_memory(Context *ctx)
{
ctx->isfail = ctx->out_of_memory = 1;
} // out_of_memory
static inline void *Malloc(Context *ctx, const size_t len)
{
void *retval = ctx->malloc((int) len, ctx->malloc_data);
if (retval == NULL)
out_of_memory(ctx);
return retval;
} // Malloc
static inline char *StrDup(Context *ctx, const char *str)
{
char *retval = (char *) Malloc(ctx, strlen(str) + 1);
if (retval != NULL)
strcpy(retval, str);
return retval;
} // StrDup
static inline void Free(Context *ctx, void *ptr)
{
if (ptr != NULL) // check for NULL in case of dumb free() impl.
ctx->free(ptr, ctx->malloc_data);
} // Free
typedef enum Operator
{
Feb 8, 2010
Feb 8, 2010
71
OP_START_RANGE_UNARY,
72
73
74
75
76
77
78
OP_POSTINCREMENT,
OP_POSTDECREMENT,
OP_PREINCREMENT,
OP_PREDECREMENT,
OP_NEGATE,
OP_COMPLEMENT,
OP_NOT,
Feb 8, 2010
Feb 8, 2010
79
OP_END_RANGE_UNARY,
Feb 8, 2010
Feb 8, 2010
80
Feb 8, 2010
Feb 8, 2010
81
OP_START_RANGE_BINARY,
Feb 8, 2010
Feb 8, 2010
82
83
84
85
OP_DEREF_ARRAY,
OP_CALLFUNC,
OP_DEREF_STRUCT,
OP_COMMA,
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
OP_MULTIPLY,
OP_DIVIDE,
OP_MODULO,
OP_ADD,
OP_SUBTRACT,
OP_LSHIFT,
OP_RSHIFT,
OP_LESSTHAN,
OP_GREATERTHAN,
OP_LESSTHANOREQUAL,
OP_GREATERTHANOREQUAL,
OP_EQUAL,
OP_NOTEQUAL,
OP_BINARYAND,
OP_BINARYXOR,
OP_BINARYOR,
OP_LOGICALAND,
OP_LOGICALOR,
OP_ASSIGN,
OP_MULASSIGN,
OP_DIVASSIGN,
OP_MODASSIGN,
OP_ADDASSIGN,
OP_SUBASSIGN,
OP_LSHIFTASSIGN,
OP_RSHIFTASSIGN,
OP_ANDASSIGN,
OP_XORASSIGN,
OP_ORASSIGN,
Feb 8, 2010
Feb 8, 2010
115
OP_END_RANGE_BINARY,
Feb 8, 2010
Feb 8, 2010
116
Feb 8, 2010
Feb 8, 2010
117
OP_START_RANGE_TERNARY,
Feb 8, 2010
Feb 8, 2010
118
OP_CONDITIONAL,
Feb 8, 2010
Feb 8, 2010
119
OP_END_RANGE_TERNARY,
Feb 8, 2010
Feb 8, 2010
120
121
122
123
124
125
126
OP_START_RANGE_DATA,
OP_IDENTIFIER,
OP_INT_LITERAL,
OP_FLOAT_LITERAL,
OP_STRING_LITERAL,
OP_END_RANGE_DATA,
127
128
} Operator;
Feb 8, 2010
Feb 8, 2010
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
static inline int operator_is_unary(const Operator op)
{
return ((op > OP_START_RANGE_UNARY) && (op < OP_END_RANGE_UNARY));
} // operator_is_unary
static inline int operator_is_binary(const Operator op)
{
return ((op > OP_START_RANGE_BINARY) && (op < OP_END_RANGE_BINARY));
} // operator_is_binary
static inline int operator_is_ternary(const Operator op)
{
return ((op > OP_START_RANGE_TERNARY) && (op < OP_END_RANGE_TERNARY));
} // operator_is_ternary
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
typedef struct Expression
{
Operator op; // operator
} Expression;
#define NEW_EXPR(cls) \
cls *retval = Malloc(ctx, sizeof (cls)); \
if (retval == NULL) { return NULL; }
typedef struct ExpressionUnary
{
Operator op; // operator
Expression *operand;
} ExpressionUnary;
typedef struct ExpressionBinary
{
Operator op; // operator
Expression *left;
Expression *right;
} ExpressionBinary;
typedef struct ExpressionTernary
{
Operator op; // operator
Expression *left;
Expression *center;
Expression *right;
} ExpressionTernary;
typedef struct ExpressionIdentifier
{
Feb 8, 2010
Feb 8, 2010
177
Operator op; // Always OP_IDENTIFIER
178
179
180
const char *identifier;
} ExpressionIdentifier;
Feb 8, 2010
Feb 8, 2010
181
typedef struct ExpressionIntLiteral
Feb 8, 2010
Feb 8, 2010
183
Operator op; // Always OP_INT_LITERAL
184
int64 value;
Feb 8, 2010
Feb 8, 2010
185
} ExpressionIntLiteral;
Feb 8, 2010
Feb 8, 2010
187
typedef struct ExpressionFloatLiteral
Feb 8, 2010
Feb 8, 2010
189
Operator op; // Always OP_FLOAT_LITERAL
190
double value;
Feb 8, 2010
Feb 8, 2010
191
} ExpressionFloatLiteral;
Feb 8, 2010
Feb 8, 2010
193
typedef struct ExpressionStringLiteral
Feb 8, 2010
Feb 8, 2010
195
Operator op; // Always OP_STRING_LITERAL
196
const char *string;
Feb 8, 2010
Feb 8, 2010
197
} ExpressionStringLiteral;
198
199
200
201
202
static Expression *new_unary_expr(Context *ctx, const Operator op,
Expression *operand)
{
NEW_EXPR(ExpressionUnary);
Feb 8, 2010
Feb 8, 2010
203
assert(operator_is_unary(op));
204
205
206
207
208
209
210
211
212
retval->op = op;
retval->operand = operand;
return (Expression *) retval;
} // new_unary_expr
static Expression *new_binary_expr(Context *ctx, const Operator op,
Expression *left, Expression *right)
{
NEW_EXPR(ExpressionBinary);
Feb 8, 2010
Feb 8, 2010
213
assert(operator_is_binary(op));
214
215
216
217
218
219
220
221
222
223
224
retval->op = op;
retval->left = left;
retval->right = right;
return (Expression *) retval;
} // new_binary_expr
static Expression *new_ternary_expr(Context *ctx, const Operator op,
Expression *left, Expression *center,
Expression *right)
{
NEW_EXPR(ExpressionTernary);
Feb 8, 2010
Feb 8, 2010
225
assert(operator_is_ternary(op));
226
227
228
229
230
231
232
retval->op = op;
retval->left = left;
retval->center = center;
retval->right = right;
return (Expression *) retval;
} // new_ternary_expr
Feb 9, 2010
Feb 9, 2010
233
static Expression *new_identifier_expr(Context *ctx, const TokenData *data)
234
235
{
NEW_EXPR(ExpressionIdentifier);
Feb 8, 2010
Feb 8, 2010
236
retval->op = OP_IDENTIFIER;
Feb 9, 2010
Feb 9, 2010
237
retval->identifier = data->string; // cached; don't copy string.
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
return (Expression *) retval;
} // new_identifier_expr
static inline int64 strtoi64(const char *str, unsigned int len)
{
int64 retval = 0;
int64 mult = 1;
int i = 0;
while ((len) && (*str == ' '))
{
str++;
len--;
} // while
if ((len) && (*str == '-'))
{
mult = -1;
str++;
len--;
} // if
while (i < len)
{
const char ch = str[i];
if ((ch < '0') || (ch > '9'))
break;
i++;
} // while
while (--i >= 0)
{
const char ch = str[i];
retval += ((int64) (ch - '0')) * mult;
mult *= 10;
} // while
return retval;
} // strtoi64
Feb 8, 2010
Feb 8, 2010
278
static Expression *new_literal_int_expr(Context *ctx, const TokenData *data)
Feb 8, 2010
Feb 8, 2010
280
281
NEW_EXPR(ExpressionIntLiteral);
retval->op = OP_INT_LITERAL;
Feb 9, 2010
Feb 9, 2010
282
retval->value = data->i64;
283
284
285
286
287
288
289
290
291
292
293
294
return (Expression *) retval;
} // new_literal_int_expr
static inline double strtodouble(const char *_str, unsigned int len)
{
// !!! FIXME: laziness prevails.
char *str = (char *) alloca(len+1);
memcpy(str, _str, len);
str[len] = '\0';
return strtod(str, NULL);
} // strtodouble
Feb 8, 2010
Feb 8, 2010
295
static Expression *new_literal_float_expr(Context *ctx, const TokenData *data)
Feb 8, 2010
Feb 8, 2010
297
298
NEW_EXPR(ExpressionFloatLiteral);
retval->op = OP_FLOAT_LITERAL;
Feb 9, 2010
Feb 9, 2010
299
retval->value = data->dbl;
300
301
302
return (Expression *) retval;
} // new_literal_float_expr
Feb 8, 2010
Feb 8, 2010
303
static Expression *new_literal_string_expr(Context *ctx, const TokenData *data)
Feb 8, 2010
Feb 8, 2010
305
306
NEW_EXPR(ExpressionStringLiteral);
retval->op = OP_STRING_LITERAL;
Feb 9, 2010
Feb 9, 2010
307
retval->string = data->string; // cached; don't copy string.
308
309
310
return (Expression *) retval;
} // new_string_literal_expr
Feb 8, 2010
Feb 8, 2010
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
static void print_expr(const Expression *expr, const int depth)
{
int i;
for (i = 0; i < depth; i++)
printf(" ");
printf("Expression ");
switch (expr->op)
{
#define PRINT_OP(op) case op: printf("%s\n", #op); break;
PRINT_OP(OP_DEREF_ARRAY);
PRINT_OP(OP_CALLFUNC);
PRINT_OP(OP_DEREF_STRUCT);
PRINT_OP(OP_POSTINCREMENT);
PRINT_OP(OP_POSTDECREMENT);
PRINT_OP(OP_COMMA);
PRINT_OP(OP_PREINCREMENT);
PRINT_OP(OP_PREDECREMENT);
PRINT_OP(OP_NEGATE);
PRINT_OP(OP_COMPLEMENT);
PRINT_OP(OP_NOT);
PRINT_OP(OP_MULTIPLY);
PRINT_OP(OP_DIVIDE);
PRINT_OP(OP_MODULO);
PRINT_OP(OP_ADD);
PRINT_OP(OP_SUBTRACT);
PRINT_OP(OP_LSHIFT);
PRINT_OP(OP_RSHIFT);
PRINT_OP(OP_LESSTHAN);
PRINT_OP(OP_GREATERTHAN);
PRINT_OP(OP_LESSTHANOREQUAL);
PRINT_OP(OP_GREATERTHANOREQUAL);
PRINT_OP(OP_EQUAL);
PRINT_OP(OP_NOTEQUAL);
PRINT_OP(OP_BINARYAND);
PRINT_OP(OP_BINARYXOR);
PRINT_OP(OP_BINARYOR);
PRINT_OP(OP_LOGICALAND);
PRINT_OP(OP_LOGICALOR);
PRINT_OP(OP_CONDITIONAL);
PRINT_OP(OP_ASSIGN);
PRINT_OP(OP_MULASSIGN);
PRINT_OP(OP_DIVASSIGN);
PRINT_OP(OP_MODASSIGN);
PRINT_OP(OP_ADDASSIGN);
PRINT_OP(OP_SUBASSIGN);
PRINT_OP(OP_LSHIFTASSIGN);
PRINT_OP(OP_RSHIFTASSIGN);
PRINT_OP(OP_ANDASSIGN);
PRINT_OP(OP_XORASSIGN);
PRINT_OP(OP_ORASSIGN);
PRINT_OP(OP_INT_LITERAL);
PRINT_OP(OP_FLOAT_LITERAL);
PRINT_OP(OP_STRING_LITERAL);
PRINT_OP(OP_IDENTIFIER);
default: printf("---UNKNOWN!---\n"); return;
} // switch
if (operator_is_unary(expr->op))
{
const ExpressionUnary *unary = (const ExpressionUnary *) expr;
print_expr(unary->operand, depth + 1);
} // if
else if (operator_is_binary(expr->op))
{
const ExpressionBinary *binary = (const ExpressionBinary *) expr;
print_expr(binary->left, depth + 1);
print_expr(binary->right, depth + 1);
} // else if
else if (operator_is_ternary(expr->op))
{
const ExpressionTernary *ternary = (const ExpressionTernary *) expr;
print_expr(ternary->left, depth + 1);
print_expr(ternary->center, depth + 1);
print_expr(ternary->right, depth + 1);
} // else if
else
{
for (i = 0; i < (depth + 1); i++)
printf(" ");
if (expr->op == OP_IDENTIFIER)
{
const ExpressionIdentifier *ident = (const ExpressionIdentifier *) expr;
printf("(%s)\n", ident->identifier);
} // if
else if (expr->op == OP_INT_LITERAL)
{
const ExpressionIntLiteral *lit = (const ExpressionIntLiteral *) expr;
printf("(%lld)\n", (long long) lit->value);
} // if
else if (expr->op == OP_FLOAT_LITERAL)
{
const ExpressionFloatLiteral *lit = (const ExpressionFloatLiteral *) expr;
printf("(%lf)\n", lit->value);
} // if
else if (expr->op == OP_STRING_LITERAL)
{
const ExpressionStringLiteral *lit = (const ExpressionStringLiteral *) expr;
printf("(\"%s\")\n", lit->string);
} // if
else
{
assert(0 && "Shouldn't hit this.");
} // else
} // else
} // print_expr
Feb 8, 2010
Feb 8, 2010
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
static double run_expr(const Expression *expr)
{
if (operator_is_unary(expr->op))
{
const ExpressionUnary *unary = (const ExpressionUnary *) expr;
if (expr->op == OP_NEGATE)
return -run_expr(unary->operand);
else if (expr->op == OP_COMPLEMENT)
return (double) (~((int64)run_expr(unary->operand)));
else if (expr->op == OP_NOT)
return (run_expr(unary->operand) == 0.0) ? 1.0 : 0.0;
} // if
else if (operator_is_binary(expr->op))
{
const ExpressionBinary *binary = (const ExpressionBinary *) expr;
if (expr->op == OP_MULTIPLY)
return run_expr(binary->left) * run_expr(binary->right);
else if (expr->op == OP_DIVIDE)
return run_expr(binary->left) / run_expr(binary->right);
else if (expr->op == OP_ADD)
return run_expr(binary->left) + run_expr(binary->right);
else if (expr->op == OP_SUBTRACT)
return run_expr(binary->left) - run_expr(binary->right);
else if (expr->op == OP_LESSTHAN)
return (run_expr(binary->left) < run_expr(binary->right)) ? 1.0 : 0.0;
else if (expr->op == OP_GREATERTHAN)
return (run_expr(binary->left) > run_expr(binary->right)) ? 1.0 : 0.0;
else if (expr->op == OP_LESSTHANOREQUAL)
return (run_expr(binary->left) <= run_expr(binary->right)) ? 1.0 : 0.0;
else if (expr->op == OP_GREATERTHANOREQUAL)
return (run_expr(binary->left) >= run_expr(binary->right)) ? 1.0 : 0.0;
else if (expr->op == OP_EQUAL)
return (run_expr(binary->left) == run_expr(binary->right)) ? 1.0 : 0.0;
else if (expr->op == OP_NOTEQUAL)
return (run_expr(binary->left) == run_expr(binary->right)) ? 1.0 : 0.0;
else if (expr->op == OP_LOGICALAND)
return (((int64)run_expr(binary->left)) && ((int64)run_expr(binary->right))) ? 1.0 : 0.0;
else if (expr->op == OP_LOGICALOR)
return (((int64)run_expr(binary->left)) || ((int64)run_expr(binary->right))) ? 1.0 : 0.0;
else if (expr->op == OP_BINARYAND)
return (double)(((int64)run_expr(binary->left)) & ((int64)run_expr(binary->right)));
else if (expr->op == OP_BINARYOR)
return (double)(((int64)run_expr(binary->left)) | ((int64)run_expr(binary->right)));
else if (expr->op == OP_BINARYXOR)
return (double)(((int64)run_expr(binary->left)) ^ ((int64)run_expr(binary->right)));
else if (expr->op == OP_LSHIFT)
return (double)(((int64)run_expr(binary->left)) << ((int64)run_expr(binary->right)));
else if (expr->op == OP_RSHIFT)
return (double)(((int64)run_expr(binary->left)) >> ((int64)run_expr(binary->right)));
else if (expr->op == OP_MODULO)
return (double)(((int64)run_expr(binary->left)) % ((int64)run_expr(binary->right)));
} // else if
else if (operator_is_ternary(expr->op))
{
const ExpressionTernary *ternary = (const ExpressionTernary *) expr;
if (expr->op == OP_CONDITIONAL)
return (run_expr(ternary->left) != 0.0) ? run_expr(ternary->center) : run_expr(ternary->right);
} // else if
else
{
if (expr->op == OP_INT_LITERAL)
{
const ExpressionIntLiteral *lit = (const ExpressionIntLiteral *) expr;
return ((double) lit->value);
} // if
else if (expr->op == OP_FLOAT_LITERAL)
{
const ExpressionFloatLiteral *lit = (const ExpressionFloatLiteral *) expr;
return lit->value;
} // if
} // else
return 0.0; // oh well.
} // run_expr
Feb 8, 2010
Feb 8, 2010
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
static void free_expr(Context *ctx, Expression *expr)
{
if (operator_is_unary(expr->op))
{
const ExpressionUnary *unary = (const ExpressionUnary *) expr;
free_expr(ctx, unary->operand);
} // if
else if (operator_is_binary(expr->op))
{
const ExpressionBinary *binary = (const ExpressionBinary *) expr;
free_expr(ctx, binary->left);
free_expr(ctx, binary->right);
} // else if
else if (operator_is_ternary(expr->op))
{
const ExpressionTernary *ternary = (const ExpressionTernary *) expr;
free_expr(ctx, ternary->left);
free_expr(ctx, ternary->center);
free_expr(ctx, ternary->right);
} // else if
Feb 9, 2010
Feb 9, 2010
517
518
519
// don't need to free extra fields in other types at the moment.
Feb 8, 2010
Feb 8, 2010
520
521
522
523
Free(ctx, expr);
} // free_expr
static void parse_complete(Context *ctx, Expression *expr)
Feb 8, 2010
Feb 8, 2010
524
525
{
print_expr(expr, 0);
Feb 8, 2010
Feb 8, 2010
526
printf("Result: %lf\n\n", run_expr(expr));
Feb 8, 2010
Feb 8, 2010
527
free_expr(ctx, expr);
Feb 8, 2010
Feb 8, 2010
528
529
530
} // parse_complete
Feb 9, 2010
Feb 9, 2010
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
// !!! FIXME: sort of cut-and-paste from the preprocessor...
// this is djb's xor hashing function.
static inline uint32 hash_string_djbxor(const char *str, unsigned int len)
{
register uint32 hash = 5381;
while (len--)
hash = ((hash << 5) + hash) ^ *(str++);
return hash;
} // hash_string_djbxor
static inline uint8 hash_string(const char *str, const unsigned int len)
{
return (uint8) hash_string_djbxor(str, len);
} // hash_string
static const char *cache_string(Context *ctx, const char *str,
const unsigned int len)
{
const uint8 hash = hash_string(str, len);
StringBucket *bucket = ctx->string_hashtable[hash];
StringBucket *prev = NULL;
while (bucket)
{
const char *bstr = bucket->string;
if ((strncmp(bstr, str, len) == 0) && (bstr[len] == 0))
{
// Matched! Move this to the front of the list.
if (prev != NULL)
{
assert(prev->next == bucket);
prev->next = bucket->next;
bucket->next = ctx->string_hashtable[hash];
ctx->string_hashtable[hash] = bucket;
} // if
return bstr; // already cached
} // if
prev = bucket;
bucket = bucket->next;
} // while
// no match, add to the table.
bucket = (StringBucket *) Malloc(ctx, sizeof (StringBucket));
if (bucket == NULL)
return NULL;
bucket->string = (char *) Malloc(ctx, len + 1);
if (bucket->string == NULL)
{
Free(ctx, bucket);
return NULL;
} // if
memcpy(bucket->string, str, len);
bucket->string[len] = '\0';
bucket->next = ctx->string_hashtable[hash];
ctx->string_hashtable[hash] = bucket;
return bucket->string;
} // cache_string
static void free_string_cache(Context *ctx)
{
size_t i;
for (i = 0; i < STATICARRAYLEN(ctx->string_hashtable); i++)
{
StringBucket *bucket = ctx->string_hashtable[i];
ctx->string_hashtable[i] = NULL;
while (bucket)
{
StringBucket *next = bucket->next;
Free(ctx, bucket->string);
Free(ctx, bucket);
bucket = next;
} // while
} // for
} // free_string_cache
Feb 8, 2010
Feb 8, 2010
607
608
609
// This is where the actual parsing happens. It's Lemon-generated!
#define __MOJOSHADER_CALC_COMPILER__ 1
#include "calculator.h"
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
static int convert_to_lemon_token(const Context *ctx)
{
switch (ctx->tokenval)
{
case ((Token) ','): return TOKEN_CALC_COMMA;
case ((Token) '='): return TOKEN_CALC_ASSIGN;
case ((Token) TOKEN_ADDASSIGN): return TOKEN_CALC_ADDASSIGN;
case ((Token) TOKEN_SUBASSIGN): return TOKEN_CALC_SUBASSIGN;
case ((Token) TOKEN_MULTASSIGN): return TOKEN_CALC_MULASSIGN;
case ((Token) TOKEN_DIVASSIGN): return TOKEN_CALC_DIVASSIGN;
case ((Token) TOKEN_MODASSIGN): return TOKEN_CALC_MODASSIGN;
case ((Token) TOKEN_LSHIFTASSIGN): return TOKEN_CALC_LSHIFTASSIGN;
case ((Token) TOKEN_RSHIFTASSIGN): return TOKEN_CALC_RSHIFTASSIGN;
case ((Token) TOKEN_ANDASSIGN): return TOKEN_CALC_ANDASSIGN;
case ((Token) TOKEN_ORASSIGN): return TOKEN_CALC_ORASSIGN;
case ((Token) TOKEN_XORASSIGN): return TOKEN_CALC_XORASSIGN;
case ((Token) '?'): return TOKEN_CALC_QUESTION;
case ((Token) TOKEN_OROR): return TOKEN_CALC_OROR;
case ((Token) TOKEN_ANDAND): return TOKEN_CALC_ANDAND;
case ((Token) '|'): return TOKEN_CALC_OR;
case ((Token) '^'): return TOKEN_CALC_XOR;
case ((Token) '&'): return TOKEN_CALC_AND;
case ((Token) TOKEN_EQL): return TOKEN_CALC_EQL;
case ((Token) TOKEN_NEQ): return TOKEN_CALC_NEQ;
case ((Token) '<'): return TOKEN_CALC_LT;
case ((Token) TOKEN_LEQ): return TOKEN_CALC_LEQ;
case ((Token) '>'): return TOKEN_CALC_GT;
case ((Token) TOKEN_GEQ): return TOKEN_CALC_GEQ;
case ((Token) TOKEN_LSHIFT): return TOKEN_CALC_LSHIFT;
case ((Token) TOKEN_RSHIFT): return TOKEN_CALC_RSHIFT;
case ((Token) '+'): return TOKEN_CALC_PLUS;
case ((Token) '-'): return TOKEN_CALC_MINUS;
case ((Token) '*'): return TOKEN_CALC_STAR;
case ((Token) '/'): return TOKEN_CALC_SLASH;
case ((Token) '%'): return TOKEN_CALC_PERCENT;
case ((Token) '!'): return TOKEN_CALC_EXCLAMATION;
case ((Token) '~'): return TOKEN_CALC_COMPLEMENT;
case ((Token) TOKEN_DECREMENT): return TOKEN_CALC_MINUSMINUS;
case ((Token) TOKEN_INCREMENT): return TOKEN_CALC_PLUSPLUS;
case ((Token) '.'): return TOKEN_CALC_DOT;
case ((Token) '['): return TOKEN_CALC_LBRACKET;
case ((Token) ']'): return TOKEN_CALC_RBRACKET;
case ((Token) '('): return TOKEN_CALC_LPAREN;
case ((Token) ')'): return TOKEN_CALC_RPAREN;
case ((Token) TOKEN_INT_LITERAL): return TOKEN_CALC_INT_CONSTANT;
case ((Token) TOKEN_FLOAT_LITERAL): return TOKEN_CALC_FLOAT_CONSTANT;
case ((Token) TOKEN_STRING_LITERAL): return TOKEN_CALC_STRING_LITERAL;
case ((Token) ':'): return TOKEN_CALC_COLON;
//case ((Token) ';'): return TOKEN_CALC_SEMICOLON;
//case ((Token) '{'): return TOKEN_CALC_LBRACE;
//case ((Token) '}'): return TOKEN_CALC_RBRACE;
case ((Token) TOKEN_IDENTIFIER): return TOKEN_CALC_IDENTIFIER;
case TOKEN_EOI: return 0;
case TOKEN_BAD_CHARS: printf("bad chars from lexer\n"); return 0;
case TOKEN_PREPROCESSING_ERROR: printf("error from lexer\n"); return 0;
default: assert(0 && "unexpected token from lexer\n"); return 0;
} // switch
return 0;
} // convert_to_lemon_token
static void MOJOSHADER_compile(const char *filename,
const char *source, unsigned int sourcelen,
const MOJOSHADER_preprocessorDefine *defines,
unsigned int define_count,
MOJOSHADER_includeOpen include_open,
MOJOSHADER_includeClose include_close,
MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
{
Context ctx;
Feb 9, 2010
Feb 9, 2010
681
682
TokenData data;
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
if (m == NULL) m = MOJOSHADER_internal_malloc;
if (f == NULL) f = MOJOSHADER_internal_free;
memset(&ctx, '\0', sizeof (Context));
ctx.malloc = m;
ctx.free = f;
ctx.malloc_data = d;
ctx.preprocessor = preprocessor_start(filename, source, sourcelen,
include_open, include_close,
defines, define_count, 0, m, f, d);
void *pParser = ParseCalculatorAlloc(m, d);
#if DEBUG_COMPILER_PARSER
ParseCalculatorTrace(stdout, "COMPILER: ");
#endif
do {
Feb 9, 2010
Feb 9, 2010
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
ctx.token = preprocessor_nexttoken(ctx.preprocessor, &ctx.tokenlen,
&ctx.tokenval);
const int lemon_token = convert_to_lemon_token(&ctx);
switch (lemon_token)
{
case TOKEN_CALC_INT_CONSTANT:
data.i64 = strtoi64(ctx.token, ctx.tokenlen);
break;
case TOKEN_CALC_FLOAT_CONSTANT:
data.dbl = strtodouble(ctx.token, ctx.tokenlen);
break;
case TOKEN_CALC_STRING_LITERAL:
case TOKEN_CALC_IDENTIFIER:
data.string = cache_string(&ctx, ctx.token, ctx.tokenlen);
break;
default:
data.i64 = 0;
break;
} // switch
ParseCalculator(pParser, lemon_token, data, &ctx);
Feb 8, 2010
Feb 8, 2010
726
} while ((!ctx.isfail) && (ctx.tokenval != TOKEN_EOI));
Feb 9, 2010
Feb 9, 2010
727
728
ParseCalculatorFree(pParser, f, d);
Feb 9, 2010
Feb 9, 2010
729
730
731
// !!! FIXME: destruct (ctx) here.
free_string_cache(&ctx);
} // MOJOSHADER_compile
732
733
734
735
736
737
738
739
740
741
int main(int argc, char **argv)
{
const char *ln;
size_t len = 0;
FILE *io = stdin;
const char *filename = "<stdin>";
while ((ln = fgetln(io, &len)) != NULL)
{
Feb 8, 2010
Feb 8, 2010
742
743
744
if (len == 1)
continue;
else if ((len == 5) && (memcmp(ln, "quit\n", 5) == 0))
745
break;
Feb 8, 2010
Feb 8, 2010
746
747
else if ((len == 2) && (memcmp(ln, "q\n", 2) == 0))
break;
748
749
750
751
752
753
754
755
756
757
MOJOSHADER_compile(filename, ln, (unsigned int) len,
NULL, 0, NULL, NULL, NULL, NULL, NULL);
} // while
fclose(io);
return 0;
} // main
// end of calculator.c ...