Skip to content

Commit

Permalink
feat: accept ArraySort
Browse files Browse the repository at this point in the history
  • Loading branch information
jac3km4 committed Aug 3, 2024
1 parent 7df5069 commit 3bac4c2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions compiler/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub enum Diagnostic {
ref or wref, future versions of the compiler will reject this code"
)]
ClassWithNoIndirectionDeprecation(Ident, Span),
#[error("cannot sort this type of array, only arrays of primitives can be sorted")]
InvalidSortType(Span),
#[error("syntax error, expected {0}")]
SyntaxError(ExpectedSet, Span),
#[error("{0}")]
Expand Down Expand Up @@ -135,6 +137,7 @@ impl Diagnostic {
| Self::AddMethodConflict(span)
| Self::NonClassRefDeprecation(_, span)
| Self::ClassWithNoIndirectionDeprecation(_, span)
| Self::InvalidSortType(span)
| Self::CompileError(_, span)
| Self::SyntaxError(_, span)
| Self::CteError(_, span) => *span,
Expand All @@ -144,6 +147,7 @@ impl Diagnostic {
pub fn code(&self) -> &'static str {
match self {
Self::CompileError(cause, _) => cause.code(),
Self::InvalidUseOfTemporary(_) => "UNSUPPORTED",
_ => "OTHER",
}
}
Expand Down
7 changes: 7 additions & 0 deletions compiler/src/typechecker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,13 @@ impl<'a> TypeChecker<'a> {
checked_args.push(first_arg);
*elem
}
(Intrinsic::ArraySort, TypeId::Array(inner)) => {
if !matches!(*inner, TypeId::Prim(_)) {
self.diagnostics.push(Diagnostic::InvalidSortType(span));
}
checked_args.push(first_arg);
TypeId::Void
}
(Intrinsic::ToString, first_arg_type) => {
if first_arg_type == TypeId::Variant && first_arg.is_prvalue() {
self.diagnostics
Expand Down
19 changes: 19 additions & 0 deletions compiler/tests/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -998,3 +998,22 @@ fn compile_static_arrays() {
];
TestContext::compiled(vec![sources]).unwrap().run("Testing", check);
}

#[test]
fn compile_array_sort() {
let sources = r#"
func Testing() {
let a: [String];
ArraySort(a);
}
"#;

let check = check_code![
pat!(ArrayClear(_)),
mem!(Local(a)),
pat!(ArraySort(_)),
mem!(Local(a)),
pat!(Nop)
];
TestContext::compiled(vec![sources]).unwrap().run("Testing", check);
}
15 changes: 15 additions & 0 deletions compiler/tests/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,18 @@ fn fail_on_bad_variant_ops() {
errs
);
}

#[test]
fn fail_on_nonprim_sort() {
let sources = r#"
struct Dummy {}
func Testing() {
let arr: [Dummy];
ArraySort(arr);
}
"#;

let (_, errs) = compiled(vec![sources]).unwrap();
assert!(matches!(&errs[..], &[Diagnostic::InvalidSortType(_),]), "{:?}", errs);
}

0 comments on commit 3bac4c2

Please sign in to comment.