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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
use crate::compiler::{Compilelet, Compiler};
use crate::types::{
CompilerError, CompilerResult, Expression, ExpressionKind, Pattern, ValuePattern,
};
use strontium::machine::instruction::{
CalculationMethod, ComparisonMethod, Instruction, Interrupt, InterruptKind,
};
use strontium::machine::register::RegisterValue;
pub struct CallCompilelet;
impl Compilelet for CallCompilelet {
fn compile(
&self,
compiler: &mut Compiler,
expression: Expression,
target_register: Option<String>,
) -> CompilerResult<Vec<Instruction>> {
let mut instructions = Vec::new();
if let ExpressionKind::Call(call) = expression.kind {
let method_name = call.name;
let signature = call.signature.clone();
match method_name.as_str() {
"print" => {
let value_register = compiler.registers.allocate_register();
if let Some(pattern) = signature {
instructions.append(&mut self.compile_print_argument(
compiler,
pattern,
value_register.clone(),
)?);
} else {
instructions.push(Instruction::Load {
value: RegisterValue::Empty,
register: value_register.clone(),
});
}
instructions.push(Instruction::Interrupt {
interrupt: Interrupt {
address: value_register,
kind: InterruptKind::Print,
},
});
let destination_register =
target_register.unwrap_or_else(|| compiler.registers.allocate_register());
instructions.push(Instruction::Load {
value: RegisterValue::Empty,
register: destination_register,
});
}
"sqrt" => {
if let Some(pattern) = signature {
let arg_register = compiler.registers.allocate_register();
instructions.append(&mut self.compile_print_argument(
compiler,
pattern,
arg_register.clone(),
)?);
let destination_register = target_register
.unwrap_or_else(|| compiler.registers.allocate_register());
instructions.push(Instruction::Calculate {
method: CalculationMethod::SQRT,
operand1: arg_register.clone(),
operand2: arg_register,
destination: destination_register.clone(),
});
if compiler.context.recursion_depth == 1 && compiler.context.repl_mode {
instructions.push(Instruction::Interrupt {
interrupt: Interrupt {
address: destination_register,
kind: InterruptKind::Print,
},
});
}
}
}
// Built-in arithmetic operators
"+" | "-" | "*" | "/" | "^" | "%" => {
let method = match method_name.as_str() {
"+" => CalculationMethod::ADD,
"-" => CalculationMethod::SUBTRACT,
"*" => CalculationMethod::MULTIPLY,
"/" => CalculationMethod::DIVIDE,
"^" => CalculationMethod::POWER,
"%" => CalculationMethod::MODULO,
_ => unreachable!(),
};
if let Some(Pattern::Pair(pair)) = signature {
let left_expr =
if let Pattern::Value(ValuePattern { expression }) = *pair.left {
*expression
} else {
unreachable!()
};
let right_expr =
if let Pattern::Value(ValuePattern { expression }) = *pair.right {
*expression
} else {
unreachable!()
};
let left_register = compiler.registers.allocate_register();
instructions.append(
&mut compiler
.compile_expression(left_expr, Some(left_register.clone()))?,
);
let right_register = compiler.registers.allocate_register();
instructions.append(
&mut compiler
.compile_expression(right_expr, Some(right_register.clone()))?,
);
let destination_register = target_register
.unwrap_or_else(|| compiler.registers.allocate_register());
instructions.push(Instruction::Calculate {
method,
operand1: left_register,
operand2: right_register,
destination: destination_register.clone(),
});
if compiler.context.recursion_depth == 1 && compiler.context.repl_mode {
instructions.push(Instruction::Interrupt {
interrupt: Interrupt {
address: destination_register,
kind: InterruptKind::Print,
},
});
}
}
}
// Built-in comparison operators
"==" | "!=" | "<" | "<=" | ">" | ">=" => {
let method = match method_name.as_str() {
"==" => ComparisonMethod::EQ,
"!=" => ComparisonMethod::NEQ,
"<" => ComparisonMethod::LT,
"<=" => ComparisonMethod::LTE,
">" => ComparisonMethod::GT,
">=" => ComparisonMethod::GTE,
_ => unreachable!(),
};
if let Some(Pattern::Pair(pair)) = signature {
let left_expr =
if let Pattern::Value(ValuePattern { expression }) = *pair.left {
*expression
} else {
unreachable!()
};
let right_expr =
if let Pattern::Value(ValuePattern { expression }) = *pair.right {
*expression
} else {
unreachable!()
};
let left_register = compiler.registers.allocate_register();
instructions.append(
&mut compiler
.compile_expression(left_expr, Some(left_register.clone()))?,
);
let right_register = compiler.registers.allocate_register();
instructions.append(
&mut compiler
.compile_expression(right_expr, Some(right_register.clone()))?,
);
let destination_register = target_register
.unwrap_or_else(|| compiler.registers.allocate_register());
instructions.push(Instruction::Compare {
method,
operand1: left_register,
operand2: right_register,
destination: destination_register.clone(),
});
if compiler.context.recursion_depth == 1 && compiler.context.repl_mode {
instructions.push(Instruction::Interrupt {
interrupt: Interrupt {
address: destination_register,
kind: InterruptKind::Print,
},
});
}
}
}
// Any other method calls (user-defined multimethods)
_ => {
// Verify the multimethod exists
if !compiler.multimethods.contains_key(&method_name) {
return Err(CompilerError::MethodNotFound(method_name.clone()));
}
// Compile the argument expression into the 'arg' register
// The argument is what will be matched against patterns at runtime
if let Some(call_sig) = signature {
// Extract the value from the pattern and compile it
match call_sig {
Pattern::Value(ValuePattern { expression }) => {
instructions.append(
&mut compiler
.compile_expression(*expression, Some("arg".to_string()))?,
);
}
_ => {
// For other patterns, try to compile them directly
// This handles things like tuple arguments
return Err(CompilerError::Generic(
"Only value patterns supported in calls currently".to_string(),
));
}
}
}
// Generate DISPATCH instruction - runtime will match arg against patterns
instructions.push(Instruction::Dispatch {
method_name: method_name.clone(),
});
// Copy the return value to the target register
let destination_register =
target_register.unwrap_or_else(|| compiler.registers.allocate_register());
instructions.push(Instruction::Copy {
source: "ret".to_string(),
destination: destination_register.clone(),
});
// Print result at top level
if compiler.context.recursion_depth == 1 && compiler.context.repl_mode {
instructions.push(Instruction::Interrupt {
interrupt: Interrupt {
address: destination_register,
kind: InterruptKind::Print,
},
});
}
}
}
}
Ok(instructions)
}
}
impl CallCompilelet {
fn compile_print_argument(
&self,
compiler: &mut Compiler,
pattern: Pattern,
target_register: String,
) -> CompilerResult<Vec<Instruction>> {
match pattern {
Pattern::Value(ValuePattern { expression }) => {
compiler.compile_expression(*expression, Some(target_register))
}
Pattern::Variable(variable) => compiler.compile_expression(
Expression {
kind: ExpressionKind::Pattern(Pattern::Variable(variable)),
start_pos: 0,
end_pos: 0,
},
Some(target_register),
),
_ => Err(CompilerError::Generic(
"print only supports value and variable arguments".to_string(),
)),
}
}
}