-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirth.c
More file actions
2009 lines (1617 loc) · 44.6 KB
/
Copy pathfirth.c
File metadata and controls
2009 lines (1617 loc) · 44.6 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
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include <errno.h>
#if defined(_WIN32) || defined(_WIN64)
# include <direct.h>
# define CHDIR _chdir
# define GETCWD _getcwd
#else
# include <unistd.h>
# define CHDIR chdir
# define GETCWD getcwd
#endif
#include "firth.h"
#if FTH_INCLUDE_FLOAT == 1
# include "firth_float.h"
#endif
#if FTH_CASE_SENSITIVE == 1
# define STRING_COMPARE strcmp
#else
#if defined(_WIN32) || defined(_WIN64)
# define STRING_COMPARE _stricmp
#else
# define STRING_COMPARE strcasecmp
#endif
#endif
//
bool true = ~0;
bool false = 0;
static DictionaryEntry *fth_tick_internal(FirthState *pFirth, const char *word);
#if 0
# define LOG(s) puts(s);
#else
# define LOG(s)
#endif
//
static void forth_default_output(char *s)
{
puts(s);
}
// Firth formatted output function
void firth_printf(FirthState *pFirth, char *format, ...)
{
char buf[FTH_MAX_PRINTF_SIZE];
va_list valist;
va_start(valist, format);
vsnprintf(buf, FTH_MAX_PRINTF_SIZE, format, valist);
va_end(valist);
pFirth->firth_print(buf);
}
//
static FirthNumber isInteger(const char *s)
{
if (*s != '-' && *s != '+' && !isdigit(*s))
return FTH_FALSE;
// bare '+' or '-' with no following digit is not an integer
if ((*s == '-' || *s == '+') && !isdigit(*(s + 1)))
return FTH_FALSE;
s++;
for (; *s; s++)
{
if (!isdigit(*s))
return FTH_FALSE;
}
return FTH_TRUE;
}
// given an xt get the data pointer
static FirthNumber* fth_body_internal(DictionaryEntry *xt)
{
return (FirthNumber*)(xt->name + xt->name_len + 1);
}
// implements >BODY ( addr -- d )
static FirthNumber fth_body(FirthState *pFirth)
{
DictionaryEntry *xt = (DictionaryEntry *)fth_pop(pFirth);
return fth_push(pFirth, (FirthNumber)(FirthNumber*)(xt->name + xt->name_len + 1));
}
// push a value onto the return stack
static FirthNumber fth_r_push(FirthState *pFirth, FirthNumber val)
{
// check for return stack overflow
if ((pFirth->RP - pFirth->return_stack) + 1 > FTH_RETURN_STACK_SIZE)
{
pFirth->firth_print("Return Stack overflow!\n");
return FTH_FALSE;
}
*pFirth->RP++ = val;
FirthNumber depth = pFirth->RP - pFirth->return_stack;
if (depth > pFirth->maxr)
pFirth->maxr = depth;
return FTH_TRUE;
}
// pop a value off the return stack
static FirthNumber fth_r_pop(FirthState *pFirth)
{
// check for stack underflow
if (pFirth->RP <= pFirth->return_stack)
{
pFirth->firth_print("Return Stack underflow!\n");
return FTH_FALSE;
}
pFirth->RP--;
return *pFirth->RP;
}
// push the top of return stack onto the stack
static FirthNumber fth_r_fetch(FirthState *pFirth)
{
// check for stack underflow
if (pFirth->RP <= pFirth->return_stack)
{
pFirth->firth_print("Return Stack underflow!\n");
return FTH_FALSE;
}
return fth_push(pFirth, *(pFirth->RP - 1));
}
// return pointer to the base filename without path
static const char *fth_basename(const char *s)
{
if (!strchr(s, FTH_DIR_SEPARATOR))
return s;
const char *p = s + strlen(s);
for (; p != s; p--)
if (*p == FTH_DIR_SEPARATOR)
{
p++;
break;
}
return p;
}
// return pointer to the path name
static char *fth_dirname(char *s)
{
if (!strchr(s, FTH_DIR_SEPARATOR))
return ".";
char *p = (char*)fth_basename(s);
if (p == s)
return s;
p--;
*p = 0;
return s;
}
//
static FILE *fth_pop_file(FirthState *pFirth)
{
if (pFirth->ISP <= 1)
{
pFirth->firth_print("Input Stack underflow!\n");
return stdin; // default to stdin
}
pFirth->ISP--;
// close previous file
fclose(pFirth->input_stack[pFirth->ISP].fd);
pFirth->input_stack[pFirth->ISP].fd = NULL; // zero out the closed file ptr
CHDIR(pFirth->input_stack[pFirth->ISP].dirname);
pFirth->input_stack[pFirth->ISP].dirname[0] = '\0';
return pFirth->input_stack[pFirth->ISP - 1].fd;
}
//
static FirthNumber fth_push_file(FirthState *pFirth, FILE *f, const char *filename)
{
char path[FTH_MAX_PATH];
char oldWorkingDir[FTH_MAX_PATH];
if (pFirth->ISP + 1 > FTH_INPUT_STACK_SIZE)
{
pFirth->firth_print("Input Stack overflow!\n");
return FTH_FALSE;
}
// save current working dir
if (!GETCWD(oldWorkingDir, sizeof(oldWorkingDir)))
{
pFirth->firth_print("Error: could not get current working directory\n");
return FTH_FALSE;
}
strncpy(path, filename, FTH_MAX_PATH - 1);
path[FTH_MAX_PATH - 1] = '\0';
const char *file = fth_basename(path);
const char *dir = fth_dirname(path);
// set new working dir
if (STRING_COMPARE(dir, oldWorkingDir))
CHDIR(dir);
pFirth->input_stack[pFirth->ISP].fd = f;
strncpy(pFirth->input_stack[pFirth->ISP++].dirname, oldWorkingDir, FTH_MAX_PATH - 1);
pFirth->input_stack[pFirth->ISP - 1].dirname[FTH_MAX_PATH - 1] = '\0';
pFirth->BLK = f;
return FTH_TRUE;
}
// implements KEY - get a new key from the terminal
static FirthNumber fth_key(FirthState *pFirth)
{
return fth_push(pFirth, getc(stdin));
}
// implements ACCEPT - read in a line of text
// ( addr limit -- size )
static FirthNumber fth_accept(FirthState *pFirth)
{
int c;
FirthNumber count = 0;
LOG("ACCEPT");
FirthNumber limit = fth_pop(pFirth);
FirthNumber addr = fth_pop(pFirth);
char *p = (char*)addr;
// read at most limit characters from the input and store at addr
while ((c = getc(pFirth->BLK)) != '\n' && count < limit)
{
*p++ = c;
count++;
}
// make sure input string is null terminated
memset(p, 0, FTH_MAX_WORD_NAME - (p - pFirth->TIB));
pFirth->INP = pFirth->TIB;
fth_push(pFirth, count);
return FTH_TRUE;
}
//
static FirthNumber fth_fill_tib(FirthState *pFirth)
{
// fill input bufer
fth_push(pFirth, (FirthNumber)pFirth->TIB);
fth_push(pFirth, sizeof(pFirth->TIB) - 1);
fth_accept(pFirth);
pFirth->tib_len = fth_pop(pFirth);
return FTH_TRUE;
}
// implements WORD - read in a new word with the given delimiter
// ( delim -- addr )
static FirthNumber fth_word(FirthState *pFirth)
{
int c;
LOG("WORD");
FirthNumber delim = fth_pop(pFirth);
// skip any leading delimiter chars
while ((c = *pFirth->INP) == delim)
pFirth->INP++;
// start with the current input pointer
char *p = pFirth->INP;
// at this point c contains a non delimiter character
// read until delimiter, EOL, EOF or buffer end
while ( (p - pFirth->TIB < pFirth->tib_len) && c != 0 && c != delim && c != '\n' && c != EOF )
{
p++;
c = *p;
}
// handle End-Of-File
if (c == EOF)
{
pFirth->BLK = fth_pop_file(pFirth);
}
// make word in TIB asciiz
*p = '\0';
// put addr of word on stack
fth_push(pFirth, (FirthNumber)pFirth->INP);
// advance IN pointer to beyond the word
pFirth->INP = p + 1;
return FTH_TRUE;
}
// create a new empty dictionary entry withe name given by word
static FirthNumber fth_make_dict_entry(FirthState *pFirth, const char *word)
{
// see if the word already exists and if so warn about it
DictionaryEntry *pEntry = (DictionaryEntry*)fth_tick_internal(pFirth, word);
if (pEntry)
{
firth_printf(pFirth, "Note: redefining an existing word (%s).\n", pEntry->name);
}
// check if we are out of memory
if (pFirth->CP - pFirth->dictionary_base > FTH_ENV_SIZE)
{
firth_printf(pFirth, "Failed to create (%s) out of dictionary space!\n", word);
return FTH_FALSE;
}
DictionaryEntry *pNewHead = (DictionaryEntry *)pFirth->CP;
pNewHead->code_pointer = NULL;
pNewHead->next = pFirth->head;
pNewHead->flags.immediate = 0;
pNewHead->flags.hidden = 0;
pNewHead->flags.colon_word = 0;
pNewHead->flags.compile_only = 0;
pNewHead->flags.xt_on_stack = 0;
pNewHead->flags.constant = 0;
pNewHead->flags.variable = 0;
pNewHead->flags.unused = 0;
// BYTE name_len can hold at most 255; reject names that would overflow it
size_t word_len = strlen(word);
if (word_len > 254)
{
firth_printf(pFirth, "Error: word name too long (%s)\n", word);
return FTH_FALSE;
}
pNewHead->name_len = (BYTE)(word_len + 1); // +1 to allow for null terminator
strncpy(pNewHead->name, word, word_len + 1);
pFirth->head = pNewHead;
pFirth->CP += sizeof(DictionaryEntry) + pFirth->head->name_len + 1;
return FTH_TRUE;
}
// implements CREATE
// create a new empty dictionary entry
static FirthNumber fth_create(FirthState *pFirth)
{
// get the next word in input stream
fth_push(pFirth, ' ');
fth_word(pFirth);
char *newWord = (char*)fth_pop(pFirth);
return fth_make_dict_entry(pFirth, newWord);
}
//
static FirthNumber fth_write_to_cp(FirthState *pFirth, FirthNumber val)
{
*((FirthNumber*)pFirth->CP) = val;
pFirth->CP += sizeof(FirthNumber); // advance dictionary pointer
return FTH_TRUE;
}
#if FTH_INCLUDE_FLOAT == 1
// run-time implementation of FCONSTANT
static FirthNumber fth_fconst_imp(FirthState *pFirth)
{
DictionaryEntry *pDict = (DictionaryEntry*)fth_pop(pFirth);
FirthFloat val = *(FirthFloat*)fth_body_internal(pDict);
fth_pushf(pFirth, val);
return FTH_TRUE;
}
// run-time implementation of FVARIABLE
static FirthNumber fth_fvar_imp(FirthState *pFirth)
{
DictionaryEntry *pDict = (DictionaryEntry*)fth_pop(pFirth);
FirthNumber addr = (FirthNumber)fth_body_internal(pDict);
fth_push(pFirth, addr);
return FTH_TRUE;
}
// run-time implementation of user FVARIABLE
static FirthNumber fth_user_fvar_imp(FirthState *pFirth)
{
DictionaryEntry *pDict = (DictionaryEntry*)fth_pop(pFirth);
FirthFloat **ppVar = (FirthFloat**)fth_body_internal(pDict);
fth_pushf(pFirth, (FirthFloat)**ppVar);
return FTH_TRUE;
}
// create a FCONSTANT word
static FirthNumber fth_fconst(FirthState *pFirth)
{
// create a new dictionary entry for this constant
fth_create(pFirth);
// the stack at this point contains | const_val |
FirthFloat n = fth_popf(pFirth);
pFirth->head->code_pointer = fth_fconst_imp;
pFirth->head->flags.xt_on_stack = 1;
pFirth->head->flags.constant = 1;
// store constant value in dictionary
fth_write_to_cp(pFirth, *(FirthNumber*)&n);
return FTH_TRUE;
}
// create a FVARIABLE word
static FirthNumber fth_fvar(FirthState *pFirth)
{
// create a new dictionary entry for this variable
fth_create(pFirth);
pFirth->head->code_pointer = fth_fvar_imp;
pFirth->head->flags.xt_on_stack = 1;
pFirth->head->flags.variable = 1;
// store value in dictionary
fth_write_to_cp(pFirth, FTH_UNINITIALIZED);
return FTH_TRUE;
}
// push a float literal value onto the stack
static FirthNumber fth_fliteral(FirthState *pFirth)
{
// fetch the next address and use it to get the literal value
FirthFloat val = *(FirthFloat*)pFirth->IP++;
// push literal value onto the stack
fth_pushf(pFirth, val);
return FTH_TRUE;
}
#endif
// run-time implementation of CONSTANT
static FirthNumber fth_const_imp(FirthState *pFirth)
{
DictionaryEntry *pDict = (DictionaryEntry*)fth_pop(pFirth);
FirthNumber val = *fth_body_internal(pDict);
fth_push(pFirth, val);
return FTH_TRUE;
}
// run-time implementation of VARIABLE
static FirthNumber fth_var_imp(FirthState *pFirth)
{
DictionaryEntry *pDict = (DictionaryEntry*)fth_pop(pFirth);
FirthNumber addr = (FirthNumber)fth_body_internal(pDict);
fth_push(pFirth, addr);
return FTH_TRUE;
}
//
static FirthNumber fth_user_var_imp(FirthState *pFirth)
{
DictionaryEntry *pDict = (DictionaryEntry*)fth_pop(pFirth);
FirthNumber **ppVar = (FirthNumber**)fth_body_internal(pDict);
fth_push(pFirth, (FirthNumber)*ppVar);
return FTH_TRUE;
}
// create a CONSTANT word
static FirthNumber fth_const(FirthState *pFirth)
{
// create a new dictionary entry for this constant
fth_create(pFirth);
// the stack at this point contains | const_val |
FirthNumber n = fth_pop(pFirth);
pFirth->head->code_pointer = fth_const_imp;
pFirth->head->flags.xt_on_stack = 1;
pFirth->head->flags.constant = 1;
// store constant value in dictionary
fth_write_to_cp(pFirth, n);
return FTH_TRUE;
}
// create a VARIABLE word
static FirthNumber fth_var(FirthState *pFirth)
{
// create a new dictionary entry for this variable
fth_create(pFirth);
pFirth->head->code_pointer = fth_var_imp;
pFirth->head->flags.xt_on_stack = 1;
pFirth->head->flags.variable = 1;
// store value in dictionary
fth_write_to_cp(pFirth, FTH_UNINITIALIZED);
return FTH_TRUE;
}
// implements NUMBER - look at input and if a number push it onto the stack
// Note: if not a number leaves input addr on stack
static FirthNumber fth_number(FirthState *pFirth)
{
char *input = (char*)fth_peek(pFirth);
if (isInteger(input))
{
char *end;
errno = 0;
FirthNumber n = (FirthNumber)strtol(input, &end, 10);
if (errno != 0 || end == input)
return FTH_FALSE;
fth_pop(pFirth);
return fth_push(pFirth, n);
}
#if FTH_INCLUDE_FLOAT == 1
if (isdigit(input[0]) && (strchr(input, '.') || strchr(input, 'e')))
{
FirthFloat num = (FirthFloat)atof(input);
fth_pop(pFirth);
return fth_pushf(pFirth, num);
}
#endif
return FTH_FALSE;
}
// push a literal value onto the stack
static FirthNumber fth_literal(FirthState *pFirth)
{
// fetch the next address and use it to get the literal value
FirthNumber val = *pFirth->IP++;
// push literal value onto the stack
fth_push(pFirth, val);
return FTH_TRUE;
}
//
static DictionaryEntry *fth_tick_internal(FirthState *pFirth, const char *word)
{
DictionaryEntry *pDict = (DictionaryEntry*)pFirth->head;
for (; pDict; pDict = pDict->next)
{
if (!STRING_COMPARE(pDict->name, word))
{
// return the dictionary entry
return pDict;
}
}
return NULL;
}
//
static FirthNumber fth_interpreter_tick(FirthState *pFirth)
{
// get a WORD from input
fth_push(pFirth, ' ');
fth_word(pFirth);
char *word = (char*)fth_peek(pFirth);
// if buffer is empty return
if (0 == *word)
{
fth_push(pFirth, FTH_FALSE);
return FTH_FALSE;
}
// if word exists, put its xt on the stack
DictionaryEntry *pDict = pFirth->head;
for (; pDict; pDict = pDict->next)
{
if (!pDict->flags.hidden && !STRING_COMPARE(pDict->name, word))
{
// word found so pop the input addr and push xt for the word onto the stack
fth_pop(pFirth);
fth_push(pFirth, (FirthNumber)pDict);
return fth_push(pFirth, FTH_TRUE);
}
}
fth_push(pFirth, FTH_FALSE);
return FTH_FALSE;
}
// implements TICK word
// Note: if word not found in dictionary, leaves the input addr on the stack
static FirthNumber fth_tick(FirthState *pFirth)
{
// get a WORD from input
fth_push(pFirth, ' ');
fth_word(pFirth);
char *word = (char*)fth_peek(pFirth);
// if buffer is empty return
if (0 == *word)
return FTH_FALSE;
// if word exists, put its xt on the stack
DictionaryEntry *pDict = pFirth->head;
for (; pDict; pDict = pDict->next)
{
if (!pDict->flags.hidden && !STRING_COMPARE(pDict->name, word))
{
// word found so pop the input addr and push xt for the word onto the stack
fth_pop(pFirth);
return fth_push(pFirth, (FirthNumber)pDict);
}
}
return FTH_FALSE;
}
// execute a colon word
static FirthNumber fth_address_interpreter(FirthState *pFirth)
{
// push IP on return stack
fth_r_push(pFirth, (FirthNumber)pFirth->IP);
// get the xt from the stack
DictionaryEntry *pThisWord = (DictionaryEntry*)fth_pop(pFirth);
// use the xt to set the IP
pFirth->IP = fth_body_internal(pThisWord);
while (*pFirth->IP)
{
DictionaryEntry *pNextWord = (DictionaryEntry*)*pFirth->IP++;
// if we are going to call a colon word, put the xt on the stack
if (pNextWord->flags.colon_word || pNextWord->flags.xt_on_stack)
fth_push(pFirth, (FirthNumber)pNextWord);
FirthFunc f = (FirthFunc)pNextWord->code_pointer;
f(pFirth);
}
// pop IP off return stack
pFirth->IP = (FirthNumber*)fth_r_pop(pFirth);
return FTH_TRUE;
}
// implements ':' - begin compiling a new word
static FirthNumber fth_colon(FirthState *pFirth)
{
// create a new dictionary entry
fth_create(pFirth);
pFirth->head->code_pointer = fth_address_interpreter;
pFirth->head->flags.colon_word = 1;
pFirth->head->flags.hidden = 1; // new WORDS are not visible until fully defined
pFirth->compiling = true;
return FTH_TRUE;
}
// implements ';' - end definition of a new WORD
static FirthNumber fth_semicolon(FirthState *pFirth)
{
// mark the EXIT of this word
fth_write_to_cp(pFirth, 0);
pFirth->head->flags.hidden = 0; // new WORDS become visible only when fully defined
pFirth->compiling = false;
return FTH_TRUE;
}
// implements EXIT
static FirthNumber fth_exit(FirthState *pFirth)
{
fth_write_to_cp(pFirth, 0);
return FTH_TRUE;
}
// implementes COMPILE - compile the words in a new colon definition
static FirthNumber fth_compile(FirthState *pFirth)
{
LOG("COMPILE");
// inner interpreter loop
while (pFirth->compiling)
{
// if result is TRUE then there is an xt on the stack
// otherwise the addr of an input that is not a WORD
FirthNumber result = fth_pop(pFirth);
// get the next word
if (result == FTH_TRUE)
{
// get the xt of the word to compile
DictionaryEntry *pDict = (DictionaryEntry*)fth_pop(pFirth);
// if it is an immediate word execute it, otherwise thread it
if (pDict->flags.immediate)
{
// immediate words sometimes need their xt on the stack
if (pDict->flags.colon_word || pDict->flags.xt_on_stack)
fth_push(pFirth, (FirthNumber)pDict);
FirthFunc f = (FirthFunc)pDict->code_pointer;
f(pFirth);
}
else
{
// write instruction to new colon word definition
fth_write_to_cp(pFirth, (FirthNumber)pDict);
}
}
else
{
// the addr of the "word" is now on the stack
char *input = (char*)fth_peek(pFirth);
if (input[0] == '\0')
{
// empty string on the stack, discard it!!
fth_pop(pFirth);
// (re-)fill input bufer
fth_fill_tib(pFirth);
}
else if (isInteger(input))
{
DictionaryEntry *xt = fth_tick_internal(pFirth, "LITERAL");
fth_write_to_cp(pFirth, (FirthNumber)xt);
// pop the addr off the stack, convert it to a number and put back on stack
fth_number(pFirth);
fth_write_to_cp(pFirth, fth_pop(pFirth));
}
else if (isdigit(input[0]) && (strchr(input, '.') || strchr(input, 'e')))
{
DictionaryEntry *xt = fth_tick_internal(pFirth, "FLITERAL");
fth_write_to_cp(pFirth, (FirthNumber)xt);
// pop the addr off the stack, convert it to a number and put back on stack
fth_number(pFirth);
FirthFloat f = fth_popf(pFirth);
fth_write_to_cp(pFirth, *(FirthNumber*)&f);
}
else
{
// unknown WORD
// TODO - stop compiling?
char *s = (char*)fth_pop(pFirth);
firth_printf(pFirth, "%s ?\n", s);
break;
}
}
// look for the next word
if (pFirth->compiling)
fth_interpreter_tick(pFirth);
}
return FTH_TRUE;
}
// implements EXECUTE - execute the xt on TOS
// ( xt -- )
static FirthNumber fth_execute(FirthState *pFirth)
{
LOG("EXECUTE");
DictionaryEntry *pDict = (DictionaryEntry*)fth_peek(pFirth);
// do not execute compile only words
if ( pDict->flags.compile_only )
{
pFirth->firth_print(" action is not a function\n");
return FTH_FALSE;
}
// leave the xt on the stack only if it is a colon word or a defining word
if ( !pDict->flags.colon_word && !pDict->flags.xt_on_stack )
{
fth_pop(pFirth);
}
// execute the word
FirthFunc f = pDict->code_pointer;
return f(pFirth);
}
// implements INTERPRET
static FirthNumber fth_interpret(FirthState *pFirth)
{
LOG("INTERPRET");
while (true)
{
// read input and try to find a word
fth_interpreter_tick(pFirth);
if (pFirth->compiling)
{
fth_compile(pFirth);
}
else
{
FirthNumber result = fth_pop(pFirth);
if (result == FTH_TRUE)
{
// TICK found an existing word,
// the xt is on the stack, EXECUTE it
fth_execute(pFirth);
}
else if (!fth_number(pFirth))
{
char *s = (char*)fth_pop(pFirth);
// if there was text but we don't understand it
if (0 != *s)
firth_printf(pFirth, "%s ?\n", s);
break;
}
}
}
return FTH_TRUE;
}
// implements QUIT
static FirthNumber fth_quit(FirthState *pFirth)
{
LOG("QUIT");
// show user prompt
if (pFirth->BLK == stdin && pFirth->show_prompts)
pFirth->firth_print("firth>");
// TODO - reset return stacks?
if (pFirth->RP != pFirth->return_stack)
{
firth_printf(pFirth, "Warning: return stack imbalance detected\n");
pFirth->RP = pFirth->return_stack;
}
// fill input bufer
fth_fill_tib(pFirth);
fth_interpret(pFirth);
if (pFirth->BLK == stdin && pFirth->show_prompts)
pFirth->firth_print(" ok\n");
return FTH_TRUE;
}
// implements IMMEDIATE
// make the last word created immediate
static FirthNumber fth_immediate(FirthState *pFirth)
{
pFirth->head->flags.immediate = 1;
return FTH_TRUE;
}
// implements HIDE
// make the last word created hidden
static FirthNumber fth_hide(FirthState *pFirth)
{
pFirth->head->flags.hidden = 1;
return FTH_TRUE;
}
// implements REVEAL
static FirthNumber fth_reveal(FirthState *pFirth)
{
pFirth->head->flags.hidden = 0;
return FTH_TRUE;
}
// implements CELLS
// computes the dictionary space needed for N cells
static FirthNumber fth_cells(FirthState *pFirth)
{
FirthNumber n = fth_pop(pFirth);
return fth_push(pFirth, n * sizeof(FirthNumber));
}
// implements ALLOT
// reserve N bytes in the dictionary
static FirthNumber fth_allot(FirthState *pFirth)
{
FirthNumber n = fth_pop(pFirth);
pFirth->CP += n;
return FTH_TRUE;
}
// implements ']'
// ( -- )
static FirthNumber fth_stop_compile(FirthState *pFirth)
{
pFirth->compiling = 0;
return FTH_TRUE;
}
// implements '['
// ( -- )
static FirthNumber fth_start_compile(FirthState *pFirth)
{
pFirth->compiling = 1;
return FTH_TRUE;
}
// implements .S
static FirthNumber fth_dots(FirthState *pFirth)
{
// make a copy of the stack
FirthNumber *s = pFirth->SP;
pFirth->firth_print("Stack Top -> [ ");
while (s-- != pFirth->stack)
{
if (pFirth->hexmode)
firth_printf(pFirth, "0x%0X ", *s);
else
firth_printf(pFirth, "%d ", *s);
}
pFirth->firth_print("]\n");
return FTH_TRUE;
}
// implements DEPTH - push the current stack size onto the stack
// ( -- n )
static FirthNumber fth_depth(FirthState *pFirth)
{
FirthNumber depth = (pFirth->SP - pFirth->stack);
fth_push(pFirth, depth);
return FTH_TRUE;
}
// run-time behavior of a marker - reset dictionary state to the marker
static FirthNumber fth_marker_imp(FirthState *pFirth)
{
DictionaryEntry *xt = (DictionaryEntry*)fth_pop(pFirth);
// reset dictionary head