Skip to content

Commit

Permalink
Parse let assert
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Feb 13, 2023
1 parent 9e9834f commit 0c021a4
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
8 changes: 7 additions & 1 deletion compiler-core/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,13 @@ where

Some((start, Token::Let, _)) => {
let _ = self.next_tok();
self.parse_assignment(start, AssignmentKind::Let)?
let kind = if let Some((_, Token::Assert, _)) = self.tok0 {
_ = self.next_tok();
AssignmentKind::Assert
} else {
AssignmentKind::Let
};
self.parse_assignment(start, kind)?
}

Some((start, Token::Assert, _)) => {
Expand Down
1 change: 1 addition & 0 deletions compiler-core/src/type_/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::*;
use crate::ast::{UntypedExpr, UntypedModule};

mod assert;
mod errors;
mod functions;
mod imports;
Expand Down
71 changes: 71 additions & 0 deletions compiler-core/src/type_/tests/assert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use crate::assert_infer;

#[test]
fn empty_list() {
assert_infer!("assert [] = [] 1", "Int");
}

#[test]
fn list_one() {
assert_infer!("assert [a] = [1] a", "Int");
}

#[test]
fn list_two() {
assert_infer!("assert [a, 2] = [1] a", "Int");
}

#[test]
fn list_spread() {
assert_infer!("assert [a, ..] = [1] a", "Int");
}

#[test]
fn list_spread_discard() {
assert_infer!("assert [a, .._] = [1] a", "Int");
}

#[test]
fn list_spread_discard_comma_after() {
assert_infer!("assert [a, .._,] = [1] a", "Int");
}

#[test]
fn in_fn() {
assert_infer!("fn(x) { assert [a] = x a }", "fn(List(a)) -> a");
}

#[test]
fn in_fn_list_int() {
assert_infer!("fn(x) { assert [a] = x a + 1 }", "fn(List(Int)) -> Int");
}

#[test]
fn discard_named() {
assert_infer!("assert _x = 1 2.0", "Float");
}

#[test]
fn discard() {
assert_infer!("assert _ = 1 2.0", "Float");
}

#[test]
fn tuple() {
assert_infer!("assert #(tag, x) = #(1.0, 1) x", "Int");
}

#[test]
fn tuple_in_fn() {
assert_infer!("fn(x) { assert #(a, b) = x a }", "fn(#(a, b)) -> a");
}

#[test]
fn annotation() {
assert_infer!("assert 5: Int = 5 5", "Int");
}

#[test]
fn new_syntax() {
assert_infer!("let assert Ok(x) = Error(1)", "Result(a, Int)");
}

0 comments on commit 0c021a4

Please sign in to comment.