This commit is contained in:
2025-11-30 15:39:55 -07:00
parent 1c52ec2b9c
commit 15603f8bbe
2 changed files with 293 additions and 175 deletions

View File

@@ -83,7 +83,7 @@ fn incorrect_args_count() -> anyhow::Result<()> {
assert!(matches!( assert!(matches!(
compiled, compiled,
Err(super::super::Error::AgrumentMismatch(_)) Err(super::super::Error::AgrumentMismatch(_, _))
)); ));
Ok(()) Ok(())

View File

@@ -150,41 +150,41 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
Ok(None) Ok(None)
} }
Expression::Block(expr_block) => { Expression::Block(expr_block) => {
self.expression_block(expr_block, scope)?; self.expression_block(expr_block.node, scope)?;
Ok(None) Ok(None)
} }
Expression::If(expr_if) => { Expression::If(expr_if) => {
self.expression_if(expr_if, scope)?; self.expression_if(expr_if.node, scope)?;
Ok(None) Ok(None)
} }
Expression::Loop(expr_loop) => { Expression::Loop(expr_loop) => {
self.expression_loop(expr_loop, scope)?; self.expression_loop(expr_loop.node, scope)?;
Ok(None) Ok(None)
} }
Expression::Syscall(SysCall::System(system_syscall)) => { Expression::Syscall(spanned_syscall) => {
self.expression_syscall_system(system_syscall, scope) self.expression_syscall_system(spanned_syscall.node, spanned_syscall.span, scope)
} }
Expression::While(expr_while) => { Expression::While(expr_while) => {
self.expression_while(expr_while, scope)?; self.expression_while(expr_while.node, scope)?;
Ok(None) Ok(None)
} }
Expression::Break => { Expression::Break(_) => {
self.expression_break()?; self.expression_break()?;
Ok(None) Ok(None)
} }
Expression::Continue => { Expression::Continue(_) => {
self.expression_continue()?; self.expression_continue()?;
Ok(None) Ok(None)
} }
Expression::DeviceDeclaration(expr_dev) => { Expression::DeviceDeclaration(expr_dev) => {
self.expression_device(expr_dev)?; self.expression_device(expr_dev.node, expr_dev.span)?;
Ok(None) Ok(None)
} }
Expression::Declaration(var_name, expr) => { Expression::Declaration(var_name, expr) => {
self.expression_declaration(var_name.node, *expr, scope) self.expression_declaration(var_name, **expr, scope)
} }
Expression::Assignment(assign_expr) => { Expression::Assignment(assign_expr) => {
self.expression_assignment(assign_expr, scope)?; self.expression_assignment(assign_expr.node, scope)?;
Ok(None) Ok(None)
} }
Expression::Invocation(expr_invoke) => { Expression::Invocation(expr_invoke) => {
@@ -204,40 +204,45 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
})) }))
} }
Expression::Binary(bin_expr) => { Expression::Binary(bin_expr) => {
let result = self.expression_binary(bin_expr, scope)?; let result = self.expression_binary(bin_expr.node, scope)?;
Ok(Some(result)) Ok(Some(result))
} }
Expression::Logical(log_expr) => { Expression::Logical(log_expr) => {
let result = self.expression_logical(log_expr, scope)?; let result = self.expression_logical(log_expr.node, scope)?;
Ok(Some(result)) Ok(Some(result))
} }
Expression::Literal(Literal::Number(num)) => { Expression::Literal(spanned_lit) => match spanned_lit.node {
let temp_name = self.next_temp_name(); Literal::Number(num) => {
let loc = scope.add_variable(&temp_name, LocationRequest::Temp)?; let temp_name = self.next_temp_name();
self.emit_variable_assignment(&temp_name, &loc, num.to_string())?; let loc = scope.add_variable(&temp_name, LocationRequest::Temp)?;
Ok(Some(CompilationResult { self.emit_variable_assignment(&temp_name, &loc, num.to_string())?;
location: loc, Ok(Some(CompilationResult {
temp_name: Some(temp_name), location: loc,
})) temp_name: Some(temp_name),
} }))
Expression::Literal(Literal::Boolean(b)) => { }
let val = if b { "1" } else { "0" }; Literal::Boolean(b) => {
let temp_name = self.next_temp_name(); let val = if b { "1" } else { "0" };
let loc = scope.add_variable(&temp_name, LocationRequest::Temp)?; let temp_name = self.next_temp_name();
self.emit_variable_assignment(&temp_name, &loc, val)?; let loc = scope.add_variable(&temp_name, LocationRequest::Temp)?;
Ok(Some(CompilationResult { self.emit_variable_assignment(&temp_name, &loc, val)?;
location: loc, Ok(Some(CompilationResult {
temp_name: Some(temp_name), location: loc,
})) temp_name: Some(temp_name),
} }))
}
_ => Ok(None), // String literals don't return values in this context typically
},
Expression::Variable(name) => { Expression::Variable(name) => {
let loc = scope.get_location_of(&name.node)?; let loc = scope
.get_location_of(&name.node)
.map_err(|_| Error::UnknownIdentifier(name.node.clone(), name.span))?;
Ok(Some(CompilationResult { Ok(Some(CompilationResult {
location: loc, location: loc,
temp_name: None, // User variable, do not free temp_name: None, // User variable, do not free
})) }))
} }
Expression::Priority(inner_expr) => self.expression(*inner_expr, scope), Expression::Priority(inner_expr) => self.expression(**inner_expr, scope),
Expression::Negation(inner_expr) => { Expression::Negation(inner_expr) => {
// Compile negation as 0 - inner // Compile negation as 0 - inner
let (inner_str, cleanup) = self.compile_operand(*inner_expr, scope)?; let (inner_str, cleanup) = self.compile_operand(*inner_expr, scope)?;
@@ -256,10 +261,13 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
temp_name: Some(result_name), temp_name: Some(result_name),
})) }))
} }
_ => Err(Error::Unknown(format!( _ => Err(Error::Unknown(
"Expression type not yet supported in general expression context: {:?}", format!(
expr "Expression type not yet supported in general expression context: {:?}",
))), expr
),
None,
)),
} }
} }
@@ -289,16 +297,20 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
fn expression_declaration<'v>( fn expression_declaration<'v>(
&mut self, &mut self,
var_name: String, var_name: Spanned<String>,
expr: Expression, expr: Expression,
scope: &mut VariableScope<'v>, scope: &mut VariableScope<'v>,
) -> Result<Option<CompilationResult>, Error> { ) -> Result<Option<CompilationResult>, Error> {
let name_str = var_name.node;
let name_span = var_name.span;
// optimization. Check for a negated numeric literal // optimization. Check for a negated numeric literal
if let Expression::Negation(box_expr) = &expr if let Expression::Negation(box_expr) = &expr
&& let Expression::Literal(Literal::Number(neg_num)) = &**box_expr && let Expression::Literal(spanned_lit) = &**box_expr
&& let Literal::Number(neg_num) = &spanned_lit.node
{ {
let loc = scope.add_variable(&var_name, LocationRequest::Persist)?; let loc = scope.add_variable(&name_str, LocationRequest::Persist)?;
self.emit_variable_assignment(&var_name, &loc, format!("-{neg_num}"))?; self.emit_variable_assignment(&name_str, &loc, format!("-{neg_num}"))?;
return Ok(Some(CompilationResult { return Ok(Some(CompilationResult {
location: loc, location: loc,
temp_name: None, temp_name: None,
@@ -306,40 +318,59 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
} }
let (loc, temp_name) = match expr { let (loc, temp_name) = match expr {
Expression::Literal(Literal::Number(num)) => { Expression::Literal(spanned_lit) => match spanned_lit.node {
let var_location = Literal::Number(num) => {
scope.add_variable(var_name.clone(), LocationRequest::Persist)?; let var_location =
scope.add_variable(name_str.clone(), LocationRequest::Persist)?;
self.emit_variable_assignment(&var_name, &var_location, num)?; self.emit_variable_assignment(&name_str, &var_location, num)?;
(var_location, None) (var_location, None)
} }
Expression::Literal(Literal::Boolean(b)) => { Literal::Boolean(b) => {
let val = if b { "1" } else { "0" }; let val = if b { "1" } else { "0" };
let var_location = let var_location =
scope.add_variable(var_name.clone(), LocationRequest::Persist)?; scope.add_variable(name_str.clone(), LocationRequest::Persist)?;
self.emit_variable_assignment(&var_name, &var_location, val)?; self.emit_variable_assignment(&name_str, &var_location, val)?;
(var_location, None) (var_location, None)
} }
_ => return Ok(None),
},
Expression::Invocation(invoke_expr) => { Expression::Invocation(invoke_expr) => {
self.expression_function_invocation(invoke_expr, scope)?; self.expression_function_invocation(invoke_expr, scope)?;
let loc = scope.add_variable(&var_name, LocationRequest::Persist)?; let loc = scope.add_variable(&name_str, LocationRequest::Persist)?;
self.emit_variable_assignment( self.emit_variable_assignment(
&var_name, &name_str,
&loc, &loc,
format!("r{}", VariableScope::RETURN_REGISTER), format!("r{}", VariableScope::RETURN_REGISTER),
)?; )?;
(loc, None) (loc, None)
} }
Expression::Syscall(SysCall::System(call)) => { Expression::Syscall(spanned_call) => {
if self.expression_syscall_system(call, scope)?.is_none() { let sys_call = spanned_call.node;
return Err(Error::Unknown("SysCall did not return a value".into())); let SysCall::System(call) = sys_call else {
// Math syscalls might be handled differently or here
// For now assuming System returns value
return Err(Error::Unknown(
"Math syscall not yet supported in declaration".into(),
Some(spanned_call.span),
));
}; };
let loc = scope.add_variable(&var_name, LocationRequest::Persist)?; if self
.expression_syscall_system(call, spanned_call.span, scope)?
.is_none()
{
return Err(Error::Unknown(
"SysCall did not return a value".into(),
Some(spanned_call.span),
));
};
let loc = scope.add_variable(&name_str, LocationRequest::Persist)?;
self.emit_variable_assignment( self.emit_variable_assignment(
&var_name, &name_str,
&loc, &loc,
format!("r{}", VariableScope::RETURN_REGISTER), format!("r{}", VariableScope::RETURN_REGISTER),
)?; )?;
@@ -348,12 +379,12 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
} }
// Support assigning binary expressions to variables directly // Support assigning binary expressions to variables directly
Expression::Binary(bin_expr) => { Expression::Binary(bin_expr) => {
let result = self.expression_binary(bin_expr, scope)?; let result = self.expression_binary(bin_expr.node, scope)?;
let var_loc = scope.add_variable(&var_name, LocationRequest::Persist)?; let var_loc = scope.add_variable(&name_str, LocationRequest::Persist)?;
// Move result from temp to new persistent variable // Move result from temp to new persistent variable
let result_reg = self.resolve_register(&result.location)?; let result_reg = self.resolve_register(&result.location)?;
self.emit_variable_assignment(&var_name, &var_loc, result_reg)?; self.emit_variable_assignment(&name_str, &var_loc, result_reg)?;
// Free the temp result // Free the temp result
if let Some(name) = result.temp_name { if let Some(name) = result.temp_name {
@@ -362,12 +393,12 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
(var_loc, None) (var_loc, None)
} }
Expression::Logical(log_expr) => { Expression::Logical(log_expr) => {
let result = self.expression_logical(log_expr, scope)?; let result = self.expression_logical(log_expr.node, scope)?;
let var_loc = scope.add_variable(&var_name, LocationRequest::Persist)?; let var_loc = scope.add_variable(&name_str, LocationRequest::Persist)?;
// Move result from temp to new persistent variable // Move result from temp to new persistent variable
let result_reg = self.resolve_register(&result.location)?; let result_reg = self.resolve_register(&result.location)?;
self.emit_variable_assignment(&var_name, &var_loc, result_reg)?; self.emit_variable_assignment(&name_str, &var_loc, result_reg)?;
// Free the temp result // Free the temp result
if let Some(name) = result.temp_name { if let Some(name) = result.temp_name {
@@ -376,8 +407,11 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
(var_loc, None) (var_loc, None)
} }
Expression::Variable(name) => { Expression::Variable(name) => {
let src_loc = scope.get_location_of(&name.node)?; let src_loc = scope
let var_loc = scope.add_variable(&var_name, LocationRequest::Persist)?; .get_location_of(&name.node)
.map_err(|_| Error::UnknownIdentifier(name.node.clone(), name.span))?;
let var_loc = scope.add_variable(&name_str, LocationRequest::Persist)?;
// Handle loading from stack if necessary // Handle loading from stack if necessary
let src_str = match src_loc { let src_str = match src_loc {
@@ -396,16 +430,24 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
format!("r{}", VariableScope::TEMP_STACK_REGISTER) format!("r{}", VariableScope::TEMP_STACK_REGISTER)
} }
}; };
self.emit_variable_assignment(&var_name, &var_loc, src_str)?; self.emit_variable_assignment(&name_str, &var_loc, src_str)?;
(var_loc, None) (var_loc, None)
} }
Expression::Priority(inner) => { Expression::Priority(inner) => {
return self.expression_declaration(var_name, *inner, scope); return self.expression_declaration(
Spanned {
node: name_str,
span: name_span,
},
*inner,
scope,
);
} }
_ => { _ => {
return Err(Error::Unknown(format!( return Err(Error::Unknown(
"`{var_name}` declaration of this type is not supported/implemented." format!("`{name_str}` declaration of this type is not supported/implemented."),
))); Some(name_span),
));
} }
}; };
@@ -425,7 +467,10 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
expression, expression,
} = expr; } = expr;
let location = scope.get_location_of(&identifier.node)?; let location = scope
.get_location_of(&identifier.node)
.map_err(|_| Error::UnknownIdentifier(identifier.node.clone(), identifier.span))?;
let (val_str, cleanup) = self.compile_operand(*expression, scope)?; let (val_str, cleanup) = self.compile_operand(*expression, scope)?;
let debug_tag = if self.config.debug { let debug_tag = if self.config.debug {
@@ -461,19 +506,21 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
fn expression_function_invocation( fn expression_function_invocation(
&mut self, &mut self,
invoke_expr: InvocationExpression, invoke_expr: Spanned<InvocationExpression>,
stack: &mut VariableScope, stack: &mut VariableScope,
) -> Result<(), Error> { ) -> Result<(), Error> {
if !self.function_locations.contains_key(&invoke_expr.name.node) { let InvocationExpression { name, arguments } = invoke_expr.node;
return Err(Error::UnknownIdentifier(invoke_expr.name.node));
if !self.function_locations.contains_key(&name.node) {
return Err(Error::UnknownIdentifier(name.node.clone(), name.span));
} }
let Some(args) = self.function_metadata.get(&invoke_expr.name.node) else { let Some(args) = self.function_metadata.get(&name.node) else {
return Err(Error::UnknownIdentifier(invoke_expr.name.node)); return Err(Error::UnknownIdentifier(name.node.clone(), name.span));
}; };
if args.len() != invoke_expr.arguments.len() { if args.len() != arguments.len() {
return Err(Error::AgrumentMismatch(invoke_expr.name.node)); return Err(Error::AgrumentMismatch(name.node, name.span));
} }
// backup all used registers to the stack // backup all used registers to the stack
@@ -482,38 +529,47 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
stack.add_variable(format!("temp_{register}"), LocationRequest::Stack)?; stack.add_variable(format!("temp_{register}"), LocationRequest::Stack)?;
self.write_output(format!("push r{register}"))?; self.write_output(format!("push r{register}"))?;
} }
for arg in invoke_expr.arguments { for arg in arguments {
match arg { match arg {
Expression::Literal(Literal::Number(num)) => { Expression::Literal(spanned_lit) => match spanned_lit.node {
let num_str = num.to_string(); Literal::Number(num) => {
self.write_output(format!("push {num_str}"))?; let num_str = num.to_string();
} self.write_output(format!("push {num_str}"))?;
Expression::Literal(Literal::Boolean(b)) => {
let val = if b { "1" } else { "0" };
self.write_output(format!("push {val}"))?;
}
Expression::Variable(var_name) => match stack.get_location_of(var_name.node)? {
VariableLocation::Persistant(reg) | VariableLocation::Temporary(reg) => {
self.write_output(format!("push r{reg}"))?;
} }
VariableLocation::Stack(stack_offset) => { Literal::Boolean(b) => {
self.write_output(format!( let val = if b { "1" } else { "0" };
"sub r{0} sp {stack_offset}", self.write_output(format!("push {val}"))?;
VariableScope::TEMP_STACK_REGISTER
))?;
self.write_output(format!(
"get r{0} db r{0}",
VariableScope::TEMP_STACK_REGISTER
))?;
self.write_output(format!(
"push r{0}",
VariableScope::TEMP_STACK_REGISTER
))?;
} }
_ => {}
}, },
Expression::Variable(var_name) => {
let loc = stack
.get_location_of(var_name.node.clone())
.map_err(|_| Error::UnknownIdentifier(var_name.node, var_name.span))?;
match loc {
VariableLocation::Persistant(reg) | VariableLocation::Temporary(reg) => {
self.write_output(format!("push r{reg}"))?;
}
VariableLocation::Stack(stack_offset) => {
self.write_output(format!(
"sub r{0} sp {stack_offset}",
VariableScope::TEMP_STACK_REGISTER
))?;
self.write_output(format!(
"get r{0} db r{0}",
VariableScope::TEMP_STACK_REGISTER
))?;
self.write_output(format!(
"push r{0}",
VariableScope::TEMP_STACK_REGISTER
))?;
}
}
}
Expression::Binary(bin_expr) => { Expression::Binary(bin_expr) => {
// Compile the binary expression to a temp register // Compile the binary expression to a temp register
let result = self.expression_binary(bin_expr, stack)?; let result = self.expression_binary(bin_expr.node, stack)?;
let reg_str = self.resolve_register(&result.location)?; let reg_str = self.resolve_register(&result.location)?;
self.write_output(format!("push {reg_str}"))?; self.write_output(format!("push {reg_str}"))?;
if let Some(name) = result.temp_name { if let Some(name) = result.temp_name {
@@ -522,7 +578,7 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
} }
Expression::Logical(log_expr) => { Expression::Logical(log_expr) => {
// Compile the logical expression to a temp register // Compile the logical expression to a temp register
let result = self.expression_logical(log_expr, stack)?; let result = self.expression_logical(log_expr.node, stack)?;
let reg_str = self.resolve_register(&result.location)?; let reg_str = self.resolve_register(&result.location)?;
self.write_output(format!("push {reg_str}"))?; self.write_output(format!("push {reg_str}"))?;
if let Some(name) = result.temp_name { if let Some(name) = result.temp_name {
@@ -530,22 +586,30 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
} }
} }
_ => { _ => {
return Err(Error::Unknown(format!( return Err(Error::Unknown(
"Attempted to call `{}` with an unsupported argument type", format!(
invoke_expr.name.node "Attempted to call `{}` with an unsupported argument type",
))); name.node
),
Some(name.span),
));
} }
} }
} }
// jump to the function and store current line in ra // jump to the function and store current line in ra
self.write_output(format!("jal {}", invoke_expr.name.node))?; self.write_output(format!("jal {}", name.node))?;
for register in active_registers { for register in active_registers {
let VariableLocation::Stack(stack_offset) = let VariableLocation::Stack(stack_offset) = stack
stack.get_location_of(format!("temp_{register}"))? .get_location_of(format!("temp_{register}"))
.map_err(|e| Error::ScopeError(e))?
else { else {
return Err(Error::UnknownIdentifier(format!("temp_{register}"))); // This shouldn't happen if we just added it
return Err(Error::Unknown(
format!("Failed to recover temp_{register}"),
Some(name.span),
));
}; };
self.write_output(format!( self.write_output(format!(
"sub r{0} sp {stack_offset}", "sub r{0} sp {stack_offset}",
@@ -564,9 +628,13 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
Ok(()) Ok(())
} }
fn expression_device(&mut self, expr: DeviceDeclarationExpression) -> Result<(), Error> { fn expression_device(
&mut self,
expr: DeviceDeclarationExpression,
span: Span,
) -> Result<(), Error> {
if self.devices.contains_key(&expr.name.node) { if self.devices.contains_key(&expr.name.node) {
return Err(Error::DuplicateIdentifier(expr.name.node)); return Err(Error::DuplicateIdentifier(expr.name.node, span));
} }
self.devices.insert(expr.name.node, expr.device); self.devices.insert(expr.name.node, expr.device);
@@ -683,9 +751,10 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
self.write_output(format!("j {end_label}"))?; self.write_output(format!("j {end_label}"))?;
Ok(()) Ok(())
} else { } else {
// This is a semantic error, but for now we can return a generic error Err(Error::Unknown(
// Ideally we'd have a specific error type for this "Break statement outside of loop".into(),
Err(Error::Unknown("Break statement outside of loop".into())) None,
))
} }
} }
@@ -694,7 +763,10 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
self.write_output(format!("j {start_label}"))?; self.write_output(format!("j {start_label}"))?;
Ok(()) Ok(())
} else { } else {
Err(Error::Unknown("Continue statement outside of loop".into())) Err(Error::Unknown(
"Continue statement outside of loop".into(),
None,
))
} }
} }
@@ -706,6 +778,7 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
VariableLocation::Temporary(r) | VariableLocation::Persistant(r) => Ok(format!("r{r}")), VariableLocation::Temporary(r) | VariableLocation::Persistant(r) => Ok(format!("r{r}")),
VariableLocation::Stack(_) => Err(Error::Unknown( VariableLocation::Stack(_) => Err(Error::Unknown(
"Cannot resolve Stack location directly to register string without context".into(), "Cannot resolve Stack location directly to register string without context".into(),
None,
)), )),
} }
} }
@@ -720,26 +793,28 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
scope: &mut VariableScope, scope: &mut VariableScope,
) -> Result<(String, Option<String>), Error> { ) -> Result<(String, Option<String>), Error> {
// Optimization for literals // Optimization for literals
if let Expression::Literal(Literal::Number(n)) = expr { if let Expression::Literal(spanned_lit) = &expr {
return Ok((n.to_string(), None)); if let Literal::Number(n) = spanned_lit.node {
} return Ok((n.to_string(), None));
}
// Optimization for boolean literals if let Literal::Boolean(b) = spanned_lit.node {
if let Expression::Literal(Literal::Boolean(b)) = expr { return Ok((if b { "1".to_string() } else { "0".to_string() }, None));
return Ok((if b { "1".to_string() } else { "0".to_string() }, None)); }
} }
// Optimization for negated literals used as operands. // Optimization for negated literals used as operands.
// E.g., `1 + -2` -> return "-2" string, no register used. // E.g., `1 + -2` -> return "-2" string, no register used.
if let Expression::Negation(inner) = &expr if let Expression::Negation(inner) = &expr
&& let Expression::Literal(Literal::Number(n)) = &**inner && let Expression::Literal(spanned_lit) = &**inner
&& let Literal::Number(n) = spanned_lit.node
{ {
return Ok((format!("-{}", n), None)); return Ok((format!("-{}", n), None));
} }
let result = self let result = self.expression(expr, scope)?.ok_or(Error::Unknown(
.expression(expr, scope)? "Expression did not return a value".into(),
.ok_or(Error::Unknown("Expression did not return a value".into()))?; None,
))?;
match result.location { match result.location {
VariableLocation::Temporary(r) | VariableLocation::Persistant(r) => { VariableLocation::Temporary(r) | VariableLocation::Persistant(r) => {
@@ -774,7 +849,24 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
scope: &mut VariableScope, scope: &mut VariableScope,
) -> Result<(String, Option<String>), Error> { ) -> Result<(String, Option<String>), Error> {
let expr = match val { let expr = match val {
LiteralOrVariable::Literal(l) => Expression::Literal(l), LiteralOrVariable::Literal(l) => {
// We need to manufacture a spanned literal
// Since this method is usually called from contexts where we lost the original span
// (Syscall enums don't keep span on inner literals usually, but we passed span to expression_syscall_system)
// Actually, LiteralOrVariable stores Spanned<String> for variables, but Literals are just Literal.
// We'll create a dummy span for the Literal if we have to wrap it back in Expression.
// Or better, just handle logic here.
let dummy_span = Span {
start_line: 0,
start_col: 0,
end_line: 0,
end_col: 0,
};
Expression::Literal(Spanned {
node: l,
span: dummy_span,
})
}
LiteralOrVariable::Variable(v) => Expression::Variable(v), LiteralOrVariable::Variable(v) => Expression::Variable(v),
}; };
self.compile_operand(expr, scope) self.compile_operand(expr, scope)
@@ -942,7 +1034,8 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
scope: &mut VariableScope<'v>, scope: &mut VariableScope<'v>,
) -> Result<VariableLocation, Error> { ) -> Result<VariableLocation, Error> {
if let Expression::Negation(neg_expr) = &expr if let Expression::Negation(neg_expr) = &expr
&& let Expression::Literal(Literal::Number(neg_num)) = &**neg_expr && let Expression::Literal(spanned_lit) = &**neg_expr
&& let Literal::Number(neg_num) = &spanned_lit.node
{ {
let loc = VariableLocation::Persistant(VariableScope::RETURN_REGISTER); let loc = VariableLocation::Persistant(VariableScope::RETURN_REGISTER);
self.emit_variable_assignment("returnValue", &loc, format!("-{neg_num}"))?; self.emit_variable_assignment("returnValue", &loc, format!("-{neg_num}"))?;
@@ -950,7 +1043,10 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
}; };
match expr { match expr {
Expression::Variable(var_name) => match scope.get_location_of(var_name.node)? { Expression::Variable(var_name) => match scope
.get_location_of(&var_name.node)
.map_err(|_| Error::UnknownIdentifier(var_name.node, var_name.span))?
{
VariableLocation::Temporary(reg) | VariableLocation::Persistant(reg) => { VariableLocation::Temporary(reg) | VariableLocation::Persistant(reg) => {
self.write_output(format!( self.write_output(format!(
"move r{} r{reg} {}", "move r{} r{reg} {}",
@@ -970,23 +1066,26 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
))?; ))?;
} }
}, },
Expression::Literal(Literal::Number(num)) => { Expression::Literal(spanned_lit) => match spanned_lit.node {
self.emit_variable_assignment( Literal::Number(num) => {
"returnValue", self.emit_variable_assignment(
&VariableLocation::Persistant(VariableScope::RETURN_REGISTER), "returnValue",
num, &VariableLocation::Persistant(VariableScope::RETURN_REGISTER),
)?; num,
} )?;
Expression::Literal(Literal::Boolean(b)) => { }
let val = if b { "1" } else { "0" }; Literal::Boolean(b) => {
self.emit_variable_assignment( let val = if b { "1" } else { "0" };
"returnValue", self.emit_variable_assignment(
&VariableLocation::Persistant(VariableScope::RETURN_REGISTER), "returnValue",
val, &VariableLocation::Persistant(VariableScope::RETURN_REGISTER),
)?; val,
} )?;
}
_ => {}
},
Expression::Binary(bin_expr) => { Expression::Binary(bin_expr) => {
let result = self.expression_binary(bin_expr, scope)?; let result = self.expression_binary(bin_expr.node, scope)?;
let result_reg = self.resolve_register(&result.location)?; let result_reg = self.resolve_register(&result.location)?;
self.write_output(format!( self.write_output(format!(
"move r{} {}", "move r{} {}",
@@ -998,7 +1097,7 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
} }
} }
Expression::Logical(log_expr) => { Expression::Logical(log_expr) => {
let result = self.expression_logical(log_expr, scope)?; let result = self.expression_logical(log_expr.node, scope)?;
let result_reg = self.resolve_register(&result.location)?; let result_reg = self.resolve_register(&result.location)?;
self.write_output(format!( self.write_output(format!(
"move r{} {}", "move r{} {}",
@@ -1010,10 +1109,10 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
} }
} }
_ => { _ => {
return Err(Error::Unknown(format!( return Err(Error::Unknown(
"Unsupported `return` statement: {:?}", format!("Unsupported `return` statement: {:?}", expr),
expr None,
))); ));
} }
} }
@@ -1025,6 +1124,7 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
fn expression_syscall_system<'v>( fn expression_syscall_system<'v>(
&mut self, &mut self,
expr: System, expr: System,
span: Span,
scope: &mut VariableScope<'v>, scope: &mut VariableScope<'v>,
) -> Result<Option<CompilationResult>, Error> { ) -> Result<Option<CompilationResult>, Error> {
match expr { match expr {
@@ -1045,6 +1145,7 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
let Literal::String(str_lit) = hash_arg else { let Literal::String(str_lit) = hash_arg else {
return Err(Error::AgrumentMismatch( return Err(Error::AgrumentMismatch(
"Arg1 expected to be a string literal.".into(), "Arg1 expected to be a string literal.".into(),
span,
)); ));
}; };
@@ -1059,23 +1160,27 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
System::SetOnDevice(device, logic_type, variable) => { System::SetOnDevice(device, logic_type, variable) => {
let (variable, var_cleanup) = self.compile_operand(*variable, scope)?; let (variable, var_cleanup) = self.compile_operand(*variable, scope)?;
let LiteralOrVariable::Variable(device) = device else { let LiteralOrVariable::Variable(device_spanned) = device else {
return Err(Error::AgrumentMismatch( return Err(Error::AgrumentMismatch(
"Arg1 expected to be a variable".into(), "Arg1 expected to be a variable".into(),
span,
)); ));
}; };
let Some(device) = self.devices.get(&device.node) else { let device_name = device_spanned.node;
return Err(Error::InvalidDevice(device.node));
let Some(device_val) = self.devices.get(&device_name) else {
return Err(Error::InvalidDevice(device_name, device_spanned.span));
}; };
let Literal::String(logic_type) = logic_type else { let Literal::String(logic_type) = logic_type else {
return Err(Error::AgrumentMismatch( return Err(Error::AgrumentMismatch(
"Arg2 expected to be a string".into(), "Arg2 expected to be a string".into(),
span,
)); ));
}; };
self.write_output(format!("s {} {} {}", device, logic_type, variable))?; self.write_output(format!("s {} {} {}", device_val, logic_type, variable))?;
if let Some(temp_var) = var_cleanup { if let Some(temp_var) = var_cleanup {
scope.free_temp(temp_var)?; scope.free_temp(temp_var)?;
@@ -1085,15 +1190,16 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
} }
System::SetOnDeviceBatched(device_hash, logic_type, variable) => { System::SetOnDeviceBatched(device_hash, logic_type, variable) => {
let (var, var_cleanup) = self.compile_operand(*variable, scope)?; let (var, var_cleanup) = self.compile_operand(*variable, scope)?;
let (device_hash, device_hash_cleanup) = let (device_hash_val, device_hash_cleanup) =
self.compile_literal_or_variable(device_hash, scope)?; self.compile_literal_or_variable(device_hash, scope)?;
let Literal::String(logic_type) = logic_type else { let Literal::String(logic_type) = logic_type else {
return Err(Error::AgrumentMismatch( return Err(Error::AgrumentMismatch(
"Arg2 expected to be a string".into(), "Arg2 expected to be a string".into(),
span,
)); ));
}; };
self.write_output(format!("sb {} {} {}", device_hash, logic_type, var))?; self.write_output(format!("sb {} {} {}", device_hash_val, logic_type, var))?;
if let Some(var_cleanup) = var_cleanup { if let Some(var_cleanup) = var_cleanup {
scope.free_temp(var_cleanup)?; scope.free_temp(var_cleanup)?;
@@ -1106,26 +1212,30 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
Ok(None) Ok(None)
} }
System::LoadFromDevice(device, logic_type) => { System::LoadFromDevice(device, logic_type) => {
let LiteralOrVariable::Variable(device) = device else { let LiteralOrVariable::Variable(device_spanned) = device else {
return Err(Error::AgrumentMismatch( return Err(Error::AgrumentMismatch(
"Arg1 expected to be a variable".into(), "Arg1 expected to be a variable".into(),
span,
)); ));
}; };
let Some(device) = self.devices.get(&device.node) else { let device_name = device_spanned.node;
return Err(Error::InvalidDevice(device.node));
let Some(device_val) = self.devices.get(&device_name) else {
return Err(Error::InvalidDevice(device_name, device_spanned.span));
}; };
let Literal::String(logic_type) = logic_type else { let Literal::String(logic_type) = logic_type else {
return Err(Error::AgrumentMismatch( return Err(Error::AgrumentMismatch(
"Arg2 expected to be a string".into(), "Arg2 expected to be a string".into(),
span,
)); ));
}; };
self.write_output(format!( self.write_output(format!(
"l r{} {} {}", "l r{} {} {}",
VariableScope::RETURN_REGISTER, VariableScope::RETURN_REGISTER,
device, device_val,
logic_type logic_type
))?; ))?;
@@ -1135,7 +1245,10 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
})) }))
} }
t => Err(Error::Unknown(format!("{t:?}\n\nNot yet implemented"))), t => Err(Error::Unknown(
format!("{t:?}\n\nNot yet implemented"),
Some(span),
)),
} }
} }
@@ -1150,10 +1263,10 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
name, name,
arguments, arguments,
body, body,
} = *expr; } = expr.node;
if self.function_locations.contains_key(&name.node) { if self.function_locations.contains_key(&name.node) {
return Err(Error::DuplicateIdentifier(name.node)); return Err(Error::DuplicateIdentifier(name.node.clone(), name.span));
} }
self.function_metadata.insert( self.function_metadata.insert(
@@ -1192,6 +1305,7 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
VariableLocation::Stack(_) => { VariableLocation::Stack(_) => {
return Err(Error::Unknown( return Err(Error::Unknown(
"Attempted to save to stack without tracking in scope".into(), "Attempted to save to stack without tracking in scope".into(),
Some(var_name.span),
)); ));
} }
@@ -1199,6 +1313,7 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
return Err(Error::Unknown( return Err(Error::Unknown(
"Attempted to return a Temporary scoped variable from a Persistant request" "Attempted to return a Temporary scoped variable from a Persistant request"
.into(), .into(),
Some(var_name.span),
)); ));
} }
} }
@@ -1233,11 +1348,13 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
} }
// Get the saved return address and save it back into `ra` // Get the saved return address and save it back into `ra`
let VariableLocation::Stack(ra_stack_offset) = let VariableLocation::Stack(ra_stack_offset) = block_scope
block_scope.get_location_of(format!("{}_ra", name.node))? .get_location_of(format!("{}_ra", name.node))
.map_err(|e| Error::ScopeError(e))?
else { else {
return Err(Error::Unknown( return Err(Error::Unknown(
"Stored return address not in stack as expected".into(), "Stored return address not in stack as expected".into(),
Some(name.span),
)); ));
}; };
@@ -1258,3 +1375,4 @@ impl<'a, W: std::io::Write> Compiler<'a, W> {
Ok(()) Ok(())
} }
} }