-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathmodifier.cpp
More file actions
163 lines (148 loc) · 4.35 KB
/
modifier.cpp
File metadata and controls
163 lines (148 loc) · 4.35 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
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
#include "modifier.h"
using namespace febcode;
ExprPtr Modifier::Call(const std::string& name, const std::vector<ExprPtr>& args)
{
std::vector<ExprPtr> copyArgs;
std::vector<Type> argTypes;
for (const auto& arg : args)
{
copyArgs.emplace_back(clone(arg.get()));
argTypes.push_back(arg->valType);
}
int index = prg.resolveFunction(name, argTypes);
if (index < 0)
{
throw std::runtime_error("Undefined function: " + name);
}
Type returnType = prg.functions[index].returnType;
ExprPtr c = std::make_unique<CallExpr>(name, std::move(copyArgs));
c->valType = returnType;
return c;
}
ExprPtr Modifier::Index(const ExprPtr& object, const ExprPtr& index)
{
if (object->valType && object->valType->kind == TypeKind::Array)
{
Type elementType = object->valType->elementType;
ExprPtr i = std::make_unique<IndexExpr>(clone(object.get()), clone(index.get()));
i->valType = elementType;
return i;
}
else
{
throw std::runtime_error("Cannot index non-array type");
}
}
ExprPtr Modifier::Member(const ExprPtr& object, const std::string& property)
{
if (object->valType && object->valType->kind == TypeKind::Struct)
{
const auto& fields = object->valType->fields;
auto it = std::find_if(fields.begin(), fields.end(), [&](const auto& field) { return field.second == property; });
if (it != fields.end())
{
Type fieldType = it->first;
ExprPtr m = std::make_unique<MemberExpr>(clone(object.get()), property);
m->valType = fieldType;
return m;
}
else
{
throw std::runtime_error("Struct type does not have member: " + property);
}
}
else if (object->valType && object->valType->kind == TypeKind::Vec2)
{
if (property == "x" || property == "y")
{
Type fieldType = prg.types.Double();
ExprPtr m = std::make_unique<MemberExpr>(clone(object.get()), property);
m->valType = fieldType;
return m;
}
else
{
throw std::runtime_error("Vec2 type does not have member: " + property);
}
}
else if (object->valType && object->valType->kind == TypeKind::Vec3)
{
if (property == "x" || property == "y" || property == "z")
{
Type fieldType = prg.types.Double();
ExprPtr m = std::make_unique<MemberExpr>(clone(object.get()), property);
m->valType = fieldType;
return m;
}
else
{
throw std::runtime_error("Vec3 type does not have member: " + property);
}
}
else
{
throw std::runtime_error("Cannot access member of non-struct type");
}
}
ExprPtr Modifier::Initializer(Type type)
{
if ((type == nullptr) || (type->kind != TypeKind::Array) || (type->elementType == nullptr))
{
throw std::runtime_error("Invalid type in Initializer");
}
Type elementType = type->elementType;
size_t arraySize = type->arraySize;
std::vector<ExprPtr> elements;
for (int i = 0; i < arraySize; ++i)
{
elements.push_back(Zero(elementType));
}
ExprPtr init = std::make_unique<InitExpr>(std::move(elements));
init->valType = type;
return init;
}
ExprPtr Modifier::Constructor(Type type)
{
if ((type == nullptr) || (type->kind != TypeKind::Struct))
{
throw std::runtime_error("Invalid type in Constructor");
}
std::vector<ExprPtr> init;
for (const auto& field : type->fields)
{
init.push_back(Zero(field.first));
}
ExprPtr i = std::make_unique<ConstructorExpr>(type, std::move(init));
return i;
}
ExprPtr Modifier::Zero(Type type)
{
switch (type->kind)
{
case TypeKind::Bool : return Literal(false);
case TypeKind::Int : return Literal(0);
case TypeKind::Double: return Literal(0.0);
case TypeKind::Vec2 : return Literal(vec2());
case TypeKind::Vec3 : return Literal(vec3());
case TypeKind::Mat2 : return Literal(mat2());
case TypeKind::Mat3 : return Literal(mat3());
case TypeKind::Array : return Initializer(type);
case TypeKind::Struct: return Constructor(type);
default:
throw std::runtime_error("Unsupported type for Zero");
}
}
ExprPtr Modifier::Unary(UnaryOp op, const Expression* arg)
{
ExprPtr u = std::make_unique<UnaryExpr>(op, clone(arg));
u->valType = arg->valType;
return u;
}
ExprPtr Modifier::Binary(BinaryOp op, const Expression* left, const Expression* right)
{
// get the signature for this operator and operand types (this throws if the operator is not defined for these types)
BinaryOpSignature sig = prg.resolveBinaryOp(op, left->valType, right->valType);
ExprPtr b = std::make_unique<BinaryExpr>(std::move(clone(left)), op, std::move(clone(right)));
b->valType = sig.resultType;
return b;
}