Merge pull request #79 from mist-go/syntax-patches

Syntax patches
This commit is contained in:
2026-06-16 13:55:48 +02:00
committed by GitHub
18 changed files with 181 additions and 141 deletions
+18 -4
View File
@@ -236,12 +236,26 @@ impl GetRust for TypeExpr {
mutable, mutable,
ty, ty,
} => { } => {
let mutable = if *mutable { "mut " } else { "" };
if let Some(lifetime) = lifetime { if let Some(lifetime) = lifetime {
format!("&'{} {mutable}{}", lifetime.get_rust(), ty.get_rust()) match lifetime {
Lifetime::Lifetime(v) => {
format!(
"&'{} {}{}",
v.get_rust(),
if *mutable { "mut " } else { "" },
ty.get_rust()
)
}
Lifetime::Unsafe => {
format!(
"*{} {}",
if *mutable { "mut" } else { "const" },
ty.get_rust()
)
}
}
} else { } else {
format!("&{mutable}{}", ty.get_rust()) format!("&{}{}", if *mutable { "mut " } else { "" }, ty.get_rust())
} }
} }
+7 -1
View File
@@ -17,10 +17,16 @@ pub struct Identifier(pub String);
#[derive(Debug, Clone, Serialize, Default)] #[derive(Debug, Clone, Serialize, Default)]
pub struct ParamList(pub Vec<VarDecl>); pub struct ParamList(pub Vec<VarDecl>);
#[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash)]
pub enum Lifetime {
Lifetime(Identifier),
Unsafe,
}
#[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash)]
pub enum TypeExpr { pub enum TypeExpr {
Ref { Ref {
lifetime: Option<Identifier>, lifetime: Option<Lifetime>,
mutable: bool, mutable: bool,
ty: Box<TypeExpr>, ty: Box<TypeExpr>,
}, },
+8 -5
View File
@@ -82,6 +82,7 @@ module_program = {
visibility = { "pub" ~ ("(" ~ static_path ~ ")")? } visibility = { "pub" ~ ("(" ~ static_path ~ ")")? }
mutable = { "mut" } mutable = { "mut" }
unsafe_kw = { "unsafe" }
// ====================================================== // ======================================================
// GENERICS // GENERICS
@@ -97,15 +98,17 @@ generics = { "<" ~ (generic ~ ("," ~ generic)*)? ~ ">" }
// ====================================================== // ======================================================
tuple_type = { tuple_type = {
"[" ~ (type_expr ~ ("," ~ type_expr)*)? ~ ","? ~ "]" "(" ~ (type_expr ~ ("," ~ type_expr)*)? ~ ","? ~ ")"
} }
path_type = { path_type = {
static_path ~ generics? static_path ~ generics?
} }
ref_lifetime = { lifetime | unsafe_kw }
ref_type = { ref_type = {
"*" ~ lifetime? ~ mutable? ~ type_expr "*" ~ ref_lifetime? ~ mutable? ~ type_expr
} }
dyn_type = { dyn_type = {
@@ -142,11 +145,11 @@ multi_pat_template = _{
} }
tuple_pattern = { tuple_pattern = {
"[" ~ (pattern | etc_pattern) ~ "," ~ multi_pat_template ~ "]" "(" ~ (pattern | etc_pattern) ~ "," ~ multi_pat_template ~ ")"
} }
named_tuple_pattern = { named_tuple_pattern = {
static_path ~ "[" ~ multi_pat_template ~ "]" static_path ~ "(" ~ multi_pat_template ~ ")"
} }
struct_destruct = { struct_destruct = {
@@ -330,7 +333,7 @@ trait_decl = {
override_kw = { "override" ~ ("(" ~ expr_path ~ ")")? } override_kw = { "override" ~ ("(" ~ expr_path ~ ")")? }
function_decl = { function_decl = {
visibility? ~ override_kw? ~ "fn" ~ identifier ~ generics_decl? ~ "(" ~ method_args ~ ")" ~ type_expr? ~ (block | semicolon) visibility? ~ override_kw? ~ "fn" ~ ("void" | type_expr) ~ identifier ~ generics_decl? ~ "(" ~ method_args ~ ")" ~ (block | semicolon)
} }
// ====================================================== // ======================================================
+2 -2
View File
@@ -28,8 +28,8 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExpr {
Rule::ref_type => { Rule::ref_type => {
ast_expr!(TypeExpr::Ref { ast_expr!(TypeExpr::Ref {
lifetime: consume_rule(&mut inner, Rule::lifetime) lifetime: consume_rule(&mut inner, Rule::ref_lifetime)
.map(|v| v.into_inner().next().map(Identifier::try_from)) .map(|v| v.into_inner().next().map(Lifetime::try_from))
.unwrap_or_default() .unwrap_or_default()
.transpose(), .transpose(),
mutable: Ok(listen_rule(&mut inner, Rule::mutable)) as AstResult<bool>, mutable: Ok(listen_rule(&mut inner, Rule::mutable)) as AstResult<bool>,
+22 -6
View File
@@ -2,7 +2,7 @@ use crate::{
Rule, Rule,
ast::*, ast::*,
ast_ensure, ast_expr, ast_ensure, ast_expr,
error::{AstError, AstResult, IntoErr}, error::{self, AstError, AstResult, IntoErr},
parser::{consume_rule, listen_rule}, parser::{consume_rule, listen_rule},
}; };
@@ -14,6 +14,11 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FunctionDecl {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
let visibility = Visibility::try_from(&mut inner); let visibility = Visibility::try_from(&mut inner);
let is_override = consume_rule(&mut inner, Rule::override_kw).map(Override::try_from).transpose(); let is_override = consume_rule(&mut inner, Rule::override_kw).map(Override::try_from).transpose();
let return_type = consume_rule(&mut inner, Rule::type_expr)
.map(TypeExpr::try_from)
.transpose();
let name = Identifier::try_from(inner.next().unwrap()); let name = Identifier::try_from(inner.next().unwrap());
let generics = consume_rule(&mut inner, Rule::generics_decl) let generics = consume_rule(&mut inner, Rule::generics_decl)
@@ -34,7 +39,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FunctionDecl {
type_: Some(if is_ref { type_: Some(if is_ref {
TypeExpr::Ref { TypeExpr::Ref {
lifetime: lifetime:
lifetime.map(|v| Identifier::try_from(v.into_inner().next().unwrap())) lifetime.map(|v| Lifetime::try_from(v.into_inner().next().unwrap()))
.transpose() .transpose()
.expect("Failed to get lifetime identifier"), .expect("Failed to get lifetime identifier"),
mutable, mutable,
@@ -59,10 +64,6 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FunctionDecl {
}) })
.unwrap_or_else(|| Ok(ParamList(self_param.into_iter().collect()))); .unwrap_or_else(|| Ok(ParamList(self_param.into_iter().collect())));
let return_type = consume_rule(&mut inner, Rule::type_expr)
.map(TypeExpr::try_from)
.transpose();
let body = inner.next().map(Block::try_from).transpose(); let body = inner.next().map(Block::try_from).transpose();
ast_expr!(Self { ast_expr!(Self {
@@ -87,3 +88,18 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Override {
}) })
} }
} }
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Lifetime {
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let mut inner = pair.clone().into_inner();
match pair.as_rule() {
Rule::ref_lifetime => inner.next().unwrap().try_into(),
Rule::lifetime => ast_expr!(Lifetime::Lifetime(inner.next().unwrap().try_into())),
Rule::unsafe_kw => Ok(Lifetime::Unsafe),
_ => error::AstError::bug_unimplemented(pair),
}
}
}
+14 -14
View File
@@ -1,11 +1,11 @@
pub module decl_test; pub module decl_test;
pub fn test_let_basic() { pub fn void test_let_basic() {
let x = 42; let x = 42;
assert_eq!(x, 42); assert_eq!(x, 42);
} }
pub fn test_let_typed() { pub fn void test_let_typed() {
let x i32 = 42; let x i32 = 42;
assert_eq!(x, 42); assert_eq!(x, 42);
@@ -19,57 +19,57 @@ pub fn test_let_typed() {
assert_eq!(f, 3.14); assert_eq!(f, 3.14);
} }
pub fn test_let_mut() { pub fn void test_let_mut() {
let mut _x = 42; let mut _x = 42;
_x = 100; _x = 100;
assert_eq!(_x, 100); assert_eq!(_x, 100);
} }
pub fn test_let_tuple_destructure() { pub fn void test_let_tuple_destructure() {
let [a, b] = (10, "hello"); let (a, b) = (10, "hello");
assert_eq!(a, 10); assert_eq!(a, 10);
assert_eq!(b, "hello"); assert_eq!(b, "hello");
} }
pub fn test_let_nested_tuple() { pub fn void test_let_nested_tuple() {
let [a, [b, c]] = (1, (2, 3)); let (a, (b, c)) = (1, (2, 3));
assert_eq!(a, 1); assert_eq!(a, 1);
assert_eq!(b, 2); assert_eq!(b, 2);
assert_eq!(c, 3); assert_eq!(c, 3);
} }
pub fn test_let_pattern_irrefutable() { pub fn void test_let_pattern_irrefutable() {
let x = 5; let x = 5;
let [y, z] = (1, 2); let (y, z) = (1, 2);
assert_eq!(x, 5); assert_eq!(x, 5);
assert_eq!(y, 1); assert_eq!(y, 1);
assert_eq!(z, 2); assert_eq!(z, 2);
} }
pub fn test_multiple_decls() { pub fn void test_multiple_decls() {
let a = 1; let a = 1;
let b = 2; let b = 2;
let c = 3; let c = 3;
assert_eq!(a + b + c, 6); assert_eq!(a + b + c, 6);
} }
pub fn test_let_with_type_and_init() { pub fn void test_let_with_type_and_init() {
let x f64 = 10.0; let x f64 = 10.0;
assert_eq!(x, 10.0); assert_eq!(x, 10.0);
} }
pub fn test_string_literal() { pub fn void test_string_literal() {
let s = "hello, world!"; let s = "hello, world!";
assert_eq!(s.len(), 13); assert_eq!(s.len(), 13);
} }
pub fn test_let_bool() { pub fn void test_let_bool() {
let t = true; let t = true;
let f = false; let f = false;
assert_eq!(t, !f); assert_eq!(t, !f);
} }
pub fn run_tests() { pub fn void run_tests() {
test_let_basic(); test_let_basic();
test_let_typed(); test_let_typed();
test_let_mut(); test_let_mut();
+18 -18
View File
@@ -1,6 +1,6 @@
pub module expr_test; pub module expr_test;
pub fn test_literals() { pub fn void test_literals() {
assert_eq!(42, 42); assert_eq!(42, 42);
assert_eq!(3.14, 3.14); assert_eq!(3.14, 3.14);
assert_eq!(true, true); assert_eq!(true, true);
@@ -8,28 +8,28 @@ pub fn test_literals() {
assert_eq!("hello", "hello"); assert_eq!("hello", "hello");
} }
pub fn test_tuples() { pub fn void test_tuples() {
let t = (1, true, "three"); let t = (1, true, "three");
assert_eq!(t.0, 1); assert_eq!(t.0, 1);
assert_eq!(t.1, true); assert_eq!(t.1, true);
assert_eq!(t.2, "three"); assert_eq!(t.2, "three");
} }
pub fn test_arrays() { pub fn void test_arrays() {
let arr = [1, 2, 3]; let arr = [1, 2, 3];
assert_eq!(arr[0], 1); assert_eq!(arr[0], 1);
assert_eq!(arr[1], 2); assert_eq!(arr[1], 2);
assert_eq!(arr[2], 3); assert_eq!(arr[2], 3);
} }
pub fn test_array_repeat() { pub fn void test_array_repeat() {
let arr = [0; 5]; let arr = [0; 5];
assert_eq!(arr[0], 0); assert_eq!(arr[0], 0);
assert_eq!(arr[4], 0); assert_eq!(arr[4], 0);
assert_eq!(arr.len(), 5); assert_eq!(arr.len(), 5);
} }
pub fn test_binary_arith() { pub fn void test_binary_arith() {
assert_eq!(10 + 5, 15); assert_eq!(10 + 5, 15);
assert_eq!(10 - 5, 5); assert_eq!(10 - 5, 5);
assert_eq!(10 * 5, 50); assert_eq!(10 * 5, 50);
@@ -37,7 +37,7 @@ pub fn test_binary_arith() {
assert_eq!(10 % 3, 1); assert_eq!(10 % 3, 1);
} }
pub fn test_binary_cmp() { pub fn void test_binary_cmp() {
assert_eq!(5 < 10, true); assert_eq!(5 < 10, true);
assert_eq!(10 <= 10, true); assert_eq!(10 <= 10, true);
assert_eq!(10 > 5, true); assert_eq!(10 > 5, true);
@@ -46,14 +46,14 @@ pub fn test_binary_cmp() {
assert_eq!(5 != 10, true); assert_eq!(5 != 10, true);
} }
pub fn test_binary_logic() { pub fn void test_binary_logic() {
assert_eq!(true && true, true); assert_eq!(true && true, true);
assert_eq!(true && false, false); assert_eq!(true && false, false);
assert_eq!(true || false, true); assert_eq!(true || false, true);
assert_eq!(false || false, false); assert_eq!(false || false, false);
} }
pub fn test_binary_bit() { pub fn void test_binary_bit() {
assert_eq!(5 & 3, 1); assert_eq!(5 & 3, 1);
assert_eq!(5 | 3, 7); assert_eq!(5 | 3, 7);
assert_eq!(5 ^ 3, 6); assert_eq!(5 ^ 3, 6);
@@ -61,20 +61,20 @@ pub fn test_binary_bit() {
assert_eq!(4 >> 1, 2); assert_eq!(4 >> 1, 2);
} }
pub fn test_prefix() { pub fn void test_prefix() {
let x = 42; let x = 42;
assert_eq!(-x, -42); assert_eq!(-x, -42);
assert_eq!(!true, false); assert_eq!(!true, false);
assert_eq!(!false, true); assert_eq!(!false, true);
} }
pub fn test_ref_deref() { pub fn void test_ref_deref() {
let x = 42; let x = 42;
let r = &x; let r = &x;
assert_eq!(*r, 42); assert_eq!(*r, 42);
} }
pub fn test_postfix_inc_dec() { pub fn void test_postfix_inc_dec() {
let mut x = 5; let mut x = 5;
x++; x++;
assert_eq!(x, 6); assert_eq!(x, 6);
@@ -82,17 +82,17 @@ pub fn test_postfix_inc_dec() {
assert_eq!(x, 5); assert_eq!(x, 5);
} }
pub fn test_as_cast() { pub fn void test_as_cast() {
let x = 42; let x = 42;
assert_eq!(x as f64, 42.0); assert_eq!(x as f64, 42.0);
} }
pub fn test_closure() { pub fn void test_closure() {
let add = fn(a, b) -> a + b; let add = fn(a, b) -> a + b;
assert_eq!(add(2, 3), 5); assert_eq!(add(2, 3), 5);
} }
pub fn test_block_expr() { pub fn void test_block_expr() {
let x = { let x = {
let y = 10; let y = 10;
y + 5 y + 5
@@ -100,7 +100,7 @@ pub fn test_block_expr() {
assert_eq!(x, 15); assert_eq!(x, 15);
} }
pub fn test_if_expr() { pub fn void test_if_expr() {
let x = if true { 1 } else { 2 }; let x = if true { 1 } else { 2 };
assert_eq!(x, 1); assert_eq!(x, 1);
@@ -108,7 +108,7 @@ pub fn test_if_expr() {
assert_eq!(y, 2); assert_eq!(y, 2);
} }
pub fn test_range_expr() { pub fn void test_range_expr() {
let r = 0..5; let r = 0..5;
let mut count = 0; let mut count = 0;
for i in r { for i in r {
@@ -117,7 +117,7 @@ pub fn test_range_expr() {
assert_eq!(count, 5); assert_eq!(count, 5);
} }
pub fn test_binary_assign() { pub fn void test_binary_assign() {
let mut x = 10; let mut x = 10;
x += 5; x += 5;
assert_eq!(x, 15); assert_eq!(x, 15);
@@ -131,7 +131,7 @@ pub fn test_binary_assign() {
assert_eq!(x, 2); assert_eq!(x, 2);
} }
pub fn run_tests() { pub fn void run_tests() {
test_literals(); test_literals();
test_tuples(); test_tuples();
test_arrays(); test_arrays();
+1 -1
View File
@@ -1,3 +1,3 @@
pub module common; pub module common;
pub fn test_package() {} pub fn void test_package() {}
+17 -17
View File
@@ -1,6 +1,6 @@
pub module statement_test; pub module statement_test;
pub fn test_var_decl() { pub fn void test_var_decl() {
let x = 42; let x = 42;
assert_eq!(x, 42); assert_eq!(x, 42);
@@ -11,13 +11,13 @@ pub fn test_var_decl() {
assert_eq!(name, "mist"); assert_eq!(name, "mist");
} }
pub fn test_var_decl_with_init() { pub fn void test_var_decl_with_init() {
let mut x = 0; let mut x = 0;
x = 5; x = 5;
assert_eq!(x, 5); assert_eq!(x, 5);
} }
pub fn test_if_statement() { pub fn void test_if_statement() {
let mut x = 0; let mut x = 0;
if true { if true {
@@ -33,7 +33,7 @@ pub fn test_if_statement() {
assert_eq!(x, 3); assert_eq!(x, 3);
} }
pub fn test_if_else_if() { pub fn void test_if_else_if() {
let mut x = 0; let mut x = 0;
if x == 1 { if x == 1 {
@@ -46,7 +46,7 @@ pub fn test_if_else_if() {
assert_eq!(x, 20); assert_eq!(x, 20);
} }
pub fn test_while_loop() { pub fn void test_while_loop() {
let mut count = 0; let mut count = 0;
while count < 5 { while count < 5 {
@@ -55,7 +55,7 @@ pub fn test_while_loop() {
assert_eq!(count, 5); assert_eq!(count, 5);
} }
pub fn test_for_loop() { pub fn void test_for_loop() {
let mut sum = 0; let mut sum = 0;
for i in 0..4 { for i in 0..4 {
@@ -64,7 +64,7 @@ pub fn test_for_loop() {
assert_eq!(sum, 6); assert_eq!(sum, 6);
} }
pub fn test_loop_stmt() { pub fn void test_loop_stmt() {
let mut count = 0; let mut count = 0;
loop { loop {
@@ -76,7 +76,7 @@ pub fn test_loop_stmt() {
assert_eq!(count, 3); assert_eq!(count, 3);
} }
pub fn test_break_continue() { pub fn void test_break_continue() {
let mut sum = 0; let mut sum = 0;
for i in 0..10 { for i in 0..10 {
@@ -91,11 +91,11 @@ pub fn test_break_continue() {
assert_eq!(sum, 1 + 3 + 5); assert_eq!(sum, 1 + 3 + 5);
} }
pub fn test_return_from_fn() i32 { pub fn i32 test_return() {
return 42; return 42;
} }
pub fn test_block_scope() { pub fn void test_block_scope() {
let x = { let x = {
let inner = 10; let inner = 10;
inner + 5 inner + 5
@@ -103,7 +103,7 @@ pub fn test_block_scope() {
assert_eq!(x, 15); assert_eq!(x, 15);
} }
pub fn test_match_literal() { pub fn void test_match_literal() {
let x = 2; let x = 2;
let result; let result;
@@ -116,7 +116,7 @@ pub fn test_match_literal() {
assert_eq!(result, 20); assert_eq!(result, 20);
} }
pub fn test_match_or_pattern() { pub fn void test_match_or_pattern() {
let x = 1; let x = 1;
let result i32; let result i32;
@@ -128,7 +128,7 @@ pub fn test_match_or_pattern() {
assert_eq!(result, 100); assert_eq!(result, 100);
} }
pub fn test_match_default() { pub fn void test_match_default() {
let x = 99; let x = 99;
let result; let result;
@@ -139,17 +139,17 @@ pub fn test_match_default() {
assert_eq!(result, 99); assert_eq!(result, 99);
} }
pub fn test_for_with_pattern_destructure() { pub fn void test_for_with_pattern_destructure() {
let pairs = [(1, "a"), (2, "b")]; let pairs = [(1, "a"), (2, "b")];
let mut keys = 0; let mut keys = 0;
for [k, _] in pairs { for (k, _) in pairs {
keys += k; keys += k;
} }
assert_eq!(keys, 3); assert_eq!(keys, 3);
} }
pub fn run_tests() { pub fn void run_tests() {
test_var_decl(); test_var_decl();
test_var_decl_with_init(); test_var_decl_with_init();
test_if_statement(); test_if_statement();
@@ -158,7 +158,7 @@ pub fn run_tests() {
test_for_loop(); test_for_loop();
test_loop_stmt(); test_loop_stmt();
test_break_continue(); test_break_continue();
assert_eq!(test_return_from_fn(), 42); assert_eq!(test_return(), 42);
test_block_scope(); test_block_scope();
test_match_literal(); test_match_literal();
test_match_or_pattern(); test_match_or_pattern();
+12 -12
View File
@@ -1,16 +1,16 @@
pub module types_test; pub module types_test;
pub fn test_integer_type() { pub fn void test_integer_type() {
let x i32 = 42; let x i32 = 42;
assert_eq!(x, 42); assert_eq!(x, 42);
} }
pub fn test_float_type() { pub fn void test_float_type() {
let x f64 = 3.14; let x f64 = 3.14;
assert_eq!(x, 3.14); assert_eq!(x, 3.14);
} }
pub fn test_bool_type() { pub fn void test_bool_type() {
let x bool = true; let x bool = true;
assert_eq!(x, true); assert_eq!(x, true);
@@ -18,41 +18,41 @@ pub fn test_bool_type() {
assert_eq!(y, false); assert_eq!(y, false);
} }
pub fn test_string_type() { pub fn void test_string_type() {
let s *str = "hello"; let s *str = "hello";
assert_eq!(s, "hello"); assert_eq!(s, "hello");
} }
pub fn test_tuple_type() { pub fn void test_tuple_type() {
let t [i32, *str] = (42, "answer"); let t (i32, *str) = (42, "answer");
assert_eq!(t.0, 42); assert_eq!(t.0, 42);
assert_eq!(t.1, "answer"); assert_eq!(t.1, "answer");
} }
pub fn test_ref_type() { pub fn void test_ref_type() {
let x i32 = 42; let x i32 = 42;
let r *i32 = &x; let r *i32 = &x;
assert_eq!(*r, 42); assert_eq!(*r, 42);
} }
pub fn test_mut_ref_type() { pub fn void test_mut_ref_type() {
let mut x = 42; let mut x = 42;
let r *mut i32 = &mut x; let r *mut i32 = &mut x;
*r = 100; *r = 100;
assert_eq!(x, 100); assert_eq!(x, 100);
} }
pub fn test_void_type() {} pub fn void test_void_type() {}
fn add(a i32, b i32) i32 { fn i32 add(a i32, b i32) {
a + b a + b
} }
pub fn test_fn_return_type() { pub fn void test_fn_return_type() {
assert_eq!(add(3, 4), 7); assert_eq!(add(3, 4), 7);
} }
pub fn run_tests() { pub fn void run_tests() {
test_integer_type(); test_integer_type();
test_float_type(); test_float_type();
test_bool_type(); test_bool_type();
+3 -3
View File
@@ -5,19 +5,19 @@ pub struct Debuggable {
value i32, value i32,
} }
pub fn test_derive_attribute() { pub fn void test_derive_attribute() {
let d = Debuggable { value: 42 }; let d = Debuggable { value: 42 };
assert_eq!(d.value, 42); assert_eq!(d.value, 42);
} }
#[allow(unused_variables)] #[allow(unused_variables)]
pub fn test_allow_attribute() { pub fn void test_allow_attribute() {
let x = 42; let x = 42;
let y = "unused"; let y = "unused";
assert_eq!(x, 42); assert_eq!(x, 42);
} }
pub fn run_tests() { pub fn void run_tests() {
test_derive_attribute(); test_derive_attribute();
test_allow_attribute(); test_allow_attribute();
} }
+16 -15
View File
@@ -8,15 +8,15 @@ pub class Animal<T> {
self.name = "Rex".to_string(); self.name = "Rex".to_string();
} }
pub fn speak(*self) { pub fn String speak(*self) {
println!("Unknown"); "Unknown".to_string()
} }
pub fn test_speak(*self) { pub fn void test_speak(*self) {
self.speak(); assert_eq!(self.speak(), "Woof!");
} }
pub fn static_method() {} pub fn void static_method() {}
} }
pub class Mammal<T> : Animal<T> { pub class Mammal<T> : Animal<T> {
@@ -24,10 +24,9 @@ pub class Mammal<T> : Animal<T> {
super = Super::new(); super = Super::new();
} }
pub fn args_test(*self, x i32, y *str) {} pub fn void args_test(*self, x i32, y *str) {
assert_eq!(x, 10);
pub fn returning<'a>(*self) *'a str { assert_eq!(y, "hello");
"Mammal"
} }
} }
@@ -36,21 +35,23 @@ pub class Dog : Mammal<i32> {
super = Super::new(); super = Super::new();
} }
pub override(Animal<i32>) fn speak(*self) { pub override(Animal<i32>) fn String speak(*self) {
println!("Woof!"); "Woof!".to_string()
} }
pub override fn returning(*self) *str { impl std::fmt::Display {
"Dog" fn Result<(), std::fmt::Error> fmt(*self, f *mut std::fmt::Formatter<'_>) {
write!(f, "🐾 {}", self.name)
}
} }
} }
pub fn run_tests() { pub fn void run_tests() {
let dawg = Dog::new(); let dawg = Dog::new();
dawg.args_test(10, "hello"); dawg.args_test(10, "hello");
dawg.test_speak(); dawg.test_speak();
println!("{}", dawg.returning()); assert_eq!(format!("{dawg}"), "🐾 Rex");
} }
+8 -8
View File
@@ -2,22 +2,22 @@ pub module enum_test;
enum OptionInt { enum OptionInt {
None, None,
Some[i32], Some(i32),
} }
pub fn test_enum_named() { pub fn void test_enum_named() {
let x = OptionInt::None; let x = OptionInt::None;
match x { match x {
OptionInt::None => { assert_eq!(1, 1); } OptionInt::None => { assert_eq!(1, 1); }
OptionInt::Some[_] => { assert_eq!(0, 1); } OptionInt::Some(_) => { assert_eq!(0, 1); }
} }
} }
pub fn test_enum_tuple() { pub fn void test_enum_tuple() {
let x = OptionInt::Some(42); let x = OptionInt::Some(42);
match x { match x {
OptionInt::None => { assert_eq!(0, 1); } OptionInt::None => { assert_eq!(0, 1); }
OptionInt::Some[v] => { assert_eq!(v, 42); } OptionInt::Some(v) => { assert_eq!(v, 42); }
} }
} }
@@ -26,7 +26,7 @@ enum Shape {
Rect { w i32, h i32 }, Rect { w i32, h i32 },
} }
pub fn test_enum_struct() { pub fn void test_enum_struct() {
let c = Shape::Circle { radius: 5 }; let c = Shape::Circle { radius: 5 };
match c { match c {
Shape::Circle { radius } => { assert_eq!(radius, 5); } Shape::Circle { radius } => { assert_eq!(radius, 5); }
@@ -46,7 +46,7 @@ enum Color {
Blue, Blue,
} }
pub fn test_enum_eq() { pub fn void test_enum_eq() {
let c = Color::Red; let c = Color::Red;
match c { match c {
Color::Red => { assert_eq!(1, 1); } Color::Red => { assert_eq!(1, 1); }
@@ -54,7 +54,7 @@ pub fn test_enum_eq() {
} }
} }
pub fn run_tests() { pub fn void run_tests() {
test_enum_named(); test_enum_named();
test_enum_tuple(); test_enum_tuple();
test_enum_struct(); test_enum_struct();
+13 -13
View File
@@ -1,60 +1,60 @@
pub module function_test; pub module function_test;
fn greet() { fn void greet() {
let mut x = 1; let mut x = 1;
x++; x++;
} }
pub fn test_basic_function() { pub fn void test_basic_function() {
greet(); greet();
} }
fn add(a i32, b i32) i32 { fn i32 add(a i32, b i32) {
a + b a + b
} }
pub fn test_fn_params() { pub fn void test_fn_params() {
assert_eq!(add(2, 3), 5); assert_eq!(add(2, 3), 5);
} }
pub fn test_fn_return() i32 { pub fn i32 test_fn_return() {
42 42
} }
pub fn test_fn_early_return() i32 { pub fn i32 test_fn_early_return() {
return 100; return 100;
0 0
} }
pub fn test_fn_mut_param(mut x i32) i32 { pub fn i32 test_fn_mut_param(mut x i32) {
x = x + 10; x = x + 10;
x x
} }
pub fn test_method_call() { pub fn void test_method_call() {
let s = "hello"; let s = "hello";
assert_eq!(s.len(), 5); assert_eq!(s.len(), 5);
assert_eq!(s.to_uppercase(), "HELLO"); assert_eq!(s.to_uppercase(), "HELLO");
} }
fn triple(x i32) i32 { fn i32 triple(x i32) {
add(x, add(x, x)) add(x, add(x, x))
} }
pub fn test_nested_fn_calls() { pub fn void test_nested_fn_calls() {
assert_eq!(triple(5), 15); assert_eq!(triple(5), 15);
} }
fn factorial(n i32) i32 { fn i32 factorial(n i32) {
if n <= 1 { 1 } if n <= 1 { 1 }
else { n * factorial(n - 1) } else { n * factorial(n - 1) }
} }
pub fn test_recursive_fn() { pub fn void test_recursive_fn() {
assert_eq!(factorial(5), 120); assert_eq!(factorial(5), 120);
} }
pub fn run_tests() { pub fn void run_tests() {
test_basic_function(); test_basic_function();
test_fn_params(); test_fn_params();
assert_eq!(test_fn_return(), 42); assert_eq!(test_fn_return(), 42);
+8 -8
View File
@@ -6,34 +6,34 @@ struct Vector2 {
} }
impl Vector2 { impl Vector2 {
pub fn magnitude(*self) i32 { pub fn i32 magnitude(*self) {
self.x * self.x + self.y * self.y self.x * self.x + self.y * self.y
} }
} }
pub fn test_inherent_impl() { pub fn void test_inherent_impl() {
let v = Vector2 { x: 3, y: 4 }; let v = Vector2 { x: 3, y: 4 };
assert_eq!(v.magnitude(), 25); assert_eq!(v.magnitude(), 25);
} }
trait Display { trait Display {
fn display(*self) String; fn String display(*self);
} }
impl Display for Vector2 { impl Display for Vector2 {
fn display(*self) String { fn String display(*self) {
format!("({}, {})", self.x, self.y) format!("({}, {})", self.x, self.y)
} }
} }
pub fn test_trait_impl_block() { pub fn void test_trait_impl_block() {
let v = Vector2 { x: 1, y: 2 }; let v = Vector2 { x: 1, y: 2 };
let s = v.display(); let s = v.display();
assert_eq!(s.len() > 0, true); assert_eq!(s.len() > 0, true);
} }
impl Vector2 { impl Vector2 {
pub fn add(*self, other Vector2) Vector2 { pub fn Vector2 add(*self, other Vector2) {
Vector2 { Vector2 {
x: self.x + other.x, x: self.x + other.x,
y: self.y + other.y, y: self.y + other.y,
@@ -41,7 +41,7 @@ impl Vector2 {
} }
} }
pub fn test_multiple_impls() { pub fn void test_multiple_impls() {
let a = Vector2 { x: 1, y: 2 }; let a = Vector2 { x: 1, y: 2 };
let b = Vector2 { x: 3, y: 4 }; let b = Vector2 { x: 3, y: 4 };
let c = a.add(b); let c = a.add(b);
@@ -49,7 +49,7 @@ pub fn test_multiple_impls() {
assert_eq!(c.y, 6); assert_eq!(c.y, 6);
} }
pub fn run_tests() { pub fn void run_tests() {
test_inherent_impl(); test_inherent_impl();
test_trait_impl_block(); test_trait_impl_block();
test_multiple_impls(); test_multiple_impls();
+5 -5
View File
@@ -5,19 +5,19 @@ struct Point {
y i32, y i32,
} }
pub fn test_struct_create_access() { pub fn void test_struct_create_access() {
let p = Point { x: 10, y: 20 }; let p = Point { x: 10, y: 20 };
assert_eq!(p.x, 10); assert_eq!(p.x, 10);
assert_eq!(p.y, 20); assert_eq!(p.y, 20);
} }
pub fn test_struct_mut_field() { pub fn void test_struct_mut_field() {
let mut p = Point { x: 1, y: 2 }; let mut p = Point { x: 1, y: 2 };
p.x = 100; p.x = 100;
assert_eq!(p.x, 100); assert_eq!(p.x, 100);
} }
pub fn test_struct_destructure() { pub fn void test_struct_destructure() {
let p = Point { x: 3, y: 4 }; let p = Point { x: 3, y: 4 };
let Point { x, y } = p; let Point { x, y } = p;
assert_eq!(x, 3); assert_eq!(x, 3);
@@ -29,7 +29,7 @@ struct Line {
end Point, end Point,
} }
pub fn test_struct_nested() { pub fn void test_struct_nested() {
let l = Line { let l = Line {
start: Point { x: 0, y: 0 }, start: Point { x: 0, y: 0 },
end: Point { x: 5, y: 10 }, end: Point { x: 5, y: 10 },
@@ -38,7 +38,7 @@ pub fn test_struct_nested() {
assert_eq!(l.end.y, 10); assert_eq!(l.end.y, 10);
} }
pub fn run_tests() { pub fn void run_tests() {
test_struct_create_access(); test_struct_create_access();
test_struct_mut_field(); test_struct_mut_field();
test_struct_destructure(); test_struct_destructure();
+7 -7
View File
@@ -1,11 +1,11 @@
pub module trait_test; pub module trait_test;
trait Speak { trait Speak {
fn speak(*self) String; fn String speak(*self);
} }
trait Greet : Speak { trait Greet : Speak {
fn greet(*self) String; fn String greet(*self);
} }
struct Dog { struct Dog {
@@ -13,29 +13,29 @@ struct Dog {
} }
impl Speak for Dog { impl Speak for Dog {
fn speak(*self) String { fn String speak(*self) {
"woof".to_string() "woof".to_string()
} }
} }
impl Greet for Dog { impl Greet for Dog {
fn greet(*self) String { fn String greet(*self) {
"hello".to_string() "hello".to_string()
} }
} }
pub fn test_trait_impl() { pub fn void test_trait_impl() {
let d = Dog { name: "Rex".to_string() }; let d = Dog { name: "Rex".to_string() };
assert_eq!(d.speak(), "woof"); assert_eq!(d.speak(), "woof");
} }
pub fn test_trait_with_requirement() { pub fn void test_trait_with_requirement() {
let d = Dog { name: "Buddy".to_string() }; let d = Dog { name: "Buddy".to_string() };
assert_eq!(d.speak(), "woof"); assert_eq!(d.speak(), "woof");
assert_eq!(d.greet(), "hello"); assert_eq!(d.greet(), "hello");
} }
pub fn run_tests() { pub fn void run_tests() {
test_trait_impl(); test_trait_impl();
test_trait_with_requirement(); test_trait_with_requirement();
} }
+1 -1
View File
@@ -1,4 +1,4 @@
fn main() { fn void main() {
println!("running common tests..."); println!("running common tests...");
common::test_package(); common::test_package();
common::expr_test::run_tests(); common::expr_test::run_tests();