-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodes.hpp
More file actions
81 lines (62 loc) · 1.88 KB
/
nodes.hpp
File metadata and controls
81 lines (62 loc) · 1.88 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
#pragma once
#include "error.hpp"
#include <memory>
namespace lambda {
enum class LambdaType : uint8_t { VARIABLE, FUNCTION, APPLICATION };
#ifdef LAMBDA_NOALIGN
#pragma pack(push, 1)
#endif
class LambdaBase {
public:
virtual ~LambdaBase() = default;
virtual inline LambdaType getType() const noexcept = 0;
};
class Variable : public LambdaBase {
public:
// Using uint16_t because it's pleanty enough
// Using uint32_t doesnt change optimized size, but does change
// "real" size
std::uint16_t count;
char name;
Variable(char name, std::uint16_t count = 0) : name(name), count(count) {}
inline LambdaType getType() const noexcept override {
return LambdaType::VARIABLE;
}
inline bool operator==(const Variable& other) const noexcept {
return name == other.name;
}
};
class Function : public LambdaBase {
public:
Variable bound;
std::unique_ptr<LambdaBase> body;
Function(Variable bound, std::unique_ptr<LambdaBase> body)
: bound(std::move(bound)), body(std::move(body)) {
if (!this->body)
throw LambdaLibException("Failed to create a function, body is nullptr.");
}
inline LambdaType getType() const noexcept override {
return LambdaType::FUNCTION;
}
};
class Application : public LambdaBase {
public:
// These unique ptr are 16 bytes each, because we have virtual destructor (and
// virtual function?)
std::unique_ptr<LambdaBase> left;
std::unique_ptr<LambdaBase> right;
Application(
std::unique_ptr<LambdaBase> left, std::unique_ptr<LambdaBase> right)
: left(std::move(left)), right(std::move(right)) {
if (!(this->left && this->right))
throw LambdaLibException(
"Failed to create an application, one or more arguments are nullptr.");
}
inline LambdaType getType() const noexcept override {
return LambdaType::APPLICATION;
}
};
#ifdef LAMBDA_NOALIGN
#pragma pack(pop)
#endif
} // namespace lambda