-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.ts
215 lines (200 loc) · 6.52 KB
/
types.ts
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import debug from './util/debug';
import join from './util/join';
const { sum } = require('./mpl/sum.mpl'); // tslint:disable-line
import { Variable } from './api';
import { RegisterAgnosticTargetInfo } from './TargetInfo';
import { TypeError } from './TypeError';
import * as deepEqual from 'deep-equal';
export type ProductComponent = {
name: string;
type: Type;
};
type Permission = 'stdout';
export type TypeReference = { namedType: string };
export type String = { kind: 'String' };
export type Integer = { kind: 'Integer' };
export type Boolean = { kind: 'Boolean' };
export type Function = {
kind: 'Function';
permissions: Permission[];
arguments: (Type | TypeReference)[];
returnType: Type | TypeReference;
};
export type List = { kind: 'List'; of: Type };
export type Product = { kind: 'Product'; members: ProductComponent[] };
export type Method = {
name: string;
function: Type; // TODO: Should maybe just be function?
};
export type Type = {
type: String | Integer | Boolean | Function | List | Product;
methods: Method[];
original?: TypeReference;
};
export const toString = (type: Type): string => {
// TODO: Include the original in here somehow?
switch (type.type.kind) {
case 'String':
case 'Integer':
case 'Boolean':
return type.type.kind;
case 'Function':
return type.type.kind + '<' + join(type.type.arguments.map(toString), ', ') + '>';
case 'Product':
return (
'{' +
type.type.members.map(member => `${member.name}: ${toString(member.type)}`) +
'}'
);
case 'List':
return `${toString(type.type.of)}[]`;
default:
// Seems like this is where user-defined types end up? Shouldn't be.
return (type.type as any).kind;
}
};
export type TypeDeclaration = { name: string; type: Type };
export const resolve = (
unresolved: Type | TypeReference,
availableTypes,
sourceLocation
): Type | { errors: TypeError[]; newVariables: Variable[] } => {
if ((unresolved as any) == 'ImplicitThis') {
throw debug('need resolving for methods');
}
if (!('namedType' in unresolved)) {
return unresolved;
}
if (!availableTypes) debug('no declarations');
const type = availableTypes.find(d => d.name == unresolved.namedType);
if (type) {
return {
type: type.type.type, // lol
original: unresolved,
methods: type.type.methods,
};
}
const builtin = builtinTypes[unresolved.namedType];
if (builtin) {
return builtin;
}
return {
errors: [
{
kind: 'unknownType',
name: (unresolved as TypeReference).namedType,
sourceLocation,
},
],
newVariables: [],
};
};
export const equal = (a: Type, b: Type): boolean => {
// Should we allow assigning one product to another if they have different names but identical members? That would be "structural typing" which I'm not sure I want.
if (!deepEqual(a.original, b.original)) return false;
if (a.type.kind == 'Function' && b.type.kind == 'Function') {
if (a.type.arguments.length != b.type.arguments.length) {
return false;
}
for (let i = 0; i < a.type.arguments.length; i++) {
const tA = a.type.arguments[i];
if ('namedType' in tA) {
throw debug('need to handle refs here');
}
const tB = b.type.arguments[i];
if ('namedType' in tB) {
throw debug('need to handle refs here');
}
if (!equal(tA, tB)) {
return false;
}
}
return true;
}
if (a.type.kind == 'Product' && b.type.kind == 'Product') {
const bProduct = b.type;
const allInLeftPresentInRight = a.type.members.every(memberA =>
bProduct.members.some(
memberB => memberA.name == memberB.name && equal(memberA.type, memberB.type)
)
);
const aProduct = a.type;
const allInRightPresentInLeft = b.type.members.every(memberB =>
aProduct.members.some(
memberA => memberA.name == memberB.name && equal(memberA.type, memberB.type)
)
);
return allInLeftPresentInRight && allInRightPresentInLeft;
}
if (a.type.kind == 'List' && b.type.kind == 'List') {
return equal(a.type.of, b.type.of);
}
return a.type.kind == b.type.kind;
};
export const Product = (members, methods): Type => ({
type: { kind: 'Product', members },
methods,
});
export const Function = (args, permissions, returnType): Type => ({
type: { kind: 'Function', arguments: args, permissions, returnType },
methods: [],
});
export const List = (ofType): Type => ({
type: { kind: 'List', of: ofType },
methods: [],
});
export const builtinTypes: { [index: string]: Type } = {
String: {
type: { kind: 'String' },
methods: [
{
name: 'startsWith',
function: Function([{ namedType: 'String' }, { namedType: 'String' }], [], {
namedType: 'Boolean',
}),
},
],
},
Integer: { type: { kind: 'Integer' }, methods: [] },
Boolean: { type: { kind: 'Boolean' }, methods: [] },
};
// TODO: Require these to be imported in user code
export const builtinFunctions: Variable[] = [
{
name: 'length',
type: Function([builtinTypes.String], [], builtinTypes.Integer),
exported: false,
},
{
name: 'print',
type: Function([builtinTypes.String], [], builtinTypes.Integer),
exported: false,
},
{
name: 'readInt',
type: Function([], ['stdout'], builtinTypes.Integer),
exported: false,
},
];
export const typeSize = (
targetInfo: RegisterAgnosticTargetInfo,
type: Type,
typeDeclarations: TypeDeclaration[]
): number => {
switch (type.type.kind) {
case 'List':
// Pointer + size
return targetInfo.bytesInWord * 2;
case 'Product':
return sum(
type.type.members.map(m => typeSize(targetInfo, m.type, typeDeclarations))
);
case 'Boolean':
case 'Function':
case 'String':
case 'Integer':
return targetInfo.bytesInWord;
default:
throw debug(`${(type as any).kind} unhandled in typeSize`);
}
};