Merge pull request #108 from mist-go/struct-field-style

Allman struct/enum field style, tuple var decls, cleanup
This commit is contained in:
2026-07-04 19:34:57 +02:00
committed by GitHub
18 changed files with 99 additions and 51 deletions
+31 -8
View File
@@ -188,14 +188,37 @@ impl GenRust for Statement {
impl GenRust for VarDecl { impl GenRust for VarDecl {
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
self.name.gen_rust(ctx, cg); if self.tuple_names.is_empty() {
self.name.gen_rust(ctx, cg);
} else {
cg.add("(");
self.name.gen_rust(ctx, cg);
for name in &self.tuple_names {
cg.add(", ");
name.gen_rust(ctx, cg);
}
cg.add(")");
}
cg.add( if self.true_type {
&self cg.add(
.type_ &self
.as_ref() .type_
.map(|t| format!(": {}", t.get_rust())) .as_ref()
.unwrap_or_default(), .map(|t| format!(": {}", t.get_rust()))
); .unwrap_or_default(),
);
} else {
cg.add(
&self
.type_
.as_ref()
.map(|t| {
let t = vec![t.get_rust(); self.tuple_names.len() + 1];
format!(": ({})", t.join(", "))
})
.unwrap_or_default(),
);
}
} }
} }
+3 -1
View File
@@ -44,8 +44,10 @@ pub struct MatchItem(pub Vec<Pattern>, pub Expression);
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub struct VarDecl { pub struct VarDecl {
pub name: Pattern,
pub type_: Option<TypeExpr>, pub type_: Option<TypeExpr>,
pub name: Pattern,
pub true_type: bool,
pub tuple_names: Vec<Pattern>,
} }
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
+1
View File
@@ -126,6 +126,7 @@ pub struct FieldDecl {
pub visibility: Visibility, pub visibility: Visibility,
pub type_: TypeExpr, pub type_: TypeExpr,
pub name: Identifier, pub name: Identifier,
// pub tuple_names: Vec<Identifier>,
} }
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
+8 -9
View File
@@ -225,9 +225,12 @@ attributes = {
// ====================================================== // ======================================================
// DECLARATION HELPERS // DECLARATION HELPERS
// ====================================================== // ======================================================
as_kw = { "as" }
var_decl = { var_decl = {
("let" ~ pattern) ("let" ~ pattern ~ ("," ~ pattern)*)
| type_expr ~ (!tuple_pattern ~ pattern) | (type_expr ~ as_kw ~ (!tuple_pattern ~ pattern ~ ("," ~ pattern)+))
| (type_expr ~ (!tuple_pattern ~ pattern ~ ("," ~ pattern)*))
} }
field = { field = {
@@ -240,7 +243,7 @@ param = {
} }
field_list = { field_list = {
(field ~ ("," ~ field)* ~ ","?)? (field ~ ";")*
} }
param_list = { param_list = {
@@ -264,7 +267,7 @@ declare_module = {
// ====================================================== // ======================================================
struct_decl = { struct_decl = {
visibility? ~ "struct" ~ identifier ~ generics_decl? ~ "{" ~ field_list? ~ "}" visibility? ~ "struct" ~ identifier ~ generics_decl? ~ "{" ~ field_list ~ "}"
} }
// ====================================================== // ======================================================
@@ -289,12 +292,8 @@ enum_field = _{
| enum_named | enum_named
} }
enum_fields = _{
enum_field ~ ("," ~ enum_field)* ~ ","?
}
enum_decl = { enum_decl = {
visibility? ~ "enum" ~ identifier ~ generics_decl? ~ "{" ~ enum_fields? ~ "}" visibility? ~ "enum" ~ identifier ~ generics_decl? ~ "{" ~ (enum_field ~ ";")* ~ "}"
} }
// ====================================================== // ======================================================
+3 -1
View File
@@ -2,7 +2,7 @@ use crate::{
Rule, Rule,
ast::*, ast::*,
error::{AstError, collect_recovered}, error::{AstError, collect_recovered},
parser::consume_rule, parser::{consume_rule, listen_rule},
}; };
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDeclStmt { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDeclStmt {
@@ -55,7 +55,9 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDecl {
type_: consume_rule(&mut inner, Rule::type_expr) type_: consume_rule(&mut inner, Rule::type_expr)
.map(TypeExpr::try_from) .map(TypeExpr::try_from)
.transpose()?, .transpose()?,
true_type: listen_rule(&mut inner, Rule::as_kw),
name: Pattern::try_from(inner.next().unwrap())?, name: Pattern::try_from(inner.next().unwrap())?,
tuple_names: collect_recovered(inner)?,
}) })
} }
+1 -5
View File
@@ -54,11 +54,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
.transpose() .transpose()
.map(|v| v.unwrap_or_default())?, .map(|v| v.unwrap_or_default())?,
fields: inner fields: collect_recovered(inner.next().unwrap().into_inner())?,
.next()
.map(|pair| collect_recovered(pair.into_inner()))
.transpose()
.map(|v| v.unwrap_or_default())?,
}), }),
Rule::class_decl => Ok(TopLevelKind::ClassDecl { Rule::class_decl => Ok(TopLevelKind::ClassDecl {
+32 -1
View File
@@ -1,5 +1,4 @@
pub module decl_test; pub module decl_test;
pub module decl_test;
pub void test_let_basic() pub void test_let_basic()
{ {
@@ -7,6 +6,13 @@ pub void test_let_basic()
assert_eq!(x, 42); assert_eq!(x, 42);
} }
pub void test_let_repeat()
{
let x, y = (42, 67);
assert_eq!(x, 42);
assert_eq!(y, 67);
}
pub void test_let_typed() pub void test_let_typed()
{ {
i32 x = 42; i32 x = 42;
@@ -19,6 +25,29 @@ pub void test_let_typed()
assert_eq!(f, 3.14); assert_eq!(f, 3.14);
} }
pub void test_let_repeat_typed()
{
i32 x, x1 = (42, 67);
assert_eq!(x, 42);
assert_eq!(x1, 67);
str& s, s1 = ("hello", "world");
assert_eq!(s, "hello");
assert_eq!(s1, "world");
bool b, b1 = (true, false);
assert_eq!(b, true);
assert_eq!(b1, false);
f64 f, f1 = (3.14, 5.5);
assert_eq!(f, 3.14);
assert_eq!(f1, 5.5);
(i32, f64) as a, b = (70, 1.4);
assert_eq!(a, 70);
assert_eq!(b, 1.4);
}
pub void test_let_mut() pub void test_let_mut()
{ {
let mut _x = 42; let mut _x = 42;
@@ -80,7 +109,9 @@ pub void test_let_bool()
pub void run_tests() pub void run_tests()
{ {
test_let_basic(); test_let_basic();
test_let_repeat();
test_let_typed(); test_let_typed();
test_let_repeat_typed();
test_let_mut(); test_let_mut();
test_let_tuple_destructure(); test_let_tuple_destructure();
test_let_nested_tuple(); test_let_nested_tuple();
-1
View File
@@ -1,5 +1,4 @@
pub module expr_test; pub module expr_test;
pub module expr_test;
pub void test_literals() pub void test_literals()
{ {
-1
View File
@@ -1,4 +1,3 @@
pub module package;
pub module common; pub module common;
pub void test_package() {} pub void test_package() {}
-1
View File
@@ -1,5 +1,4 @@
pub module statement_test; pub module statement_test;
pub module statement_test;
pub void test_var_decl() pub void test_var_decl()
{ {
-1
View File
@@ -1,5 +1,4 @@
pub module types_test; pub module types_test;
pub module types_test;
pub void test_integer_type() { pub void test_integer_type() {
i32 x = 42; i32 x = 42;
+1 -2
View File
@@ -1,10 +1,9 @@
pub module attribute_test; 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()
-1
View File
@@ -1,5 +1,4 @@
pub module class_test; pub module class_test;
pub module class_test;
pub class Animal pub class Animal
{ {
+12 -8
View File
@@ -1,10 +1,9 @@
pub module enum_test; 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()
@@ -35,8 +34,13 @@ pub void test_enum_tuple()
enum Shape enum Shape
{ {
Circle {i32 radius}, Circle {
Rect {i32 w, i32 h}, i32 radius;
};
Rect {
i32 w;
i32 h;
};
} }
pub void test_enum_struct() pub void test_enum_struct()
@@ -68,9 +72,9 @@ 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()
-1
View File
@@ -1,5 +1,4 @@
pub module function_test; pub module function_test;
pub module function_test;
void greet() void greet()
{ {
+2 -3
View File
@@ -1,10 +1,9 @@
pub module impl_test; pub module impl_test;
pub module impl_test;
struct Vector2 struct Vector2
{ {
i32 x, i32 x;
i32 y, i32 y;
} }
impl Vector2 impl Vector2
+4 -5
View File
@@ -1,10 +1,9 @@
pub module struct_test; 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()
@@ -40,8 +39,8 @@ pub void test_struct_destructure()
struct Line struct Line
{ {
Point start, Point start;
Point end, Point end;
} }
pub void test_struct_nested() pub void test_struct_nested()
+1 -2
View File
@@ -1,5 +1,4 @@
pub module trait_test; pub module trait_test;
pub module trait_test;
trait Speak trait Speak
{ {
@@ -13,7 +12,7 @@ trait Greet : Speak
struct Dog struct Dog
{ {
String name, String name;
} }
impl Speak for Dog impl Speak for Dog