Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-generate main.zng #5

Open
kraktus opened this issue Aug 3, 2024 · 2 comments
Open

Auto-generate main.zng #5

kraktus opened this issue Aug 3, 2024 · 2 comments

Comments

@kraktus
Copy link

kraktus commented Aug 3, 2024

Hello, thanks for this promising project.

I’ve a rust crate https://github.com/kraktus/rs-retroboard-chess that has sparked the interest of cpp users. I’d like to make it available.

The good thing is that it mostly returns simple types or wrapper around it, however the api surface is still quite large to make a main.zng file manually. Is there a way to generate it from the rust code? Even imperfectly, as a way to save time.

@HKalbasi
Copy link
Owner

HKalbasi commented Aug 5, 2024

I added a very low effort implementation of a main.zng generator, you can use it by generating your project's json docs by cargo +nightly rustdoc -Zunstable-options --output-format=json and copy the result into zngur/zngur-autozng/doc.json and then run autozng using cargo run. I did it for your project, and here is the result:

type crate::MoveKind {
    #heap_allocated;
    fn new(Option, Option) -> Result;
    fn is_uncapture(self) -> bool;
    fn is_en_passant(self) -> bool;
    fn is_unpromotion(self) -> bool;
    fn to_retro_uci(self) -> String;
}
type crate::UnMove {
    #heap_allocated;
    fn from_retro_uci(&str) -> Result;
    fn new(Square, Square, MoveKind) -> Self;
    fn to_retro_uci(self) -> String;
    fn is_uncapture(self) -> bool;
    fn uncapture(self) -> Option;
    fn is_unpromotion(self) -> bool;
    fn is_en_passant(self) -> bool;
    fn uncapture_square(self) -> Option;
    fn mirror(self) -> Self;
}
type crate::RetroPockets {
    #heap_allocated;
    fn color(self, Color) -> &RetroPocket;
    fn color_mut(self, Color) -> &mut RetroPocket;
    fn from_str(&str, &str) -> Result;
}
type crate::RetroBoard {
    #heap_allocated;
    fn new_no_pockets(&str) -> Result;
    fn new(&str, &str, &str) -> Result;
    fn push(self, &UnMove);
    fn pseudo_legal_unmoves(self, &mut UnMoveList);
    fn legal_unmoves(self) -> UnMoveList;
    fn board(self) -> &Board;
    fn retro_turn(self) -> Color;
    fn us(self) -> Bitboard;
    fn our(self, Role) -> Bitboard;
    fn them(self) -> Bitboard;
    fn their(self, Role) -> Bitboard;
    fn king_of(self, Color) -> Square;
    fn flip_vertical(self);
    fn flip_horizontal(self);
    fn flip_diagonal(self);
    fn flip_anti_diagonal(self);
    fn rotate_90(self);
    fn rotate_180(self);
    fn rotate_270(self);
}
type crate::ParseRetroPocketError {
    #heap_allocated;
}
type crate::RetroPocket {
    #heap_allocated;
    fn decr(self, Role);
}
type crate::ParseRetroUciError {
    #heap_allocated;
}

It is broken in multiple ways, and you can use it just as a template. You need to fix these things for it to become usable:

  • Names should be full qualified, like crate::UnMove, ::shakmaty::Role or ::std::string::String.
  • Generics are ignored and should be added
  • Generic types used in function arguments and return types should be added as separate types, with appropriate methods like unwrap.
  • self types are wrong (I can fix this easily if it is annoying).
  • You probably want to do this once for shakmaty crate as well.

The autozng is a simple 200 line script which you can easily read and improve, and ideally we want to have a full featured autozng and it is in the scope of this project, however I currently don't have time to make it complete and polished, but I can add some more features if you desire.

Please tell me if you hit any problems in using zngur, or have some ideas. My own project that triggered me to create zngur became fully Rust, so now there is no real project using it. But I still believe in zngur idea and would really like to see a real project use zngur, so I will help you if you hit any zngur related problem.

@kraktus
Copy link
Author

kraktus commented Aug 5, 2024

Thanks! I actually did something similar 2 years ago: https://github.com/kraktus/cargo-extern-fn and got quite far using proc macros, but it was based on cxx which turned out to be too limited in terms of fondamental type support (not even Option), and seemingly no active development from the maintainers to add them or accept PRs.

Will definitely check it out once I've more time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants