-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinterpreter.hpp
3154 lines (2564 loc) · 103 KB
/
interpreter.hpp
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
#pragma once
#include <algorithm>
#include <cassert>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <memory>
#include <regex>
#include <sstream>
#include <stdexcept>
#include <unordered_map>
#include <utility>
#include <vector>
namespace lexer {
struct Token {
enum class Category {
IDENTIFIER,
LAMBDA,
DOT,
COMMA,
EQUAL,
ASSIGN,
OPEN_PAREN,
CLOSE_PAREN,
OPEN_BRACE,
CLOSE_BRACE,
COLON,
ARROW,
EXCLAMATION,
SEMICOLON,
CONSTANT_TRUE,
CONSTANT_FALSE,
KEYWORD_BOOL,
KEYWORD_IF,
KEYWORD_THEN,
KEYWORD_ELSE,
CONSTANT_ZERO,
KEYWORD_NAT,
KEYWORD_SUCC,
KEYWORD_PRED,
KEYWORD_ISZERO,
KEYWORD_LET,
KEYWORD_IN,
KEYWORD_REF,
KEYWORD_REF_TYPE,
KEYWORD_SOURCE_TYPE,
KEYWORD_SINK_TYPE,
CONSTANT_UNIT,
KEYWORD_UNIT_TYPE,
KEYWORD_FIX,
MARKER_END,
MARKER_INVALID,
};
enum class IdentitySubCategory {
VARIABLE,
RECORD_LABEL,
};
explicit Token(Category category = Category::MARKER_INVALID,
std::string text = "")
: category_(category),
text_(category == Category::IDENTIFIER ? text : "") {}
bool operator==(const Token &other) const {
return category_ == other.category_ && text_ == other.text_;
}
bool operator!=(const Token &other) const { return !(*this == other); }
Category GetCategory() const { return category_; }
std::string GetText() const { return text_; }
private:
Category category_ = Category::MARKER_INVALID;
std::string text_;
};
std::ostream &operator<<(std::ostream &out, Token token);
namespace {
const std::string kLambdaInputSymbol = "l";
const std::string kKeywordTop = "Top";
const std::string kKeywordBool = "Bool";
const std::string kKeywordNat = "Nat";
const std::string kKeywordLet = "let";
const std::string kKeywordIn = "in";
const std::string kKeywordRef = "ref";
const std::string kKeywordRefType = "Ref";
const std::string kKeywordSourceType = "Source";
const std::string kKeywordSinkType = "Sink";
const std::string kKeywordUnit = "unit";
const std::string kKeywordUnitType = "Unit";
const std::string kKeywordFix = "fix";
} // namespace
class Lexer {
public:
explicit Lexer(std::istringstream &&in) {
std::istringstream iss(SurroundTokensBySpaces(std::move(in)));
token_strings_ =
std::vector<std::string>(std::istream_iterator<std::string>{iss},
std::istream_iterator<std::string>());
}
Token NextToken() {
Token token;
if (current_token_ == token_strings_.size()) {
current_token_ = token_strings_.size() + 1;
token = Token(Token::Category::MARKER_END);
return token;
}
static std::unordered_map<std::string, Token::Category>
token_str_to_cat = {
{kLambdaInputSymbol, Token::Category::LAMBDA},
{".", Token::Category::DOT},
{",", Token::Category::COMMA},
{"=", Token::Category::EQUAL},
{"(", Token::Category::OPEN_PAREN},
{")", Token::Category::CLOSE_PAREN},
{"{", Token::Category::OPEN_BRACE},
{"}", Token::Category::CLOSE_BRACE},
{":", Token::Category::COLON},
{"->", Token::Category::ARROW},
{":=", Token::Category::ASSIGN},
{"!", Token::Category::EXCLAMATION},
{";", Token::Category::SEMICOLON},
{"true", Token::Category::CONSTANT_TRUE},
{"false", Token::Category::CONSTANT_FALSE},
{kKeywordBool, Token::Category::KEYWORD_BOOL},
{"if", Token::Category::KEYWORD_IF},
{"then", Token::Category::KEYWORD_THEN},
{"else", Token::Category::KEYWORD_ELSE},
{"0", Token::Category::CONSTANT_ZERO},
{kKeywordNat, Token::Category::KEYWORD_NAT},
{"succ", Token::Category::KEYWORD_SUCC},
{"pred", Token::Category::KEYWORD_PRED},
{"iszero", Token::Category::KEYWORD_ISZERO},
{kKeywordLet, Token::Category::KEYWORD_LET},
{kKeywordIn, Token::Category::KEYWORD_IN},
{kKeywordRef, Token::Category::KEYWORD_REF},
{kKeywordRefType, Token::Category::KEYWORD_REF_TYPE},
{kKeywordSourceType, Token::Category::KEYWORD_SOURCE_TYPE},
{kKeywordSinkType, Token::Category::KEYWORD_SINK_TYPE},
{kKeywordUnit, Token::Category::CONSTANT_UNIT},
{kKeywordUnitType, Token::Category::KEYWORD_UNIT_TYPE},
{kKeywordFix, Token::Category::KEYWORD_FIX},
};
auto token_string = token_strings_[current_token_];
if (token_str_to_cat.find(token_string) != std::end(token_str_to_cat)) {
token = Token(token_str_to_cat[token_string]);
} else if (IsIdentifierName(token_string)) {
token = Token(Token::Category::IDENTIFIER, token_string);
}
++current_token_;
return token;
}
void PutBackToken() {
if (current_token_ > 0) {
--current_token_;
}
}
private:
std::string SurroundTokensBySpaces(std::istringstream &&in) {
std::ostringstream processed_stream;
char c;
while (in.get(c)) {
// Check for one-character separators and surround them with spaces.
if (c == ',' || c == '.' || c == '=' || c == '(' || c == ')' ||
c == '{' || c == '}' || c == '!' || c == ';') {
processed_stream << " " << c << " ";
} else if (c == '-') {
// Check for the two-character serparator '->' and surround it
// with spaces.
if (in.peek() == '>') {
in.get(c);
processed_stream << " -> ";
} else {
// Just write '-' and let the lexing error be
// discovered later.
processed_stream << " - ";
}
} else if (c == ':') {
// Check for the two-character serparator ':=' and surround it
// with spaces.
if (in.peek() == '=') {
in.get(c);
processed_stream << " := ";
} else {
processed_stream << " : ";
}
} else {
processed_stream << c;
}
}
return processed_stream.str();
}
bool IsIdentifierName(std::string token_text) {
static const std::regex id_regex("[_[:alpha:]][_[:alnum:]]*");
return std::regex_match(token_text, id_regex);
}
private:
std::vector<std::string> token_strings_;
int current_token_ = 0;
};
std::ostream &operator<<(std::ostream &out, Token token) {
static std::unordered_map<Token::Category, std::string> token_to_str = {
{Token::Category::LAMBDA, "λ"},
{Token::Category::DOT, "."},
{Token::Category::COMMA, ","},
{Token::Category::EQUAL, "="},
{Token::Category::OPEN_PAREN, "("},
{Token::Category::CLOSE_PAREN, ")"},
{Token::Category::OPEN_BRACE, "{"},
{Token::Category::CLOSE_BRACE, "}"},
{Token::Category::COLON, ":"},
{Token::Category::ARROW, "->"},
{Token::Category::ASSIGN, ":="},
{Token::Category::EXCLAMATION, "!"},
{Token::Category::SEMICOLON, ";"},
{Token::Category::CONSTANT_TRUE, "<true>"},
{Token::Category::CONSTANT_FALSE, "<false>"},
{Token::Category::KEYWORD_BOOL, "<Bool>"},
{Token::Category::KEYWORD_IF, "<if>"},
{Token::Category::KEYWORD_THEN, "<then>"},
{Token::Category::KEYWORD_ELSE, "<else>"},
{Token::Category::CONSTANT_ZERO, "0"},
{Token::Category::KEYWORD_NAT, "<Nat>"},
{Token::Category::KEYWORD_SUCC, "succ"},
{Token::Category::KEYWORD_PRED, "pred"},
{Token::Category::KEYWORD_ISZERO, "iszero"},
{Token::Category::KEYWORD_LET, "let"},
{Token::Category::KEYWORD_IN, "in"},
{Token::Category::KEYWORD_REF, "ref"},
{Token::Category::KEYWORD_REF_TYPE, "Ref"},
{Token::Category::KEYWORD_SOURCE_TYPE, kKeywordSourceType},
{Token::Category::KEYWORD_SINK_TYPE, kKeywordSinkType},
{Token::Category::CONSTANT_UNIT, "unit"},
{Token::Category::KEYWORD_UNIT_TYPE, "Unit"},
{Token::Category::KEYWORD_FIX, kKeywordFix},
{Token::Category::MARKER_END, "<END>"},
{Token::Category::MARKER_INVALID, "<INVALID>"},
};
if (token_to_str.find(token.GetCategory()) != std::end(token_to_str)) {
out << token_to_str[token.GetCategory()];
} else {
switch (token.GetCategory()) {
case Token::Category::IDENTIFIER:
out << token.GetText();
break;
default:
out << "<ILLEGAL_TOKEN>";
}
}
return out;
}
} // namespace lexer
namespace parser {
// TODO Add support for creating named (and anonymouse) record types.
// Might be helpful to check "ascriptions" (see tapl sec. 11.4).
class Type {
friend std::ostream &operator<<(std::ostream &, const Type &);
enum class ReferenceCategory {
REF,
SOURCE,
SINK,
};
public:
static Type &Top() {
static Type type;
type.category_ = TypeCategory::TOP;
return type;
}
static Type &IllTyped() {
static Type type;
return type;
}
static Type &Bool() {
static Type type(BaseType::BOOL);
return type;
}
static Type &Nat() {
static Type type(BaseType::NAT);
return type;
}
static Type &Unit() {
static Type type(BaseType::UNIT);
return type;
}
static Type &Function(Type &lhs, Type &rhs) {
static std::vector<std::unique_ptr<Type>> type_pool;
auto result =
std::find_if(std::begin(type_pool), std::end(type_pool),
[&](const std::unique_ptr<Type> &type) {
return type->lhs_ == &lhs && type->rhs_ == &rhs;
});
if (result != std::end(type_pool)) {
return **result;
}
type_pool.emplace_back(std::unique_ptr<Type>(new Type(lhs, rhs)));
return *type_pool.back();
}
using RecordFields = std::unordered_map<std::string, Type &>;
static Type &Record(RecordFields fields) {
static std::vector<std::unique_ptr<Type>> type_pool;
auto result = std::find_if(std::begin(type_pool), std::end(type_pool),
[&](const std::unique_ptr<Type> &type) {
return type->record_fields_ == fields;
});
if (result != std::end(type_pool)) {
return **result;
}
type_pool.emplace_back(
std::unique_ptr<Type>(new Type(std::move(fields))));
return *type_pool.back();
}
static Type &Ref(Type &ref_type) {
return Ref(ref_type, ReferenceCategory::REF);
}
static Type &Source(Type &ref_type) {
return Ref(ref_type, ReferenceCategory::SOURCE);
}
static Type &Sink(Type &ref_type) {
return Ref(ref_type, ReferenceCategory::SINK);
}
static Type &Ref(Type &ref_type, ReferenceCategory ref_category) {
static std::vector<std::unique_ptr<Type>> type_pool;
auto result =
std::find_if(std::begin(type_pool), std::end(type_pool),
[&](const std::unique_ptr<Type> &type) {
return type->ref_type_ == &ref_type &&
type->ref_category_ == ref_category;
});
if (result != std::end(type_pool)) {
return **result;
}
type_pool.emplace_back(
std::unique_ptr<Type>(new Type(&ref_type, ref_category)));
return *type_pool.back();
}
Type(const Type &) = delete;
Type &operator=(const Type &) = delete;
Type(Type &&) = delete;
Type &operator=(Type &&) = delete;
~Type() = default;
bool operator==(const Type &other) const {
if (category_ != other.category_) {
return false;
}
switch (category_) {
case TypeCategory::TOP:
return other.category_ == TypeCategory::TOP;
case TypeCategory::ILL:
return other.category_ == TypeCategory::ILL;
case TypeCategory::BASE:
return base_type_ == other.base_type_;
case TypeCategory::FUNCTION:
assert(lhs_ && rhs_ && other.lhs_ && other.rhs_);
return (*lhs_ == *other.lhs_) && (*rhs_ == *other.rhs_);
case TypeCategory::REF:
return *ref_type_ == *other.ref_type_;
case TypeCategory::RECORD:
return record_fields_ == other.record_fields_;
}
}
bool operator!=(const Type &other) const { return !(*this == other); }
bool IsIllTyped() const { return category_ == TypeCategory::ILL; }
bool IsTop() const { return category_ == TypeCategory::TOP; }
bool IsBool() const {
return category_ == TypeCategory::BASE && base_type_ == BaseType::BOOL;
}
bool IsNat() const {
return category_ == TypeCategory::BASE && base_type_ == BaseType::NAT;
}
bool IsUnit() const {
return category_ == TypeCategory::BASE && base_type_ == BaseType::UNIT;
}
bool IsFunction() const { return category_ == TypeCategory::FUNCTION; }
bool IsRecord() const { return category_ == TypeCategory::RECORD; }
bool IsRef() const {
return category_ == TypeCategory::REF &&
ref_category_ == ReferenceCategory::REF;
}
bool IsSource() const {
return category_ == TypeCategory::REF &&
ref_category_ == ReferenceCategory::SOURCE;
}
bool IsSink() const {
return category_ == TypeCategory::REF &&
ref_category_ == ReferenceCategory::SINK;
}
Type &FunctionLHS() const {
if (!IsFunction()) {
throw std::invalid_argument("Invalid function type.");
}
return *lhs_;
}
Type &FunctionRHS() const {
if (!IsFunction()) {
throw std::invalid_argument("Invalid function type.");
}
return *rhs_;
}
const RecordFields &GetRecordFields() const {
if (!IsRecord()) {
throw std::invalid_argument("Invalid record type.");
}
return record_fields_;
}
Type &RefType() const {
if (!IsRef() && !IsSource() && !IsSink()) {
throw std::invalid_argument("Expected Ref/Source/Sink type.");
}
return *ref_type_;
}
int StorageSize() const {
if (IsBool() || IsUnit()) {
return 1;
} else if (IsNat() || IsFunction()) {
return sizeof(int);
} else if (IsRecord()) {
int total_size = 1;
for (auto &field : GetRecordFields()) {
total_size += field.second.StorageSize();
}
return total_size;
}
std::ostringstream ss;
ss << "Type size cannot be computed: " << *this;
throw std::logic_error(ss.str());
}
private:
enum class TypeCategory {
BASE,
FUNCTION,
RECORD,
TOP,
REF,
ILL,
};
enum class BaseType {
BOOL,
NAT,
UNIT,
};
Type() = default;
explicit Type(BaseType base_type)
: base_type_(base_type), category_(TypeCategory::BASE) {}
Type(Type &lhs, Type &rhs)
: lhs_(&lhs), rhs_(&rhs), category_(TypeCategory::FUNCTION) {}
Type(RecordFields fields)
: record_fields_(std::move(fields)), category_(TypeCategory::RECORD) {}
Type(Type *ref_type, ReferenceCategory ref_category)
: ref_type_(ref_type),
category_(TypeCategory::REF),
ref_category_(ref_category) {}
TypeCategory category_ = TypeCategory::ILL;
BaseType base_type_;
Type *lhs_ = nullptr;
Type *rhs_ = nullptr;
RecordFields record_fields_{};
Type *ref_type_ = nullptr;
ReferenceCategory ref_category_;
};
std::ostream &operator<<(std::ostream &out, const Type &type) {
if (type.IsTop()) {
out << lexer::kKeywordTop;
} else if (type.IsBool()) {
out << lexer::kKeywordBool;
} else if (type.IsNat()) {
out << lexer::kKeywordNat;
} else if (type.IsUnit()) {
out << lexer::kKeywordUnitType;
} else if (type.IsFunction()) {
out << "(" << *type.lhs_ << " "
<< lexer::Token(lexer::Token::Category::ARROW) << " " << *type.rhs_
<< ")";
} else if (type.IsRecord()) {
out << "{";
bool first = true;
for (auto &field : type.record_fields_) {
if (!first) {
out << ", ";
}
first = false;
out << field.first << ":" << field.second;
}
out << "}";
} else if (type.IsRef()) {
out << "Ref " << *type.ref_type_;
} else if (type.IsSource()) {
out << "Source " << *type.ref_type_;
} else if (type.IsSink()) {
out << "Sink " << *type.ref_type_;
} else {
out << "Ⱦ";
}
return out;
}
class Term {
friend std::ostream &operator<<(std::ostream &, const Term &);
public:
enum class Category {
EMPTY,
LAMBDA,
VARIABLE,
APPLICATION,
IF,
TRUE,
FALSE,
SUCC,
PRED,
ISZERO,
ZERO,
RECORD,
PROJECTION,
LET,
REF,
DEREF,
ASSIGNMENT,
UNIT,
SEQUENCE,
PARENTHESIZED,
FIX,
STORE_LOCATION,
};
static Term Lambda(std::string arg_name, Type &arg_type) {
Term result;
result.lambda_arg_name_ = arg_name;
result.lambda_arg_type_ = &arg_type;
result.category_ = Category::LAMBDA;
return result;
}
static Term Variable(std::string var_name, int de_bruijn_idx) {
Term result;
result.variable_name_ = var_name;
result.de_bruijn_idx_ = de_bruijn_idx;
result.category_ = Category::VARIABLE;
return result;
}
static Term Application(std::unique_ptr<Term> lhs,
std::unique_ptr<Term> rhs) {
Term result;
result.category_ = Category::APPLICATION;
result.application_lhs_ = std::move(lhs);
result.application_rhs_ = std::move(rhs);
return result;
}
static Term If() {
Term result;
result.category_ = Category::IF;
return result;
}
static Term True() {
Term result;
result.category_ = Category::TRUE;
return result;
}
static Term False() {
Term result;
result.category_ = Category::FALSE;
return result;
}
static Term Succ() {
Term result;
result.category_ = Category::SUCC;
return result;
}
static Term Pred() {
Term result;
result.category_ = Category::PRED;
return result;
}
static Term IsZero() {
Term result;
result.category_ = Category::ISZERO;
return result;
}
static Term Zero() {
Term result;
result.category_ = Category::ZERO;
return result;
}
static Term Record() {
Term result;
result.category_ = Category::RECORD;
return result;
}
static Term Projection(std::unique_ptr<Term> term, std::string label) {
Term result;
result.projection_term_ = std::move(term);
result.projection_label_ = label;
result.category_ = Category::PROJECTION;
return result;
}
static Term Let(std::string binding_name) {
Term result;
result.category_ = Category::LET;
result.let_binding_name_ = binding_name;
return result;
}
static Term Ref() {
Term result;
result.category_ = Category::REF;
return result;
}
static Term Deref() {
Term result;
result.category_ = Category::DEREF;
return result;
}
static Term Assignment(std::unique_ptr<Term> lhs) {
Term result;
result.assignment_lhs_ = std::move(lhs);
result.category_ = Category::ASSIGNMENT;
return result;
}
static Term Unit() {
Term result;
result.category_ = Category::UNIT;
return result;
}
static Term StoreLocation(int location) {
Term result;
result.category_ = Category::STORE_LOCATION;
result.store_location_ = location;
return result;
}
static Term Sequence(std::unique_ptr<Term> lhs) {
Term result;
result.sequence_lhs_ = std::move(lhs);
result.category_ = Category::SEQUENCE;
return result;
}
static Term ParenthesizedTerm() {
Term result;
result.category_ = Category::PARENTHESIZED;
return result;
}
static Term FixTerm() {
Term result;
result.category_ = Category::FIX;
return result;
}
Term() = default;
Term(const Term &) = delete;
Term &operator=(const Term &) = delete;
Term(Term &&) = default;
Term &operator=(Term &&) = default;
~Term() = default;
bool IsLambda() const { return category_ == Category::LAMBDA; }
void MarkAsComplete() { is_complete_ = true; }
bool IsVariable() const { return category_ == Category::VARIABLE; }
bool IsApplication() const { return category_ == Category::APPLICATION; }
bool IsIf() const { return category_ == Category::IF; }
bool IsTrue() const { return category_ == Category::TRUE; }
bool IsFalse() const { return category_ == Category::FALSE; }
bool IsSucc() const { return category_ == Category::SUCC; }
bool IsPred() const { return category_ == Category::PRED; }
bool IsIsZero() const { return category_ == Category::ISZERO; }
bool IsConstantZero() const { return category_ == Category::ZERO; }
bool IsRecord() const { return category_ == Category::RECORD; }
bool IsProjection() const { return category_ == Category::PROJECTION; }
bool IsLet() const { return category_ == Category::LET; }
bool IsRef() const { return category_ == Category::REF; }
bool IsDeref() const { return category_ == Category::DEREF; }
bool IsAssignment() const { return category_ == Category::ASSIGNMENT; }
bool IsUnit() const { return category_ == Category::UNIT; }
bool IsStoreLocation() const {
return category_ == Category::STORE_LOCATION;
}
bool IsSequence() const { return category_ == Category::SEQUENCE; }
bool IsParenthesized() const {
return category_ == Category::PARENTHESIZED;
}
bool IsFix() const { return category_ == Category::FIX; }
bool IsInvalid() const {
if (IsLambda()) {
return lambda_arg_name_.empty() || !lambda_arg_type_ ||
!lambda_body_;
} else if (IsVariable()) {
return variable_name_.empty();
} else if (IsApplication()) {
return !application_lhs_ || !application_rhs_;
} else if (IsIf()) {
return !if_condition_ || !if_then_ || !if_else_;
} else if (IsTrue() || IsFalse() || IsConstantZero() || IsUnit() ||
IsStoreLocation()) {
return false;
} else if (IsSucc()) {
return !unary_op_arg_;
} else if (IsPred()) {
return !unary_op_arg_;
} else if (IsIsZero()) {
return !unary_op_arg_;
} else if (IsRecord()) {
return record_labels_.size() == 0 ||
record_labels_.size() != record_terms_.size();
} else if (IsProjection()) {
return !projection_term_;
} else if (IsLet()) {
return let_binding_name_.empty() || !let_bound_term_ ||
!let_body_term_;
} else if (IsRef()) {
return !ref_term_;
} else if (IsDeref()) {
return !deref_term_;
} else if (IsAssignment()) {
return !assignment_lhs_ || !assignment_rhs_;
} else if (IsSequence()) {
return !sequence_lhs_ || !sequence_rhs_;
} else if (IsParenthesized()) {
return !parenthesized_term_;
} else if (IsFix()) {
return !fix_term_;
}
return true;
}
bool IsEmpty() const { return category_ == Category::EMPTY; }
enum Category Category() const { return category_; }
Term &Combine(Term &&term) {
if (term.IsInvalid()) {
throw std::invalid_argument(
"Term::Combine() received an invalid Term.");
}
if (IsLambda()) {
if (!lambda_body_) {
lambda_body_ = std::make_unique<Term>(std::move(term));
} else {
ConvertToApplication(std::move(term));
}
} else if (IsVariable()) {
ConvertToApplication(std::move(term));
variable_name_ = "";
} else if (IsApplication()) {
ConvertToApplication(std::move(term));
} else if (IsIf()) {
if (!if_condition_) {
if_condition_ = std::make_unique<Term>(std::move(term));
} else if (!if_then_) {
if_then_ = std::make_unique<Term>(std::move(term));
} else {
if (!if_else_) {
if_else_ = std::make_unique<Term>(std::move(term));
} else {
ConvertToApplication(std::move(term));
}
}
} else if (IsSucc()) {
if (!unary_op_arg_) {
unary_op_arg_ = std::make_unique<Term>(std::move(term));
} else {
throw std::invalid_argument(
"Trying to combine with succ(...).");
}
} else if (IsPred()) {
if (!unary_op_arg_) {
unary_op_arg_ = std::make_unique<Term>(std::move(term));
} else {
throw std::invalid_argument(
"Trying to combine with pred(...).");
}
} else if (IsIsZero()) {
if (!unary_op_arg_) {
unary_op_arg_ = std::make_unique<Term>(std::move(term));
} else {
throw std::invalid_argument(
"Trying to combine with iszero(...).");
}
} else if (IsTrue() || IsFalse() || IsConstantZero() || IsUnit()) {
throw std::invalid_argument("Trying to combine with a constant.");
} else if (IsRecord()) {
if (is_complete_) {
throw std::logic_error(
"Trying to combine with a complete Record.");
}
if (!IsRecordExpectingTerm()) {
throw std::logic_error(
"Trying to combine with a Record while it isn't expecting "
"a value.");
}
record_terms_.push_back(std::make_unique<Term>(std::move(term)));
} else if (IsProjection()) {
ConvertToApplication(std::move(term));
} else if (IsLet()) {
if (!let_bound_term_) {
let_bound_term_ = std::make_unique<Term>(std::move(term));
} else {
if (!let_body_term_) {
let_body_term_ = std::make_unique<Term>(std::move(term));
} else {
ConvertToApplication(std::move(term));
}