-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.cpp
3158 lines (2877 loc) · 101 KB
/
read.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
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <list>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <list>
#include <stack>
#include <queue>
#include <memory>
#include "lexer.h"
#include "cheme_types.h"
#define HIDDEN_CHEME_LT "hidden_cheme_less_than"
#define HIDDEN_CHEME_GT "hidden_cheme_greater_than"
#define HIDDEN_CHEME_EQ "hidden_cheme_equals"
#define HIDDEN_CHEME_INT_TO_DOUBLE "hidden_cheme_int_to_double"
#define HIDDEN_CHEME_DOUBLE_TO_INT "hidden_cheme_double_to_int"
#define HIDDEN_CHEME_ADD_INTS "hidden_cheme_add_ints"
#define HIDDEN_CHEME_SUB_INTS "hidden_cheme_sub_ints"
#define HIDDEN_CHEME_MUL_INTS "hidden_cheme_mul_ints"
#define HIDDEN_CHEME_CMP_INTS "hidden_cheme_cmp_ints"
#define HIDDEN_CHEME_ANY_SIZE "hidden_cheme_any_size"
#define HIDDEN_CHEME_ANY_TYPE_NAME "hidden_cheme_any_type_name"
#define HIDDEN_CHEME_ANY_DPTR_PTR "hidden_cheme_any_dptr_ptr"
#define HIDDEN_CHEME_ANY_DPTR "hidden_cheme_any_dptr"
#define HIDDEN_CHEME_ANY_SPTR "hidden_cheme_any_sptr"
#define HIDDEN_CHEME_ANY_TYPE_PTR "hidden_cheme_any_type_ptr"
#define HIDDEN_CHEME_ANY_TYPE "any_type"
extern "C" {
extern const int base_type_count;
extern cheme_type_desc_data hidden_cheme_type_desc_data00000000_data;
extern cheme_type_desc_data hidden_cheme_type_desc_data00000001_data;
extern cheme_type_desc_data hidden_cheme_type_desc_data00000002_data;
extern cheme_type_desc_data hidden_cheme_type_desc_data00000003_data;
extern cheme_type_desc_data hidden_cheme_type_desc_data00000004_data;
extern cheme_type_desc_data hidden_cheme_type_desc_data00000005_data;
extern cheme_type_desc_data hidden_cheme_type_desc_data00000006_data;
}
cheme_type_desc
cheme_type_desc_int = &hidden_cheme_type_desc_data00000000_data,
cheme_type_desc_ptr_char = &hidden_cheme_type_desc_data00000001_data,
cheme_type_desc_ptr_anylist = &hidden_cheme_type_desc_data00000002_data,
cheme_type_desc_sym = &hidden_cheme_type_desc_data00000003_data,
cheme_type_desc_unit = &hidden_cheme_type_desc_data00000004_data,
cheme_type_desc_char = &hidden_cheme_type_desc_data00000005_data,
cheme_type_desc_anylist = &hidden_cheme_type_desc_data00000006_data;
bool is_first_and_last_layer = true;
std::string indices_file = "";
std::string first_layer_source = "";
void load_cur_token();
struct ErrorContext {
long pos;
int flat_index;
};
ErrorContext current_error_context;
int current_flat_index;
std::list<ErrorContext> error_contexts;
void push_error_context(ErrorContext ec) {
error_contexts.push_back(ec);
}
void pop_error_context() {
error_contexts.pop_back();
}
std::string error_context_to_string_multi_layer();
std::string error_context_to_string() {
if (error_contexts.empty())
return "Empty error context stack";
char buf[256];
if (is_first_and_last_layer) {
sprintf(buf, "char %ld", error_contexts.back().pos);
return buf;
} else {
return error_context_to_string_multi_layer();
}
}
int temp_index = 1;
std::string make_temp_name(int temp_index) {
char buf[80];
sprintf(buf, "hidden_cheme_temp%08d", temp_index);
return std::string(buf);
}
#define CHEME_TYPE_DESC_VAR_PREFIX "hidden_cheme_type_desc_data"
#define TYPE_DESC_NUM_LEN 8
#define TYPE_DESC_NUM_LEN_STR "8"
std::string make_type_desc_data_name(int index) {
char buf[80];
sprintf(buf, CHEME_TYPE_DESC_VAR_PREFIX"%0"TYPE_DESC_NUM_LEN_STR"d", index);
return std::string(buf);
}
std::string make_tup_name(int index) {
char buf[80];
sprintf(buf, "struct hidden_cheme_tup_%08d", index);
return std::string(buf);
}
std::string get_next_temp() {
return make_temp_name(temp_index++);
}
int num;
char word[1000];
void cheme_printf_empty_stacks_to_stderr();
#define ABORT(x) ASSERT(false,x)
#define ABORT1(x,y) ASSERT1(false,x,y)
#define ASSERT(x,y) ((x) || (fputs((y),stderr), fprintf(stderr, "\nAt %s\n", error_context_to_string().c_str()), /*cheme_printf_empty_stacks_to_stderr(),*/ fprintf(stderr, "At compiler source %s:%d\n", __FILE__, __LINE__) , abort(), true))
#define ASSERT1(x,y,z) ((x) || (fprintf(stderr, (std::string(y) + "\n").c_str(), z), ASSERT_EPILOGUE
#define ASSERT2(x,y,a,b) ((x) || (fprintf(stderr, (std::string(y) + "\n").c_str(), a, b), ASSERT_EPILOGUE
#define ASSERT_EPILOGUE fprintf(stderr, "\nAt %s\n", error_context_to_string().c_str()), /*cheme_printf_empty_stacks_to_stderr(),*/ fprintf(stderr, "At compiler source %s:%d\n", __FILE__, __LINE__) , abort(), true))
typedef struct {
char *buf_origin;
char *buf_ptr;
size_t buf_size;
} cheme_printf_context;
#define PRINTF_ZONE_CURRENT_BODY 0
#define PRINTF_ZONE_TOP 1
std::stack<cheme_printf_context> cheme_printf_top_zone_stack;
std::stack< std::stack<cheme_printf_context> > cheme_printf_body_zones_stack;
std::queue< char* > cheme_printf_done_body_zones;
std::stack<int> cheme_printf_zone_stack;
void cheme_printf_push_body() {
cheme_printf_body_zones_stack.push(std::stack<cheme_printf_context>());
cheme_printf_body_zones_stack.top().push(cheme_printf_context());
}
void cheme_printf_pop_body() {
ASSERT(!cheme_printf_body_zones_stack.empty(),
"cheme_printf_pop_body: No body zone to pop");
ASSERT(cheme_printf_body_zones_stack.top().size() == 1,
"cheme_printf_pop_body: popped body stack must be of size 1");
cheme_printf_done_body_zones.push
(cheme_printf_body_zones_stack.top().top().buf_origin);
cheme_printf_body_zones_stack.pop();
}
void cheme_printf_set_zone(int zone) {
cheme_printf_zone_stack.push(zone);
}
void cheme_printf_unset_zone() {
ASSERT(!cheme_printf_zone_stack.empty(),
"cheme_printf_unset_zone: no cheme_printf_set_zone to undo");
cheme_printf_zone_stack.pop();
}
std::stack<cheme_printf_context> *cheme_printf_get_current_stack() {
ASSERT(!cheme_printf_zone_stack.empty(),
"cheme_printf_get_current_stack: zone stack is empty");
switch (cheme_printf_zone_stack.top()) {
case PRINTF_ZONE_TOP:
return &cheme_printf_top_zone_stack;
case PRINTF_ZONE_CURRENT_BODY:
ASSERT(!cheme_printf_body_zones_stack.empty(),
"cheme_printf_get_current_stack: there is nbo top body zone");
return &cheme_printf_body_zones_stack.top();
default:
ABORT("cheme_printf_get_current_stack: got unknown zone code");
}
}
int cheme_vprintf_to_context(cheme_printf_context &cur, const char *format,
va_list argp)
{
if (cur.buf_origin == NULL) {
cur.buf_ptr = cur.buf_origin = (char*)malloc(4096);
cur.buf_size = 4096;
}
size_t written;
while (1) {
const size_t max_write =
(cur.buf_size - (cur.buf_ptr - cur.buf_origin));
va_list old_argp; va_copy(old_argp, argp);
written = vsnprintf(cur.buf_ptr, max_write, format, argp);
va_copy(argp, old_argp);
if (written >= max_write) {
ptrdiff_t buf_offset = cur.buf_ptr - cur.buf_origin;
cur.buf_origin = (char*)realloc(cur.buf_origin,
cur.buf_size * 2);
cur.buf_ptr = (cur.buf_origin + buf_offset);
cur.buf_size *= 2;
} else break;
}
cur.buf_ptr += written;
va_end(argp);
return written;
}
int cheme_printf(const char *format, ...) {
va_list argp;
std::stack<cheme_printf_context> *p = cheme_printf_get_current_stack();
ASSERT(!p->empty(), "cheme_printf: empty stack");
cheme_printf_context &cur = p->top();
va_start(argp, format);
int written = cheme_vprintf_to_context(cur, format, argp);
va_end(argp);
return written;
}
void cheme_printf_push() {
std::stack<cheme_printf_context> *p = cheme_printf_get_current_stack();
p->push(cheme_printf_context());
cheme_printf_context &cur = p->top();
cur.buf_origin = cur.buf_ptr = NULL;
cur.buf_size = 0;
}
bool cheme_printf_is_stack_empty() {
return cheme_printf_get_current_stack()->empty();
}
char *cheme_printf_pop() {
ASSERT(!cheme_printf_get_current_stack()->empty(),
"cheme_printf_pop: cannot pop from an empty stack");
cheme_printf_context cur = cheme_printf_get_current_stack()->top();
cheme_printf_get_current_stack()->pop();
return cur.buf_origin == NULL ? strdup("") : cur.buf_origin;
}
void cheme_printf_empty_stacks_to_stderr() {
fprintf(stderr, "Emptying stacks:\n");
cheme_printf_set_zone(PRINTF_ZONE_TOP);
fprintf(stderr, "PRE_MAIN stack:\n");
while (!cheme_printf_is_stack_empty()) {
fprintf(stderr, "---Element---\n%s\n", cheme_printf_pop());
}
cheme_printf_unset_zone();
cheme_printf_set_zone(PRINTF_ZONE_CURRENT_BODY);
fprintf(stderr, "MAIN stack:\n");
while (!cheme_printf_is_stack_empty()) {
fprintf(stderr, "---Element---\n%s\n", cheme_printf_pop());
}
cheme_printf_unset_zone();
}
#define printf DO NOT USE
template <class T>
class Maybe {
public:
Maybe() { valid = false; }
Maybe(const T &data) : valid(true), data(data) {}
bool is_valid() const { return valid; }
const T &get_data() const
{ ASSERT(valid, "Maybe::data: valid is false"); return data; }
private:
bool valid;
T data;
};
template <class T, class S>
class BinTyped {
public:
bool is_left() { return left; }
bool is_right() { return !left; }
const T &get_left() {
ASSERT(is_left(), "BinTyped: get_left called when not left");
return t;
}
const S &get_right() {
ASSERT(is_right(), "BinTyped: get_right called when not right");
return s;
}
void set(const T &t) { left = true; this->t = t; }
void set(const S &s) { left = false; this->s = s; }
BinTyped(const T &t) : left(true) { this->t = t; }
BinTyped(const S &s) : left(false) { this->s = s; }
private:
bool left;
T t; S s;
};
template <class T> void incr(T &t) { ++t; }
template <class A, class B, class FUNC>
A foldl(const FUNC &func, A acc, const B begin, const B end) {
for (B it = begin;
it != end;
++it)
{
acc =
func(acc,
*it);
}
return acc;
}
template <class A, class B, class FUNC>
A foldl(const FUNC &func, A acc, const B &list) {
return foldl(func, acc, list.begin(), list.end());
}
template <class A, class B>
const A &pair_first(const std::pair<A,B> &pair) { return pair.first; }
template <int V, class T>
int fixed_value(const T &) { return V; }
template <class T>
typename T::const_iterator begin_func(const T &x) { return x.begin(); }
template <class T>
const T &list_at(const std::list<T> &l, int index) {
typename std::list<T>::const_iterator i = l.begin();
while(index--) ++i;
return *i;
}
class Type {
public:
std::string name;
std::list<Type> type_params;
// public:
// int type;
// union {
// int i;
// char c;
// } u;
// std::list<Type> ret_and_params; // must be outside u
};
bool is_callable_type(const Type &type) {
return type.name == "func";
}
class IsCallableWithNParams {
public: IsCallableWithNParams(size_t n) : n(n) {}
bool operator()(const Type &t) { return is_callable_type(t) &&
t.type_params.size() == n; }
private: const size_t n;
};
bool same_types(const Type &t1, const Type &t2) {
#define CASE(X,Y) case X: return t2.type == X && t1.Y == t2.Y;
return t1.name == t2.name &&
t1.type_params.size() == t2.type_params.size() &&
std::equal(t1.type_params.begin(), t1.type_params.end(),
t2.type_params.begin(), same_types);
#undef CASE
}
class SameType {
public: SameType(const Type &t) : t(t) {}
bool operator()(const Type &t2) { return same_types(t, t2); }
private: Type t;
};
std::string type_to_string(const Type &type) {
if (type.type_params.empty()) return type.name;
std::string str = "(";
str += type.name;
for (std::list<Type>::const_iterator it = type.type_params.begin();
it != type.type_params.end(); ++it)
{
str += " ";
str += type_to_string(*it);
}
str += ")";
return str;
}
int type_cmp(const Type &t1, const Type &t2) {
return strcmp(type_to_string(t1).c_str(),
type_to_string(t2).c_str());
}
#define TOKEN_TYPE_EOF 0
#define TOKEN_TYPE_OPEN 1
#define TOKEN_TYPE_CLOSE 2
#define TOKEN_TYPE_INT 3
#define TOKEN_TYPE_WORD 4
#define TOKEN_TYPE_STRING 5
#define TOKEN_TYPE_CHAR 6
struct Token {
int type;
std::string str;
int num;
bool is_specific_word(const char *word) const {
return type == TOKEN_TYPE_WORD && !strcmp(str.c_str(), word);
}
bool operator ==(const Token &tok) const
{ return type == tok.type && str == tok.str; }
};
struct TermMetaData {
void set_type(const Type &typ) { type = Maybe<Type>(typ); }
const Type &get_type() {
ASSERT(type.is_valid(),
"TermMetaData::get_type - there is no type");
return type.get_data();
}
bool has_type() { return type.is_valid(); }
private:
Maybe<Type> type;
};
#define TERM_TYPE_LIST 0
#define TERM_TYPE_SINGLE 1
struct Term {
int type;
std::list<Term> list;
Term *parent;
int index;
ErrorContext ec;
Token single;
TermMetaData metadata;
bool is_single_specific_word(const char *word) const {
return type == TERM_TYPE_SINGLE && single.is_specific_word(word);
}
bool is_single_word() const {
return type == TERM_TYPE_SINGLE && single.type == TOKEN_TYPE_WORD;
}
bool is_single_int() const {
return type == TERM_TYPE_SINGLE && single.type == TOKEN_TYPE_INT;
}
};
std::string add_term_to_string(const std::string &s, const Term &term) {
std::string term_to_string(const Term &t);
return s + " " + term_to_string(term);
}
std::string term_to_string(const Term &t) {
switch (t.type) {
case TERM_TYPE_SINGLE:
switch (t.single.type) {
case TOKEN_TYPE_INT:
{
char buf[128]; sprintf(buf, "%d", t.single.num); return buf;
}
case TOKEN_TYPE_WORD:
case TOKEN_TYPE_STRING:
case TOKEN_TYPE_CHAR:
return t.single.str;
default:
ASSERT1(false, "term_to_string: Unknown t.single.type %d",
t.single.type);
}
break;
case TERM_TYPE_LIST:
return foldl(add_term_to_string, std::string("("), t.list) + ")";
default:
ASSERT1(false, "term_to_string: Unknown t.type %d", t.type);
}
}
int term_cmp(const Term &t1, const Term &t2) {
return strcmp(term_to_string(t1).c_str(),
term_to_string(t2).c_str());
}
Token make_simple_token(int type) {
Token t;
t.type = type;
return t;
}
Token make_eof_token() { return make_simple_token(TOKEN_TYPE_EOF); }
Token make_open_token() { return make_simple_token(TOKEN_TYPE_OPEN); }
Token make_close_token() { return make_simple_token(TOKEN_TYPE_CLOSE); }
Token parse_int_to_token(const int num) {
Token tok;
tok.type = TOKEN_TYPE_INT;
tok.num = num;
return tok;
}
Token parse_word_to_token(const std::string &str) {
Token tok;
tok.type = TOKEN_TYPE_WORD;
tok.str = str;
return tok;
}
Token parse_string_to_token(const std::string &str) {
Token tok;
tok.type = TOKEN_TYPE_STRING;
tok.str = str;
return tok;
}
Token parse_char_to_token(const std::string &str) {
Token tok;
tok.type = TOKEN_TYPE_CHAR;
tok.str = str;
return tok;
}
Term parse_word_to_term(const std::string &str) {
Term term;
term.type = TERM_TYPE_SINGLE;
term.single = parse_word_to_token(str);
return term;
}
Token read_token_for_real() {
start:
extern int yyoffset;
current_error_context.pos = yyoffset;
switch (yylex()) {
case 0: return make_eof_token();
case OPEN: return make_open_token();
case CLOSE: return make_close_token();
case NUMBER: return parse_int_to_token(num);
case WORD: return parse_word_to_token(word);
case STRING: return parse_string_to_token(word);
case CHAR: return parse_char_to_token(word);
case SPACE: goto start;
case UNKNOWN: fprintf(stderr, "Unknown token %s\n", word); ABORT("");
default: ABORT("");
}
// begin:
// int c = fgetc(f);
// switch(c) {
// case EOF:
// return EOF_TOKEN;
// case '(':
// return OPEN_TOKEN;
// case ')':
// return CLOSE_TOKEN;
// default:;
// }
// if (isspace(c))
// goto begin; // tail recurse
// std::string str; str += c;
// while ((c = fgetc(f)) != EOF && !isspace(c) && c != ')') {
// str += c;
// }
// if (c != EOF) ungetc(c, f);
// return parse_word_to_token(str);
}
Token hidden_cur_token;
void load_cur_token() { hidden_cur_token = read_token_for_real(); }
void init_parsing(FILE *f) {
yyrestart(f);
load_cur_token();
current_flat_index = 0;
}
Token peep_token() {
return hidden_cur_token;
}
Token read_token() {
Token t = hidden_cur_token;
load_cur_token();
return t;
}
Term read_term(int index) {
Term ret;
ret.ec = current_error_context;
const Token tok = read_token();
// printf("read_term: got token with type %d\n", tok.type);
switch (tok.type) {
case TOKEN_TYPE_INT:
case TOKEN_TYPE_WORD:
case TOKEN_TYPE_STRING:
case TOKEN_TYPE_CHAR:
ret.type = TERM_TYPE_SINGLE;
ret.single = tok;
break;
case TOKEN_TYPE_OPEN:
{
ret.type = TERM_TYPE_LIST;
// printf("read_term: recursing\n");
int index2 = 0;
while (peep_token().type != TOKEN_TYPE_CLOSE &&
peep_token().type != TOKEN_TYPE_EOF) {
// printf("read_term: calling read_term\n");
ret.list.push_back(read_term(index2++));
}
if (read_token().type != TOKEN_TYPE_CLOSE) {
ABORT("read_term: expected )");
}
break;
}
default:
push_error_context(current_error_context);
ABORT1("read_term: expected ( or a simple term, "
"instead got token type %d\n", tok.type);
pop_error_context();
}
ret.index = index;
ret.ec.flat_index = current_flat_index++;
return ret;
// printf("read_term: done\n");
}
std::string error_context_to_string_multi_layer() {
char buf[512];
FILE *f = fopen(indices_file.c_str(), "r");
init_parsing(f);
int i;
Term term;
for (i = 0; i < error_contexts.back().flat_index + 1; ++i)
term = read_term(i);
fclose(f);
std::string str = "/home/tohava/cheme/index2pos ";
std::list<Term>::reverse_iterator it = term.list.rbegin();
for ( ; it != term.list.rend(); ++it) {
sprintf(buf, "%d", it->single.num);
str += " ";
str += buf;
}
str += " < " + first_layer_source + " > tmp";
system(str.c_str());
f = fopen("tmp","r");
int pos = -1;
fscanf(f, "%d", &pos);
fclose(f);
sprintf(buf, "char %ld", (long int)pos);
return buf;
}
cheme_anylist *convert_term_list_to_anylist(const std::list<Term> &l) {
cheme_any convert_term_to_any(const Term &t);
std::list<Term>::const_iterator it = l.begin();
cheme_anylist *head = NULL;
cheme_anylist **p = &head;
while (it != l.end()) {
*p = new cheme_anylist();
(*p)->data.car = convert_term_to_any(*it);
(*p)->data.cdr = NULL;
p = &((*p)->data.cdr);
++it;
}
return head;
}
cheme_any anylist_ptr_to_any(cheme_anylist *head) {
cheme_any ret;
ret.t = cheme_type_desc_ptr_anylist;
memcpy(ret.s, &head, sizeof(head));
return ret;
}
std::string unescape(std::string s) {
std::string ret;
ret.reserve(s.size());
const char *p = s.c_str();
while (*p != '\0') {
if (*p == '\\') {
++p;
#define SIMPLE(c,d) case c: ret += d; break;
switch (*p) {
SIMPLE('0','\0')
SIMPLE('n','\n')
SIMPLE('r','\r')
SIMPLE('\\','\\')
SIMPLE('"','"')
default:
ASSERT1(false, "unescape: unknown escape sequence (%c)", *p);
}
++p;
#undef SIMPLE
} else {
ret += *p; ++p;
}
}
return ret;
}
cheme_any convert_term_to_any(const Term &t) {
cheme_any ret;
switch (t.type) {
case TERM_TYPE_SINGLE:
switch (t.single.type) {
case TOKEN_TYPE_INT:
ret.t = cheme_type_desc_int;
memcpy(ret.s, &t.single.num, sizeof(t.single.num));
break;
case TOKEN_TYPE_WORD:
{
ret.t = cheme_type_desc_sym;
char *p;
p = strdup(t.single.str.c_str());
cheme_sym s = {p};
memcpy(ret.s, &s, sizeof(s));
break;
}
case TOKEN_TYPE_CHAR:
{
ret.t = cheme_type_desc_char;
char c = unescape(t.single.str)[1];
memcpy(ret.s, &c, sizeof(char));
break;
}
case TOKEN_TYPE_STRING:
{
ret.t = cheme_type_desc_ptr_char;
char *p = strdup
(unescape
(t.single.str.substr
(1, t.single.str.size() - 2)).c_str());
memcpy(ret.s, &p, sizeof(char*));
break;
}
default:
ASSERT1(false, "read_term_as_any: Unknown token type %d\n", t.single.type);
}
break;
case TERM_TYPE_LIST:
{
ret = anylist_ptr_to_any(convert_term_list_to_anylist(t.list));
}
}
return ret;
}
cheme_any read_all_terms_as_any_from_file_handle(FILE *f) {
if (f == NULL) {
cheme_any ret;
ret.t = cheme_type_desc_unit;
return ret;
}
init_parsing(f);
std::list<Term> all_terms;
for (int i = 0; peep_token().type != TOKEN_TYPE_EOF; ++i)
all_terms.push_back(read_term(i));
return anylist_ptr_to_any(convert_term_list_to_anylist(all_terms));
}
cheme_any read_term_from_file_as_any(FILE *f) {
init_parsing(f);
return convert_term_to_any(read_term(0));
}
extern "C" {
cheme_any read_term_as_any() {
return convert_term_to_any(read_term(0));
}
cheme_any read_all_terms_as_any() {
return read_all_terms_as_any_from_file_handle(stdin);
}
cheme_any read_all_terms_as_any_from_file(char *name) {
FILE *f = fopen(name, "r");
cheme_any result = read_all_terms_as_any_from_file_handle(f);
if (f != NULL) fclose(f);
return result;
}
cheme_any read_term_from_str(char *str) {
FILE *f = fopen("quote_temp", "w+b"); //tmpfile();
fputs(str, f);
rewind(f);
return read_term_from_file_as_any(f);
fclose(f);
}
int init_read_term_as_any() {
init_parsing(stdin);
return 0;
}
}
void set_term_parent_worker(Term *term, Term *parent) {
term->parent = parent;
if (term->type == TERM_TYPE_LIST) {
std::list<Term>::iterator it = term->list.begin();
for (; it != term->list.end(); ++it) {
set_term_parent_worker(&*it, term);
}
}
}
void set_term_parent(Term &term) {
set_term_parent_worker(&term, NULL);
}
void write_token(FILE *f, const Token &tok) {
switch (tok.type) {
case TOKEN_TYPE_OPEN:
fputc('(', f);
break;
case TOKEN_TYPE_CLOSE:
fputc(')', f);
break;
case TOKEN_TYPE_WORD:
fputc(' ', f);
fputs(tok.str.c_str(), f);
fputc(' ', f);
break;
case TOKEN_TYPE_EOF:
ABORT("Cannot display EOF");
default:
ABORT("write_token: unknown token");
}
}
// class WriteTerm {
// public:
// WriteTerm(FILE *f) : f(f) {}
// void operator()(const Term &term) {
// void write_term(FILE *f, const Term &term);
// write_term(f, term);
// }
// private:
// FILE *f;
// };
// void write_term(FILE *f, const Term &term) {
// switch (term.type) {
// case TERM_TYPE_SINGLE:
// write_token(f, term.single);
// break;
// case TERM_TYPE_LIST:
// write_token(f, OPEN_TOKEN);
// std::for_each(term.list.begin(), term.list.end(), WriteTerm(f));
// write_token(f, CLOSE_TOKEN);
// break;
// default:
// ABORT("write_term: Unknown term");
// }
// }
Term force_to_list(const Term &term) {
switch (term.type) {
case TERM_TYPE_LIST: return term;
case TERM_TYPE_SINGLE:
{
Term t;
t.type = TERM_TYPE_LIST;
t.list.push_back(term);
return t;
}
default:
ABORT("");
}
}
bool same_terms(const Term &t1, const Term &t2);
bool same_term_lists(const std::list<Term> &t1,
const std::list<Term> &t2) {
return t1.size() == t2.size() &&
std::equal(t1.begin(), t1.end(), t2.begin(), same_terms);
}
bool same_type_lists(const std::list<Type> &t1,
const std::list<Type> &t2) {
return t1.size() == t2.size() &&
std::equal(t1.begin(), t1.end(), t2.begin(), same_types);
}
bool same_terms(const Term &t1, const Term &t2) {
#define CASE1(X,Y) case X: return t2.type == X && t1.Y == t2.Y;
#define CASE2(X,Y,F) case X: return t2.type == X && F(t1.Y,t2.Y);
switch (t1.type) {
CASE1(TERM_TYPE_SINGLE,single);
CASE2(TERM_TYPE_LIST,list,same_term_lists);
default:
ABORT("same_terms: Unknown term type");
}
#undef CASE2
#undef CASE1
}
#define TYPE_TYPE_INT 0
#define TYPE_TYPE_CHAR 1
#define TYPE_TYPE_VOID 2
template <class T, class FUNC>
T filter(const T &values, const FUNC &fun) {
T ret;
typename T::const_iterator iter = values.begin();
for (; iter != values.end(); ++iter) {
// printf("filter: debug: %d\n", iter != values.end());
if (fun(*iter)) {
// printf("filter: pushing back\n");
ret.push_back(*iter);
}
}
return ret;
}
template <class T, class FUNC>
bool all(FUNC f, const T &list) {
typename T::const_iterator it = list.begin();
for (; it != list.end(); ++it) if (!f(it)) return false;
return true;
}
template <class T, class FUNC, class S>
std::list<S> map(FUNC f, const T &list, S *dummy) {
std::list<S> ret;
for (typename T::const_iterator it = list.begin();
it != list.end(); ++it)
{
ret.push_back(f(*it));
}
return ret;
}
template <class T, class FUNC>
void set_map(FUNC f, T &t)
{
std::transform(t.begin(), t.end(), t.begin(), t.end(), f);
}
template <class A, class B, class C, class F>
void set_filter_with_assoc2(const F &pred,A &list1, B &list2, C &list3) {
ASSERT(list1.size() == list2.size() &&
list2.size() == list3.size(),
"set_filter_with_assoc2: lists should have same size");
typename A::iterator a = list1.begin();
typename B::iterator b = list2.begin();
typename C::iterator c = list3.begin();
for ( ; a != list1.end(); ++a, ++b, ++c) {
if (pred(*a)) {
list1.erase(a); list2.erase(b); list3.erase(c);
}
}
}
class FunctionManager {
private:
class FunctionData {
public:
Type t;
int scope_depth;
int lambda_depth;
std::string true_name;
bool is_c_func;
};
typedef std::map<std::string, std::list<FunctionData> > map_t;
typedef std::list< std::pair< std::string, std::list<map_t::iterator> > >
scope_t;
public:
static bool accessible(const std::string &name)
{
FunctionData *p = get(name);
return p != NULL && (p->lambda_depth == 0 ||
p->lambda_depth == lambda_depth ||
p->is_c_func);
}
static const Type &type(const std::string &name) {
FunctionData *p = get(name);
ASSERT(p != NULL, "FunctionManager::type: unknown name");
return p->t;
}
static const std::string &true_name(const std::string &name) {
FunctionData *p = get(name);
ASSERT(p != NULL, "FunctionManager::true_name: unknown name");