Compare commits
4 Commits
0.4.7
...
e94fc0f5de
| Author | SHA1 | Date | |
|---|---|---|---|
|
e94fc0f5de
|
|||
|
b51800eb77
|
|||
|
87951ab12f
|
|||
|
00b0d4df26
|
@@ -47,3 +47,4 @@ mod logic_expression;
|
|||||||
mod loops;
|
mod loops;
|
||||||
mod math_syscall;
|
mod math_syscall;
|
||||||
mod syscall;
|
mod syscall;
|
||||||
|
mod tuple_literals;
|
||||||
|
|||||||
317
rust_compiler/libs/compiler/src/test/tuple_literals.rs
Normal file
317
rust_compiler/libs/compiler/src/test/tuple_literals.rs
Normal file
@@ -0,0 +1,317 @@
|
|||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use indoc::indoc;
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_literal_declaration() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile!(
|
||||||
|
debug
|
||||||
|
r#"
|
||||||
|
let (x, y) = (1, 2);
|
||||||
|
"#
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 1
|
||||||
|
move r9 2
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_literal_declaration_with_underscore() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile!(
|
||||||
|
debug
|
||||||
|
r#"
|
||||||
|
let (x, _) = (1, 2);
|
||||||
|
"#
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 1
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_literal_assignment() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile!(
|
||||||
|
debug
|
||||||
|
r#"
|
||||||
|
let x = 0;
|
||||||
|
let y = 0;
|
||||||
|
(x, y) = (5, 10);
|
||||||
|
"#
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 0
|
||||||
|
move r9 0
|
||||||
|
move r8 5
|
||||||
|
move r9 10
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_literal_with_variables() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile!(
|
||||||
|
debug
|
||||||
|
r#"
|
||||||
|
let a = 42;
|
||||||
|
let b = 99;
|
||||||
|
let (x, y) = (a, b);
|
||||||
|
"#
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 42
|
||||||
|
move r9 99
|
||||||
|
move r10 r8
|
||||||
|
move r11 r9
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_literal_three_elements() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile!(
|
||||||
|
debug
|
||||||
|
r#"
|
||||||
|
let (x, y, z) = (1, 2, 3);
|
||||||
|
"#
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 1
|
||||||
|
move r9 2
|
||||||
|
move r10 3
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_literal_assignment_with_underscore() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile!(
|
||||||
|
debug
|
||||||
|
r#"
|
||||||
|
let i = 0;
|
||||||
|
let x = 123;
|
||||||
|
(i, _) = (456, 789);
|
||||||
|
"#
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 0
|
||||||
|
move r9 123
|
||||||
|
move r8 456
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_return_simple() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile!(
|
||||||
|
debug
|
||||||
|
r#"
|
||||||
|
fn getPair() {
|
||||||
|
return (10, 20);
|
||||||
|
};
|
||||||
|
let (x, y) = getPair();
|
||||||
|
"#
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
getPair:
|
||||||
|
push ra
|
||||||
|
push 10
|
||||||
|
push 20
|
||||||
|
move r15 1
|
||||||
|
j __internal_L1
|
||||||
|
__internal_L1:
|
||||||
|
pop ra
|
||||||
|
j ra
|
||||||
|
main:
|
||||||
|
jal getPair
|
||||||
|
pop r9
|
||||||
|
pop r8
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_return_with_underscore() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile!(
|
||||||
|
debug
|
||||||
|
r#"
|
||||||
|
fn getPair() {
|
||||||
|
return (5, 15);
|
||||||
|
};
|
||||||
|
let (x, _) = getPair();
|
||||||
|
"#
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
getPair:
|
||||||
|
push ra
|
||||||
|
push 5
|
||||||
|
push 15
|
||||||
|
move r15 1
|
||||||
|
j __internal_L1
|
||||||
|
__internal_L1:
|
||||||
|
pop ra
|
||||||
|
j ra
|
||||||
|
main:
|
||||||
|
jal getPair
|
||||||
|
pop r8
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_return_three_elements() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile!(
|
||||||
|
debug
|
||||||
|
r#"
|
||||||
|
fn getTriple() {
|
||||||
|
return (1, 2, 3);
|
||||||
|
};
|
||||||
|
let (a, b, c) = getTriple();
|
||||||
|
"#
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
getTriple:
|
||||||
|
push ra
|
||||||
|
push 1
|
||||||
|
push 2
|
||||||
|
push 3
|
||||||
|
move r15 1
|
||||||
|
j __internal_L1
|
||||||
|
__internal_L1:
|
||||||
|
pop ra
|
||||||
|
j ra
|
||||||
|
main:
|
||||||
|
jal getTriple
|
||||||
|
pop r10
|
||||||
|
pop r9
|
||||||
|
pop r8
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_return_assignment() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile!(
|
||||||
|
debug
|
||||||
|
r#"
|
||||||
|
fn getPair() {
|
||||||
|
return (42, 84);
|
||||||
|
};
|
||||||
|
let i = 0;
|
||||||
|
let j = 0;
|
||||||
|
(i, j) = getPair();
|
||||||
|
"#
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
getPair:
|
||||||
|
push ra
|
||||||
|
push 42
|
||||||
|
push 84
|
||||||
|
move r15 1
|
||||||
|
j __internal_L1
|
||||||
|
__internal_L1:
|
||||||
|
pop ra
|
||||||
|
j ra
|
||||||
|
main:
|
||||||
|
move r8 0
|
||||||
|
move r9 0
|
||||||
|
push r8
|
||||||
|
push r9
|
||||||
|
jal getPair
|
||||||
|
pop r9
|
||||||
|
pop r8
|
||||||
|
pop r9
|
||||||
|
pop r8
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,7 +9,8 @@ use parser::{
|
|||||||
AssignmentExpression, BinaryExpression, BlockExpression, ConstDeclarationExpression,
|
AssignmentExpression, BinaryExpression, BlockExpression, ConstDeclarationExpression,
|
||||||
DeviceDeclarationExpression, Expression, FunctionExpression, IfExpression,
|
DeviceDeclarationExpression, Expression, FunctionExpression, IfExpression,
|
||||||
InvocationExpression, Literal, LiteralOr, LiteralOrVariable, LogicalExpression,
|
InvocationExpression, Literal, LiteralOr, LiteralOrVariable, LogicalExpression,
|
||||||
LoopExpression, MemberAccessExpression, Spanned, TernaryExpression, WhileExpression,
|
LoopExpression, MemberAccessExpression, Spanned, TernaryExpression,
|
||||||
|
TupleAssignmentExpression, TupleDeclarationExpression, WhileExpression,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use rust_decimal::Decimal;
|
use rust_decimal::Decimal;
|
||||||
@@ -465,6 +466,14 @@ impl<'a> Compiler<'a> {
|
|||||||
temp_name: Some(result_name),
|
temp_name: Some(result_name),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
Expression::TupleDeclaration(tuple_decl) => {
|
||||||
|
self.expression_tuple_declaration(tuple_decl.node, scope)?;
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
Expression::TupleAssignment(tuple_assign) => {
|
||||||
|
self.expression_tuple_assignment(tuple_assign.node, scope)?;
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
_ => Err(Error::Unknown(
|
_ => Err(Error::Unknown(
|
||||||
format!(
|
format!(
|
||||||
"Expression type not yet supported in general expression context: {:?}",
|
"Expression type not yet supported in general expression context: {:?}",
|
||||||
@@ -932,6 +941,591 @@ impl<'a> Compiler<'a> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn expression_function_invocation_with_invocation(
|
||||||
|
&mut self,
|
||||||
|
invoke_expr: &InvocationExpression<'a>,
|
||||||
|
parent_scope: &mut VariableScope<'a, '_>,
|
||||||
|
) -> Result<(), Error<'a>> {
|
||||||
|
let InvocationExpression { name, arguments } = invoke_expr;
|
||||||
|
|
||||||
|
if !self.function_locations.contains_key(name.node.as_ref()) {
|
||||||
|
self.errors
|
||||||
|
.push(Error::UnknownIdentifier(name.node.clone(), name.span));
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some(args) = self.function_metadata.get(name.node.as_ref()) else {
|
||||||
|
return Err(Error::UnknownIdentifier(name.node.clone(), name.span));
|
||||||
|
};
|
||||||
|
|
||||||
|
if args.len() != arguments.len() {
|
||||||
|
self.errors
|
||||||
|
.push(Error::AgrumentMismatch(name.node.clone(), name.span));
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let mut stack = VariableScope::scoped(parent_scope);
|
||||||
|
|
||||||
|
// backup all used registers to the stack
|
||||||
|
let active_registers = stack.registers();
|
||||||
|
for register in &active_registers {
|
||||||
|
stack.add_variable(
|
||||||
|
Cow::from(format!("temp_{register}")),
|
||||||
|
LocationRequest::Stack,
|
||||||
|
None,
|
||||||
|
)?;
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Push(Operand::Register(*register)),
|
||||||
|
Some(name.span),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
for arg in arguments {
|
||||||
|
match &arg.node {
|
||||||
|
Expression::Literal(spanned_lit) => match &spanned_lit.node {
|
||||||
|
Literal::Number(num) => {
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Push(Operand::Number((*num).into())),
|
||||||
|
Some(spanned_lit.span),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
Literal::Boolean(b) => {
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Push(Operand::Number(Number::from(*b).into())),
|
||||||
|
Some(spanned_lit.span),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
},
|
||||||
|
Expression::Variable(var_name) => {
|
||||||
|
let loc = match stack.get_location_of(&var_name.node, Some(var_name.span)) {
|
||||||
|
Ok(l) => l,
|
||||||
|
Err(_) => {
|
||||||
|
self.errors.push(Error::UnknownIdentifier(
|
||||||
|
var_name.node.clone(),
|
||||||
|
var_name.span,
|
||||||
|
));
|
||||||
|
VariableLocation::Temporary(0)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match loc {
|
||||||
|
VariableLocation::Persistant(reg) | VariableLocation::Temporary(reg) => {
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Push(Operand::Register(reg)),
|
||||||
|
Some(var_name.span),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
VariableLocation::Constant(lit) => {
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Push(extract_literal(lit, false)?),
|
||||||
|
Some(var_name.span),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
VariableLocation::Stack(stack_offset) => {
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Sub(
|
||||||
|
Operand::Register(VariableScope::TEMP_STACK_REGISTER),
|
||||||
|
Operand::StackPointer,
|
||||||
|
Operand::Number(stack_offset.into()),
|
||||||
|
),
|
||||||
|
Some(var_name.span),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Get(
|
||||||
|
Operand::Register(VariableScope::TEMP_STACK_REGISTER),
|
||||||
|
Operand::Device(Cow::from("db")),
|
||||||
|
Operand::Register(VariableScope::TEMP_STACK_REGISTER),
|
||||||
|
),
|
||||||
|
Some(var_name.span),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Push(Operand::Register(
|
||||||
|
VariableScope::TEMP_STACK_REGISTER,
|
||||||
|
)),
|
||||||
|
Some(var_name.span),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
VariableLocation::Device(_) => {
|
||||||
|
self.errors.push(Error::Unknown(
|
||||||
|
"Device references not supported in function arguments".into(),
|
||||||
|
Some(var_name.span),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
self.errors.push(Error::Unknown(
|
||||||
|
"Only literals and variables supported in function arguments".into(),
|
||||||
|
Some(arg.span),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some(_location) = self.function_locations.get(&name.node) else {
|
||||||
|
self.errors
|
||||||
|
.push(Error::UnknownIdentifier(name.node.clone(), name.span));
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::JumpAndLink(Operand::Label(name.node.clone())),
|
||||||
|
Some(name.span),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
// pop all registers back
|
||||||
|
for register in active_registers.iter().rev() {
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Pop(Operand::Register(*register)),
|
||||||
|
Some(name.span),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn expression_tuple_declaration(
|
||||||
|
&mut self,
|
||||||
|
tuple_decl: TupleDeclarationExpression<'a>,
|
||||||
|
scope: &mut VariableScope<'a, '_>,
|
||||||
|
) -> Result<(), Error<'a>> {
|
||||||
|
let TupleDeclarationExpression { names, value } = tuple_decl;
|
||||||
|
|
||||||
|
// Compile the right-hand side expression
|
||||||
|
// For function calls returning tuples:
|
||||||
|
// r15 = pointer to beginning of tuple on stack
|
||||||
|
// r14, r13, ... contain the tuple elements, or they're on the stack
|
||||||
|
match &value.node {
|
||||||
|
Expression::Invocation(invoke_expr) => {
|
||||||
|
// Execute the function call
|
||||||
|
// Tuple values are on the stack, sp points after the last pushed value
|
||||||
|
// Pop them in reverse order (from end to beginning)
|
||||||
|
self.expression_function_invocation_with_invocation(invoke_expr, scope)?;
|
||||||
|
|
||||||
|
// First pass: allocate variables in order
|
||||||
|
let mut var_locations = Vec::new();
|
||||||
|
for name_spanned in names.iter() {
|
||||||
|
// Skip underscores
|
||||||
|
if name_spanned.node.as_ref() == "_" {
|
||||||
|
var_locations.push(None);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add variable to scope
|
||||||
|
let var_location = scope.add_variable(
|
||||||
|
name_spanned.node.clone(),
|
||||||
|
LocationRequest::Persist,
|
||||||
|
Some(name_spanned.span),
|
||||||
|
)?;
|
||||||
|
var_locations.push(Some(var_location));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Second pass: pop in reverse order and assign to locations
|
||||||
|
for (idx, var_loc_opt) in var_locations.iter().enumerate().rev() {
|
||||||
|
if let Some(var_location) = var_loc_opt {
|
||||||
|
let var_reg = self.resolve_register(&var_location)?;
|
||||||
|
|
||||||
|
// Pop from stack into the variable's register
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Pop(Operand::Register(var_reg)),
|
||||||
|
Some(names[idx].span),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Expression::Tuple(tuple_expr) => {
|
||||||
|
// Direct tuple literal: (value1, value2, ...)
|
||||||
|
let tuple_elements = &tuple_expr.node;
|
||||||
|
|
||||||
|
// Validate tuple size matches names
|
||||||
|
if tuple_elements.len() != names.len() {
|
||||||
|
return Err(Error::Unknown(
|
||||||
|
format!(
|
||||||
|
"Tuple size mismatch: expected {} elements, got {}",
|
||||||
|
names.len(),
|
||||||
|
tuple_elements.len()
|
||||||
|
),
|
||||||
|
Some(value.span),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compile each element and assign to corresponding variable
|
||||||
|
for (_index, (name_spanned, element)) in
|
||||||
|
names.iter().zip(tuple_elements.iter()).enumerate()
|
||||||
|
{
|
||||||
|
// Skip underscores
|
||||||
|
if name_spanned.node.as_ref() == "_" {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add variable to scope
|
||||||
|
let var_location = scope.add_variable(
|
||||||
|
name_spanned.node.clone(),
|
||||||
|
LocationRequest::Persist,
|
||||||
|
Some(name_spanned.span),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
// Compile the element expression - handle common cases directly
|
||||||
|
match &element.node {
|
||||||
|
Expression::Literal(lit) => {
|
||||||
|
let value_operand = extract_literal(lit.node.clone(), false)?;
|
||||||
|
self.emit_variable_assignment(&var_location, value_operand)?;
|
||||||
|
}
|
||||||
|
Expression::Variable(var) => {
|
||||||
|
let var_loc = match scope.get_location_of(&var.node, Some(var.span)) {
|
||||||
|
Ok(l) => l,
|
||||||
|
Err(_) => {
|
||||||
|
self.errors
|
||||||
|
.push(Error::UnknownIdentifier(var.node.clone(), var.span));
|
||||||
|
VariableLocation::Temporary(0)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let value_operand = match &var_loc {
|
||||||
|
VariableLocation::Temporary(reg)
|
||||||
|
| VariableLocation::Persistant(reg) => Operand::Register(*reg),
|
||||||
|
VariableLocation::Constant(lit) => {
|
||||||
|
extract_literal(lit.clone(), false)?
|
||||||
|
}
|
||||||
|
VariableLocation::Stack(offset) => {
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Sub(
|
||||||
|
Operand::Register(VariableScope::TEMP_STACK_REGISTER),
|
||||||
|
Operand::StackPointer,
|
||||||
|
Operand::Number((*offset).into()),
|
||||||
|
),
|
||||||
|
Some(var.span),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Get(
|
||||||
|
Operand::Register(VariableScope::TEMP_STACK_REGISTER),
|
||||||
|
Operand::Device(Cow::from("db")),
|
||||||
|
Operand::Register(VariableScope::TEMP_STACK_REGISTER),
|
||||||
|
),
|
||||||
|
Some(var.span),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Operand::Register(VariableScope::TEMP_STACK_REGISTER)
|
||||||
|
}
|
||||||
|
VariableLocation::Device(_) => {
|
||||||
|
return Err(Error::Unknown(
|
||||||
|
"Device values not supported in tuple literals".into(),
|
||||||
|
Some(var.span),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
self.emit_variable_assignment(&var_location, value_operand)?;
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
return Err(Error::Unknown(
|
||||||
|
"Complex expressions in tuple literals not yet supported".into(),
|
||||||
|
Some(element.span),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
return Err(Error::Unknown(
|
||||||
|
"Tuple declaration only supports function invocations or tuple literals as RHS"
|
||||||
|
.into(),
|
||||||
|
Some(value.span),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn expression_tuple_assignment(
|
||||||
|
&mut self,
|
||||||
|
tuple_assign: TupleAssignmentExpression<'a>,
|
||||||
|
scope: &mut VariableScope<'a, '_>,
|
||||||
|
) -> Result<(), Error<'a>> {
|
||||||
|
let TupleAssignmentExpression { names, value } = tuple_assign;
|
||||||
|
|
||||||
|
// Similar to tuple declaration, but variables must already exist
|
||||||
|
match &value.node {
|
||||||
|
Expression::Invocation(invoke_expr) => {
|
||||||
|
// Execute the function call
|
||||||
|
// Tuple values are on the stack, sp points after the last pushed value
|
||||||
|
// Pop them in reverse order (from end to beginning)
|
||||||
|
self.expression_function_invocation_with_invocation(invoke_expr, scope)?;
|
||||||
|
|
||||||
|
// First pass: look up variable locations
|
||||||
|
let mut var_locs = Vec::new();
|
||||||
|
for name_spanned in names.iter() {
|
||||||
|
// Skip underscores
|
||||||
|
if name_spanned.node.as_ref() == "_" {
|
||||||
|
var_locs.push(None);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the existing variable location
|
||||||
|
let var_location =
|
||||||
|
match scope.get_location_of(&name_spanned.node, Some(name_spanned.span)) {
|
||||||
|
Ok(l) => l,
|
||||||
|
Err(_) => {
|
||||||
|
self.errors.push(Error::UnknownIdentifier(
|
||||||
|
name_spanned.node.clone(),
|
||||||
|
name_spanned.span,
|
||||||
|
));
|
||||||
|
VariableLocation::Temporary(0)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var_locs.push(Some(var_location));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Second pass: pop in reverse order and assign
|
||||||
|
for (idx, var_loc_opt) in var_locs.iter().enumerate().rev() {
|
||||||
|
if let Some(var_location) = var_loc_opt {
|
||||||
|
// Pop from stack and assign to variable
|
||||||
|
match var_location {
|
||||||
|
VariableLocation::Temporary(reg)
|
||||||
|
| VariableLocation::Persistant(reg) => {
|
||||||
|
// Pop directly into the variable's register
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Pop(Operand::Register(*reg)),
|
||||||
|
Some(names[idx].span),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
VariableLocation::Stack(offset) => {
|
||||||
|
// Pop into temp register, then write to variable stack
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Pop(Operand::Register(
|
||||||
|
VariableScope::TEMP_STACK_REGISTER,
|
||||||
|
)),
|
||||||
|
Some(names[idx].span),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
// Write to variable stack location
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Sub(
|
||||||
|
Operand::Register(0),
|
||||||
|
Operand::StackPointer,
|
||||||
|
Operand::Number((*offset).into()),
|
||||||
|
),
|
||||||
|
Some(names[idx].span),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Put(
|
||||||
|
Operand::Device(Cow::from("db")),
|
||||||
|
Operand::Register(0),
|
||||||
|
Operand::Register(VariableScope::TEMP_STACK_REGISTER),
|
||||||
|
),
|
||||||
|
Some(names[idx].span),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
VariableLocation::Constant(_) => {
|
||||||
|
return Err(Error::ConstAssignment(
|
||||||
|
names[idx].node.clone(),
|
||||||
|
names[idx].span,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
VariableLocation::Device(_) => {
|
||||||
|
return Err(Error::DeviceAssignment(
|
||||||
|
names[idx].node.clone(),
|
||||||
|
names[idx].span,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Expression::Tuple(tuple_expr) => {
|
||||||
|
// Direct tuple literal: (value1, value2, ...)
|
||||||
|
let tuple_elements = &tuple_expr.node;
|
||||||
|
|
||||||
|
// Validate tuple size matches names
|
||||||
|
if tuple_elements.len() != names.len() {
|
||||||
|
return Err(Error::Unknown(
|
||||||
|
format!(
|
||||||
|
"Tuple size mismatch: expected {} elements, got {}",
|
||||||
|
names.len(),
|
||||||
|
tuple_elements.len()
|
||||||
|
),
|
||||||
|
Some(value.span),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compile each element and assign to corresponding variable
|
||||||
|
for (_index, (name_spanned, element)) in
|
||||||
|
names.iter().zip(tuple_elements.iter()).enumerate()
|
||||||
|
{
|
||||||
|
// Skip underscores
|
||||||
|
if name_spanned.node.as_ref() == "_" {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the existing variable location
|
||||||
|
let var_location =
|
||||||
|
match scope.get_location_of(&name_spanned.node, Some(name_spanned.span)) {
|
||||||
|
Ok(l) => l,
|
||||||
|
Err(_) => {
|
||||||
|
self.errors.push(Error::UnknownIdentifier(
|
||||||
|
name_spanned.node.clone(),
|
||||||
|
name_spanned.span,
|
||||||
|
));
|
||||||
|
VariableLocation::Temporary(0)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Compile the element expression - handle common cases directly
|
||||||
|
match &element.node {
|
||||||
|
Expression::Literal(lit) => {
|
||||||
|
let value_operand = extract_literal(lit.node.clone(), false)?;
|
||||||
|
match &var_location {
|
||||||
|
VariableLocation::Temporary(reg)
|
||||||
|
| VariableLocation::Persistant(reg) => {
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Move(Operand::Register(*reg), value_operand),
|
||||||
|
Some(name_spanned.span),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
VariableLocation::Stack(offset) => {
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Sub(
|
||||||
|
Operand::Register(VariableScope::TEMP_STACK_REGISTER),
|
||||||
|
Operand::StackPointer,
|
||||||
|
Operand::Number((*offset).into()),
|
||||||
|
),
|
||||||
|
Some(name_spanned.span),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Put(
|
||||||
|
Operand::Device(Cow::from("db")),
|
||||||
|
Operand::Register(VariableScope::TEMP_STACK_REGISTER),
|
||||||
|
value_operand,
|
||||||
|
),
|
||||||
|
Some(name_spanned.span),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
VariableLocation::Constant(_) => {
|
||||||
|
return Err(Error::ConstAssignment(
|
||||||
|
name_spanned.node.clone(),
|
||||||
|
name_spanned.span,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
VariableLocation::Device(_) => {
|
||||||
|
return Err(Error::DeviceAssignment(
|
||||||
|
name_spanned.node.clone(),
|
||||||
|
name_spanned.span,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Expression::Variable(var) => {
|
||||||
|
let var_loc = match scope.get_location_of(&var.node, Some(var.span)) {
|
||||||
|
Ok(l) => l,
|
||||||
|
Err(_) => {
|
||||||
|
self.errors
|
||||||
|
.push(Error::UnknownIdentifier(var.node.clone(), var.span));
|
||||||
|
VariableLocation::Temporary(0)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let value_operand = match &var_loc {
|
||||||
|
VariableLocation::Temporary(reg)
|
||||||
|
| VariableLocation::Persistant(reg) => Operand::Register(*reg),
|
||||||
|
VariableLocation::Constant(lit) => {
|
||||||
|
extract_literal(lit.clone(), false)?
|
||||||
|
}
|
||||||
|
VariableLocation::Stack(offset) => {
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Sub(
|
||||||
|
Operand::Register(VariableScope::TEMP_STACK_REGISTER),
|
||||||
|
Operand::StackPointer,
|
||||||
|
Operand::Number((*offset).into()),
|
||||||
|
),
|
||||||
|
Some(var.span),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Get(
|
||||||
|
Operand::Register(VariableScope::TEMP_STACK_REGISTER),
|
||||||
|
Operand::Device(Cow::from("db")),
|
||||||
|
Operand::Register(VariableScope::TEMP_STACK_REGISTER),
|
||||||
|
),
|
||||||
|
Some(var.span),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Operand::Register(VariableScope::TEMP_STACK_REGISTER)
|
||||||
|
}
|
||||||
|
VariableLocation::Device(_) => {
|
||||||
|
return Err(Error::Unknown(
|
||||||
|
"Device values not supported in tuple literals".into(),
|
||||||
|
Some(var.span),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match &var_location {
|
||||||
|
VariableLocation::Temporary(reg)
|
||||||
|
| VariableLocation::Persistant(reg) => {
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Move(Operand::Register(*reg), value_operand),
|
||||||
|
Some(name_spanned.span),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
VariableLocation::Stack(offset) => {
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Sub(
|
||||||
|
Operand::Register(VariableScope::TEMP_STACK_REGISTER),
|
||||||
|
Operand::StackPointer,
|
||||||
|
Operand::Number((*offset).into()),
|
||||||
|
),
|
||||||
|
Some(name_spanned.span),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Put(
|
||||||
|
Operand::Device(Cow::from("db")),
|
||||||
|
Operand::Register(VariableScope::TEMP_STACK_REGISTER),
|
||||||
|
value_operand,
|
||||||
|
),
|
||||||
|
Some(name_spanned.span),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
VariableLocation::Constant(_) => {
|
||||||
|
return Err(Error::ConstAssignment(
|
||||||
|
name_spanned.node.clone(),
|
||||||
|
name_spanned.span,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
VariableLocation::Device(_) => {
|
||||||
|
return Err(Error::DeviceAssignment(
|
||||||
|
name_spanned.node.clone(),
|
||||||
|
name_spanned.span,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
return Err(Error::Unknown(
|
||||||
|
"Complex expressions in tuple literals not yet supported".into(),
|
||||||
|
Some(element.span),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
return Err(Error::Unknown(
|
||||||
|
"Tuple assignment only supports function invocations or tuple literals as RHS"
|
||||||
|
.into(),
|
||||||
|
Some(value.span),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn expression_function_invocation(
|
fn expression_function_invocation(
|
||||||
&mut self,
|
&mut self,
|
||||||
invoke_expr: Spanned<InvocationExpression<'a>>,
|
invoke_expr: Spanned<InvocationExpression<'a>>,
|
||||||
@@ -1946,6 +2540,110 @@ impl<'a> Compiler<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Expression::Tuple(tuple_expr) => {
|
||||||
|
let span = expr.span;
|
||||||
|
let tuple_elements = &tuple_expr.node;
|
||||||
|
|
||||||
|
// Record the stack offset where the tuple will start
|
||||||
|
let tuple_start_offset = scope.stack_offset();
|
||||||
|
|
||||||
|
// Allocate space on the stack for each tuple element
|
||||||
|
for element in tuple_elements.iter() {
|
||||||
|
match &element.node {
|
||||||
|
Expression::Literal(lit) => {
|
||||||
|
let value_operand = extract_literal(lit.node.clone(), false)?;
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Push(value_operand),
|
||||||
|
Some(span),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
Expression::Variable(var) => {
|
||||||
|
let var_loc = match scope.get_location_of(&var.node, Some(var.span))
|
||||||
|
{
|
||||||
|
Ok(l) => l,
|
||||||
|
Err(_) => {
|
||||||
|
self.errors.push(Error::UnknownIdentifier(
|
||||||
|
var.node.clone(),
|
||||||
|
var.span,
|
||||||
|
));
|
||||||
|
VariableLocation::Temporary(0)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match &var_loc {
|
||||||
|
VariableLocation::Temporary(reg)
|
||||||
|
| VariableLocation::Persistant(reg) => {
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Push(Operand::Register(*reg)),
|
||||||
|
Some(span),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
VariableLocation::Constant(lit) => {
|
||||||
|
let value_operand = extract_literal(lit.clone(), false)?;
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Push(value_operand),
|
||||||
|
Some(span),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
VariableLocation::Stack(offset) => {
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Sub(
|
||||||
|
Operand::Register(
|
||||||
|
VariableScope::TEMP_STACK_REGISTER,
|
||||||
|
),
|
||||||
|
Operand::StackPointer,
|
||||||
|
Operand::Number((*offset).into()),
|
||||||
|
),
|
||||||
|
Some(span),
|
||||||
|
)?;
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Get(
|
||||||
|
Operand::Register(
|
||||||
|
VariableScope::TEMP_STACK_REGISTER,
|
||||||
|
),
|
||||||
|
Operand::Device(Cow::from("db")),
|
||||||
|
Operand::Register(
|
||||||
|
VariableScope::TEMP_STACK_REGISTER,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
Some(span),
|
||||||
|
)?;
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Push(Operand::Register(
|
||||||
|
VariableScope::TEMP_STACK_REGISTER,
|
||||||
|
)),
|
||||||
|
Some(span),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
VariableLocation::Device(_) => {
|
||||||
|
return Err(Error::Unknown(
|
||||||
|
"You can not return a device from a function.".into(),
|
||||||
|
Some(var.span),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
// For complex expressions, just push 0 for now
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Push(Operand::Number(
|
||||||
|
Number::Integer(0, Unit::None).into(),
|
||||||
|
)),
|
||||||
|
Some(span),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store the pointer to the tuple (stack offset) in r15
|
||||||
|
self.write_instruction(
|
||||||
|
Instruction::Move(
|
||||||
|
Operand::Register(VariableScope::RETURN_REGISTER),
|
||||||
|
Operand::Number(tuple_start_offset.into()),
|
||||||
|
),
|
||||||
|
Some(span),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
_ => {
|
_ => {
|
||||||
return Err(Error::Unknown(
|
return Err(Error::Unknown(
|
||||||
format!("Unsupported `return` statement: {:?}", expr),
|
format!("Unsupported `return` statement: {:?}", expr),
|
||||||
|
|||||||
@@ -441,7 +441,13 @@ impl<'a> Parser<'a> {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
TokenType::Keyword(Keyword::Let) => Some(self.spanned(|p| p.declaration())?),
|
TokenType::Keyword(Keyword::Let) => {
|
||||||
|
if self_matches_peek!(self, TokenType::Symbol(Symbol::LParen)) {
|
||||||
|
Some(self.spanned(|p| p.tuple_declaration())?)
|
||||||
|
} else {
|
||||||
|
Some(self.spanned(|p| p.declaration())?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TokenType::Keyword(Keyword::Device) => {
|
TokenType::Keyword(Keyword::Device) => {
|
||||||
let spanned_dev = self.spanned(|p| p.device())?;
|
let spanned_dev = self.spanned(|p| p.device())?;
|
||||||
@@ -561,9 +567,7 @@ impl<'a> Parser<'a> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
TokenType::Symbol(Symbol::LParen) => {
|
TokenType::Symbol(Symbol::LParen) => self.parenthesized_or_tuple()?,
|
||||||
self.spanned(|p| p.priority())?.node.map(|node| *node)
|
|
||||||
}
|
|
||||||
|
|
||||||
TokenType::Symbol(Symbol::Minus) => {
|
TokenType::Symbol(Symbol::Minus) => {
|
||||||
let start_span = self.current_span();
|
let start_span = self.current_span();
|
||||||
@@ -642,8 +646,8 @@ impl<'a> Parser<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
TokenType::Symbol(Symbol::LParen) => *self
|
TokenType::Symbol(Symbol::LParen) => *self
|
||||||
.spanned(|p| p.priority())?
|
.parenthesized_or_tuple()?
|
||||||
.node
|
.map(Box::new)
|
||||||
.ok_or(Error::UnexpectedEOF)?,
|
.ok_or(Error::UnexpectedEOF)?,
|
||||||
|
|
||||||
TokenType::Identifier(ref id) if SysCall::is_syscall(id) => {
|
TokenType::Identifier(ref id) if SysCall::is_syscall(id) => {
|
||||||
@@ -774,7 +778,8 @@ impl<'a> Parser<'a> {
|
|||||||
| Expression::Ternary(_)
|
| Expression::Ternary(_)
|
||||||
| Expression::Negation(_)
|
| Expression::Negation(_)
|
||||||
| Expression::MemberAccess(_)
|
| Expression::MemberAccess(_)
|
||||||
| Expression::MethodCall(_) => {}
|
| Expression::MethodCall(_)
|
||||||
|
| Expression::Tuple(_) => {}
|
||||||
_ => {
|
_ => {
|
||||||
return Err(Error::InvalidSyntax(
|
return Err(Error::InvalidSyntax(
|
||||||
self.current_span(),
|
self.current_span(),
|
||||||
@@ -1081,17 +1086,43 @@ impl<'a> Parser<'a> {
|
|||||||
end_col: right.span.end_col,
|
end_col: right.span.end_col,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Check if the left side is a tuple, and if so, create a TupleAssignment
|
||||||
|
let node = if let Expression::Tuple(tuple_expr) = &left.node {
|
||||||
|
// Extract variable names from the tuple, handling underscores
|
||||||
|
let mut names = Vec::new();
|
||||||
|
for item in &tuple_expr.node {
|
||||||
|
if let Expression::Variable(var) = &item.node {
|
||||||
|
names.push(var.clone());
|
||||||
|
} else {
|
||||||
|
return Err(Error::InvalidSyntax(
|
||||||
|
item.span,
|
||||||
|
String::from("Tuple assignment can only contain variable names"),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Expression::TupleAssignment(Spanned {
|
||||||
|
span,
|
||||||
|
node: TupleAssignmentExpression {
|
||||||
|
names,
|
||||||
|
value: boxed!(right),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Expression::Assignment(Spanned {
|
||||||
|
span,
|
||||||
|
node: AssignmentExpression {
|
||||||
|
assignee: boxed!(left),
|
||||||
|
expression: boxed!(right),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
expressions.insert(
|
expressions.insert(
|
||||||
i,
|
i,
|
||||||
Spanned {
|
Spanned {
|
||||||
span,
|
span,
|
||||||
node: Expression::Assignment(Spanned {
|
node,
|
||||||
span,
|
|
||||||
node: AssignmentExpression {
|
|
||||||
assignee: boxed!(left),
|
|
||||||
expression: boxed!(right),
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -1117,8 +1148,12 @@ impl<'a> Parser<'a> {
|
|||||||
expressions.pop().ok_or(Error::UnexpectedEOF)
|
expressions.pop().ok_or(Error::UnexpectedEOF)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn priority(&mut self) -> Result<Option<Box<Spanned<Expression<'a>>>>, Error<'a>> {
|
fn parenthesized_or_tuple(
|
||||||
|
&mut self,
|
||||||
|
) -> Result<Option<Spanned<tree_node::Expression<'a>>>, Error<'a>> {
|
||||||
|
let start_span = self.current_span();
|
||||||
let current_token = self.current_token.as_ref().ok_or(Error::UnexpectedEOF)?;
|
let current_token = self.current_token.as_ref().ok_or(Error::UnexpectedEOF)?;
|
||||||
|
|
||||||
if !token_matches!(current_token, TokenType::Symbol(Symbol::LParen)) {
|
if !token_matches!(current_token, TokenType::Symbol(Symbol::LParen)) {
|
||||||
return Err(Error::UnexpectedToken(
|
return Err(Error::UnexpectedToken(
|
||||||
self.current_span(),
|
self.current_span(),
|
||||||
@@ -1127,17 +1162,113 @@ impl<'a> Parser<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
self.assign_next()?;
|
self.assign_next()?;
|
||||||
let expression = self.expression()?.ok_or(Error::UnexpectedEOF)?;
|
|
||||||
|
|
||||||
let current_token = self.get_next()?.ok_or(Error::UnexpectedEOF)?;
|
// Handle empty tuple '()'
|
||||||
if !token_matches!(current_token, TokenType::Symbol(Symbol::RParen)) {
|
if self_matches_peek!(self, TokenType::Symbol(Symbol::RParen)) {
|
||||||
return Err(Error::UnexpectedToken(
|
self.assign_next()?;
|
||||||
Self::token_to_span(¤t_token),
|
let end_span = self.current_span();
|
||||||
current_token,
|
let span = Span {
|
||||||
));
|
start_line: start_span.start_line,
|
||||||
|
start_col: start_span.start_col,
|
||||||
|
end_line: end_span.end_line,
|
||||||
|
end_col: end_span.end_col,
|
||||||
|
};
|
||||||
|
return Ok(Some(Spanned {
|
||||||
|
span,
|
||||||
|
node: Expression::Tuple(Spanned { span, node: vec![] }),
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Some(boxed!(expression)))
|
let first_expression = self.expression()?.ok_or(Error::UnexpectedEOF)?;
|
||||||
|
|
||||||
|
if self_matches_peek!(self, TokenType::Symbol(Symbol::Comma)) {
|
||||||
|
// It is a tuple
|
||||||
|
let mut items = vec![first_expression];
|
||||||
|
while self_matches_peek!(self, TokenType::Symbol(Symbol::Comma)) {
|
||||||
|
// Next toekn is a comma, we need to consume it and advance 1 more time.
|
||||||
|
self.assign_next()?;
|
||||||
|
self.assign_next()?;
|
||||||
|
println!("{:?}", self.current_token);
|
||||||
|
items.push(self.expression()?.ok_or(Error::UnexpectedEOF)?);
|
||||||
|
}
|
||||||
|
|
||||||
|
let next = self.get_next()?.ok_or(Error::UnexpectedEOF)?;
|
||||||
|
if !token_matches!(next, TokenType::Symbol(Symbol::RParen)) {
|
||||||
|
return Err(Error::UnexpectedToken(Self::token_to_span(&next), next));
|
||||||
|
}
|
||||||
|
|
||||||
|
let end_span = Self::token_to_span(&next);
|
||||||
|
let span = Span {
|
||||||
|
start_line: start_span.start_line,
|
||||||
|
start_col: start_span.start_col,
|
||||||
|
end_line: end_span.end_line,
|
||||||
|
end_col: end_span.end_col,
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Some(Spanned {
|
||||||
|
span,
|
||||||
|
node: Expression::Tuple(Spanned { span, node: items }),
|
||||||
|
}))
|
||||||
|
} else {
|
||||||
|
// It is just priority
|
||||||
|
let next = self.get_next()?.ok_or(Error::UnexpectedEOF)?;
|
||||||
|
if !token_matches!(next, TokenType::Symbol(Symbol::RParen)) {
|
||||||
|
return Err(Error::UnexpectedToken(Self::token_to_span(&next), next));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Some(Spanned {
|
||||||
|
span: first_expression.span,
|
||||||
|
node: Expression::Priority(boxed!(first_expression)),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tuple_declaration(&mut self) -> Result<Expression<'a>, Error<'a>> {
|
||||||
|
// 'let' is consumed before this call
|
||||||
|
// expect '('
|
||||||
|
let next = self.get_next()?.ok_or(Error::UnexpectedEOF)?;
|
||||||
|
if !token_matches!(next, TokenType::Symbol(Symbol::LParen)) {
|
||||||
|
return Err(Error::UnexpectedToken(Self::token_to_span(&next), next));
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut names = Vec::new();
|
||||||
|
while !self_matches_peek!(self, TokenType::Symbol(Symbol::RParen)) {
|
||||||
|
let token = self.get_next()?.ok_or(Error::UnexpectedEOF)?;
|
||||||
|
let span = Self::token_to_span(&token);
|
||||||
|
if let TokenType::Identifier(id) = token.token_type {
|
||||||
|
names.push(Spanned { span, node: id });
|
||||||
|
} else {
|
||||||
|
return Err(Error::UnexpectedToken(span, token));
|
||||||
|
}
|
||||||
|
|
||||||
|
if self_matches_peek!(self, TokenType::Symbol(Symbol::Comma)) {
|
||||||
|
self.assign_next()?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.assign_next()?; // consume ')'
|
||||||
|
|
||||||
|
let assign = self.get_next()?.ok_or(Error::UnexpectedEOF)?;
|
||||||
|
|
||||||
|
if !token_matches!(assign, TokenType::Symbol(Symbol::Assign)) {
|
||||||
|
return Err(Error::UnexpectedToken(Self::token_to_span(&assign), assign));
|
||||||
|
}
|
||||||
|
|
||||||
|
self.assign_next()?; // Consume the `=`
|
||||||
|
|
||||||
|
let value = self.expression()?.ok_or(Error::UnexpectedEOF)?;
|
||||||
|
|
||||||
|
let semi = self.get_next()?.ok_or(Error::UnexpectedEOF)?;
|
||||||
|
if !token_matches!(semi, TokenType::Symbol(Symbol::Semicolon)) {
|
||||||
|
return Err(Error::UnexpectedToken(Self::token_to_span(&semi), semi));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Expression::TupleDeclaration(Spanned {
|
||||||
|
span: names.first().map(|n| n.span).unwrap_or(value.span),
|
||||||
|
node: TupleDeclarationExpression {
|
||||||
|
names,
|
||||||
|
value: boxed!(value),
|
||||||
|
},
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn invocation(&mut self) -> Result<InvocationExpression<'a>, Error<'a>> {
|
fn invocation(&mut self) -> Result<InvocationExpression<'a>, Error<'a>> {
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ fn test_function_invocation() -> Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_priority_expression() -> Result<()> {
|
fn test_priority_expression() -> Result<()> {
|
||||||
let input = r#"
|
let input = r#"
|
||||||
let x = (4);
|
let x = (4 + 3);
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
let tokenizer = Tokenizer::from(input);
|
let tokenizer = Tokenizer::from(input);
|
||||||
@@ -120,7 +120,7 @@ fn test_priority_expression() -> Result<()> {
|
|||||||
|
|
||||||
let expression = parser.parse()?.unwrap();
|
let expression = parser.parse()?.unwrap();
|
||||||
|
|
||||||
assert_eq!("(let x = 4)", expression.to_string());
|
assert_eq!("(let x = ((4 + 3)))", expression.to_string());
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -137,7 +137,7 @@ fn test_binary_expression() -> Result<()> {
|
|||||||
assert_eq!("(((45 * 2) - (15 / 5)) + (5 ** 2))", expr.to_string());
|
assert_eq!("(((45 * 2) - (15 / 5)) + (5 ** 2))", expr.to_string());
|
||||||
|
|
||||||
let expr = parser!("(5 - 2) * 10;").parse()?.unwrap();
|
let expr = parser!("(5 - 2) * 10;").parse()?.unwrap();
|
||||||
assert_eq!("((5 - 2) * 10)", expr.to_string());
|
assert_eq!("(((5 - 2)) * 10)", expr.to_string());
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -170,7 +170,7 @@ fn test_ternary_expression() -> Result<()> {
|
|||||||
fn test_complex_binary_with_ternary() -> Result<()> {
|
fn test_complex_binary_with_ternary() -> Result<()> {
|
||||||
let expr = parser!("let i = (x ? 1 : 3) * 2;").parse()?.unwrap();
|
let expr = parser!("let i = (x ? 1 : 3) * 2;").parse()?.unwrap();
|
||||||
|
|
||||||
assert_eq!("(let i = ((x ? 1 : 3) * 2))", expr.to_string());
|
assert_eq!("(let i = (((x ? 1 : 3)) * 2))", expr.to_string());
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -191,3 +191,65 @@ fn test_nested_ternary_right_associativity() -> Result<()> {
|
|||||||
assert_eq!("(let i = (a ? b : (c ? d : e)))", expr.to_string());
|
assert_eq!("(let i = (a ? b : (c ? d : e)))", expr.to_string());
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_declaration() -> Result<()> {
|
||||||
|
let expr = parser!("let (x, _) = (1, 2);").parse()?.unwrap();
|
||||||
|
|
||||||
|
assert_eq!("(let (x, _) = (1, 2))", expr.to_string());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_assignment() -> Result<()> {
|
||||||
|
let expr = parser!("(x, y) = (1, 2);").parse()?.unwrap();
|
||||||
|
|
||||||
|
assert_eq!("((x, y) = (1, 2))", expr.to_string());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_assignment_with_underscore() -> Result<()> {
|
||||||
|
let expr = parser!("(x, _) = (1, 2);").parse()?.unwrap();
|
||||||
|
|
||||||
|
assert_eq!("((x, _) = (1, 2))", expr.to_string());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_declaration_with_function_call() -> Result<()> {
|
||||||
|
let expr = parser!("let (x, y) = doSomething();").parse()?.unwrap();
|
||||||
|
|
||||||
|
assert_eq!("(let (x, y) = doSomething())", expr.to_string());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_declaration_with_function_call_with_underscore() -> Result<()> {
|
||||||
|
let expr = parser!("let (x, _) = doSomething();").parse()?.unwrap();
|
||||||
|
|
||||||
|
assert_eq!("(let (x, _) = doSomething())", expr.to_string());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_assignment_with_function_call() -> Result<()> {
|
||||||
|
let expr = parser!("(x, y) = doSomething();").parse()?.unwrap();
|
||||||
|
|
||||||
|
assert_eq!("((x, y) = doSomething())", expr.to_string());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_assignment_with_function_call_with_underscore() -> Result<()> {
|
||||||
|
let expr = parser!("(x, _) = doSomething();").parse()?.unwrap();
|
||||||
|
|
||||||
|
assert_eq!("((x, _) = doSomething())", expr.to_string());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
@@ -245,6 +245,42 @@ impl<'a> std::fmt::Display for DeviceDeclarationExpression<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
pub struct TupleDeclarationExpression<'a> {
|
||||||
|
pub names: Vec<Spanned<Cow<'a, str>>>,
|
||||||
|
pub value: Box<Spanned<Expression<'a>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> std::fmt::Display for TupleDeclarationExpression<'a> {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
let names = self
|
||||||
|
.names
|
||||||
|
.iter()
|
||||||
|
.map(|n| n.node.to_string())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ");
|
||||||
|
write!(f, "(let ({}) = {})", names, self.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
pub struct TupleAssignmentExpression<'a> {
|
||||||
|
pub names: Vec<Spanned<Cow<'a, str>>>,
|
||||||
|
pub value: Box<Spanned<Expression<'a>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> std::fmt::Display for TupleAssignmentExpression<'a> {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
let names = self
|
||||||
|
.names
|
||||||
|
.iter()
|
||||||
|
.map(|n| n.node.to_string())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ");
|
||||||
|
write!(f, "(({}) = {})", names, self.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub struct IfExpression<'a> {
|
pub struct IfExpression<'a> {
|
||||||
pub condition: Box<Spanned<Expression<'a>>>,
|
pub condition: Box<Spanned<Expression<'a>>>,
|
||||||
@@ -348,6 +384,9 @@ pub enum Expression<'a> {
|
|||||||
Return(Option<Box<Spanned<Expression<'a>>>>),
|
Return(Option<Box<Spanned<Expression<'a>>>>),
|
||||||
Syscall(Spanned<SysCall<'a>>),
|
Syscall(Spanned<SysCall<'a>>),
|
||||||
Ternary(Spanned<TernaryExpression<'a>>),
|
Ternary(Spanned<TernaryExpression<'a>>),
|
||||||
|
Tuple(Spanned<Vec<Spanned<Expression<'a>>>>),
|
||||||
|
TupleAssignment(Spanned<TupleAssignmentExpression<'a>>),
|
||||||
|
TupleDeclaration(Spanned<TupleDeclarationExpression<'a>>),
|
||||||
Variable(Spanned<Cow<'a, str>>),
|
Variable(Spanned<Cow<'a, str>>),
|
||||||
While(Spanned<WhileExpression<'a>>),
|
While(Spanned<WhileExpression<'a>>),
|
||||||
}
|
}
|
||||||
@@ -384,8 +423,20 @@ impl<'a> std::fmt::Display for Expression<'a> {
|
|||||||
),
|
),
|
||||||
Expression::Syscall(e) => write!(f, "{}", e),
|
Expression::Syscall(e) => write!(f, "{}", e),
|
||||||
Expression::Ternary(e) => write!(f, "{}", e),
|
Expression::Ternary(e) => write!(f, "{}", e),
|
||||||
|
Expression::Tuple(e) => {
|
||||||
|
let items = e
|
||||||
|
.node
|
||||||
|
.iter()
|
||||||
|
.map(|x| x.to_string())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ");
|
||||||
|
write!(f, "({})", items)
|
||||||
|
}
|
||||||
|
Expression::TupleAssignment(e) => write!(f, "{}", e),
|
||||||
|
Expression::TupleDeclaration(e) => write!(f, "{}", e),
|
||||||
Expression::Variable(id) => write!(f, "{}", id),
|
Expression::Variable(id) => write!(f, "{}", id),
|
||||||
Expression::While(e) => write!(f, "{}", e),
|
Expression::While(e) => write!(f, "{}", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user