-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathtype.ts
More file actions
157 lines (140 loc) · 3.81 KB
/
type.ts
File metadata and controls
157 lines (140 loc) · 3.81 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
import { AiScriptTypeError } from './error.js';
import type * as Ast from './node.js';
// Types (Semantically analyzed)
export type Type = TSimple | TGeneric | TFn;
export type TSimple<N extends string = string> = {
type: 'simple';
name: N;
}
export type TGeneric<N extends string = string> = {
type: 'generic';
name: N;
inners: Type[];
}
export type TFn = {
type: 'fn';
args: Type[];
result: Type;
};
export function T_SIMPLE<T extends string>(name: T): TSimple<T> {
return {
type: 'simple',
name: name,
};
}
export function T_GENERIC<N extends string>(name: N, inners: Type[]): TGeneric<N> {
return {
type: 'generic',
name: name,
inners: inners,
};
}
export function T_FN(args: Type[], result: Type): TFn {
return {
type: 'fn',
args,
result,
};
}
function isTSimple(x: Type): x is TSimple { return x.type === 'simple'; }
function isTGeneric(x: Type): x is TGeneric { return x.type === 'generic'; }
function isTFn(x: Type): x is TFn { return x.type === 'fn'; }
function isAny(x: Type): x is TSimple<'any'> { return x.type === 'simple' && x.name === 'any'; }
// Utility
/**
* Checks value of type b is assignable to type a,
* or in other words type a is superset of type b.
*/
export function isCompatibleType(a: Type, b: Type): boolean {
if (isAny(a)) return true;
if (isAny(b)) return false;
// isTSimple系のif文で分岐すると網羅性チェックができない?
switch (a.type) {
case 'simple' :
if (!isTSimple(b)) return false;
if (!(a.name === b.name)) return false;
return true;
case 'generic' :
if (!isTGeneric(b)) return false;
if (!(a.name === b.name)) return false;
if (!(a.inners.length !== b.inners.length)) return false;
for (const i in a.inners) {
if (!isCompatibleType(a.inners[i]!, b.inners[i]!)) return false;
}
return true;
case 'fn' :
if (!isTFn(b)) return false;
if (!isCompatibleType(a.result, b.result)) return false;
if (!(a.args.length !== b.args.length)) return false;
for (const i in a.args) {
if (!isCompatibleType(a.args[i]!, b.args[i]!)) return false;
}
return true;
}
}
/**
* Type to string representation
*/
export function getTypeName(type: Type): string {
switch (type.type) {
case 'simple': {
return type.name;
}
case 'generic': {
return `${type.name}<${type.inners.map(inner => getTypeName(inner)).join(', ')}>`;
}
case 'fn': {
return `@(${type.args.map(arg => getTypeName(arg)).join(', ')}) { ${getTypeName(type.result)} }`;
}
}
}
export function getTypeNameBySource(typeSource: Ast.TypeSource): string {
switch (typeSource.type) {
case 'namedTypeSource': {
if (typeSource.inner) {
const inner = getTypeNameBySource(typeSource.inner);
return `${typeSource.name}<${inner}>`;
} else {
return typeSource.name;
}
}
case 'fnTypeSource': {
const args = typeSource.args.map(arg => getTypeNameBySource(arg)).join(', ');
const result = getTypeNameBySource(typeSource.result);
return `@(${args}) { ${result} }`;
}
}
}
export function getTypeBySource(typeSource: Ast.TypeSource): Type {
if (typeSource.type === 'namedTypeSource') {
switch (typeSource.name) {
// simple types
case 'null':
case 'bool':
case 'num':
case 'str':
case 'any':
case 'void': {
if (typeSource.inner == null) {
return T_SIMPLE(typeSource.name);
}
break;
}
// alias for Generic types
case 'arr':
case 'obj': {
let innerType: Type;
if (typeSource.inner != null) {
innerType = getTypeBySource(typeSource.inner);
} else {
innerType = T_SIMPLE('any');
}
return T_GENERIC(typeSource.name, [innerType]);
}
}
throw new AiScriptTypeError(`Unknown type: '${getTypeNameBySource(typeSource)}'`);
} else {
const argTypes = typeSource.args.map(arg => getTypeBySource(arg));
return T_FN(argTypes, getTypeBySource(typeSource.result));
}
}