-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathType.cpp
57 lines (49 loc) · 1.38 KB
/
Type.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
#include "Type.hpp"
Value* NamedType::createNewValue(Context* context)
{
createNewValue(context, nullptr);
}
Value* NamedType::createNewValue(Context* context, Value* value)
{
return context->createValueOfType(identifier);
}
ArrayType::ArrayType(Expression* expression, Type* arrayType):
expression(expression), arrayType(arrayType) {}
Value* ArrayType::createNewValue(Context* context)
{
createNewValue(context, nullptr);
}
Value* ArrayType::createNewValue(Context* context, Value* value)
{
Value* arraySizeValue = expression->evaluate(*context);
int arraySize = (dynamic_cast<IntegerValue*> (arraySizeValue))->value;
ArrayValue* arrayValue = new ArrayValue(arraySize);
for (int i = 0; i < arraySize; ++i)
{
arrayValue->array[i] = arrayType->createNewValue(context);
}
return arrayValue;
}
StructType::StructType(FieldListSequence* fieldListSequence)
{
for (auto& fieldList : fieldListSequence->fieldLists)
{
for (auto& identifier : fieldList->identifierList->identifiers)
{
fieldTypes[identifier] = fieldList->type;
}
}
}
Value* StructType::createNewValue(Context* context)
{
return createNewValue(context, nullptr);
}
Value* StructType::createNewValue(Context* context, Value* value)
{
StructValue* structValue = new StructValue();
for (auto& field : fieldTypes)
{
structValue->fields[field.first] = field.second->createNewValue(context);
}
return structValue;
}