-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdccsuper.cpp
1390 lines (1376 loc) · 36.8 KB
/
dccsuper.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
/*
* dccsuper.cpp
*
* Created on: Apr 26, 2015
* Author: Mike
*/
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <list>
#include <algorithm>
#include <sstream>
#include <map>
#include <iomanip>
#include <stdlib.h>
#include <string.h>
#define LOG(fmt) cout << __PRETTY_FUNCTION__ << ": " << fmt << " : " << __FILE__ << ":" << __LINE__ << endl
using namespace std;
static vector<int> indented;
enum TokenType {
TT_VARID, TT_CONID, TT_INTEGER, TT_DOUBLE, TT_STRING, TT_OPER,
TT_DATA, TT_CASE, TT_OF, TT_ARROW_TO, TT_ARROW_FROM,
TT_LPAREN, TT_RPAREN, TT_PIPE,
TT_EQUALS,
TT_SEMI, TT_INDENT, TT_OUTDENT,
TT_EOF
};
const char* TokenTypeStr[] =
{
"TT_VARID", "TT_CONID", "TT_INTEGER", "TT_DOUBLE", "TT_STRING", "TT_OPER",
"TT_DATA", "TT_CASE", "TT_OF", "TT_ARROW_TO", "TT_ARROW_FROM",
"TT_LPAREN", "TT_RPAREN", "TT_PIPE",
"TT_EQUALS",
"TT_SEMI", "TT_INDENT", "TT_OUTDENT",
"TT_EOF"
};
ostream& operator<<(ostream& out, TokenType type)
{
return out << TokenTypeStr[type];
}
struct Token {
Token() : type(TT_EOF) {}
Token(TokenType type) : type(type){}
TokenType type;
string text;
double dval = 0;
int ival = 0;
ostream& print(ostream& out) const;
};
ostream& Token::print(ostream& out) const
{
out << type;
switch (type)
{
case TT_VARID: case TT_CONID: case TT_STRING: case TT_OPER:
out << ' ' << text;
break;
case TT_INTEGER:
out << ' ' << ival;
break;
case TT_DOUBLE:
out << ' ' << dval;
break;
//TT_INDENT, TT_OUTDENT,
//TT_EOF
}
return out;
}
ostream& operator<<(ostream& out, const Token& token)
{
return token.print(out);
}
string lastline;
bool ascSymbol(int ch)
{
return string("!#$%&*+./<=>?@\\^|-~:").find(ch)!=string::npos;
}
int line_number = 0;
struct Error {
int line;
string msg;
Error(int line, const string& msg) : line(line), msg(msg) {}
};
/*
* An implementation of basically the Python indent system.
* Not really needed for the language I'm parsing because there
* are no blocks. With a case block or a let/where block this would be
* handy.
* This system may do things redundantly. The alternative would be
* to use a token buffer to help insert the inferred tokens.
* Contrast with Haskell:
* Haskell wants an opening curly brace after certain keywords, such as
* the 'where' at the beginning of a module. Either the user provides
* or it is inferred. So in that system the rules are trickier. Python
* only uses curly braces for dictionary literals and the indent and
* dedent are completely separate tokens. So indentation is obligatory
* in Python, while Haskell is okay with explicit curly braces.
*/
list<int> columns;
bool IndentDedentHandling(istream& in, Token& inferred)
{
static int column = 0;
static bool newline_done = false;
if (columns.empty())
columns.push_back(0);
while (in.good())
{
while (lastline.size()==0 && in.good())
{
std::getline(in, lastline);
line_number++;
column = 0;
newline_done = false;
}
size_t nblank = lastline.find_first_not_of(" \t\r");
if (nblank != std::string::npos)
{
int indent = 0;
for (int i=0; i<nblank; ++i)
{
if (lastline[i]=='\t')
{
indent = indent+(8-indent%8);
}
else
{
indent++;
}
}
column = indent;
if (column != columns.back())
{
if (column > columns.back())
{
columns.push_back(column);
inferred = Token(TT_INDENT);
return true;
}
if (column < columns.back())
{
auto instack = std::find(columns.begin(), columns.end(), column);
if (instack == columns.end())
{
throw Error(line_number,"Bad indent");
}
columns.pop_back();
inferred = Token(TT_OUTDENT);
return true;
}
}
lastline.erase(0,nblank);
if (lastline.size()>1 && (lastline[0]=='-' && lastline[1]=='-'))
{
// If there is a third character,
// and the three characters would make a legal 'symbol' token, then
// the two dashes do not start a comment, except of course that
// three dashes would start a comment.
if (lastline.size()<3 || lastline[2]=='-' || !ascSymbol(lastline[2]))
{
lastline.clear();
continue;
}
}
break;
}
else
{
lastline.clear();
}
}
return false;
}
/*
* This system doesn't guess anything.
* No blocks with indent-dedent.
* No semicolons except the ones in the text.
*/
bool ExplicitBlocking(istream& in, Token&)
{
while (in.good())
{
while (lastline.size()==0 && in.good())
{
std::getline(in, lastline);
line_number++;
}
size_t nblank = lastline.find_first_not_of(" \t\r");
if (nblank != std::string::npos)
{
lastline.erase(0,nblank);
if (lastline.size()>1 && (lastline[0]=='-' && lastline[1]=='-'))
{
// If there is a third character,
// and the three characters would make a legal 'symbol' token, then
// the two dashes do not start a comment, except of course that
// three dashes would start a comment.
if (lastline.size()<3 || lastline[2]=='-' || !ascSymbol(lastline[2]))
{
lastline.clear();
continue;
}
}
break;
}
else
{
lastline.clear();
}
}
return false;
}
/*
* This system infers semicolons. There are basically two rules.
* One rule is that if the last token on the line is not a semicolon,
* then one is needed and should be inferred.
* Second rule is, don't infer a semicolon if a left paren has not been
* matched with a right paren.
* Third rule is, certain operators such as '=' and '->' are binary and
* therefore need to prevent a semicolon right next to them.
* Additionally, blank lines after a semicolon don't call for inferred
* semicolons.
* This system may do things redundantly. The alternative would be
* to use a token buffer to help insert the inferred tokens.
*/
bool InferSemicolons(istream& in, Token& inferred)
{
/*
* If we run out of characters in the line, then it
* may be time for a semicolon. If the last token off
* the line was a semicolon, then one is not needed.
*/
enum { BLANK, NEED_SEMI, HAD_SEMI, HAD_OPERATOR };
static int semi_status = BLANK;
static int parens = 0;
//LOG("Entry semi_status = " << (semi_status==BLANK?"BLANK":(semi_status==NEED_SEMI?"NEED_SEMI":"HAD_SEMI")));
while (in.good())
{
while (lastline.size()==0 && in.good())
{
//LOG("EOL semi_status = " << (semi_status==BLANK?"BLANK":(semi_status==NEED_SEMI?"NEED_SEMI":"HAD_SEMI")));
if (semi_status == NEED_SEMI && parens==0)
{
inferred = Token(TT_SEMI);
semi_status = HAD_SEMI;
return true;
}
std::getline(in, lastline);
semi_status = BLANK;
line_number++;
}
size_t nblank = lastline.find_first_not_of(" \t\r");
if (nblank != std::string::npos)
{
lastline.erase(0,nblank);
if (lastline.size()>1 && (lastline[0]=='-' && lastline[1]=='-'))
{
// If there is a third character,
// and the three characters would make a legal 'symbol' token, then
// the two dashes do not start a comment, except of course that
// three dashes would start a comment.
if (lastline.size()<3 || lastline[2]=='-' || !ascSymbol(lastline[2]))
{
lastline.clear();
//LOG("comment semi_status = " << (semi_status==BLANK?"BLANK":(semi_status==NEED_SEMI?"NEED_SEMI":"HAD_SEMI")));
if (semi_status == NEED_SEMI && parens==0)
{
inferred = Token(TT_SEMI);
return true;
}
continue;
}
}
//LOG("Non-EOL (" << lastline << ") semi_status = " << (semi_status==BLANK?"BLANK":(semi_status==NEED_SEMI?"NEED_SEMI":"HAD_SEMI")));
break;
}
else
{
lastline.clear();
//LOG("EOL semi_status = " << (semi_status==BLANK?"BLANK":(semi_status==NEED_SEMI?"NEED_SEMI":"HAD_SEMI")));
if (in.good() && semi_status == NEED_SEMI && parens==0)
{
inferred = Token(TT_SEMI);
return true;
}
}
}
if (lastline[0]==';' && parens==0)
semi_status = HAD_SEMI;
else if (lastline[0]=='(')
{
parens++;
semi_status = NEED_SEMI;
}
else if (lastline[0]==')')
{
parens--;
semi_status = NEED_SEMI;
}
else if (lastline[0]=='=')
{
semi_status = HAD_OPERATOR;
}
else
semi_status = NEED_SEMI;
return false;
}
Token getToken(istream& in)
{
static bool newline_done = false;
Token inferred;
#if 1
if (InferSemicolons(in, inferred))
return inferred;
#elif 1
if (IndentDedentHandling(in, inferred))
return inferred;
#else
if (ExplicitBlocking(in, inferred))
return inferred;
#endif
if (!in.good())
return Token(TT_EOF);
if (isalpha(lastline[0]) || lastline[0]=='_')
{
//
// I am dividing the names into variable names and constructor
// names because in the future there will be constructors.
// I am not implementing qualified names because I'm not
// implementing modules.
// Currently I am recognizing # as a member of the set of
// characters that can be in a varid or conid, in order to
// play with emulating the Haskell magic hash feature.
// The character really belongs to the ascSymbol set, which
// is a subset of symbol.
//
size_t nname = lastline.find_first_not_of("abcdefghijklmnopqurstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789'#",1);
Token id;
if (isupper(lastline[0]))
id = Token(TT_CONID);
else
id = Token(TT_VARID);
id.text = lastline.substr(0,nname);
if (id.text == "data")
id = Token(TT_DATA);
else if (id.text == "case")
id = Token(TT_CASE);
else if (id.text == "of")
id = Token(TT_OF);
lastline.erase(0,nname);
return id;
}
if (isdigit(lastline[0]))
{
size_t ndigits = lastline.find_first_not_of("0123456789",1);
Token num;
if (lastline[ndigits]=='.' && isdigit(lastline[ndigits+1]))
{
num = Token(TT_DOUBLE);
ndigits = lastline.find_first_not_of("0123456789", ndigits+1);
num.dval = atof(lastline.c_str());
}
else
{
num = Token(TT_INTEGER);
num.ival = atoi(lastline.c_str());
}
lastline.erase(0,ndigits);
return num;
}
if (lastline[0]=='"')
{
// Might be hokey.
// Not de-escaping the characters ?
// Not letting string cross to next line?
size_t k=0;
for (k=1; k<lastline.length(); ++k)
{
if (lastline[k]=='"')
break;
if (lastline[k]=='\\')
++k;
}
Token str(TT_STRING);
str.text = lastline.substr(1,k-1);
lastline.erase(0,k+1);
return str;
}
/*
* Set special. These single characters are 1-character lexemes.
* This group is special because they are single-character tokens,
* unlike the ones below which a token can have a string of. Commas
* are used to make lists and tuples. Backticks are used to turn a
* function name into a binary operator, which would matter if the
* syntax level has operator precedence instead of prefix notation.
* (see varop in Haskell) Curly braces are used to bracket blocks of
* declarations. We're not parsing such things right now.
*/
if (string("();,[]`{}").find(lastline[0]) != string::npos)
{
auto ch = lastline[0];
lastline.erase(0,1);
switch (ch)
{
case '(': return Token(TT_LPAREN);
case ')': return Token(TT_RPAREN);
case ';': return Token(TT_SEMI);
default:
break;
}
Token oper(TT_OPER);
oper.text = string(1,ch);
return oper;
}
/*
* Set symbol. This bunch of characters consists of characters that
* can occur in sequences of any length. A few of the sequences are
* reserved. These are:
* .. : :: = \ | <- -> @ ~ =>
* The sequence -- can start a non-nested comment, provided that
* the third character of the sequence is not a symbol, because
* those three characters would be the first three characters of a
* varsym. The -- sequence followed by a nonsymbol is detected in
* the indentation handler.
*/
size_t nchrs = lastline.find_first_not_of("!#$%&*+./<=>?@\\^|-~:");
if (nchrs != 0)
{
Token oper(TT_OPER);
oper.text = lastline.substr(0,nchrs);
lastline.erase(0,nchrs);
if (oper.text == "=")
oper.type = TT_EQUALS;
else if (oper.text == "|")
oper.type = TT_PIPE;
else if (oper.text == "->")
oper.type = TT_ARROW_TO;
else if (oper.text == "<-")
oper.type = TT_ARROW_FROM;
return oper;
}
throw Error(line_number, string("bad chrs ")+lastline);
}
struct Parser {
Token token;
void next(istream& in)
{
token = getToken(in);
}
};
class Environment
{
public:
Environment(const vector<string>& args) : args(args) {}
void set_arguments(const vector<string>& args_) { args = args_; }
string lookup(const string& name) const;
void bind_reg(const string& name, int reg_id);
private:
vector<string> args;
map<string, int> reg_ids;
};
void Environment::bind_reg(const string& name, int reg_id)
{
reg_ids[name] = reg_id;
}
string c_id(const string& id)
{
ostringstream r;
r << std::hex;// << std::setw(2);
for (auto pid: id)
if (isalnum(pid) || pid=='_')
r << pid;
else
r << (unsigned)pid;
return r.str();
}
string Environment::lookup(const string& name) const
{
auto i_arg = find(args.begin(), args.end(), name);
ostringstream os;
if (i_arg != args.end())
{
auto index = distance(args.begin(), i_arg);
os << "args[" << index << ']';
//out << " comp_t *e" << n << " = args[" << index << "]; /* " << id << "*/" << endl;
}
else
{
//out << " comp_t *e" << n << " = &sc_" << id << "; /* " << id << "*/" <<endl;
auto i_reg_bind = reg_ids.find(name);
if (i_reg_bind != reg_ids.end())
os << "e" << (*i_reg_bind).second;
else
os << "&sc_" << c_id(name);
}
return os.str();
}
struct Node {
virtual ostream& print(ostream& out) const { return out; }
virtual int output_computation(ostream& out, int n, const Environment& env) { return n; }
virtual ~Node() {}
};
ostream& operator<<(ostream& out, const Node& node)
{
return node.print(out);
}
struct Num : Node {
Num(int ivalue) : value(ivalue) {}
Num(double dvalue) : value(dvalue) {}
ostream& print(ostream& out) const { return out << value; }
int output_computation(ostream& out, int n, const Environment& env);
double value;
};
struct Var : Node {
Var(const string& id) : id(id) {}
ostream& print(ostream& out) const { return out << id; }
int output_computation(ostream& out, int n, const Environment& env);
string id;
};
struct Str : Node {
Str(const string& text) : text(text) {}
ostream& print(ostream& out) const { return out << '"' << text << '"'; }
int output_computation(ostream& out, int n, const Environment& env);
string text;
};
struct Operator : Node {
Operator(const string& id) : id(id) {}
string id;
ostream& print(ostream& out) const { return out << id; }
int output_computation(ostream& out, int n, const Environment& env);
};
struct Ctor : Node {
Ctor(const string& id) : id(id) {}
string id; // Not Bool but True or False. Not List but Cons or Nil
vector<Node*> arguments;
int output_computation(ostream& out, int n, const Environment& env);
ostream& print(ostream& out) const
{
out << id;
auto iarg = arguments.begin();
for (;iarg != arguments.end();)
{
out << ' ';
out << **iarg; iarg++;
}
return out;
}
};
struct CtorPat : Node {
CtorPat(const string& id) : id(id) {}
string id; // Not Bool but True or False. Not List but Cons or Nil
vector<string> arguments;
int output_computation(ostream& out, int n, const Environment& env);
ostream& print(ostream& out) const
{
out << id;
auto iarg = arguments.begin();
for (;iarg != arguments.end();)
{
out << ' ';
out << *iarg; iarg++;
}
return out;
}
};
struct Case : Node {
Case(Node* scrutinee) : scrutinee(scrutinee) {}
Node* scrutinee;
struct PatExpr {
Node* pat;
Node* expr;
};
typedef list<PatExpr> PatExprs;
PatExprs patExprs;
int output_computation(ostream& out, int n, const Environment& env);
ostream& print(ostream& out) const
{
out << "case " << *scrutinee << " of { " << endl;
for (auto pat_expr: patExprs)
out << *pat_expr.pat << " -> " << *pat_expr.expr << ';' << endl;
out << "}" << endl;
return out;
}
};
struct Apply : Node {
// In many implementations of such things, an Apply node
// is a pair. But the supercombinator code will want to
// recover the arity of the (partial at times) application,
// so we may as well retain it.
Node* to_apply;
vector<Node*> arguments;
// For printing, if any of our entities is itself an application,
// print parens around it.
static ostream& print_bracketed(ostream& out, const Node* term)
{
bool paren = dynamic_cast<const Apply*>(term) != nullptr;
if (paren) out << '(';
out << *term;
if (paren) out << ')';
return out;
}
int output_computation(ostream& out, int n, const Environment& env);
ostream& print(ostream& out) const
{
print_bracketed(out, to_apply);// out << *to_apply;
auto iarg = arguments.begin();
for (;iarg != arguments.end();)
{
out << ' ';
print_bracketed(out, *iarg); iarg++;
#if 0
bool paren = dynamic_cast<Apply*>(*iarg) != nullptr;
if (paren) out << '(';
out << **iarg; iarg++;
if (paren) out << ')';
#endif
}
return out;
}
};
struct Ccall : Node {
string c_id;
vector<Node*> arguments;
ostream& print(ostream& out) const
{
out << "ccall " << c_id;
for (auto arg : arguments)
out << ' ' << *arg;
return out;
}
int output_computation(ostream& out, int n, const Environment& env);
};
class Defineable {
public:
virtual ~Defineable() {}
virtual ostream& print(ostream& out) const = 0;
virtual void output_function_prototype(ostream& out, const string& id) const = 0;
virtual void output_function_info(ostream& out, const string& id) const = 0;
virtual void output_function_definition(ostream& out, const string& id) const = 0;
};
struct Constructor {
string constructor;
vector <string> arguments;
ostream& print(ostream& out) const;
void output_function_prototype(ostream& out, const string& id) const;
void output_function_heading(ostream& out, const string& id) const;
void output_function_info(ostream& out, const string& id) const;
void output_function_definition(ostream& out, const string& id) const;
};
ostream& operator<<(ostream& out, const Constructor& constructor)
{
return constructor.print(out);
}
struct Type : public Defineable {
vector <string> arguments;
vector <Constructor> constructors;
ostream& print(ostream& out) const;
void output_function_prototype(ostream& out, const string& id) const;
void output_function_info(ostream& out, const string& id) const;
void output_function_definition(ostream& out, const string& id) const;
};
struct Function : public Defineable {
vector<string> arguments;
Node* body;
ostream& print(ostream& out) const;
void output_function_prototype(ostream& out, const string& id) const;
void output_function_heading(ostream& out, const string& id) const;
void output_function_info(ostream& out, const string& id) const;
void output_function_definition(ostream& out, const string& id) const;
};
struct Definition {
string id;
Defineable* defineable;
ostream& print(ostream& out) const;
};
ostream& operator<<(ostream& out, const Defineable& defineable)
{
return defineable.print(out);
}
ostream& operator<<(ostream& out, const Definition& definition)
{
return definition.print(out);
}
ostream& Definition::print(ostream& out) const
{
out << "Define " << id;
out << ' ' << *defineable;
return out;
}
ostream& Function::print(ostream& out) const
{
auto iargs = arguments.begin();
while (iargs != arguments.end())
out << ' ' << *iargs++;
out << '=';
out << *body;
return out;
}
ostream& Constructor::print(ostream& out) const
{
out << constructor;
for (auto argument : arguments)
out << ' ' << argument;
return out;
}
ostream& Type::print(ostream& out) const
{
auto iargs = arguments.begin();
while (iargs != arguments.end())
out << ' ' << *iargs++;
out << '=';
auto ictors = constructors.begin();
if (ictors != constructors.end())
{
out << *ictors++ ;//<< endl;
for (; ictors != constructors.end(); ++ictors)
out << " | " << *ictors;
}
return out;
}
void Constructor::output_function_heading(ostream& out, const string& id) const
{
out << "void ctor_" << c_id(constructor) << ' ';
out << "(comp_t** result, comp_t** args) /*";
for (auto arg : arguments)
out << arg << " ";
out << "*/";
}
void Constructor::output_function_info(ostream& out, const string& id) const
{
out << "comp_t sc_" << c_id(id) << " = { ctor_" << c_id(constructor) << ", " << arguments.size() << "};" << endl;
}
void Constructor::output_function_definition(ostream& out, const string& id) const
{
}
void Type::output_function_prototype(ostream& out, const string& id) const
{
for (auto ctor : constructors)
{
ctor.output_function_heading(out, ctor.constructor);
out << ';' << endl;
}
}
void Type::output_function_definition(ostream& out, const string& id) const
{
out << "enum { " << endl;
for (auto ctor : constructors)
{
out << c_id(ctor.constructor) << "," << endl;
}
out << "}; // " << id << endl;
for (auto ctor : constructors)
{
ctor.output_function_heading(out, ctor.constructor);
out << "{" << endl;
out << " *result = constructor("<< c_id(ctor.constructor) << ", " << ctor.arguments.size();
for (int arg=0; arg<ctor.arguments.size(); ++arg)
out << ", args["<<arg<<']';
out << ");" << endl;
//int n = ctor..output_function_definition(out, 0, arguments);
//out << " *result = e" << n << ";" << endl;
out << "}" << endl;
}
}
void Type::output_function_info(ostream& out, const string& id) const
{
for (auto ctor : constructors)
{
ctor.output_function_info(out, ctor.constructor);
}
}
void Function::output_function_heading(ostream& out, const string& id) const
{
out << "void fun_" << id << ' ';
out << "(comp_t** result, comp_t** args) /*";
for (auto arg : arguments)
out << arg << " ";
out << "*/";
}
void Function::output_function_info(ostream& out, const string& id) const
{
out << "comp_t sc_" << id << " = { fun_" << id << ", " << arguments.size() << "};" << endl;
}
void Function::output_function_prototype(ostream& out, const string& id) const
{
output_function_heading(out, id);
out << ';' << endl;
}
int Var::output_computation(ostream& out, int n, const Environment& env)
{
out << " comp_t *e" << n << " = " << env.lookup(id)<<"; /* " << id << "*/" << endl;
return n;
}
int Str::output_computation(ostream& out, int n, const Environment& env)
{
out << " comp_t *e" << n << " = str(\"" << text << "\");" << endl;
return n;
}
int Num::output_computation(ostream& out, int n, const Environment& env)
{
out << " comp_t *e" << n << " = num(" << value << ");" << endl;
return n;
}
int Apply::output_computation(ostream& out, int n, const Environment& env)
{
vector<int> comps;
for (int i=0; i<arguments.size(); ++i)
{
n = arguments[i]->output_computation(out, n, env);
comps.push_back(n);
++n;
}
int nfunc = to_apply->output_computation(out, n, env);
out << " comp_t *e" << n+1 << " = app(e" << nfunc << ", " << arguments.size();
for (auto comp : comps)
out << ", e" << comp;
out << ");" << endl;
return nfunc+1;
}
int Operator::output_computation(ostream& out, int n, const Environment& env)
{
out << " comp_t *e" << n << " = " << env.lookup(id)<<"; /* " << id <<" (" << c_id(id) << ") */" << endl;
return n;
}
int Ctor::output_computation(ostream& out, int n, const Environment& env)
{
vector<int> comps;
for (int i=0; i<arguments.size(); ++i)
{
n = arguments[i]->output_computation(out, n, env);
comps.push_back(n);
++n;
}
out << " *e" << n+1 << " = ctor(" << id << ", " << arguments.size();
for (auto comp : comps)
out << ", e" << comp;
out << ");" << endl;
return n+1;
}
int CtorPat::output_computation(ostream& out, int n, const Environment& env)
{
vector<int> comps;
for (int i=0; i<arguments.size(); ++i)
{
//n = arguments[i]->output_computation(out, n, env);
out << " *e" << n << " = " << env.lookup(arguments[i]) << "; // CTORPAT " << arguments[i] << endl;
comps.push_back(n);
++n;
}
out << " *e" << n+1 << " = ctor(" << id << ", " << arguments.size();
for (auto comp : comps)
out << ", e" << comp;
out << ");" << endl;
return n+1;
}
int Ccall::output_computation(ostream& out, int n, const Environment& env)
{
vector<int> comps;
for (int i=0; i<arguments.size(); ++i)
{
n = arguments[i]->output_computation(out, n, env);
comps.push_back(n);
++n;
}
out << c_id << "(eval(e" << comps[0] << ")->val.value);" << endl;
return n-1;
}
int Case::output_computation(ostream& out, int n, const Environment& env)
{
n = scrutinee->output_computation(out, n, env);
out << " e"<<n<<" = eval(e" << n << "); // force scrutinee" << endl;
int scrutinee_reg = n;
int result_reg = n+1;
out << " comp_t* e"<<result_reg<<";"<<endl;
bool ifonly=true;
for (auto pat_expr : patExprs)
{
//if scrutinee_reg->tag == pat_expr.pat
Environment case_env = env;
out << " ";
if (!ifonly)
out << "else ";
ifonly = false;
out <<"if (e" << scrutinee_reg << "->val.tag == ";
CtorPat* ctor_pat = dynamic_cast<CtorPat*>(pat_expr.pat);
int body_reg = scrutinee_reg + 2;
if (ctor_pat)
{
int pat_bind_reg = scrutinee_reg+2;
for (auto ctor_arg : ctor_pat->arguments)
case_env.bind_reg(ctor_arg, pat_bind_reg++);
body_reg = pat_bind_reg;
out << c_id(ctor_pat->id);
out << ") {" << endl;
pat_bind_reg = scrutinee_reg+2;
int ctor_arg_index = 0;
for (auto ctor_arg : ctor_pat->arguments)
{
out << " comp_t* e" << pat_bind_reg << " = e" << scrutinee_reg
<< "->args[" << ctor_arg_index << "]; // " << ctor_arg << endl;
pat_bind_reg++;
ctor_arg_index++;
}
}
else
{
out << *pat_expr.pat;
out << ") {" << endl;
}
//n = pat_expr.pat->output_computation(out, scrutinee_reg+1, env);
n = pat_expr.expr->output_computation(out, body_reg, case_env);
out << " e"<<result_reg << " = e" << n << ';'<< endl;
out << " }" << endl;
}
return result_reg;
}
void Function::output_function_definition(ostream& out, const string& id) const
{
output_function_heading(out, id);
out << "{" << endl;
int n = body->output_computation(out, 0, arguments);
out << " *result = e" << n << ";" << endl;
out << "}" << endl;
}
Var* parse_var(Parser& parser, istream& in)
{
if (parser.token.type == TT_VARID)
{
Var* var = new Var(parser.token.text);
parser.next(in);
return var;
}
return nullptr;
}
Var* parse_con(Parser& parser, istream& in)
{
if (parser.token.type == TT_CONID)
{
Var* var = new Var(parser.token.text);
parser.next(in);
return var;
}
return nullptr;
}
Node* parse_literal(Parser& parser, istream& in)
{
if (parser.token.type == TT_INTEGER)
{
Num* num = new Num(parser.token.ival);
parser.next(in);
return num;
}
if (parser.token.type == TT_DOUBLE)
{
Num* num = new Num(parser.token.dval);
parser.next(in);
return num;
}
if (parser.token.type == TT_STRING)
{
Str* str = new Str(parser.token.text);
parser.next(in);
return str;
}
// This is where character and string literals go
return nullptr;
}
Node* parse_oper(Parser& parser, istream& in)
{
if (parser.token.type == TT_OPER)
{
Operator* oper = new Operator(parser.token.text);
parser.next(in);
return oper;
}
return nullptr;
}
Node* parse_apat(Parser& parser, istream& in)
{
Node* apat = parse_var(parser,in);
if (!apat)
{
Var* conid = parse_con(parser,in);
if (conid)
{
//LOG(parser.token);
vector<Node*> arguments;
vector<string> names;
for (Node* argpat = parse_apat(parser, in);
argpat != nullptr;
argpat = parse_apat(parser, in))