diff --git a/crates/codegen/src/fmt/expr.rs b/crates/codegen/src/fmt/expr.rs index 7e3ece0..ce55bdd 100644 --- a/crates/codegen/src/fmt/expr.rs +++ b/crates/codegen/src/fmt/expr.rs @@ -141,16 +141,18 @@ impl GenMist for Postfix { cg.add(close); } Postfix::StructCall(fields) => { - cg.add("{"); + cg.addln(" {"); + cg.indent += 1; for (name, expr) in fields { - cg.add(&name.get_mist()); + cg.add_indented(&name.get_mist()); if let Some(expr) = expr { cg.add(": "); expr.gen_mist(ctx, cg); } - cg.add(", "); + cg.addln(","); } - cg.add("}"); + cg.indent -= 1; + cg.add_indented("}"); } Postfix::Index(idx) => { cg.add("["); @@ -168,8 +170,8 @@ impl GenMist for Postfix { cg.add(" "); expr.gen_mist(ctx, cg); } - Postfix::Increment => cg.add(" += 1"), - Postfix::Decrement => cg.add(" -= 1"), + Postfix::Increment => cg.add("++"), + Postfix::Decrement => cg.add("--"), } } } diff --git a/crates/codegen/src/fmt/top_level.rs b/crates/codegen/src/fmt/top_level.rs index 0eb242a..99c299e 100644 --- a/crates/codegen/src/fmt/top_level.rs +++ b/crates/codegen/src/fmt/top_level.rs @@ -4,17 +4,19 @@ use crate::fmt::{Context, GenMist, GetMist, MistCodegen}; impl GenMist for ImplDecl { fn gen_mist(&self, ctx: &mut Context, cg: &mut MistCodegen) { - cg.add_indented("impl"); + cg.add_indented("impl "); let generics = self.generics.get_mist(); if !generics.is_empty() { cg.add(&generics); cg.add(" "); } - cg.add(&self.target.get_mist()); if let Some(trait_) = &self.trait_ { - cg.add(" for "); cg.add(&trait_.get_mist()); + cg.add(" for "); } + + cg.add(&self.target.get_mist()); + cg.addln(" {"); cg.indent += 1; @@ -79,6 +81,7 @@ impl GenMist for FunctionDecl { if let Some(body) = &self.body { cg.add(" "); body.gen_mist(ctx, cg); + cg.addln(""); } else { cg.addln(";"); } @@ -125,7 +128,7 @@ impl GetMist for EnumItem { "{} {{{}}}", id.get_mist(), s.iter() - .map(|field| format!("{}: {}", field.name.get_mist(), field.type_.get_mist())) + .map(FieldDecl::get_mist) .collect::>() .join(", ") ), @@ -237,7 +240,7 @@ impl GenMist for TopLevelKind { generics.get_mist() )); if !requirements.is_empty() { - cg.add(": "); + cg.add(" : "); cg.add( &requirements .iter() @@ -273,7 +276,7 @@ impl GenMist for TopLevelKind { cg.add(" : "); cg.add(&inherits.get_mist()); } - cg.addln(" {"); + cg.addln(" {\n"); cg.indent += 1; for field in fields { @@ -286,6 +289,10 @@ impl GenMist for TopLevelKind { } if let Some(constructor) = constructor { + if fields.len() > 0 { + cg.addln(""); + } + cg.add_indented(&constructor.item.visibility.get_mist()); cg.add("constructor"); cg.add(&constructor.item.generics.get_mist()); @@ -299,9 +306,17 @@ impl GenMist for TopLevelKind { cg.add(") "); constructor.item.body.gen_mist(ctx, cg); cg.addln(""); + + if items.len() > 0 { + cg.addln(""); + } } - for item in items { + for (i, item) in items.iter().enumerate() { + if i > 0 { + cg.addln(""); + } + match item { ClassItem::Method(method) => method.gen_mist(ctx, cg), ClassItem::ImplDecl(impl_) => impl_.gen_mist(ctx, cg), diff --git a/mist-test/src/common/decl_test.mist b/mist-test/src/common/decl_test.mist index 57a8717..1374c12 100644 --- a/mist-test/src/common/decl_test.mist +++ b/mist-test/src/common/decl_test.mist @@ -8,13 +8,10 @@ pub void test_let_basic() { pub void test_let_typed() { i32 x = 42; assert_eq!(x, 42); - str& s = "hello"; assert_eq!(s, "hello"); - bool b = true; assert_eq!(b, true); - f64 f = 3.14; assert_eq!(f, 3.14); } diff --git a/mist-test/src/common/expr_test.mist b/mist-test/src/common/expr_test.mist index c344a00..5877501 100644 --- a/mist-test/src/common/expr_test.mist +++ b/mist-test/src/common/expr_test.mist @@ -101,15 +101,22 @@ pub void test_block_expr() { } pub void test_if_expr() { - let x = if true { 1 } else { 2 }; + let x = if true { + 1 + } else { + 2 + }; assert_eq!(x, 1); - - let y = if false { 1 } else { 2 }; + let y = if false { + 1 + } else { + 2 + }; assert_eq!(y, 2); } pub void test_range_expr() { - let r = 0..5; + let r = 0 .. 5; let mut count = 0; for i in r { count++; diff --git a/mist-test/src/common/statement_test.mist b/mist-test/src/common/statement_test.mist index 00ca939..4f35d21 100644 --- a/mist-test/src/common/statement_test.mist +++ b/mist-test/src/common/statement_test.mist @@ -3,10 +3,8 @@ pub module statement_test; pub void test_var_decl() { let x = 42; assert_eq!(x, 42); - i32 y = 10; assert_eq!(y, 10); - str& name = "mist"; assert_eq!(name, "mist"); } @@ -19,12 +17,10 @@ pub void test_var_decl_with_init() { pub void test_if_statement() { let mut x = 0; - if true { x = 1; } assert_eq!(x, 1); - if false { x = 2; } else { @@ -35,7 +31,6 @@ pub void test_if_statement() { pub void test_if_else_if() { let mut x = 0; - if x == 1 { x = 10; } else if x == 0 { @@ -48,7 +43,6 @@ pub void test_if_else_if() { pub void test_while_loop() { let mut count = 0; - while count < 5 { count++; } @@ -57,8 +51,7 @@ pub void test_while_loop() { pub void test_for_loop() { let mut sum = 0; - - for i in 0..4 { + for i in 0 .. 4 { sum += i; } assert_eq!(sum, 6); @@ -66,7 +59,6 @@ pub void test_for_loop() { pub void test_loop_stmt() { let mut count = 0; - loop { count++; if count >= 3 { @@ -78,8 +70,7 @@ pub void test_loop_stmt() { pub void test_break_continue() { let mut sum = 0; - - for i in 0..10 { + for i in 0 .. 10 { if i % 2 == 0 { continue; } @@ -106,12 +97,13 @@ pub void test_block_scope() { pub void test_match_literal() { let x = 2; let result; - match x { - 1 => { result = 10; } - 2 => result = 20; - 3 => result = 30, - _ => panic!(); + 1 => { + result = 10; + } + 2 => result = 20;, + 3 => result = 30;, + _ => panic!();, } assert_eq!(result, 20); } @@ -119,11 +111,14 @@ pub void test_match_literal() { pub void test_match_or_pattern() { let x = 1; i32 result; - match x { - 1 | 2 => { result = 100; } - 3 => { result = 200; } - _ => panic!(); + 1 | 2 => { + result = 100; + } + 3 => { + result = 200; + } + _ => panic!();, } assert_eq!(result, 100); } @@ -131,10 +126,13 @@ pub void test_match_or_pattern() { pub void test_match_default() { let x = 99; let result; - match x { - 1 => { result = 10; } - y => { result = y; } + 1 => { + result = 10; + } + y => { + result = y; + } } assert_eq!(result, 99); } @@ -142,7 +140,6 @@ pub void test_match_default() { pub void test_for_with_pattern_destructure() { let pairs = [(1, "a"), (2, "b")]; let mut keys = 0; - for (k, _) in pairs { keys += k; } diff --git a/mist-test/src/common/types_test.mist b/mist-test/src/common/types_test.mist index 59f4f30..7c7e62d 100644 --- a/mist-test/src/common/types_test.mist +++ b/mist-test/src/common/types_test.mist @@ -13,7 +13,6 @@ pub void test_float_type() { pub void test_bool_type() { bool x = true; assert_eq!(x, true); - bool y = false; assert_eq!(y, false); } @@ -37,12 +36,13 @@ pub void test_ref_type() { pub void test_mut_ref_type() { let mut x = 42; - i32 mut& r = &mut x; + i32 &mut r = &mut x; *r = 100; assert_eq!(x, 100); } -pub void test_void_type() {} +pub void test_void_type() { +} i32 add(i32 a, i32 b) { a + b diff --git a/mist-test/src/items/attribute_test.mist b/mist-test/src/items/attribute_test.mist index 03dd461..1530912 100644 --- a/mist-test/src/items/attribute_test.mist +++ b/mist-test/src/items/attribute_test.mist @@ -6,7 +6,9 @@ pub struct Debuggable { } pub void test_derive_attribute() { - let d = Debuggable { value: 42 }; + let d = Debuggable { + value: 42, + }; assert_eq!(d.value, 42); } diff --git a/mist-test/src/items/class_test.mist b/mist-test/src/items/class_test.mist index 3c82038..09f65d1 100644 --- a/mist-test/src/items/class_test.mist +++ b/mist-test/src/items/class_test.mist @@ -1,36 +1,52 @@ pub module class_test; pub class Animal { + pub String name; + constructor() { self.name = "Rex".to_string(); } + pub String speak(&self) { "Unknown".to_string() - } pub void test_speak<'a>(&'a self) { + } + + pub void test_speak<'a>(&'a self) { assert_eq!(self.speak(), "Woof!"); - } pub void static_method() { - }} + } + + pub void static_method() { + } +} pub class Mammal : Animal { + constructor() { super = Super::new(); } + pub void args_test(&self, i32 x, str& y) { assert_eq!(x, 10); assert_eq!(y, "hello"); - }} + } +} pub class Dog : Mammal { + constructor() { super = Super::new(); } + pub String speak(&self) override(Animal) { "Woof!".to_string() - } implstd::fmt::Display { + } + + impl std::fmt::Display { Result<(), std::fmt::Error> fmt(&self, std::fmt::Formatter<'_>& mut f) { write!(f, "🐾 {}", self.name) - } } + } + } } pub void run_tests() { @@ -38,4 +54,4 @@ pub void run_tests() { dawg.args_test(10, "hello"); dawg.test_speak(); assert_eq!(format!("{dawg}"), "🐾 Rex"); -} \ No newline at end of file +} diff --git a/mist-test/src/items/enum_test.mist b/mist-test/src/items/enum_test.mist index 2eafd99..1bae19a 100644 --- a/mist-test/src/items/enum_test.mist +++ b/mist-test/src/items/enum_test.mist @@ -8,35 +8,50 @@ enum OptionInt { pub void test_enum_named() { let x = OptionInt::None; match x { - OptionInt::None => { assert_eq!(1, 1); } - OptionInt::Some(_) => { assert_eq!(0, 1); } + OptionInt::None => { + assert_eq!(1, 1); + } + OptionInt::Some(_) => { + assert_eq!(0, 1); + } } } pub void test_enum_tuple() { let x = OptionInt::Some(42); match x { - OptionInt::None => { assert_eq!(0, 1); } - OptionInt::Some(v) => { assert_eq!(v, 42); } + OptionInt::None => { + assert_eq!(0, 1); + } + OptionInt::Some(v) => { + assert_eq!(v, 42); + } } } enum Shape { - Circle { i32 radius }, - Rect { i32 w, i32 h }, + Circle {i32 radius}, + Rect {i32 w, i32 h}, } pub void test_enum_struct() { - let c = Shape::Circle { radius: 5 }; + let c = Shape::Circle{radius: 5, }; match c { - Shape::Circle { radius } => { assert_eq!(radius, 5); } - Shape::Rect { .. } => { assert_eq!(0, 1); } + Shape::Circle {radius} => { + assert_eq!(radius, 5); + } + Shape::Rect {..} => { + assert_eq!(0, 1); + } } - - let r = Shape::Rect { w: 3, h: 4 }; + let r = Shape::Rect{w: 3, h: 4, }; match r { - Shape::Circle { .. } => { assert_eq!(0, 1); } - Shape::Rect { w, h } => { assert_eq!(w * h, 12); } + Shape::Circle {..} => { + assert_eq!(0, 1); + } + Shape::Rect {w, h} => { + assert_eq!(w * h, 12); + } } } @@ -49,8 +64,12 @@ enum Color { pub void test_enum_eq() { let c = Color::Red; match c { - Color::Red => { assert_eq!(1, 1); } - _ => { assert_eq!(0, 1); } + Color::Red => { + assert_eq!(1, 1); + } + _ => { + assert_eq!(0, 1); + } } } diff --git a/mist-test/src/items/function_test.mist b/mist-test/src/items/function_test.mist index a795225..d5126c5 100644 --- a/mist-test/src/items/function_test.mist +++ b/mist-test/src/items/function_test.mist @@ -46,8 +46,11 @@ pub void test_nested_fn_calls() { } i32 factorial(i32 n) { - if n <= 1 { 1 } - else { n * factorial(n - 1) } + if n <= 1 { + 1 + } else { + n * factorial(n - 1) + } } pub void test_recursive_fn() { diff --git a/mist-test/src/items/impl_test.mist b/mist-test/src/items/impl_test.mist index 36c83ab..02f3630 100644 --- a/mist-test/src/items/impl_test.mist +++ b/mist-test/src/items/impl_test.mist @@ -12,7 +12,10 @@ impl Vector2 { } pub void test_inherent_impl() { - let v = Vector2 { x: 3, y: 4 }; + let v = Vector2 { + x: 3, + y: 4, + }; assert_eq!(v.magnitude(), 25); } @@ -27,7 +30,10 @@ impl Display for Vector2 { } pub void test_trait_impl_block() { - let v = Vector2 { x: 1, y: 2 }; + let v = Vector2 { + x: 1, + y: 2, + }; let s = v.display(); assert_eq!(s.len() > 0, true); } @@ -42,8 +48,14 @@ impl Vector2 { } pub void test_multiple_impls() { - let a = Vector2 { x: 1, y: 2 }; - let b = Vector2 { x: 3, y: 4 }; + let a = Vector2 { + x: 1, + y: 2, + }; + let b = Vector2 { + x: 3, + y: 4, + }; let c = a.add(b); assert_eq!(c.x, 4); assert_eq!(c.y, 6); diff --git a/mist-test/src/items/struct_test.mist b/mist-test/src/items/struct_test.mist index 1bb4e3e..6a8d121 100644 --- a/mist-test/src/items/struct_test.mist +++ b/mist-test/src/items/struct_test.mist @@ -6,20 +6,29 @@ struct Point { } pub 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.y, 20); } pub void test_struct_mut_field() { - let mut p = Point { x: 1, y: 2 }; + let mut p = Point { + x: 1, + y: 2, + }; p.x = 100; assert_eq!(p.x, 100); } pub void test_struct_destructure() { - let p = Point { x: 3, y: 4 }; - let Point { x, y } = p; + let p = Point { + x: 3, + y: 4, + }; + let Point {x, y} = p; assert_eq!(x, 3); assert_eq!(y, 4); } @@ -31,8 +40,14 @@ struct Line { pub void test_struct_nested() { let l = Line { - start: Point { x: 0, y: 0 }, - end: Point { x: 5, y: 10 }, + start: Point { + x: 0, + y: 0, + }, + end: Point { + x: 5, + y: 10, + }, }; assert_eq!(l.start.x, 0); assert_eq!(l.end.y, 10); diff --git a/mist-test/src/items/trait_test.mist b/mist-test/src/items/trait_test.mist index 6a26325..8593a7a 100644 --- a/mist-test/src/items/trait_test.mist +++ b/mist-test/src/items/trait_test.mist @@ -25,12 +25,16 @@ impl Greet for Dog { } pub void test_trait_impl() { - let d = Dog { name: "Rex".to_string() }; + let d = Dog { + name: "Rex".to_string(), + }; assert_eq!(d.speak(), "woof"); } pub 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.greet(), "hello"); }