This repository has been archived by the owner on Feb 20, 2024. It is now read-only.
/
type.sk
140 lines (118 loc) · 3.29 KB
/
type.sk
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
namespace Skew {
enum TypeKind {
LAMBDA
SPECIAL
SYMBOL
}
class Type {
const id = _createID
var kind TypeKind
var symbol Symbol
var environment Environment = null
var substitutions List<Type> = null
var argumentTypes List<Type> = null
var returnType Type = null
var substitutionCache IntMap<Type> = null # Maps a type environment id to this type in that environment
def parameters List<ParameterSymbol> {
return
symbol == null ? null :
symbol.kind.isObject ? symbol.asObjectSymbol.parameters :
symbol.kind.isFunction ? symbol.asFunctionSymbol.parameters :
null
}
def isParameterized bool {
return substitutions != null
}
def isWrapped bool {
return symbol != null && symbol.kind == .OBJECT_WRAPPED
}
def isClass bool {
return symbol != null && symbol.kind == .OBJECT_CLASS
}
def isInterface bool {
return symbol != null && symbol.kind == .OBJECT_INTERFACE
}
def isEnum bool {
return symbol != null && symbol.kind == .OBJECT_ENUM
}
def isParameter bool {
return symbol != null && symbol.kind.isParameter
}
# Type parameters are not guaranteed to be nullable since generics are
# implemented through type erasure and the substituted type may be "int"
def isReference bool {
return symbol == null || !symbol.isValueType && !symbol.kind.isParameter && (symbol.kind != .OBJECT_WRAPPED || symbol.asObjectSymbol.wrappedType.isReference)
}
def toString string {
if kind == .SYMBOL {
if isParameterized {
var name = symbol.fullName + "<"
for i in 0..substitutions.count {
if i != 0 {
name += ", "
}
name += substitutions[i].toString
}
return name + ">"
}
return symbol.fullName
}
if kind == .LAMBDA {
var result = "fn("
for i in 0..argumentTypes.count {
if i != 0 {
result += ", "
}
result += argumentTypes[i].toString
}
return result + (returnType != null ? ") " + returnType.toString : ")")
}
return self == DYNAMIC ? "dynamic" : "null"
}
def baseType Type {
return isClass ? symbol.asObjectSymbol.baseType : null
}
def interfaceTypes List<Type> {
return isClass ? symbol.asObjectSymbol.interfaceTypes : null
}
}
namespace Type {
var DYNAMIC Type = null
var NULL Type = null
def initialize {
if DYNAMIC == null {
DYNAMIC = Type.new(.SPECIAL, null)
}
if NULL == null {
NULL = Type.new(.SPECIAL, null)
}
}
def _createID int {
return ++_nextID
}
var _nextID = 0
}
class Environment {
const id = _createID
const parameters List<ParameterSymbol>
const substitutions List<Type>
const mergeCache IntMap<Environment> = null
# This is just for debugging
def toString string {
var text = "("
for i in 0..parameters.count {
if i != 0 {
text += ", "
}
text += parameters[i].name + " => " + substitutions[i].toString
}
return text + ")"
}
}
namespace Environment {
def _createID int {
return ++_nextID
}
var _nextID = 0
}
}