diff --git a/crates/codegen/src/lib.rs b/crates/codegen/src/lib.rs index a8f4433..168b0af 100644 --- a/crates/codegen/src/lib.rs +++ b/crates/codegen/src/lib.rs @@ -156,18 +156,6 @@ impl GetRust for Path { } } -impl GetRust for TypePostfix { - fn get_rust(&self) -> String { - match self { - TypePostfix::Ref => format!("&"), - TypePostfix::RefMut => format!("&mut "), - TypePostfix::RefLifetime(lifetime) => format!("&'{} ", lifetime.get_rust()), - TypePostfix::RefMutLifetime(lifetime) => format!("&'{} mut ", lifetime.get_rust()), - TypePostfix::Dyn => format!("dyn "), - } - } -} - impl GetRust for Visibility { fn get_rust(&self) -> String { match self { @@ -186,12 +174,47 @@ impl GetRust for Identifier { impl GetRust for TypeExpr { fn get_rust(&self) -> String { - self.1 - .iter() - .map(TypePostfix::get_rust) - .rev() - .collect::() - + &self.0.get_rust() + match self { + // TypePostfix::Ref => format!("&"), + // TypePostfix::RefMut => format!("&mut "), + // TypePostfix::RefLifetime(lifetime) => format!("&'{} ", lifetime.get_rust()), + // TypePostfix::RefMutLifetime(lifetime) => format!("&'{} mut ", lifetime.get_rust()), + // TypePostfix::Dyn => format!("dyn "), + Self::Path(path, generics) => { + if let Some(generics) = generics { + format!("{}{}", get_static_type_path(path), generics.get_rust()) + } else { + get_static_type_path(path) + } + } + Self::Lifetime(name) => format!("'{}", name.get_rust()), + Self::Tuple(types) => format!( + "({})", + types + .into_iter() + .map(|t| t.get_rust()) + .collect::>() + .join(", ") + ), + + Self::Ref { + lifetime, + mutable, + ty, + } => { + let mutable = if *mutable { "mut " } else { "" }; + + if let Some(lifetime) = lifetime { + format!("&'{} {mutable}{}", lifetime.get_rust(), ty.get_rust()) + } else { + format!("&{mutable}{}", ty.get_rust()) + } + } + + Self::Dyn(ty) => { + format!("dyn {}", ty.get_rust()) + } + } } } @@ -205,29 +228,6 @@ pub fn get_static_type_path(path: &Path) -> String { } } -impl GetRust for TypeExprKind { - fn get_rust(&self) -> String { - match self { - TypeExprKind::Path(path, generics) => { - if let Some(generics) = generics { - format!("{}{}", get_static_type_path(path), generics.get_rust()) - } else { - get_static_type_path(path) - } - } - TypeExprKind::Lifetime(name) => format!("'{}", name.get_rust()), - TypeExprKind::Tuple(types) => format!( - "({})", - types - .into_iter() - .map(|t| t.get_rust()) - .collect::>() - .join(", ") - ), - } - } -} - impl GenRust for Pattern { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { match self { diff --git a/crates/codegen/src/top_level.rs b/crates/codegen/src/top_level.rs index bc4ef66..94073c5 100644 --- a/crates/codegen/src/top_level.rs +++ b/crates/codegen/src/top_level.rs @@ -88,8 +88,11 @@ impl GenRust for FunctionDecl { param.gen_rust(ctx, cg); } - cg.add(") -> "); - cg.add(&self.return_type.get_rust()); + cg.add(") "); + if let Some(return_type) = &self.return_type { + cg.add("-> "); + cg.add(&return_type.get_rust()); + } if let Some(body) = &self.body { cg.add(" "); @@ -234,10 +237,14 @@ impl GenRust for (&Vec>, &Spanned) { let mut constructor_params = vec![VarDecl { name: Pattern::Path(false, Path(vec![Identifier(String::from("self"))])), - type_: Some(TypeExpr( - TypeExprKind::Path(Path(vec![Identifier(String::from("Self"))]), None), - vec![TypePostfix::RefMut], - )), + type_: Some(TypeExpr::Ref { + lifetime: None, + mutable: true, + ty: Box::new(TypeExpr::Path( + Path(vec![Identifier(String::from("Self"))]), + None, + )), + }), }]; constructor_params.append(&mut self.1.item.params.0.clone()); @@ -250,7 +257,7 @@ impl GenRust for (&Vec>, &Spanned) { name: Identifier(String::from("constructor")), generics: self.1.item.generics.clone(), params: ParamList(constructor_params), - return_type: TypeExpr::no_px(TypeExprKind::Tuple(Vec::new())), + return_type: Some(TypeExpr::Tuple(Vec::new())), body: Some(self.1.item.body.clone()), }, } @@ -423,10 +430,7 @@ impl GenRust for TopLevelKind { let mut impl_ = impl_.clone(); impl_.item.trait_ = Some(impl_.item.target); - impl_.item.target = TypeExpr( - TypeExprKind::Path(Path(vec![name.clone()]), None), - Vec::new(), - ); + impl_.item.target = TypeExpr::Path(Path(vec![name.clone()]), None); impl_.gen_rust(ctx, cg); } diff --git a/crates/parser/src/ast/mod.rs b/crates/parser/src/ast/mod.rs index 23e9ce6..6a975df 100644 --- a/crates/parser/src/ast/mod.rs +++ b/crates/parser/src/ast/mod.rs @@ -18,19 +18,13 @@ pub struct Identifier(pub String); pub struct ParamList(pub Vec); #[derive(Debug, Clone, Serialize)] -pub struct TypeExpr(pub TypeExprKind, pub Vec); - -#[derive(Debug, Clone, Serialize)] -pub enum TypePostfix { - Ref, - RefMut, - RefLifetime(Identifier), - RefMutLifetime(Identifier), - Dyn, -} - -#[derive(Debug, Clone, Serialize)] -pub enum TypeExprKind { +pub enum TypeExpr { + Ref { + lifetime: Option, + mutable: bool, + ty: Box, + }, + Dyn(Box), Path(Path, Option), Tuple(Vec), Lifetime(Identifier), @@ -43,20 +37,11 @@ pub struct Spanned { pub item: T, } -impl TypeExpr { - pub fn no_px(kind: TypeExprKind) -> Self { - Self(kind, Vec::new()) - } -} - impl From for Generic { fn from(value: GenericDecl) -> Self { match value { GenericDecl::Lifetime(life) => Generic::Lifetime(life), - GenericDecl::Type(ty, _) => Generic::Type(TypeExpr( - TypeExprKind::Path(Path(vec![ty]), None), - Vec::new(), - )), + GenericDecl::Type(ty, _) => Generic::Type(TypeExpr::Path(Path(vec![ty]), None)), } } } diff --git a/crates/parser/src/ast/top_level.rs b/crates/parser/src/ast/top_level.rs index f87d011..dd29e9b 100644 --- a/crates/parser/src/ast/top_level.rs +++ b/crates/parser/src/ast/top_level.rs @@ -97,7 +97,7 @@ pub struct FunctionDecl { pub name: Identifier, pub generics: GenericsDecl, pub params: ParamList, - pub return_type: TypeExpr, + pub return_type: Option, pub body: Option, } diff --git a/crates/parser/src/grammar.pest b/crates/parser/src/grammar.pest index 7eba693..e4f33fb 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -31,6 +31,7 @@ keyword = { | "dyn" | "loop" | "fn" + | "unsafe" } identifier = @{ @@ -76,7 +77,6 @@ program = { visibility = { "pub" ~ ("(" ~ static_path ~ ")")? } mutable = { "mut" } -let_kw = { "let" } // ====================================================== // GENERICS @@ -100,15 +100,15 @@ path_type = { } ref_type = { - mutable? ~ lifetime? ~ "*" + "*" ~ lifetime? ~ mutable? ~ type_expr } dyn_type = { - "dyn" + "dyn" ~ type_expr } type_expr = { - (path_type | tuple_type) ~ (ref_type | dyn_type)* + (path_type | tuple_type | ref_type | dyn_type) } lifetime = { @@ -192,11 +192,15 @@ attributes = { // DECLARATION HELPERS // ====================================================== var_decl = { - (let_kw | type_expr) ~ pattern + "let" ~ pattern ~ type_expr? } field = { - visibility? ~ type_expr ~ identifier + visibility? ~ identifier ~ type_expr +} + +param = { + pattern ~ type_expr? } field_list = { @@ -204,7 +208,7 @@ field_list = { } param_list = { - (var_decl ~ ("," ~ var_decl)* ~ ","?)? + (param ~ ("," ~ param)* ~ ","?)? } // ====================================================== @@ -274,7 +278,7 @@ class_field = { } self_param = { - "self" ~ mutable? ~ deref_px? + (deref_px ~ lifetime?)? ~ mutable? ~ "self" } method_args = _{ @@ -319,7 +323,7 @@ trait_decl = { // ====================================================== function_decl = { - visibility? ~ type_expr ~ identifier ~ generics_decl? ~ "(" ~ method_args ~ ")" ~ (block | semicolon) + visibility? ~ "fn" ~ identifier ~ generics_decl? ~ "(" ~ method_args ~ ")" ~ type_expr? ~ (block | semicolon) } // ====================================================== @@ -449,7 +453,10 @@ match_stmt = { // CLOSURES // ------------------------------------------------------ -closure = { type_expr? ~ "fn" ~ "(" ~ param_list ~ ")" ~ expr } +closure = { + "fn" ~ "(" ~ param_list ~ ")" ~ type_expr? ~ block + | "fn" ~ "(" ~ param_list ~ ")" ~ type_expr? ~ "->" ~ expr +} // ------------------------------------------------------ // TUPLES & ARRAYS @@ -628,4 +635,3 @@ bin_op = { expr = { term ~ (bin_op ~ term)* } term = { prefix* ~ primary ~ postfix* } - diff --git a/crates/parser/src/parser/common/decl.rs b/crates/parser/src/parser/common/decl.rs index b0af0e0..7a07fb9 100644 --- a/crates/parser/src/parser/common/decl.rs +++ b/crates/parser/src/parser/common/decl.rs @@ -2,7 +2,7 @@ use crate::{ Rule, ast::*, ast_expr, - error::{AstError, IntoErr}, + error::{AstError, IntoErr, collect_recovered}, }; impl<'a> TryFrom> for VarDeclStmt { @@ -48,25 +48,12 @@ impl<'a> TryFrom> for VarDecl { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { match pair.as_rule() { - Rule::var_decl => { + Rule::var_decl | Rule::param => { let mut inner = pair.into_inner(); - let type_ = inner - .next() - .and_then(|pair| { - if pair.as_rule() == Rule::let_kw { - None - } else { - Some(TypeExpr::try_from(pair)) - } - }) - .transpose(); - - let name = Pattern::try_from(inner.next().unwrap()); - ast_expr!(VarDecl { - type_: type_, - name: name, + name: Pattern::try_from(inner.next().unwrap()), + type_: inner.next().map(TypeExpr::try_from).transpose(), }) } @@ -85,8 +72,8 @@ impl<'a> TryFrom> for FieldDecl { ast_expr!(FieldDecl { visibility: Visibility::try_from(&mut inner), - type_: TypeExpr::try_from(inner.next().unwrap()), name: Identifier::try_from(inner.next().unwrap()), + type_: TypeExpr::try_from(inner.next().unwrap()), }) } @@ -94,3 +81,11 @@ impl<'a> TryFrom> for FieldDecl { } } } + +impl<'a> TryFrom> for ParamList { + type Error = AstError<'a, Self>; + + fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { + Ok(ParamList(collect_recovered(pair.into_inner()).get()?)) + } +} diff --git a/crates/parser/src/parser/common/expr.rs b/crates/parser/src/parser/common/expr.rs index e105511..be1790a 100644 --- a/crates/parser/src/parser/common/expr.rs +++ b/crates/parser/src/parser/common/expr.rs @@ -77,10 +77,10 @@ impl<'a> TryFrom> for Expression { Rule::closure => { ast_expr!(Expression::Closure { + params: collect_recovered(inner.next().unwrap().into_inner()), return_type: consume_rule(&mut inner, Rule::type_expr) .map(TypeExpr::try_from) .transpose(), - params: collect_recovered(inner.next().unwrap().into_inner()), body: Expression::try_from(inner.next().unwrap()).map(Box::new), }) } @@ -109,7 +109,7 @@ impl<'a> TryFrom> for Expression { )), } } - Rule::statement | Rule::basic_stmt | Rule::control_flow => ast_expr!( + Rule::statement | Rule::basic_stmt | Rule::control_flow | Rule::block => ast_expr!( Expression::Statement(pair.try_into().get_map(Box::new).map(Box::new)) ), diff --git a/crates/parser/src/parser/common/types.rs b/crates/parser/src/parser/common/types.rs index 2bf19d6..fc610f0 100644 --- a/crates/parser/src/parser/common/types.rs +++ b/crates/parser/src/parser/common/types.rs @@ -2,67 +2,10 @@ use crate::{ Rule, ast::*, ast_ensure, ast_expr, - error::{AstError, IntoErr, collect_recovered}, + error::{AstError, AstResult, IntoErr, collect_recovered}, parser::{consume_rule, listen_rule}, }; -impl<'a> TryFrom> for TypePostfix { - type Error = AstError<'a, Self>; - - fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { - let rule = pair.as_rule(); - let mut inner = pair.clone().into_inner(); - - match rule { - Rule::ref_type => { - let mutable = listen_rule(&mut inner, Rule::mutable); - let lifetime = consume_rule(&mut inner, Rule::lifetime) - .map(|pair| Identifier::try_from(pair.into_inner().next().unwrap())) - .transpose() - .get()?; - - Ok(if mutable { - if let Some(lifetime) = lifetime { - TypePostfix::RefMutLifetime(lifetime) - } else { - TypePostfix::RefMut - } - } else { - if let Some(lifetime) = lifetime { - TypePostfix::RefLifetime(lifetime) - } else { - TypePostfix::Ref - } - }) - } - - Rule::dyn_type => Ok(TypePostfix::Dyn), - - _ => AstError::bug_unimplemented(pair), - } - } -} - -impl<'a> TryFrom> for TypeExprKind { - type Error = AstError<'a, Self>; - - fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { - let rule = pair.as_rule(); - let mut inner = pair.clone().into_inner(); - - match rule { - Rule::tuple_type => ast_expr!(TypeExprKind::Tuple(collect_recovered(inner))), - Rule::path_type => { - ast_expr!(TypeExprKind::Path( - Path::try_from(inner.next().unwrap()), - inner.next().map(Generics::try_from).transpose() - )) - } - _ => AstError::bug_unimplemented(pair), - } - } -} - impl<'a> TryFrom> for TypeExpr { type Error = AstError<'a, Self>; @@ -72,13 +15,32 @@ impl<'a> TryFrom> for TypeExpr { match rule { Rule::generic => Self::try_from(inner.next().unwrap()), - Rule::type_expr => ast_expr!(TypeExpr( - inner.next().unwrap().try_into(), - collect_recovered(inner), - )), - Rule::lifetime => ast_expr!(TypeExprKind::Lifetime(inner.next().unwrap().try_into())) - .get_map(TypeExpr::no_px) - .map(TypeExpr::no_px), + Rule::type_expr => inner.next().unwrap().try_into(), + Rule::lifetime => ast_expr!(TypeExpr::Lifetime(inner.next().unwrap().try_into())), + + Rule::tuple_type => ast_expr!(TypeExpr::Tuple(collect_recovered(inner))), + Rule::path_type => { + ast_expr!(TypeExpr::Path( + Path::try_from(inner.next().unwrap()), + inner.next().map(Generics::try_from).transpose() + )) + } + + Rule::ref_type => { + ast_expr!(TypeExpr::Ref { + lifetime: consume_rule(&mut inner, Rule::lifetime) + .map(|v| v.into_inner().next().map(Identifier::try_from)) + .unwrap_or_default() + .transpose(), + mutable: Ok(listen_rule(&mut inner, Rule::mutable)) as AstResult, + ty: TypeExpr::try_from(inner.next().unwrap()).map(Box::new), + }) + } + Rule::dyn_type => { + ast_expr!(TypeExpr::Dyn( + TypeExpr::try_from(inner.next().unwrap()).map(Box::new), + )) + } _ => AstError::bug_unimplemented(pair), } diff --git a/crates/parser/src/parser/items/function.rs b/crates/parser/src/parser/items/function.rs index 8f75627..3919cce 100644 --- a/crates/parser/src/parser/items/function.rs +++ b/crates/parser/src/parser/items/function.rs @@ -2,7 +2,7 @@ use crate::{ Rule, ast::*, ast_ensure, ast_expr, - error::{AstError, AstResult, IntoErr, collect_recovered}, + error::{AstError, AstResult, IntoErr}, parser::{consume_rule, listen_rule}, }; @@ -13,7 +13,6 @@ impl<'a> TryFrom> for FunctionDecl { ast_ensure!(pair, Rule::function_decl => { let mut inner = pair.into_inner(); let visibility = Visibility::try_from(&mut inner); - let return_type = TypeExpr::try_from(inner.next().unwrap()); let name = Identifier::try_from(inner.next().unwrap()); let generics = consume_rule(&mut inner, Rule::generics_decl) @@ -23,24 +22,26 @@ impl<'a> TryFrom> for FunctionDecl { let self_param = consume_rule(&mut inner, Rule::self_param).map(|param| { let mut param_inner = param.into_inner(); - let mutable = listen_rule(&mut param_inner, Rule::mutable); let is_ref = listen_rule(&mut param_inner, Rule::deref_px); + let lifetime = consume_rule(&mut param_inner, Rule::lifetime); + let mutable = listen_rule(&mut param_inner, Rule::mutable); let name = Pattern::Path(mutable && !is_ref, Path(vec![Identifier(String::from("self"))])); + let self_ty = TypeExpr::Path(Path(vec![Identifier(String::from("Self"))]), None); VarDecl { name: name.clone(), - type_: Some(TypeExpr( - TypeExprKind::Path(Path(vec![Identifier("Self".to_string())]), None), - if is_ref { - vec![if mutable { - TypePostfix::RefMut - } else { - TypePostfix::Ref - }] - } else { - Vec::new() - }, - )), + type_: Some(if is_ref { + TypeExpr::Ref { + lifetime: + lifetime.map(|v| Identifier::try_from(v.into_inner().next().unwrap())) + .transpose() + .expect("Failed to get lifetime identifier"), + mutable, + ty: Box::new(self_ty) + } + } else { + self_ty + }), } }); @@ -57,6 +58,10 @@ impl<'a> TryFrom> for FunctionDecl { }) .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(); ast_expr!(Self { @@ -70,11 +75,3 @@ impl<'a> TryFrom> for FunctionDecl { }) } } - -impl<'a> TryFrom> for ParamList { - type Error = AstError<'a, Self>; - - fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { - Ok(ParamList(collect_recovered(pair.into_inner()).get()?)) - } -} diff --git a/mist-test/src/common/attribute_test.mist b/mist-test/src/common/attribute_test.mist new file mode 100644 index 0000000..313a20d --- /dev/null +++ b/mist-test/src/common/attribute_test.mist @@ -0,0 +1,21 @@ +#[derive(Debug)] +pub struct Debuggable { + value i32, +} + +pub fn test_derive_attribute() { + let d = Debuggable { value: 42 }; + assert_eq!(d.value, 42); +} + +#[allow(unused_variables)] +pub fn test_allow_attribute() { + let x = 42; + let y = "unused"; + assert_eq!(x, 42); +} + +pub fn run_tests() { + test_derive_attribute(); + test_allow_attribute(); +} diff --git a/mist-test/src/common/decl_test.mist b/mist-test/src/common/decl_test.mist index 8c67ab1..60eda7f 100644 --- a/mist-test/src/common/decl_test.mist +++ b/mist-test/src/common/decl_test.mist @@ -1,42 +1,42 @@ -pub void test_let_basic() { +pub fn test_let_basic() { let x = 42; assert_eq!(x, 42); } -pub void test_let_typed() { - i32 x = 42; +pub fn test_let_typed() { + let x i32 = 42; assert_eq!(x, 42); - str* s = "hello"; + let s *str = "hello"; assert_eq!(s, "hello"); - bool b = true; + let b bool = true; assert_eq!(b, true); - f64 f = 3.14; + let f f64 = 3.14; assert_eq!(f, 3.14); } -pub void test_let_mut() { - let mut x = 42; - x = 100; - assert_eq!(x, 100); +pub fn test_let_mut() { + let mut _x = 42; + _x = 100; + assert_eq!(_x, 100); } -pub void test_let_tuple_destructure() { +pub fn test_let_tuple_destructure() { let [a, b] = (10, "hello"); assert_eq!(a, 10); assert_eq!(b, "hello"); } -pub void test_let_nested_tuple() { +pub fn test_let_nested_tuple() { let [a, [b, c]] = (1, (2, 3)); assert_eq!(a, 1); assert_eq!(b, 2); assert_eq!(c, 3); } -pub void test_let_pattern_irrefutable() { +pub fn test_let_pattern_irrefutable() { let x = 5; let [y, z] = (1, 2); assert_eq!(x, 5); @@ -44,30 +44,30 @@ pub void test_let_pattern_irrefutable() { assert_eq!(z, 2); } -pub void test_multiple_decls() { +pub fn test_multiple_decls() { let a = 1; let b = 2; let c = 3; assert_eq!(a + b + c, 6); } -pub void test_let_with_type_and_init() { - f64 x = 10.0; +pub fn test_let_with_type_and_init() { + let x f64 = 10.0; assert_eq!(x, 10.0); } -pub void test_string_literal() { +pub fn test_string_literal() { let s = "hello, world!"; assert_eq!(s.len(), 13); } -pub void test_let_bool() { +pub fn test_let_bool() { let t = true; let f = false; assert_eq!(t, !f); } -pub void run_tests() { +pub fn run_tests() { test_let_basic(); test_let_typed(); test_let_mut(); diff --git a/mist-test/src/common/expr_test.mist b/mist-test/src/common/expr_test.mist index 1132fba..dc4f85f 100644 --- a/mist-test/src/common/expr_test.mist +++ b/mist-test/src/common/expr_test.mist @@ -1,6 +1,4 @@ -use std::cmp; - -pub void test_literals() { +pub fn test_literals() { assert_eq!(42, 42); assert_eq!(3.14, 3.14); assert_eq!(true, true); @@ -8,28 +6,28 @@ pub void test_literals() { assert_eq!("hello", "hello"); } -pub void test_tuples() { +pub fn test_tuples() { let t = (1, true, "three"); assert_eq!(t.0, 1); assert_eq!(t.1, true); assert_eq!(t.2, "three"); } -pub void test_arrays() { +pub fn test_arrays() { let arr = [1, 2, 3]; assert_eq!(arr[0], 1); assert_eq!(arr[1], 2); assert_eq!(arr[2], 3); } -pub void test_array_repeat() { +pub fn test_array_repeat() { let arr = [0; 5]; assert_eq!(arr[0], 0); assert_eq!(arr[4], 0); assert_eq!(arr.len(), 5); } -pub void test_binary_arith() { +pub fn test_binary_arith() { assert_eq!(10 + 5, 15); assert_eq!(10 - 5, 5); assert_eq!(10 * 5, 50); @@ -37,7 +35,7 @@ pub void test_binary_arith() { assert_eq!(10 % 3, 1); } -pub void test_binary_cmp() { +pub fn test_binary_cmp() { assert_eq!(5 < 10, true); assert_eq!(10 <= 10, true); assert_eq!(10 > 5, true); @@ -46,14 +44,14 @@ pub void test_binary_cmp() { assert_eq!(5 != 10, true); } -pub void test_binary_logic() { +pub fn test_binary_logic() { assert_eq!(true && true, true); assert_eq!(true && false, false); assert_eq!(true || false, true); assert_eq!(false || false, false); } -pub void test_binary_bit() { +pub fn test_binary_bit() { assert_eq!(5 & 3, 1); assert_eq!(5 | 3, 7); assert_eq!(5 ^ 3, 6); @@ -61,20 +59,20 @@ pub void test_binary_bit() { assert_eq!(4 >> 1, 2); } -pub void test_prefix() { +pub fn test_prefix() { let x = 42; assert_eq!(-x, -42); assert_eq!(!true, false); assert_eq!(!false, true); } -pub void test_ref_deref() { +pub fn test_ref_deref() { let x = 42; let r = &x; assert_eq!(*r, 42); } -pub void test_postfix_inc_dec() { +pub fn test_postfix_inc_dec() { let mut x = 5; x++; assert_eq!(x, 6); @@ -82,17 +80,17 @@ pub void test_postfix_inc_dec() { assert_eq!(x, 5); } -pub void test_as_cast() { +pub fn test_as_cast() { let x = 42; assert_eq!(x as f64, 42.0); } -pub void test_closure() { - let add = fn(let a, let b) a + b; +pub fn test_closure() { + let add = fn(a, b) -> a + b; assert_eq!(add(2, 3), 5); } -pub void test_block_expr() { +pub fn test_block_expr() { let x = { let y = 10; y + 5 @@ -100,7 +98,7 @@ pub void test_block_expr() { assert_eq!(x, 15); } -pub void test_if_expr() { +pub fn test_if_expr() { let x = if (true) { 1 } else { 2 }; assert_eq!(x, 1); @@ -108,7 +106,7 @@ pub void test_if_expr() { assert_eq!(y, 2); } -pub void test_range_expr() { +pub fn test_range_expr() { let r = 0..5; let mut count = 0; for (i : r) { @@ -117,7 +115,7 @@ pub void test_range_expr() { assert_eq!(count, 5); } -pub void test_binary_assign() { +pub fn test_binary_assign() { let mut x = 10; x += 5; assert_eq!(x, 15); @@ -131,7 +129,7 @@ pub void test_binary_assign() { assert_eq!(x, 2); } -pub void run_tests() { +pub fn run_tests() { test_literals(); test_tuples(); test_arrays(); diff --git a/mist-test/src/common/statement_test.mist b/mist-test/src/common/statement_test.mist index 1aab6fa..69c44d9 100644 --- a/mist-test/src/common/statement_test.mist +++ b/mist-test/src/common/statement_test.mist @@ -1,21 +1,21 @@ -pub void test_var_decl() { +pub fn test_var_decl() { let x = 42; assert_eq!(x, 42); - i32 y = 10; + let y i32 = 10; assert_eq!(y, 10); - str* name = "mist"; + let name *str = "mist"; assert_eq!(name, "mist"); } -pub void test_var_decl_with_init() { +pub fn test_var_decl_with_init() { let mut x = 0; x = 5; assert_eq!(x, 5); } -pub void test_if_statement() { +pub fn test_if_statement() { let mut x = 0; if (true) { @@ -31,7 +31,7 @@ pub void test_if_statement() { assert_eq!(x, 3); } -pub void test_if_else_if() { +pub fn test_if_else_if() { let mut x = 0; if (x == 1) { @@ -44,7 +44,7 @@ pub void test_if_else_if() { assert_eq!(x, 20); } -pub void test_while_loop() { +pub fn test_while_loop() { let mut count = 0; while (count < 5) { @@ -53,7 +53,7 @@ pub void test_while_loop() { assert_eq!(count, 5); } -pub void test_for_loop() { +pub fn test_for_loop() { let mut sum = 0; for (i : 0..4) { @@ -62,7 +62,7 @@ pub void test_for_loop() { assert_eq!(sum, 6); } -pub void test_loop_stmt() { +pub fn test_loop_stmt() { let mut count = 0; loop { @@ -74,7 +74,7 @@ pub void test_loop_stmt() { assert_eq!(count, 3); } -pub void test_break_continue() { +pub fn test_break_continue() { let mut sum = 0; for (i : 0..10) { @@ -89,11 +89,11 @@ pub void test_break_continue() { assert_eq!(sum, 1 + 3 + 5); } -pub i32 test_return_from_fn() { +pub fn test_return_from_fn() i32 { return 42; } -pub void test_block_scope() { +pub fn test_block_scope() { let x = { let inner = 10; inner + 5 @@ -101,9 +101,9 @@ pub void test_block_scope() { assert_eq!(x, 15); } -pub void test_match_literal() { +pub fn test_match_literal() { let x = 2; - let mut result = 0; + let result; match (x) { 1 => { result = 10; } @@ -114,9 +114,9 @@ pub void test_match_literal() { assert_eq!(result, 20); } -pub void test_match_or_pattern() { +pub fn test_match_or_pattern() { let x = 1; - let mut result = 0; + let result i32; match (x) { 1 | 2 => { result = 100; } @@ -126,9 +126,9 @@ pub void test_match_or_pattern() { assert_eq!(result, 100); } -pub void test_match_default() { +pub fn test_match_default() { let x = 99; - let mut result = 0; + let result; match (x) { 1 => { result = 10; } @@ -137,7 +137,7 @@ pub void test_match_default() { assert_eq!(result, 99); } -pub void test_for_with_pattern_destructure() { +pub fn test_for_with_pattern_destructure() { let pairs = [(1, "a"), (2, "b")]; let mut keys = 0; @@ -147,7 +147,7 @@ pub void test_for_with_pattern_destructure() { assert_eq!(keys, 3); } -pub void run_tests() { +pub fn run_tests() { test_var_decl(); test_var_decl_with_init(); test_if_statement(); diff --git a/mist-test/src/common/types_test.mist b/mist-test/src/common/types_test.mist index 2fac144..62779b0 100644 --- a/mist-test/src/common/types_test.mist +++ b/mist-test/src/common/types_test.mist @@ -1,56 +1,56 @@ -pub void test_integer_type() { - i32 x = 42; +pub fn test_integer_type() { + let x i32 = 42; assert_eq!(x, 42); } -pub void test_float_type() { - f64 x = 3.14; +pub fn test_float_type() { + let x f64 = 3.14; assert_eq!(x, 3.14); } -pub void test_bool_type() { - bool x = true; +pub fn test_bool_type() { + let x bool = true; assert_eq!(x, true); - bool y = false; + let y bool = false; assert_eq!(y, false); } -pub void test_string_type() { - str* s = "hello"; +pub fn test_string_type() { + let s *str = "hello"; assert_eq!(s, "hello"); } -pub void test_tuple_type() { - [i32, str*] t = (42, "answer"); +pub fn test_tuple_type() { + let t [i32, *str] = (42, "answer"); assert_eq!(t.0, 42); assert_eq!(t.1, "answer"); } -pub void test_ref_type() { - i32 x = 42; - i32* r = &x; +pub fn test_ref_type() { + let x i32 = 42; + let r *i32 = &x; assert_eq!(*r, 42); } -pub void test_mut_ref_type() { +pub fn test_mut_ref_type() { let mut x = 42; - i32 mut* r = &mut x; + let r *mut i32 = &mut x; *r = 100; assert_eq!(x, 100); } -pub void test_void_type() {} +pub fn test_void_type() {} -i32 add(i32 a, i32 b) { +fn add(a i32, b i32) i32 { a + b } -pub void test_fn_return_type() { +pub fn test_fn_return_type() { assert_eq!(add(3, 4), 7); } -pub void run_tests() { +pub fn run_tests() { test_integer_type(); test_float_type(); test_bool_type(); diff --git a/mist-test/src/items/attribute_test.mist b/mist-test/src/items/attribute_test.mist index 3dd06b2..313a20d 100644 --- a/mist-test/src/items/attribute_test.mist +++ b/mist-test/src/items/attribute_test.mist @@ -1,21 +1,21 @@ #[derive(Debug)] pub struct Debuggable { - i32 value, + value i32, } -pub void test_derive_attribute() { +pub fn test_derive_attribute() { let d = Debuggable { value: 42 }; assert_eq!(d.value, 42); } #[allow(unused_variables)] -pub void test_allow_attribute() { +pub fn test_allow_attribute() { let x = 42; let y = "unused"; assert_eq!(x, 42); } -pub void run_tests() { +pub fn run_tests() { test_derive_attribute(); test_allow_attribute(); } diff --git a/mist-test/src/items/class_test.mist b/mist-test/src/items/class_test.mist index 90d6720..7c44635 100644 --- a/mist-test/src/items/class_test.mist +++ b/mist-test/src/items/class_test.mist @@ -1,56 +1,56 @@ pub class Counter { - pub i32 count; + pub count i32; - pub constructor(i32 start) { + pub constructor(start i32) { self.count = start; } - pub void increment(self mut*) { + pub fn increment(*mut self) { self.count++; } - pub i32 get(self*) { + pub fn get(*self) i32 { self.count } } -pub void test_class_create() { +pub fn test_class_create() { let c = Counter::new(0); assert_eq!(c.get(), 0); } -pub void test_class_method() { +pub fn test_class_method() { let mut c = Counter::new(5); c.increment(); assert_eq!(c.get(), 6); } -pub void test_class_field_access() { +pub fn test_class_field_access() { let c = Counter::new(42); assert_eq!(c.count, 42); } class NamedCounter : Counter { - pub String name; + pub name String; - pub constructor(i32 start, String name) { + pub constructor(start i32, name String) { self._super = Counter::new(start); self.name = name; } - pub String get_name(self*) { + pub fn get_name(*self) String { self.name.clone() } } -pub void test_class_inherit() { +pub fn test_class_inherit() { let nc = NamedCounter::new(10, "test".to_string()); assert_eq!(nc.get_name(), "test"); assert_eq!(nc.count, 10); } -pub void run_tests() { +pub fn run_tests() { test_class_create(); test_class_method(); test_class_field_access(); diff --git a/mist-test/src/items/enum_test.mist b/mist-test/src/items/enum_test.mist index d111175..03a69c9 100644 --- a/mist-test/src/items/enum_test.mist +++ b/mist-test/src/items/enum_test.mist @@ -3,7 +3,7 @@ enum OptionInt { Some[i32], } -pub void test_enum_named() { +pub fn test_enum_named() { let x = OptionInt::None; match (x) { OptionInt::None => { assert_eq!(1, 1); } @@ -11,7 +11,7 @@ pub void test_enum_named() { } } -pub void test_enum_tuple() { +pub fn test_enum_tuple() { let x = OptionInt::Some(42); match (x) { OptionInt::None => { assert_eq!(0, 1); } @@ -20,11 +20,11 @@ pub void test_enum_tuple() { } enum Shape { - Circle { i32 radius }, - Rect { i32 w, i32 h }, + Circle { radius i32 }, + Rect { w i32, h i32 }, } -pub void test_enum_struct() { +pub fn test_enum_struct() { let c = Shape::Circle { radius: 5 }; match (c) { Shape::Circle { radius } => { assert_eq!(radius, 5); } @@ -44,7 +44,7 @@ enum Color { Blue, } -pub void test_enum_eq() { +pub fn test_enum_eq() { let c = Color::Red; match (c) { Color::Red => { assert_eq!(1, 1); } @@ -52,7 +52,7 @@ pub void test_enum_eq() { } } -pub void run_tests() { +pub fn run_tests() { test_enum_named(); test_enum_tuple(); test_enum_struct(); diff --git a/mist-test/src/items/function_test.mist b/mist-test/src/items/function_test.mist index 0278874..02c98c5 100644 --- a/mist-test/src/items/function_test.mist +++ b/mist-test/src/items/function_test.mist @@ -1,58 +1,58 @@ -void greet() { +fn greet() { let mut x = 1; x++; } -pub void test_basic_function() { +pub fn test_basic_function() { greet(); } -i32 add(i32 a, i32 b) { +fn add(a i32, b i32) i32 { a + b } -pub void test_fn_params() { +pub fn test_fn_params() { assert_eq!(add(2, 3), 5); } -pub i32 test_fn_return() { +pub fn test_fn_return() i32 { 42 } -pub i32 test_fn_early_return() { +pub fn test_fn_early_return() i32 { return 100; 0 } -pub i32 test_fn_mut_param(i32 mut x) { +pub fn test_fn_mut_param(mut x i32) i32 { x = x + 10; x } -pub void test_method_call() { - str* s = "hello"; +pub fn test_method_call() { + let s = "hello"; assert_eq!(s.len(), 5); assert_eq!(s.to_uppercase(), "HELLO"); } -i32 triple(i32 x) { +fn triple(x i32) i32 { add(x, add(x, x)) } -pub void test_nested_fn_calls() { +pub fn test_nested_fn_calls() { assert_eq!(triple(5), 15); } -i32 factorial(i32 n) { +fn factorial(n i32) i32 { if (n <= 1) 1 - else (n) * (factorial(n - 1)) + else n * factorial(n - 1) } -pub void test_recursive_fn() { +pub fn test_recursive_fn() { assert_eq!(factorial(5), 120); } -pub void run_tests() { +pub fn run_tests() { test_basic_function(); test_fn_params(); assert_eq!(test_fn_return(), 42); diff --git a/mist-test/src/items/impl_test.mist b/mist-test/src/items/impl_test.mist index 4032913..2c9240c 100644 --- a/mist-test/src/items/impl_test.mist +++ b/mist-test/src/items/impl_test.mist @@ -1,37 +1,37 @@ struct Vector2 { - i32 x, - i32 y, + x i32, + y i32, } impl Vector2 { - pub i32 magnitude(self*) { + pub fn magnitude(*self) i32 { self.x * self.x + self.y * self.y } } -pub void test_inherent_impl() { +pub fn test_inherent_impl() { let v = Vector2 { x: 3, y: 4 }; assert_eq!(v.magnitude(), 25); } trait Display { - String display(self*); + fn display(*self) String; } impl Display for Vector2 { - String display(self*) { + fn display(*self) String { format!("({}, {})", self.x, self.y) } } -pub void test_trait_impl_block() { +pub fn test_trait_impl_block() { let v = Vector2 { x: 1, y: 2 }; let s = v.display(); assert_eq!(s.len() > 0, true); } impl Vector2 { - pub Vector2 add(self*, Vector2 other) { + pub fn add(*self, other Vector2) Vector2 { Vector2 { x: self.x + other.x, y: self.y + other.y, @@ -39,7 +39,7 @@ impl Vector2 { } } -pub void test_multiple_impls() { +pub fn test_multiple_impls() { let a = Vector2 { x: 1, y: 2 }; let b = Vector2 { x: 3, y: 4 }; let c = a.add(b); @@ -47,7 +47,7 @@ pub void test_multiple_impls() { assert_eq!(c.y, 6); } -pub void run_tests() { +pub fn run_tests() { test_inherent_impl(); test_trait_impl_block(); test_multiple_impls(); diff --git a/mist-test/src/items/struct_test.mist b/mist-test/src/items/struct_test.mist index 53698da..7475688 100644 --- a/mist-test/src/items/struct_test.mist +++ b/mist-test/src/items/struct_test.mist @@ -1,21 +1,21 @@ struct Point { - i32 x, - i32 y, + x i32, + y i32, } -pub void test_struct_create_access() { +pub fn test_struct_create_access() { let p = Point { x: 10, y: 20 }; assert_eq!(p.x, 10); assert_eq!(p.y, 20); } -pub void test_struct_mut_field() { +pub fn test_struct_mut_field() { let mut p = Point { x: 1, y: 2 }; p.x = 100; assert_eq!(p.x, 100); } -pub void test_struct_destructure() { +pub fn test_struct_destructure() { let p = Point { x: 3, y: 4 }; let Point { x, y } = p; assert_eq!(x, 3); @@ -23,11 +23,11 @@ pub void test_struct_destructure() { } struct Line { - Point start, - Point end, + start Point, + end Point, } -pub void test_struct_nested() { +pub fn test_struct_nested() { let l = Line { start: Point { x: 0, y: 0 }, end: Point { x: 5, y: 10 }, @@ -36,7 +36,7 @@ pub void test_struct_nested() { assert_eq!(l.end.y, 10); } -pub void run_tests() { +pub fn run_tests() { test_struct_create_access(); test_struct_mut_field(); test_struct_destructure(); diff --git a/mist-test/src/items/trait_test.mist b/mist-test/src/items/trait_test.mist index ba93611..55ce2c3 100644 --- a/mist-test/src/items/trait_test.mist +++ b/mist-test/src/items/trait_test.mist @@ -1,39 +1,39 @@ trait Speak { - String speak(self*); + fn speak(*self) String; +} + +trait Greet : Speak { + fn greet(*self) String; } struct Dog { - String name, + name String, } impl Speak for Dog { - String speak(self*) { + fn speak(*self) String { "woof".to_string() } } -pub void test_trait_impl() { - let d = Dog { name: "Rex".to_string() }; - assert_eq!(d.speak(), "woof"); -} - -trait Greet : Speak { - String greet(self*); -} - impl Greet for Dog { - String greet(self*) { + fn greet(*self) String { "hello".to_string() } } -pub void test_trait_with_requirement() { +pub fn test_trait_impl() { + let d = Dog { name: "Rex".to_string() }; + assert_eq!(d.speak(), "woof"); +} + +pub fn test_trait_with_requirement() { let d = Dog { name: "Buddy".to_string() }; assert_eq!(d.speak(), "woof"); assert_eq!(d.greet(), "hello"); } -pub void run_tests() { +pub fn run_tests() { test_trait_impl(); test_trait_with_requirement(); } diff --git a/mist-test/src/main.mist b/mist-test/src/main.mist index 6e8730e..e2761cb 100644 --- a/mist-test/src/main.mist +++ b/mist-test/src/main.mist @@ -1,4 +1,4 @@ -void main() { +fn main() { println!("running common tests..."); common::expr_test::run_tests(); common::statement_test::run_tests(); @@ -15,4 +15,4 @@ void main() { items::attribute_test::run_tests(); println!("\x1b[32m\nAll tests passed!\x1b[0m"); -} +} \ No newline at end of file