-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.cpp
More file actions
62 lines (54 loc) · 1.24 KB
/
lambda.cpp
File metadata and controls
62 lines (54 loc) · 1.24 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
#include "lambda.hpp"
using namespace std;
// Constructors
Lambda::Lambda(string variable)
: type_{VARIABLE}, variable_{variable}, left_{nullptr}, right_{nullptr}
{
}
Lambda::Lambda(string variable, unique_ptr<Lambda> body)
: type_{ABSTRACTION}, variable_{variable}, left_{move(body)}, right_{nullptr}
{
}
Lambda::Lambda(unique_ptr<Lambda> func, unique_ptr<Lambda> param)
: type_{APPLICATION}, variable_{}, left_{move(func)}, right_{move(param)}
{
}
// Query methods
Lambda::Type Lambda::type() const
{
return type_;
}
// Output operation
ostream &operator<<(ostream &os, const unique_ptr<Lambda> &term)
{
switch (term->type_)
{
case Lambda::VARIABLE:
os << term->variable_;
break;
case Lambda::ABSTRACTION:
os << '\\' << term->variable_ << '.' << term->left_;
break;
case Lambda::APPLICATION:
if (term->left_->type_ == Lambda::ABSTRACTION)
{
// Enclose in parenthesis
os << '(' << term->left_ << ')';
}
else
{
os << term->left_;
}
os << ' ';
if (term->right_->type_ == Lambda::ABSTRACTION || term->right_->type_ == Lambda::APPLICATION)
{
os << '(' << term->right_ << ')';
}
else
{
os << term->right_;
}
break;
}
return os;
}