-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.c
86 lines (74 loc) · 1.71 KB
/
types.c
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
/*
* types.c -- type representation
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "utils.h"
#include "types.h"
Type *newPrimitiveType(char *printName, int byte_size)
{
Type *type;
type = (Type *) allocate(sizeof(Type));
type->byte_size = byte_size;
type->kind = TYPE_KIND_PRIMITIVE;
type->u.primitiveType.printName = printName;
return type;
}
Type *newArrayType(int size, Type * baseType)
{
Type *type;
type = (Type *) allocate(sizeof(Type));
type->byte_size = size * baseType->byte_size;
type->kind = TYPE_KIND_ARRAY;
type->u.arrayType.size = size;
type->u.arrayType.baseType = baseType;
return type;
}
ParamTypes *emptyParamTypes(void)
{
ParamTypes *paramTypes;
paramTypes = (ParamTypes *) allocate(sizeof(ParamTypes));
paramTypes->isEmpty = TRUE;
return paramTypes;
}
ParamTypes *newParamTypes(Type * type, boolean isRef, ParamTypes * next)
{
ParamTypes *paramTypes;
paramTypes = (ParamTypes *) allocate(sizeof(ParamTypes));
paramTypes->isEmpty = FALSE;
paramTypes->type = type;
paramTypes->isRef = isRef;
paramTypes->next = next;
return paramTypes;
}
void showType(Type * type)
{
switch (type->kind) {
case TYPE_KIND_PRIMITIVE:
printf("%s", type->u.primitiveType.printName);
break;
case TYPE_KIND_ARRAY:
printf("array [%d] of ", type->u.arrayType.size);
showType(type->u.arrayType.baseType);
break;
default:
error("unknown type kind %d in showType", type->kind);
}
}
void showParamTypes(ParamTypes * paramTypes)
{
printf("(");
while (!paramTypes->isEmpty) {
if (paramTypes->isRef) {
printf("ref ");
}
showType(paramTypes->type);
paramTypes = paramTypes->next;
if (!paramTypes->isEmpty) {
printf(", ");
}
}
printf(")");
}