Compare commits
8 Commits
0.4.6
...
20f0f4b9a1
| Author | SHA1 | Date | |
|---|---|---|---|
|
20f0f4b9a1
|
|||
|
5a88befac9
|
|||
|
e94fc0f5de
|
|||
|
b51800eb77
|
|||
|
87951ab12f
|
|||
|
00b0d4df26
|
|||
| 6ca53e8959 | |||
|
8dfdad3f34
|
@@ -1,5 +1,9 @@
|
||||
# Changelog
|
||||
|
||||
[0.4.7]
|
||||
|
||||
- Added support for Windows CRLF endings
|
||||
|
||||
[0.4.6]
|
||||
|
||||
- Fixed bug in compiler where you were unable to assign a `const` value to
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<ModMetadata xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<Name>Slang</Name>
|
||||
<Author>JoeDiertay</Author>
|
||||
<Version>0.4.6</Version>
|
||||
<Version>0.4.7</Version>
|
||||
<Description>
|
||||
[h1]Slang: High-Level Programming for Stationeers[/h1]
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ namespace Slang
|
||||
{
|
||||
public const string PluginGuid = "com.biddydev.slang";
|
||||
public const string PluginName = "Slang";
|
||||
public const string PluginVersion = "0.4.6";
|
||||
public const string PluginVersion = "0.4.7";
|
||||
|
||||
private static Harmony? _harmony;
|
||||
|
||||
|
||||
2
rust_compiler/Cargo.lock
generated
2
rust_compiler/Cargo.lock
generated
@@ -930,7 +930,7 @@ checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e"
|
||||
|
||||
[[package]]
|
||||
name = "slang"
|
||||
version = "0.4.6"
|
||||
version = "0.4.7"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"clap",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "slang"
|
||||
version = "0.4.6"
|
||||
version = "0.4.7"
|
||||
edition = "2021"
|
||||
|
||||
[workspace]
|
||||
|
||||
@@ -47,3 +47,4 @@ mod logic_expression;
|
||||
mod loops;
|
||||
mod math_syscall;
|
||||
mod syscall;
|
||||
mod tuple_literals;
|
||||
|
||||
539
rust_compiler/libs/compiler/src/test/tuple_literals.rs
Normal file
539
rust_compiler/libs/compiler/src/test/tuple_literals.rs
Normal file
@@ -0,0 +1,539 @@
|
||||
#[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:
|
||||
move r15 sp
|
||||
push ra
|
||||
push 10
|
||||
push 20
|
||||
move r15 1
|
||||
sub r0 sp 3
|
||||
get ra db r0
|
||||
j ra
|
||||
main:
|
||||
jal getPair
|
||||
pop r9
|
||||
pop r8
|
||||
move sp r15
|
||||
"
|
||||
}
|
||||
);
|
||||
|
||||
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:
|
||||
move r15 sp
|
||||
push ra
|
||||
push 5
|
||||
push 15
|
||||
move r15 1
|
||||
sub r0 sp 3
|
||||
get ra db r0
|
||||
j ra
|
||||
main:
|
||||
jal getPair
|
||||
pop r0
|
||||
pop r8
|
||||
move sp r15
|
||||
"
|
||||
}
|
||||
);
|
||||
|
||||
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:
|
||||
move r15 sp
|
||||
push ra
|
||||
push 1
|
||||
push 2
|
||||
push 3
|
||||
move r15 1
|
||||
sub r0 sp 4
|
||||
get ra db r0
|
||||
j ra
|
||||
main:
|
||||
jal getTriple
|
||||
pop r10
|
||||
pop r9
|
||||
pop r8
|
||||
move sp r15
|
||||
"
|
||||
}
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tuple_return_assignment() -> anyhow::Result<()> {
|
||||
let compiled = compile!(
|
||||
debug
|
||||
r#"
|
||||
fn getPair() {
|
||||
return (42, 84);
|
||||
};
|
||||
let i = 1;
|
||||
let j = 2;
|
||||
(i, j) = getPair();
|
||||
"#
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
compiled,
|
||||
indoc! {
|
||||
"
|
||||
j main
|
||||
getPair:
|
||||
move r15 sp
|
||||
push ra
|
||||
push 42
|
||||
push 84
|
||||
move r15 1
|
||||
sub r0 sp 3
|
||||
get ra db r0
|
||||
j ra
|
||||
main:
|
||||
move r8 1
|
||||
move r9 2
|
||||
jal getPair
|
||||
pop r9
|
||||
pop r8
|
||||
move sp r15
|
||||
"
|
||||
}
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tuple_return_mismatch() -> anyhow::Result<()> {
|
||||
let errors = compile!(
|
||||
result
|
||||
r#"
|
||||
fn doSomething() {
|
||||
return (1, 2, 3);
|
||||
};
|
||||
let (x, y) = doSomething();
|
||||
"#
|
||||
);
|
||||
|
||||
// Should have exactly one error about tuple size mismatch
|
||||
assert_eq!(errors.len(), 1);
|
||||
|
||||
// Check for the specific TupleSizeMismatch error
|
||||
match &errors[0] {
|
||||
crate::Error::TupleSizeMismatch(func_name, expected_size, actual_count, _) => {
|
||||
assert_eq!(func_name.as_ref(), "doSomething");
|
||||
assert_eq!(*expected_size, 3);
|
||||
assert_eq!(*actual_count, 2);
|
||||
}
|
||||
e => panic!("Expected TupleSizeMismatch error, got: {:?}", e),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tuple_return_called_by_non_tuple_return() -> anyhow::Result<()> {
|
||||
let compiled = compile!(
|
||||
debug
|
||||
r#"
|
||||
fn doSomething() {
|
||||
return (1, 2);
|
||||
};
|
||||
|
||||
fn doSomethingElse() {
|
||||
let (x, y) = doSomething();
|
||||
return y;
|
||||
};
|
||||
|
||||
let returnedValue = doSomethingElse();
|
||||
"#
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
compiled,
|
||||
indoc! {
|
||||
"
|
||||
j main
|
||||
doSomething:
|
||||
move r15 sp
|
||||
push ra
|
||||
push 1
|
||||
push 2
|
||||
move r15 1
|
||||
sub r0 sp 3
|
||||
get ra db r0
|
||||
j ra
|
||||
doSomethingElse:
|
||||
push ra
|
||||
jal doSomething
|
||||
pop r9
|
||||
pop r8
|
||||
move sp r15
|
||||
move r15 r9
|
||||
j __internal_L2
|
||||
__internal_L2:
|
||||
pop ra
|
||||
j ra
|
||||
main:
|
||||
jal doSomethingElse
|
||||
move r8 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_non_tuple_return_called_by_tuple_return() -> anyhow::Result<()> {
|
||||
let compiled = compile!(
|
||||
debug
|
||||
r#"
|
||||
fn getValue() {
|
||||
return 42;
|
||||
};
|
||||
|
||||
fn getTuple() {
|
||||
let x = getValue();
|
||||
return (x, x);
|
||||
};
|
||||
|
||||
let (a, b) = getTuple();
|
||||
"#
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
compiled,
|
||||
indoc! {
|
||||
"
|
||||
j main
|
||||
getValue:
|
||||
push ra
|
||||
move r15 42
|
||||
j __internal_L1
|
||||
__internal_L1:
|
||||
pop ra
|
||||
j ra
|
||||
getTuple:
|
||||
move r15 sp
|
||||
push ra
|
||||
jal getValue
|
||||
move r8 r15
|
||||
push r8
|
||||
push r8
|
||||
move r15 1
|
||||
sub r0 sp 3
|
||||
get ra db r0
|
||||
j ra
|
||||
main:
|
||||
jal getTuple
|
||||
pop r9
|
||||
pop r8
|
||||
move sp r15
|
||||
"
|
||||
}
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tuple_literal_size_mismatch() -> anyhow::Result<()> {
|
||||
let errors = compile!(
|
||||
result
|
||||
r#"
|
||||
let (x, y) = (1, 2, 3);
|
||||
"#
|
||||
);
|
||||
|
||||
// Should have exactly one error about tuple size mismatch
|
||||
assert_eq!(errors.len(), 1);
|
||||
assert!(matches!(errors[0], crate::Error::Unknown(_, _)));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multiple_tuple_returns_in_function() -> anyhow::Result<()> {
|
||||
let compiled = compile!(
|
||||
debug
|
||||
r#"
|
||||
fn getValue(x) {
|
||||
if (x) {
|
||||
return (1, 2);
|
||||
} else {
|
||||
return (3, 4);
|
||||
}
|
||||
};
|
||||
|
||||
let (a, b) = getValue(1);
|
||||
"#
|
||||
);
|
||||
|
||||
println!("Generated code:\n{}", compiled);
|
||||
|
||||
// Both returns are 2-tuples, should compile successfully
|
||||
assert!(compiled.contains("getValue:"));
|
||||
assert!(compiled.contains("move r15 "));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tuple_return_with_expression() -> anyhow::Result<()> {
|
||||
let compiled = compile!(
|
||||
debug
|
||||
r#"
|
||||
fn add(x, y) {
|
||||
return (x, y);
|
||||
};
|
||||
|
||||
let (a, b) = add(5, 10);
|
||||
"#
|
||||
);
|
||||
|
||||
// Should compile - we're just passing the parameter variables through
|
||||
assert!(compiled.contains("add:"));
|
||||
assert!(compiled.contains("jal "));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_nested_function_tuple_calls() -> anyhow::Result<()> {
|
||||
let compiled = compile!(
|
||||
debug
|
||||
r#"
|
||||
fn inner() {
|
||||
return (1, 2);
|
||||
};
|
||||
|
||||
fn outer() {
|
||||
let (x, y) = inner();
|
||||
return (y, x);
|
||||
};
|
||||
|
||||
let (a, b) = outer();
|
||||
"#
|
||||
);
|
||||
|
||||
// Both functions return tuples
|
||||
assert!(compiled.contains("inner:"));
|
||||
assert!(compiled.contains("outer:"));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -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) => {
|
||||
let spanned_dev = self.spanned(|p| p.device())?;
|
||||
@@ -561,9 +567,7 @@ impl<'a> Parser<'a> {
|
||||
})
|
||||
}
|
||||
|
||||
TokenType::Symbol(Symbol::LParen) => {
|
||||
self.spanned(|p| p.priority())?.node.map(|node| *node)
|
||||
}
|
||||
TokenType::Symbol(Symbol::LParen) => self.parenthesized_or_tuple()?,
|
||||
|
||||
TokenType::Symbol(Symbol::Minus) => {
|
||||
let start_span = self.current_span();
|
||||
@@ -642,8 +646,8 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
}
|
||||
TokenType::Symbol(Symbol::LParen) => *self
|
||||
.spanned(|p| p.priority())?
|
||||
.node
|
||||
.parenthesized_or_tuple()?
|
||||
.map(Box::new)
|
||||
.ok_or(Error::UnexpectedEOF)?,
|
||||
|
||||
TokenType::Identifier(ref id) if SysCall::is_syscall(id) => {
|
||||
@@ -774,7 +778,8 @@ impl<'a> Parser<'a> {
|
||||
| Expression::Ternary(_)
|
||||
| Expression::Negation(_)
|
||||
| Expression::MemberAccess(_)
|
||||
| Expression::MethodCall(_) => {}
|
||||
| Expression::MethodCall(_)
|
||||
| Expression::Tuple(_) => {}
|
||||
_ => {
|
||||
return Err(Error::InvalidSyntax(
|
||||
self.current_span(),
|
||||
@@ -1081,17 +1086,43 @@ impl<'a> Parser<'a> {
|
||||
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(
|
||||
i,
|
||||
Spanned {
|
||||
span,
|
||||
node: Expression::Assignment(Spanned {
|
||||
span,
|
||||
node: AssignmentExpression {
|
||||
assignee: boxed!(left),
|
||||
expression: boxed!(right),
|
||||
},
|
||||
}),
|
||||
node,
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -1117,8 +1148,12 @@ impl<'a> Parser<'a> {
|
||||
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)?;
|
||||
|
||||
if !token_matches!(current_token, TokenType::Symbol(Symbol::LParen)) {
|
||||
return Err(Error::UnexpectedToken(
|
||||
self.current_span(),
|
||||
@@ -1127,17 +1162,113 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
self.assign_next()?;
|
||||
let expression = self.expression()?.ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
let current_token = self.get_next()?.ok_or(Error::UnexpectedEOF)?;
|
||||
if !token_matches!(current_token, TokenType::Symbol(Symbol::RParen)) {
|
||||
return Err(Error::UnexpectedToken(
|
||||
Self::token_to_span(¤t_token),
|
||||
current_token,
|
||||
));
|
||||
// Handle empty tuple '()'
|
||||
if self_matches_peek!(self, TokenType::Symbol(Symbol::RParen)) {
|
||||
self.assign_next()?;
|
||||
let end_span = self.current_span();
|
||||
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>> {
|
||||
|
||||
@@ -112,7 +112,7 @@ fn test_function_invocation() -> Result<()> {
|
||||
#[test]
|
||||
fn test_priority_expression() -> Result<()> {
|
||||
let input = r#"
|
||||
let x = (4);
|
||||
let x = (4 + 3);
|
||||
"#;
|
||||
|
||||
let tokenizer = Tokenizer::from(input);
|
||||
@@ -120,7 +120,7 @@ fn test_priority_expression() -> Result<()> {
|
||||
|
||||
let expression = parser.parse()?.unwrap();
|
||||
|
||||
assert_eq!("(let x = 4)", expression.to_string());
|
||||
assert_eq!("(let x = ((4 + 3)))", expression.to_string());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -137,7 +137,7 @@ fn test_binary_expression() -> Result<()> {
|
||||
assert_eq!("(((45 * 2) - (15 / 5)) + (5 ** 2))", expr.to_string());
|
||||
|
||||
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(())
|
||||
}
|
||||
@@ -170,7 +170,7 @@ fn test_ternary_expression() -> Result<()> {
|
||||
fn test_complex_binary_with_ternary() -> Result<()> {
|
||||
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(())
|
||||
}
|
||||
@@ -191,3 +191,65 @@ fn test_nested_ternary_right_associativity() -> Result<()> {
|
||||
assert_eq!("(let i = (a ? b : (c ? d : e)))", expr.to_string());
|
||||
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)]
|
||||
pub struct IfExpression<'a> {
|
||||
pub condition: Box<Spanned<Expression<'a>>>,
|
||||
@@ -348,6 +384,9 @@ pub enum Expression<'a> {
|
||||
Return(Option<Box<Spanned<Expression<'a>>>>),
|
||||
Syscall(Spanned<SysCall<'a>>),
|
||||
Ternary(Spanned<TernaryExpression<'a>>),
|
||||
Tuple(Spanned<Vec<Spanned<Expression<'a>>>>),
|
||||
TupleAssignment(Spanned<TupleAssignmentExpression<'a>>),
|
||||
TupleDeclaration(Spanned<TupleDeclarationExpression<'a>>),
|
||||
Variable(Spanned<Cow<'a, str>>),
|
||||
While(Spanned<WhileExpression<'a>>),
|
||||
}
|
||||
@@ -384,8 +423,20 @@ impl<'a> std::fmt::Display for Expression<'a> {
|
||||
),
|
||||
Expression::Syscall(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::While(e) => write!(f, "{}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -115,7 +115,7 @@ macro_rules! keyword {
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Hash, Eq, Clone, Logos)]
|
||||
#[logos(skip r"[ \t\f]+")]
|
||||
#[logos(skip r"[ \r\t\f]+")]
|
||||
#[logos(extras = Extras)]
|
||||
#[logos(error(LexError, LexError::from_lexer))]
|
||||
pub enum TokenType<'a> {
|
||||
@@ -843,3 +843,20 @@ documented! {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::TokenType;
|
||||
use logos::Logos;
|
||||
|
||||
#[test]
|
||||
fn test_windows_crlf_endings() -> anyhow::Result<()> {
|
||||
let src = "let i = 0;\r\n";
|
||||
|
||||
let lexer = TokenType::lexer(src);
|
||||
|
||||
let tokens = lexer.collect::<Vec<_>>();
|
||||
|
||||
assert!(!tokens.iter().any(|res| res.is_err()));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user