-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4coder_kaitsh_calc.cpp
594 lines (538 loc) · 18.6 KB
/
4coder_kaitsh_calc.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
100
101
102
103
104
105
106
107
108
109
110
111
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
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
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
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
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
enum CalcTokenType
{
CALC_TOKEN_TYPE_Invalid,
CALC_TOKEN_TYPE_Number,
CALC_TOKEN_TYPE_String,
CALC_TOKEN_TYPE_Plus,
CALC_TOKEN_TYPE_Minus,
CALC_TOKEN_TYPE_Asterisk,
CALC_TOKEN_TYPE_Slash,
CALC_TOKEN_TYPE_Percent,
CALC_TOKEN_TYPE_Circumflex,
CALC_TOKEN_TYPE_OpenParen,
CALC_TOKEN_TYPE_CloseParen,
CALC_TOKEN_TYPE_NewLine
};
// NOTE(dgl): Cool trick from Ryan Fleury (https://github.com/ryanfleury) to define enum and a corresponding value at the same place. First we create a list containing a preprocessor functions which take the values. Then we create an enum with the types. In the enum we define the preprocessor function used in the list and we call the list. And undefine the function, so we can redefine it. For the other value we create a function with a static table. To create this table we define the preprocessor function again, call the list and undefine the funciton again.
#define CALC_NODE_TYPE_LIST \
CALC_NODE_TYPE(Error, 0)\
CALC_NODE_TYPE(Constant, 0)\
CALC_NODE_TYPE(OpenParen, 0)\
CALC_NODE_TYPE(CloseParen, 0)\
CALC_NODE_TYPE(Add, 1)\
CALC_NODE_TYPE(Subtract, 1)\
CALC_NODE_TYPE(Multiply, 2)\
CALC_NODE_TYPE(Divide, 2)\
CALC_NODE_TYPE(Modulus, 2)\
CALC_NODE_TYPE(RaiseToPower, 3)
enum CalcNodeType
{
#define CALC_NODE_TYPE(name, precedence) CALC_NODE_TYPE_##name,
CALC_NODE_TYPE_LIST
#undef CALC_NODE_TYPE
};
static int
CalcOperatorPrecedence(CalcNodeType type)
{
local_persist int precedence_table[] =
{
#define CALC_NODE_TYPE(name, precedence) precedence,
CALC_NODE_TYPE_LIST
#undef CALC_NODE_TYPE
};
return precedence_table[type];
}
struct CalcToken
{
CalcTokenType Type;
union
{
u8 *Data;
char *String;
};
int Length;
};
struct CalcNode
{
CalcNodeType Type;
double Value;
CalcNode *Next;
};
struct CalcMemory
{
u32 Size;
void* Buffer;
CalcNode *NodeBufferPtr;
CalcNode **OperatorStackPtr;
};
struct CalcTokenizer
{
u8 *At;
};
b32 IsNumeric(char c)
{
return(('0' <= c) && (c <= '9'));
}
b32 IsAlpha(char c)
{
return(('a' <= c) && (c <= 'z') ||
('A' <= c) && (c <= 'Z'));
}
internal CalcToken
GetToken(CalcTokenizer *Tokenizer)
{
CalcToken Token = {};
Token.Type = CALC_TOKEN_TYPE_Invalid;
Token.Data = Tokenizer->At;
Token.Length = 1;
char C = *Tokenizer->At;
++Tokenizer->At;
switch(C)
{
case '+': { Token.Type = CALC_TOKEN_TYPE_Plus; } break;
case '-': { Token.Type = CALC_TOKEN_TYPE_Minus; } break;
case '*': { Token.Type = CALC_TOKEN_TYPE_Asterisk; } break;
case '/': { Token.Type = CALC_TOKEN_TYPE_Slash; } break;
case '%': { Token.Type = CALC_TOKEN_TYPE_Percent; } break;
case '^': { Token.Type = CALC_TOKEN_TYPE_Circumflex; } break;
case '(': { Token.Type = CALC_TOKEN_TYPE_OpenParen; } break;
case ')': { Token.Type = CALC_TOKEN_TYPE_CloseParen; } break;
case '\n': { Token.Type = CALC_TOKEN_TYPE_NewLine; } break;
default:
{
if(IsNumeric(C))
{
Token.Type = CALC_TOKEN_TYPE_Number;
while(IsNumeric(*Tokenizer->At) ||
(*Tokenizer->At == '.') ||
(*Tokenizer->At == 'f'))
{
++Tokenizer->At;
Token.Length = Tokenizer->At - Token.Data;
}
}
else if(IsAlpha(C))
{
Token.Type = CALC_TOKEN_TYPE_String;
while(IsAlpha(*Tokenizer->At))
{
++Tokenizer->At;
Token.Length = Tokenizer->At - Token.Data;
}
}
else
{
Token.Type = CALC_TOKEN_TYPE_Invalid;
}
} break;
}
return(Token);
}
internal CalcToken
PeekToken(CalcTokenizer *Tokenizer)
{
CalcTokenizer Tokenizer2 = *Tokenizer;
CalcToken Result = GetToken(&Tokenizer2);
return(Result);
}
internal CalcNode
ParseNode(CalcTokenizer *Tokenizer, CalcToken *Token)
{
CalcNode Result = {};
Result.Type = CALC_NODE_TYPE_Error;
switch(Token->Type)
{
case CALC_TOKEN_TYPE_OpenParen: { Result.Type = CALC_NODE_TYPE_OpenParen; } break;
case CALC_TOKEN_TYPE_CloseParen: { Result.Type = CALC_NODE_TYPE_CloseParen; } break;
case CALC_TOKEN_TYPE_Plus: { Result.Type = CALC_NODE_TYPE_Add; } break;
case CALC_TOKEN_TYPE_Minus: { Result.Type = CALC_NODE_TYPE_Subtract; } break;
case CALC_TOKEN_TYPE_Asterisk:
{
CalcToken Peeked = PeekToken(Tokenizer);
if(Peeked.Type == CALC_TOKEN_TYPE_Slash)
{
Result.Type = CALC_NODE_TYPE_Error;
GetToken(Tokenizer);
}
else
{
Result.Type = CALC_NODE_TYPE_Multiply;
}
} break;
case CALC_TOKEN_TYPE_Slash:
{
CalcToken Peeked = PeekToken(Tokenizer);
if((Peeked.Type == CALC_TOKEN_TYPE_Slash) ||
(Peeked.Type == CALC_TOKEN_TYPE_Asterisk))
{
Result.Type = CALC_NODE_TYPE_Error;
GetToken(Tokenizer);
}
else
{
Result.Type = CALC_NODE_TYPE_Divide;
}
} break;
case CALC_TOKEN_TYPE_Percent: { Result.Type = CALC_NODE_TYPE_Modulus; } break;
case CALC_TOKEN_TYPE_Circumflex: { Result.Type = CALC_NODE_TYPE_RaiseToPower; } break;
case CALC_TOKEN_TYPE_Number:
{
Result.Type = CALC_NODE_TYPE_Constant;
char *StringEnd = Token->String + (Token->Length - 1);
Result.Value = strtod(Token->String, &StringEnd);
} break;
case CALC_TOKEN_TYPE_String:
{
String_Const_char TokenText = {};
TokenText.str = Token->String;
TokenText.size = Token->Length;
if((string_compare(TokenText, string_litexpr("pi")) == 0) ||
(string_compare(TokenText, string_litexpr("PI")) == 0))
{
Result.Type = CALC_NODE_TYPE_Constant;
Result.Value = 3.1415926535897932;
}
else if((string_compare(TokenText, string_litexpr("e")) == 0) ||
(string_compare(TokenText, string_litexpr("E")) == 0))
{
Result.Type = CALC_NODE_TYPE_Constant;
Result.Value = 2.7182818284590452;
}
else
{
Result.Type = CALC_NODE_TYPE_Error;
}
} break;
default: { Result.Type = CALC_NODE_TYPE_Error; } break;
}
return(Result);
}
internal int
PushOperator(CalcMemory *Memory, CalcNode *Node)
{
int Result = -1;
if(((CalcNode **)Memory->NodeBufferPtr < (Memory->OperatorStackPtr - 1)) &&
(Memory->OperatorStackPtr <= (CalcNode **)((u8 *)Memory->Buffer + Memory->Size)))
{
*Memory->OperatorStackPtr = Node;
Memory->OperatorStackPtr--;
Result = 0;
}
return Result;
}
internal CalcNode *
PeekOperator(CalcMemory *Memory)
{
CalcNode *Result = 0;
if(Memory->OperatorStackPtr < (CalcNode **)((u8 *)Memory->Buffer + Memory->Size))
{
Result = *(Memory->OperatorStackPtr + 1);
}
return(Result);
}
internal CalcNode *
PopOperator(CalcMemory *Memory)
{
CalcNode *Result = PeekOperator(Memory);
if(Result)
{
Memory->OperatorStackPtr++;
Assert(Memory->OperatorStackPtr <= (CalcNode **)((u8 *)Memory->Buffer + Memory->Size));
}
return(Result);
}
internal b32
OperatorStackEmpty(CalcMemory *Memory)
{
return(Memory->OperatorStackPtr >= (CalcNode **)((u8 *)Memory->Buffer + Memory->Size));
}
internal CalcNode *
ParseLineToPostfix(CalcTokenizer *Tokenizer, CalcMemory *Memory, void *Buffer, u64 Size)
{
// NOTE(dgl): Temp starting point of the list, because we only assign nodes to the "Next" field of the stucture.
CalcNode TmpRoot = {};
CalcNode *Last = &TmpRoot;
b32 ParsingError = false;
CalcToken Token;
while(!ParsingError && Tokenizer->At < ((u8 *)Buffer + Size))
{
Token = GetToken(Tokenizer);
if(Token.Type == CALC_TOKEN_TYPE_NewLine)
{
ParsingError = true;
}
CalcNode Parsed = ParseNode(Tokenizer, &Token);
if(Parsed.Type != CALC_NODE_TYPE_Error)
{
CalcNode *Node;
if((Memory->NodeBufferPtr + 1) < (CalcNode *)Memory->OperatorStackPtr)
{
Node = Memory->NodeBufferPtr++;
Node->Type = Parsed.Type;
Node->Value = Parsed.Value;
Node->Next = 0;
switch(Node->Type)
{
case CALC_NODE_TYPE_OpenParen:
{
if(PushOperator(Memory, Node) < 0)
{
ParsingError = true;
}
} break;
case CALC_NODE_TYPE_CloseParen:
{
CalcNode *Next;
while((Next = PopOperator(Memory)) &&
(Next->Type != CALC_NODE_TYPE_OpenParen))
{
Last->Next = Next;
Last = Next;
}
} break;
case CALC_NODE_TYPE_Add:
case CALC_NODE_TYPE_Subtract:
case CALC_NODE_TYPE_Multiply:
case CALC_NODE_TYPE_Divide:
case CALC_NODE_TYPE_Modulus:
case CALC_NODE_TYPE_RaiseToPower:
{
while(!OperatorStackEmpty(Memory) && (CalcOperatorPrecedence(PeekOperator(Memory)->Type) >= CalcOperatorPrecedence(Node->Type)))
{
CalcNode *Next = PopOperator(Memory);
Last->Next = Next;
Last = Next;
}
if(PushOperator(Memory, Node) < 0)
{
ParsingError = true;
}
} break;
case CALC_NODE_TYPE_Constant:
{
Last->Next = Node;
Last = Node;
} break;
default:
{
// TODO(dgl): ignore everything else
} break;
}
}
else
{
ParsingError = true;
}
}
}
// NOTE(dgl): Empty operator stack
while(!OperatorStackEmpty(Memory))
{
CalcNode *Next = PopOperator(Memory);
Last->Next = Next;
Last = Next;
}
Assert(Memory->OperatorStackPtr == (CalcNode **)((u8 *)Memory->Buffer + Memory->Size));
// NOTE(dgl): The first "real" node is the one after root
return(TmpRoot.Next);
}
internal double
EvaluatePostfix(CalcMemory *Memory, CalcNode *Start)
{
CalcNode *Node = Start;
b32 IsValid = true;
while(IsValid && (Node != 0))
{
// TODO(dgl): Compress calculation
switch(Node->Type)
{
case CALC_NODE_TYPE_Constant: { PushOperator(Memory, Node); } break;
case CALC_NODE_TYPE_Add:
{
CalcNode *Right = PopOperator(Memory);
CalcNode *Left = PopOperator(Memory);
if(Right && Left)
{
// NOTE(dgl): The node we store the result does not matter
Left->Value = Left->Value + Right->Value;
PushOperator(Memory, Left);
}
else
{
IsValid = false;
}
} break;
case CALC_NODE_TYPE_Subtract:
{
CalcNode *Right = PopOperator(Memory);
CalcNode *Left = PopOperator(Memory);
if(Right && Left)
{
Left->Value = Left->Value - Right->Value;
PushOperator(Memory, Left);
}
else
{
IsValid = false;
}
} break;
case CALC_NODE_TYPE_Multiply:
{
CalcNode *Right = PopOperator(Memory);
CalcNode *Left = PopOperator(Memory);
if(Right && Left)
{
Left->Value = Left->Value * Right->Value;
PushOperator(Memory, Left);
}
else
{
IsValid = false;
}
} break;
case CALC_NODE_TYPE_Divide:
{
CalcNode *Right = PopOperator(Memory);
CalcNode *Left = PopOperator(Memory);
if(Right && Left && (Right->Value != 0))
{
Left->Value = Left->Value / Right->Value;
PushOperator(Memory, Left);
}
else
{
IsValid = false;
}
} break;
case CALC_NODE_TYPE_Modulus:
{
CalcNode *Right = PopOperator(Memory);
CalcNode *Left = PopOperator(Memory);
if(Right && Left && (Right->Value != 0))
{
Left->Value = fmod(Left->Value, Right->Value);
PushOperator(Memory, Left);
}
else
{
IsValid = false;
}
} break;
case CALC_NODE_TYPE_RaiseToPower:
{
CalcNode *Right = PopOperator(Memory);
CalcNode *Left = PopOperator(Memory);
if(Right && Left)
{
Left->Value = pow(Left->Value, Right->Value);
PushOperator(Memory, Left);
}
else
{
IsValid = false;
}
} break;
default:
{
// TODO(dgl): should not happen (logging)
} break;
}
if(Node->Next)
{
Node = Node->Next;
}
else
{
Node = 0;
}
}
double Result = NAN;
if(IsValid && !OperatorStackEmpty(Memory))
{
Node = PopOperator(Memory);
Result = Node->Value;
}
return(Result);
}
internal void
RenderCommentCode(Application_Links *app, Buffer_ID buffer, Text_Layout_ID text_layout_id, i64 Position, String_Const_u8 TextBuffer)
{
CalcTokenizer Tokenizer = {};
Tokenizer.At = TextBuffer.str;
// TODO(dgl): I don't like this solution. Maybe I have a better solution in the future.
if(Tokenizer.At[2] == 'c')
{
u8 MemoryBuffer[1024*1024];
CalcMemory Memory = {};
Memory.Buffer = (void *)MemoryBuffer;
Memory.Size = ArrayCount(MemoryBuffer);
Memory.NodeBufferPtr = (CalcNode *)Memory.Buffer;
while(Tokenizer.At < (TextBuffer.str + TextBuffer.size))
{
// NOTE(dgl): Reset OperatorStackPtr for each calculation
Memory.OperatorStackPtr = (CalcNode **)((u8 *)Memory.Buffer + Memory.Size);
CalcNode *Root = ParseLineToPostfix(&Tokenizer, &Memory, TextBuffer.data, TextBuffer.size);
if(Root)
{
// TODO(dgl): Support calculating with variables or functions
double Result = EvaluatePostfix(&Memory, Root);
char ResultBuffer[256];
String_Const_u8 ResultString = {(u8 *)ResultBuffer};
if((Memory.NodeBufferPtr + 1) < (CalcNode *)Memory.OperatorStackPtr)
{
ResultString.size = sprintf(ResultBuffer, "=> %f", Result);
}
else
{
// NOTE(dgl): Currently for debugging. Output may change in the future
ResultString.size = sprintf(ResultBuffer, "=> Buffer Overflow");
}
Vec2_f32 ResultPosition = {0};
u64 PositionOffset = Tokenizer.At - 1 - TextBuffer.str;
u64 LastCharacterPosition = Position + PositionOffset;
Rect_f32 LastCharacterRect = text_layout_character_on_screen(app, text_layout_id, LastCharacterPosition);
ResultPosition.x = LastCharacterRect.x0;
ResultPosition.y = LastCharacterRect.y0;
ResultPosition.x += 15;
ARGB_Color Color = finalize_color(defcolor_comment, 1);
draw_string(app, get_face_id(app, buffer), ResultString, ResultPosition, Color);
}
}
}
else
{
// TODO(dgl): Do nothing if it is not a calc comment
}
}
internal void
KaitshRenderCommentCalc(Application_Links *app, Buffer_ID buffer, Text_Layout_ID text_layout_id)
{
// NOTE(dgl): similar to draw_comment_highlights
Token_Array token_array = get_token_array_from_buffer(app, buffer);
if (token_array.tokens != 0)
{
Scratch_Block scratch(app);
Range_i64 visible_range = text_layout_get_visible_range(app, text_layout_id);
i64 first_index = token_index_from_pos(&token_array, visible_range.first);
Token_Iterator_Array it = token_iterator_index(buffer, &token_array, first_index);
while(true)
{
Temp_Memory_Block temp(scratch);
Token *token = token_it_read(&it);
if(token->pos >= visible_range.one_past_last)
{
break;
}
String_Const_u8 tail = {};
if(token_it_check_and_get_lexeme(app, scratch, &it, TokenBaseKind_Comment, &tail))
{
// TODO(dgl): Render only Calc Comment
RenderCommentCode(app, buffer, text_layout_id, token->pos, tail);
}
if(!token_it_inc_non_whitespace(&it))
{
break;
}
}
}
}