tomoyuki-nakabayashi / Rustemu86

Apache License 2.0
5 stars 0 forks source link

命令追加をマクロで改善する #36

Closed tomoyuki-nakabayashi closed 5 years ago

tomoyuki-nakabayashi commented 5 years ago

https://doc.rust-jp.rs/the-rust-programming-language-ja/1.6/book/macros.html

マクロの展開結果を見るには、 rustc --pretty expanded を実行して下さい。

これは覚えておく必要がある。

tomoyuki-nakabayashi commented 5 years ago

nightly compilerが必要なのか。

$ rustc --pretty expanded src/main.rs
error: the `-Z unstable-options` flag must also be passed to enable the flag `pretty`
$ rustc --pretty expanded -Z unstable-options src/main.rs
error: the option `Z` is only accepted on the nightly compiler
$ rustup override add nightly
$ rustc --pretty expanded -Z unstable-options src/main.rs
#![feature(prelude_import)]
#![no_std]
#[prelude_import]
use ::std::prelude::v1::*;
#[macro_use]
extern crate std;
macro_rules! my_vec(( $ ( $ x : expr ) , * ) => {
                    {
                    let mut temp_vec = Vec :: new (  ) ; $ (
                    temp_vec . push ( $ x ) ; ) * temp_vec } } ;);

fn main() {
    let v =
        {
            let mut temp_vec = Vec::new();
            temp_vec.push(1);
            temp_vec.push(2);
            temp_vec.push(3);
            temp_vec
        };

    {
        ::io::_print(::std::fmt::Arguments::new_v1_formatted(&["", "\n"],
                                                             &match (&&v,) {
                                                                  (arg0,) =>
                                                                  [::std::fmt::ArgumentV1::new(arg0,
                                                                                               ::std::fmt::Debug::fmt)],
                                                              },
                                                             &[::std::fmt::rt::v1::Argument{position:
                                                                                                ::std::fmt::rt::v1::Position::At(0usize),
                                                                                            format:
                                                                                                ::std::fmt::rt::v1::FormatSpec{fill:
                                                                                                                                   ' ',
                                                                                                                               align:
                                                                                                                                   ::std::fmt::rt::v1::Alignment::Unknown,
                                                                                                                               flags:
                                                                                                                                   0u32,
                                                                                                                               precision:
                                                                                                                                   ::std::fmt::rt::v1::Count::Implied,
                                                                                                                               width:
                                                                                                                                   ::std::fmt::rt::v1::Count::Implied,},}]));
    };
}
tomoyuki-nakabayashi commented 5 years ago

なるほどね~。

tomoyuki-nakabayashi commented 5 years ago

プログラミングRust p.492

log_syntax!()マクロ

![feature(log_syntax)]が必要。

trace_macros!(true);

![feature(trace_macros)]が必要。

tomoyuki-nakabayashi commented 5 years ago

tt: token treeは(...), [...], {...}などのカッコで囲まれている部分すべて、または、カッコで囲まれていない単一のトークンにマッチする。

tomoyuki-nakabayashi commented 5 years ago

プログラミングRust p.495のunittest、JSONオブジェクトをvec!で作っているが、HashMapの間違いか?

    let hand_coded_value = 
        Json::Array(vec![
            Json::Object(Box::new([
                ("pitch".to_string(), Json::Number(440.0))
            ].iter().cloned().collect::<HashMap<String, Json>>()))
        ]);

vec!だとコンパイルエラーになるし。

tomoyuki-nakabayashi commented 5 years ago
        match opcode {
            Cld => meta_inst!(opcode, false, false, None, None),
            Lea => meta_inst!(opcode, true, false, None, Some(UDWord)),
            MovRmSreg => meta_inst!(opcode, true, false, None, None),
            Xor => meta_inst!(opcode, true, false, None, None),
            Hlt => meta_inst!(opcode, false, false, None, None),
            _ => None,
        }

ちょっとすっきりした。 フィールド名なくなったからこのままではダメ。 matchごとmacro化したいな。

tomoyuki-nakabayashi commented 5 years ago

かなり良くなった。

93e5170619250d1d5c4137dae950655978136ca5

まぁ一旦良いでしょう。