Fmt Allman style

This commit is contained in:
2026-06-26 01:49:00 +02:00
parent 6c9c0e03d2
commit 9b7de09b0a
10 changed files with 178 additions and 96 deletions
+7
View File
@@ -46,6 +46,13 @@ impl MistCodegen {
self.add(&line); self.add(&line);
} }
pub fn start_bracket(&mut self) {
self.addln("");
self.add_indentedln("{");
self.indent += 1;
}
pub fn into_output(self) -> String { pub fn into_output(self) -> String {
self.output self.output
} }
+26 -20
View File
@@ -17,8 +17,7 @@ impl GenMist for ImplDecl {
cg.add(&self.target.get_mist()); cg.add(&self.target.get_mist());
cg.addln(" {"); cg.start_bracket();
cg.indent += 1;
for method in &self.methods { for method in &self.methods {
method.gen_mist(ctx, cg); method.gen_mist(ctx, cg);
@@ -79,7 +78,8 @@ impl GenMist for FunctionDecl {
} }
if let Some(body) = &self.body { if let Some(body) = &self.body {
cg.add(" "); cg.addln("");
cg.add_indented("");
body.gen_mist(ctx, cg); body.gen_mist(ctx, cg);
cg.addln(""); cg.addln("");
} else { } else {
@@ -183,22 +183,25 @@ impl GenMist for TopLevelKind {
fields, fields,
} => { } => {
cg.add(&format!( cg.add(&format!(
"{}struct {}{} {{", "{}struct {}{}",
visibility.get_mist(), visibility.get_mist(),
name.get_mist(), name.get_mist(),
generics.get_mist() generics.get_mist()
)); ));
if !fields.is_empty() {
cg.addln(""); if fields.is_empty() {
cg.indent += 1; cg.add(" {}");
} else {
cg.start_bracket();
for field in fields { for field in fields {
cg.add_indentedln(&format!("{},", field.item.get_mist())); cg.add_indentedln(&format!("{},", field.item.get_mist()));
} }
cg.indent -= 1; cg.indent -= 1;
cg.add_indented("}"); cg.add_indented("}");
} else {
cg.add("}");
} }
cg.addln(""); cg.addln("");
} }
Self::EnumDecl { Self::EnumDecl {
@@ -208,22 +211,25 @@ impl GenMist for TopLevelKind {
fields, fields,
} => { } => {
cg.add(&format!( cg.add(&format!(
"{}enum {}{} {{", "{}enum {}{}",
visibility.get_mist(), visibility.get_mist(),
name.get_mist(), name.get_mist(),
generics.get_mist() generics.get_mist()
)); ));
if !fields.is_empty() {
cg.addln(""); if fields.is_empty() {
cg.indent += 1; cg.add(" {}");
} else {
cg.start_bracket();
for field in fields { for field in fields {
cg.add_indentedln(&format!("{},", field.item.get_mist())); cg.add_indentedln(&format!("{},", field.item.get_mist()));
} }
cg.indent -= 1; cg.indent -= 1;
cg.add_indented("}"); cg.add_indented("}");
} else {
cg.add("}");
} }
cg.addln(""); cg.addln("");
} }
Self::TraitDecl { Self::TraitDecl {
@@ -249,8 +255,7 @@ impl GenMist for TopLevelKind {
.join(" + "), .join(" + "),
); );
} }
cg.addln(" {"); cg.start_bracket();
cg.indent += 1;
for item in items { for item in items {
item.gen_mist(ctx, cg); item.gen_mist(ctx, cg);
} }
@@ -276,8 +281,8 @@ impl GenMist for TopLevelKind {
cg.add(" : "); cg.add(" : ");
cg.add(&inherits.get_mist()); cg.add(&inherits.get_mist());
} }
cg.addln(" {\n");
cg.indent += 1; cg.start_bracket();
for field in fields { for field in fields {
cg.add_indented(&field.item.decl.get_mist()); cg.add_indented(&field.item.decl.get_mist());
@@ -303,7 +308,8 @@ impl GenMist for TopLevelKind {
} }
param.gen_mist(ctx, cg); param.gen_mist(ctx, cg);
} }
cg.add(") "); cg.addln(") ");
cg.add_indented("");
constructor.item.body.gen_mist(ctx, cg); constructor.item.body.gen_mist(ctx, cg);
cg.addln(""); cg.addln("");
+8 -4
View File
@@ -1,11 +1,13 @@
pub module attribute_test; pub module attribute_test;
#[derive(Debug)] #[derive(Debug)]
pub struct Debuggable { pub struct Debuggable
{
i32 value, i32 value,
} }
pub void test_derive_attribute() { pub void test_derive_attribute()
{
let d = Debuggable { let d = Debuggable {
value: 42, value: 42,
}; };
@@ -13,13 +15,15 @@ pub void test_derive_attribute() {
} }
#[allow(unused_variables)] #[allow(unused_variables)]
pub void test_allow_attribute() { pub 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 void run_tests() { pub void run_tests()
{
test_derive_attribute(); test_derive_attribute();
test_allow_attribute(); test_allow_attribute();
} }
+28 -17
View File
@@ -1,55 +1,66 @@
pub module class_test; pub module class_test;
pub class Animal { pub class Animal
{
pub String name; pub String name;
constructor() { constructor()
{
self.name = "Rex".to_string(); self.name = "Rex".to_string();
} }
pub String speak(&self) { pub String speak(&self)
{
"Unknown".to_string() "Unknown".to_string()
} }
pub void test_speak<'a>(&'a self) { pub void test_speak<'a>(&'a self)
{
assert_eq!(self.speak(), "Woof!"); assert_eq!(self.speak(), "Woof!");
} }
pub void static_method() { pub void static_method()
{
} }
} }
pub class Mammal : Animal { pub class Mammal : Animal
{
constructor() { constructor()
{
super = Super::new(); super = Super::new();
} }
pub void args_test(&self, i32 x, str& y) { pub void args_test(&self, i32 x, str& y)
{
assert_eq!(x, 10); assert_eq!(x, 10);
assert_eq!(y, "hello"); assert_eq!(y, "hello");
} }
} }
pub class Dog : Mammal { pub class Dog : Mammal
{
constructor() { constructor()
{
super = Super::new(); super = Super::new();
} }
pub String speak(&self) override(Animal) { pub String speak(&self) override(Animal)
{
"Woof!".to_string() "Woof!".to_string()
} }
impl std::fmt::Display { impl std::fmt::Display
Result<(), std::fmt::Error> fmt(&self, std::fmt::Formatter<'_>& mut f) { {
Result<(), std::fmt::Error> fmt(&self, std::fmt::Formatter<'_>& mut f)
{
write!(f, "🐾 {}", self.name) write!(f, "🐾 {}", self.name)
} }
} }
} }
pub void run_tests() { pub 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();
+23 -10
View File
@@ -1,11 +1,13 @@
pub module enum_test; pub module enum_test;
enum OptionInt { enum OptionInt
{
None, None,
Some(i32), Some(i32),
} }
pub void test_enum_named() { pub void test_enum_named()
{
let x = OptionInt::None; let x = OptionInt::None;
match x { match x {
OptionInt::None => { OptionInt::None => {
@@ -17,7 +19,8 @@ pub void test_enum_named() {
} }
} }
pub void test_enum_tuple() { pub void test_enum_tuple()
{
let x = OptionInt::Some(42); let x = OptionInt::Some(42);
match x { match x {
OptionInt::None => { OptionInt::None => {
@@ -29,13 +32,17 @@ pub void test_enum_tuple() {
} }
} }
enum Shape { enum Shape
{
Circle {i32 radius}, Circle {i32 radius},
Rect {i32 w, i32 h}, Rect {i32 w, i32 h},
} }
pub void test_enum_struct() { pub void test_enum_struct()
let c = Shape::Circle{radius: 5, }; {
let c = Shape::Circle {
radius: 5,
};
match c { match c {
Shape::Circle {radius} => { Shape::Circle {radius} => {
assert_eq!(radius, 5); assert_eq!(radius, 5);
@@ -44,7 +51,10 @@ pub void test_enum_struct() {
assert_eq!(0, 1); assert_eq!(0, 1);
} }
} }
let r = Shape::Rect{w: 3, h: 4, }; let r = Shape::Rect {
w: 3,
h: 4,
};
match r { match r {
Shape::Circle {..} => { Shape::Circle {..} => {
assert_eq!(0, 1); assert_eq!(0, 1);
@@ -55,13 +65,15 @@ pub void test_enum_struct() {
} }
} }
enum Color { enum Color
{
Red, Red,
Green, Green,
Blue, Blue,
} }
pub void test_enum_eq() { pub void test_enum_eq()
{
let c = Color::Red; let c = Color::Red;
match c { match c {
Color::Red => { Color::Red => {
@@ -73,7 +85,8 @@ pub void test_enum_eq() {
} }
} }
pub void run_tests() { pub void run_tests()
{
test_enum_named(); test_enum_named();
test_enum_tuple(); test_enum_tuple();
test_enum_struct(); test_enum_struct();
+26 -13
View File
@@ -1,51 +1,62 @@
pub module function_test; pub module function_test;
void greet() { void greet()
{
let mut x = 1; let mut x = 1;
x++; x++;
} }
pub void test_basic_function() { pub void test_basic_function()
{
greet(); greet();
} }
i32 add(i32 a, i32 b) { i32 add(i32 a, i32 b)
{
a + b a + b
} }
pub void test_fn_params() { pub void test_fn_params()
{
assert_eq!(add(2, 3), 5); assert_eq!(add(2, 3), 5);
} }
pub i32 test_fn_return() { pub i32 test_fn_return()
{
42 42
} }
pub i32 test_fn_early_return() { pub i32 test_fn_early_return()
{
return 100; return 100;
0 0
} }
pub i32 test_fn_mut_param(i32 mut x) { pub i32 test_fn_mut_param(i32 mut x)
{
x = x + 10; x = x + 10;
x x
} }
pub void test_method_call() { pub 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");
} }
i32 triple(i32 x) { i32 triple(i32 x)
{
add(x, add(x, x)) add(x, add(x, x))
} }
pub void test_nested_fn_calls() { pub void test_nested_fn_calls()
{
assert_eq!(triple(5), 15); assert_eq!(triple(5), 15);
} }
i32 factorial(i32 n) { i32 factorial(i32 n)
{
if n <= 1 { if n <= 1 {
1 1
} else { } else {
@@ -53,11 +64,13 @@ i32 factorial(i32 n) {
} }
} }
pub void test_recursive_fn() { pub void test_recursive_fn()
{
assert_eq!(factorial(5), 120); assert_eq!(factorial(5), 120);
} }
pub void run_tests() { pub 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);
+24 -12
View File
@@ -1,17 +1,21 @@
pub module impl_test; pub module impl_test;
struct Vector2 { struct Vector2
{
i32 x, i32 x,
i32 y, i32 y,
} }
impl Vector2 { impl Vector2
pub i32 magnitude(&self) { {
pub i32 magnitude(&self)
{
self.x * self.x + self.y * self.y self.x * self.x + self.y * self.y
} }
} }
pub void test_inherent_impl() { pub void test_inherent_impl()
{
let v = Vector2 { let v = Vector2 {
x: 3, x: 3,
y: 4, y: 4,
@@ -19,17 +23,21 @@ pub void test_inherent_impl() {
assert_eq!(v.magnitude(), 25); assert_eq!(v.magnitude(), 25);
} }
trait Display { trait Display
{
String display(&self); String display(&self);
} }
impl Display for Vector2 { impl Display for Vector2
String display(&self) { {
String display(&self)
{
format!("({}, {})", self.x, self.y) format!("({}, {})", self.x, self.y)
} }
} }
pub void test_trait_impl_block() { pub void test_trait_impl_block()
{
let v = Vector2 { let v = Vector2 {
x: 1, x: 1,
y: 2, y: 2,
@@ -38,8 +46,10 @@ pub void test_trait_impl_block() {
assert_eq!(s.len() > 0, true); assert_eq!(s.len() > 0, true);
} }
impl Vector2 { impl Vector2
pub Vector2 add(&self, Vector2 other) { {
pub Vector2 add(&self, Vector2 other)
{
Vector2 { Vector2 {
x: self.x + other.x, x: self.x + other.x,
y: self.y + other.y, y: self.y + other.y,
@@ -47,7 +57,8 @@ impl Vector2 {
} }
} }
pub void test_multiple_impls() { pub void test_multiple_impls()
{
let a = Vector2 { let a = Vector2 {
x: 1, x: 1,
y: 2, y: 2,
@@ -61,7 +72,8 @@ pub void test_multiple_impls() {
assert_eq!(c.y, 6); assert_eq!(c.y, 6);
} }
pub void run_tests() { pub void run_tests()
{
test_inherent_impl(); test_inherent_impl();
test_trait_impl_block(); test_trait_impl_block();
test_multiple_impls(); test_multiple_impls();
+14 -7
View File
@@ -1,11 +1,13 @@
pub module struct_test; pub module struct_test;
struct Point { struct Point
{
i32 x, i32 x,
i32 y, i32 y,
} }
pub void test_struct_create_access() { pub void test_struct_create_access()
{
let p = Point { let p = Point {
x: 10, x: 10,
y: 20, y: 20,
@@ -14,7 +16,8 @@ pub void test_struct_create_access() {
assert_eq!(p.y, 20); assert_eq!(p.y, 20);
} }
pub void test_struct_mut_field() { pub void test_struct_mut_field()
{
let mut p = Point { let mut p = Point {
x: 1, x: 1,
y: 2, y: 2,
@@ -23,7 +26,8 @@ pub void test_struct_mut_field() {
assert_eq!(p.x, 100); assert_eq!(p.x, 100);
} }
pub void test_struct_destructure() { pub void test_struct_destructure()
{
let p = Point { let p = Point {
x: 3, x: 3,
y: 4, y: 4,
@@ -33,12 +37,14 @@ pub void test_struct_destructure() {
assert_eq!(y, 4); assert_eq!(y, 4);
} }
struct Line { struct Line
{
Point start, Point start,
Point end, Point end,
} }
pub void test_struct_nested() { pub void test_struct_nested()
{
let l = Line { let l = Line {
start: Point { start: Point {
x: 0, x: 0,
@@ -53,7 +59,8 @@ pub void test_struct_nested() {
assert_eq!(l.end.y, 10); assert_eq!(l.end.y, 10);
} }
pub void run_tests() { pub 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();
+20 -10
View File
@@ -1,37 +1,46 @@
pub module trait_test; pub module trait_test;
trait Speak { trait Speak
{
String speak(&self); String speak(&self);
} }
trait Greet : Speak { trait Greet : Speak
{
String greet(&self); String greet(&self);
} }
struct Dog { struct Dog
{
String name, String name,
} }
impl Speak for Dog { impl Speak for Dog
String speak(&self) { {
String speak(&self)
{
"woof".to_string() "woof".to_string()
} }
} }
impl Greet for Dog { impl Greet for Dog
String greet(&self) { {
String greet(&self)
{
"hello".to_string() "hello".to_string()
} }
} }
pub void test_trait_impl() { pub void test_trait_impl()
{
let d = Dog { let d = Dog {
name: "Rex".to_string(), name: "Rex".to_string(),
}; };
assert_eq!(d.speak(), "woof"); assert_eq!(d.speak(), "woof");
} }
pub void test_trait_with_requirement() { pub void test_trait_with_requirement()
{
let d = Dog { let d = Dog {
name: "Buddy".to_string(), name: "Buddy".to_string(),
}; };
@@ -39,7 +48,8 @@ pub void test_trait_with_requirement() {
assert_eq!(d.greet(), "hello"); assert_eq!(d.greet(), "hello");
} }
pub void run_tests() { pub void run_tests()
{
test_trait_impl(); test_trait_impl();
test_trait_with_requirement(); test_trait_with_requirement();
} }
+2 -3
View File
@@ -1,11 +1,11 @@
void main() { 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();
common::statement_test::run_tests(); common::statement_test::run_tests();
common::decl_test::run_tests(); common::decl_test::run_tests();
common::types_test::run_tests(); common::types_test::run_tests();
println!("running items tests..."); println!("running items tests...");
items::function_test::run_tests(); items::function_test::run_tests();
items::struct_test::run_tests(); items::struct_test::run_tests();
@@ -14,6 +14,5 @@ void main() {
items::trait_test::run_tests(); items::trait_test::run_tests();
items::impl_test::run_tests(); items::impl_test::run_tests();
items::attribute_test::run_tests(); items::attribute_test::run_tests();
println!("\x1b[32m\nAll tests passed!\x1b[0m"); println!("\x1b[32m\nAll tests passed!\x1b[0m");
} }