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
use std::collections::HashMap;
use base::symbol::Symbol;
use base::types;
use base::types::{Alias, KindEnv, TypeEnv, TcType, Type};
pub use self::Instruction::*;
pub type VMIndex = u32;
pub type VMTag = u32;
pub type VMInt = isize;
#[derive(Copy, Clone, Debug)]
pub enum Instruction {
PushInt(isize),
PushByte(u8),
PushFloat(f64),
PushString(VMIndex),
Push(VMIndex),
PushGlobal(VMIndex),
Call(VMIndex),
TailCall(VMIndex),
Construct(VMIndex, VMIndex),
ConstructArray(VMIndex),
GetField(VMIndex),
Split,
TestTag(VMTag),
Jump(VMIndex),
CJump(VMIndex),
Pop(VMIndex),
Slide(VMIndex),
MakeClosure(VMIndex, VMIndex),
NewClosure(VMIndex, VMIndex),
CloseClosure(VMIndex),
PushUpVar(VMIndex),
GetIndex,
AddInt,
SubtractInt,
MultiplyInt,
DivideInt,
IntLT,
IntEQ,
AddByte,
SubtractByte,
MultiplyByte,
DivideByte,
ByteLT,
ByteEQ,
AddFloat,
SubtractFloat,
MultiplyFloat,
DivideFloat,
FloatLT,
FloatEQ,
}
impl Instruction {
pub fn adjust(&self) -> i32 {
match *self {
PushInt(_) | PushByte(_) | PushFloat(_) | PushString(_) | Push(_) | PushGlobal(_) => 1,
Call(n) => -(n as i32),
TailCall(n) => -(n as i32),
Construct(_, n) |
ConstructArray(n) => 1 - n as i32,
GetField(_) => 0,
Split => -1,
TestTag(_) => 1,
Jump(_) => 0,
CJump(_) => -1,
Pop(n) => -(n as i32),
Slide(n) => -(n as i32),
MakeClosure(_, _) => 1,
NewClosure(_, _) => 1,
CloseClosure(_) => -1,
PushUpVar(_) => 1,
GetIndex => 0,
AddInt | SubtractInt | MultiplyInt | DivideInt | IntLT | IntEQ | AddFloat |
AddByte | SubtractByte | MultiplyByte | DivideByte | ByteLT | ByteEQ |
SubtractFloat | MultiplyFloat | DivideFloat | FloatLT | FloatEQ => -1,
}
}
}
#[derive(Debug)]
pub struct TypeInfos {
pub id_to_type: HashMap<String, Alias<Symbol, TcType>>,
pub type_to_id: HashMap<TcType, TcType>,
}
impl KindEnv for TypeInfos {
fn find_kind(&self, type_name: &Symbol) -> Option<types::RcKind> {
let type_name = AsRef::<str>::as_ref(type_name);
self.id_to_type
.get(type_name)
.map(|alias| {
alias.args.iter().rev().fold(types::Kind::star(), |acc, arg| {
types::Kind::function(arg.kind.clone(), acc)
})
})
}
}
impl TypeEnv for TypeInfos {
fn find_type(&self, id: &Symbol) -> Option<&TcType> {
let id = AsRef::<str>::as_ref(id);
self.id_to_type
.iter()
.filter_map(|(_, ref alias)| {
alias.typ.as_ref().and_then(|typ| {
match **typ {
Type::Variants(ref variants) => {
variants.iter().find(|v| v.0.as_ref() == id)
}
_ => None,
}
})
})
.next()
.map(|x| &x.1)
}
fn find_type_info(&self, id: &Symbol) -> Option<&Alias<Symbol, TcType>> {
let id = AsRef::<str>::as_ref(id);
self.id_to_type
.get(id)
}
fn find_record(&self, fields: &[Symbol]) -> Option<(&TcType, &TcType)> {
self.id_to_type
.iter()
.find(|&(_, alias)| {
alias.typ
.as_ref()
.map(|typ| {
match **typ {
Type::Record { fields: ref record_fields, .. } => {
fields.iter().all(|name| {
record_fields.iter().any(|f| f.name.as_ref() == name.as_ref())
})
}
_ => false,
}
})
.unwrap_or(false)
})
.and_then(|t| {
let typ = t.1.typ.as_ref().unwrap();
self.type_to_id.get(typ).map(|id_type| (id_type, typ))
})
}
}
impl TypeInfos {
pub fn new() -> TypeInfos {
TypeInfos {
id_to_type: HashMap::new(),
type_to_id: HashMap::new(),
}
}
pub fn extend(&mut self, other: TypeInfos) {
let TypeInfos { id_to_type, type_to_id } = other;
self.id_to_type.extend(id_to_type);
self.type_to_id.extend(type_to_id);
}
}