Macro problem in enum

โš“ Rust    ๐Ÿ“… 2025-10-03    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 5      

surdeus

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Macro problem in enum

I don't understand why this doesn't work properly? Or do I have to write them all by hand? I'm trying to write a toy RISC V VM, but I feel that the way to process the instructions is too complicated (and the RISC V instruction set itself has a relatively fixed structure).

Syntax Error: expected COMMArust-analyzersyntax-error
Syntax Error: expected enum variantrust-analyzersyntax-error
Syntax Error: expected COMMArust-analyzersyntax-error
Syntax Error: expected enum variantrust-analyzersyntax-error
Syntax Error: expected COMMArust-analyzersyntax-error
macro_rules! r_type {
    ( $( $name:ident ),* ) => {
        $(
            #[doc = concat!("Rๅž‹ๆŒ‡ไปค: ", stringify!($name))]
            $name {
                rd: String,
                rs1: String,
                rs2: String,
            },
        )*
    };
}

macro_rules! i_type {
    ( $( $name:ident ),* ) => {
        $(
            #[doc = concat!("Iๅž‹ๆŒ‡ไปค: ", stringify!($name))]
            $name {
                rd: String,
                rs1: String,
                rs2: i64,
            },
        )*
    };
}

macro_rules! u_type {
    ( $( $name:ident ),* ) => {
        $(
            #[doc = concat!("Uๅž‹ๆŒ‡ไปค: ", stringify!($name))]
            $name {
                rd: String,
                rs1: i64,
            },
        )*
    };
}
enum Instruction {
    // R ๅž‹
    r_type!(ADD, SUB, SLT, SLTU, AND, OR, XOR, SLL, SRL, SRA, MUL);

    // I ๅž‹
    i_type!(ADDI, SLTI, SLTIU, ANDI, ORI, XORI, SLLI, SRLI, SRAI);

    // U ๅž‹
    u_type!(LUI, AUIPC);

    // ไผชๆŒ‡ไปค
    SEQZ { rd: String, rs1: String },
    NOP,
    MV { rd: String, rs1: String },
    NOT { rd: String, rs1: String },
    Li { rd: String, rs1: i64 },
}

5 posts - 3 participants

Read full topic

๐Ÿท๏ธ Rust_feed