Skip to content

Latest commit

 

History

History
427 lines (372 loc) · 12.3 KB

calculator.c

File metadata and controls

427 lines (372 loc) · 12.3 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#define __MOJOSHADER_INTERNAL__ 1
#include "mojoshader_internal.h"
#if DEBUG_COMPILER_PARSER
#define LEMON_SUPPORT_TRACING 1
#endif
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;
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
{
57
58
59
60
61
62
63
OP_POSTINCREMENT,
OP_POSTDECREMENT,
OP_PREINCREMENT,
OP_PREDECREMENT,
OP_NEGATE,
OP_COMPLEMENT,
OP_NOT,
64
65
66
67
68
69
70
OP_END_RANGE_UNARY_OPERATORS,
OP_START_RANGE_BINARY_OPERATORS,
OP_DEREF_ARRAY,
OP_CALLFUNC,
OP_DEREF_STRUCT,
OP_COMMA,
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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,
100
101
102
103
104
105
106
107
108
109
110
111
OP_END_RANGE_BINARY_OPERATORS,
OP_START_RANGE_TERNARY_OPERATORS,
OP_CONDITIONAL,
OP_END_RANGE_TERNARY_OPERATORS,
OP_START_RANGE_DATA,
OP_IDENTIFIER,
OP_INT_LITERAL,
OP_FLOAT_LITERAL,
OP_STRING_LITERAL,
OP_END_RANGE_DATA,
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
} Operator;
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
{
147
148
149
150
151
const char *identifier;
} ExpressionIdentifier;
typedef struct ExpressionLiteralInt
{
153
154
155
156
157
int64 value;
} ExpressionLiteralInt;
typedef struct ExpressionLiteralFloat
{
159
160
161
162
163
double value;
} ExpressionLiteralFloat;
typedef struct ExpressionLiteralString
{
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
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
420
421
422
423
424
425
426
const char *string;
} ExpressionLiteralString;
static const char *new_identifier(Context *);
static Expression *new_unary_expr(Context *, const Operator, Expression *);
static Expression *new_binary_expr(Context *, const Operator, Expression *, Expression *);
static Expression *new_ternary_expr(Context *, const Operator, Expression *, Expression *, Expression *);
static Expression *new_identifier_expr(Context *, const char *);
static Expression *new_literal_int_expr(Context *);
static Expression *new_literal_float_expr(Context *);
static Expression *new_literal_string_expr(Context *);
static void parse_complete(const Expression *expr)
{
printf("parse complete!\n");
} // parse_complete
// This is where the actual parsing happens. It's Lemon-generated!
#define __MOJOSHADER_CALC_COMPILER__ 1
#include "calculator.h"
static const char *new_identifier(Context *ctx)
{
// !!! FIXME: this needs to cache strings.
const unsigned int len = ctx->tokenlen;
char *retval = Malloc(ctx, len + 1);
if (retval == NULL)
return NULL;
memcpy(retval, ctx->token, len);
retval[len] = '\0';
return retval;
} // new_identifier
static Expression *new_unary_expr(Context *ctx, const Operator op,
Expression *operand)
{
NEW_EXPR(ExpressionUnary);
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);
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);
retval->op = op;
retval->left = left;
retval->center = center;
retval->right = right;
return (Expression *) retval;
} // new_ternary_expr
static Expression *new_identifier_expr(Context *ctx, const char *identifier)
{
NEW_EXPR(ExpressionIdentifier);
retval->op = TOKEN_CALC_IDENTIFIER;
retval->identifier = identifier;
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
static Expression *new_literal_int_expr(Context *ctx)
{
NEW_EXPR(ExpressionLiteralInt);
retval->op = TOKEN_CALC_INT_CONSTANT;
retval->value = strtoi64(ctx->token, ctx->tokenlen);
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
static Expression *new_literal_float_expr(Context *ctx)
{
NEW_EXPR(ExpressionLiteralFloat);
retval->op = TOKEN_CALC_FLOAT_CONSTANT;
retval->value = strtodouble(ctx->token, ctx->tokenlen);
return (Expression *) retval;
} // new_literal_float_expr
static Expression *new_literal_string_expr(Context *ctx)
{
NEW_EXPR(ExpressionLiteralString);
retval->op = TOKEN_CALC_STRING_LITERAL;
retval->string = new_identifier(ctx);
return (Expression *) retval;
} // new_string_literal_expr
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;
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 {
ctx.token = preprocessor_nexttoken(ctx.preprocessor,
&ctx.tokenlen,
&ctx.tokenval);
ParseCalculator(pParser, convert_to_lemon_token(&ctx), 0, &ctx);
} while (ctx.tokenval != TOKEN_EOI);
ParseCalculatorFree(pParser, f, d);
}
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)
{
if ((len == 5) && (memcmp(ln, "quit\n", 5) == 0))
break;
MOJOSHADER_compile(filename, ln, (unsigned int) len,
NULL, 0, NULL, NULL, NULL, NULL, NULL);
} // while
fclose(io);
return 0;
} // main
// end of calculator.c ...