www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.format with named args

reply Vitaliy Fadeev <vital.fadeev gmail.com> writes:
Hi all!
I want readable mixin.
I want pass variable to string.
I want string with named args.
Like this:

```D
     enum JMP_ADDR_R = "RAX";

     mixin( format!"asm {
         jmp [JMP_ADDR_R];
    }"( JMP_ADDR_R ));    // IT NOT WORK
```

Not this:

```D
     enum JMP_ADDR_R = "RAX";

     mixin( format!"asm {
         jmp [%s];
    }"( JMP_ADDR_R ));    // WORK, BUT NOT-READABLE
```

In large code named identifiers more readable.

In future it will used in jump table, like this:

```D
     mixin( format!"asm {
         lea JMP_ADDR_R, qword ptr [ JMP_I_R * PTR_SIZE + TBL_ADDR 
];
         jmp [JMP_ADDR_R];
     }"( JMP_I_R, PTR_SIZE, TBL_ADDR, JMP_ADDR_R ));
```

Please, help me find a solution...
or exists std.format with named args ?
or another solution with named args ?
Oct 16 2023
parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Tuesday, 17 October 2023 at 06:46:31 UTC, Vitaliy Fadeev wrote:
 Hi all!
 I want readable mixin.
 I want pass variable to string.
 I want string with named args.
 Like this:

 ```D
     enum JMP_ADDR_R = "RAX";

     mixin( format!"asm {
         jmp [JMP_ADDR_R];
    }"( JMP_ADDR_R ));    // IT NOT WORK
 ```

 Not this:

 ```D
     enum JMP_ADDR_R = "RAX";

     mixin( format!"asm {
         jmp [%s];
    }"( JMP_ADDR_R ));    // WORK, BUT NOT-READABLE
 ```

 In large code named identifiers more readable.

 In future it will used in jump table, like this:

 ```D
     mixin( format!"asm {
         lea JMP_ADDR_R, qword ptr [ JMP_I_R * PTR_SIZE + 
 TBL_ADDR ];
         jmp [JMP_ADDR_R];
     }"( JMP_I_R, PTR_SIZE, TBL_ADDR, JMP_ADDR_R ));
 ```

 Please, help me find a solution...
 or exists std.format with named args ?
 or another solution with named args ?
Without string interpolation the best you can do is https://dlang.org/library/std/conv/text.html Or write some code yourself, unfortunately
Oct 16 2023
parent reply Vitaliy Fadeev <vital.fadeev gmail.com> writes:
On Tuesday, 17 October 2023 at 06:57:07 UTC, Imperatorn wrote:
 On Tuesday, 17 October 2023 at 06:46:31 UTC, Vitaliy Fadeev 
 wrote:
 Hi all!
 I want readable mixin.
 I want pass variable to string.
 I want string with named args.
 Like this:

 ```D
     enum JMP_ADDR_R = "RAX";

     mixin( format!"asm {
         jmp [JMP_ADDR_R];
    }"( JMP_ADDR_R ));    // IT NOT WORK
 ```

 Not this:

 ```D
     enum JMP_ADDR_R = "RAX";

     mixin( format!"asm {
         jmp [%s];
    }"( JMP_ADDR_R ));    // WORK, BUT NOT-READABLE
 ```

 In large code named identifiers more readable.

 In future it will used in jump table, like this:

 ```D
     mixin( format!"asm {
         lea JMP_ADDR_R, qword ptr [ JMP_I_R * PTR_SIZE + 
 TBL_ADDR ];
         jmp [JMP_ADDR_R];
     }"( JMP_I_R, PTR_SIZE, TBL_ADDR, JMP_ADDR_R ));
 ```

 Please, help me find a solution...
 or exists std.format with named args ?
 or another solution with named args ?
Without string interpolation the best you can do is https://dlang.org/library/std/conv/text.html Or write some code yourself, unfortunately
May be. "String interpolation" ```D mixin( format!"asm { lea JMP_ADDR_R, qword ptr [ JMP_I_R * PTR_SIZE + TBL_ADDR ]; jmp [JMP_ADDR_R]; }"( JMP_I_R, PTR_SIZE, TBL_ADDR, JMP_ADDR_R )); ``` vs text() ```D mixin( text( "asm { lea", JMP_ADDR_R, ", qword ptr [ ", JMP_I_R, " * ", PTR_SIZE, " + ", TBL_ADDR, " ]; jmp [", JMP_ADDR_R, "]; }")); ``` "String interpolation" more readable. "String interpolation" easier to type. "text()" syntax is highlighted, Thanks for the correct keywords: "String interpolation" - this is what I'm looking for. Is there a "string interpolation" solution?
Oct 17 2023
parent reply Vitaliy Fadeev <vital.fadeev gmail.com> writes:
On Tuesday, 17 October 2023 at 07:22:41 UTC, Vitaliy Fadeev wrote:
 On Tuesday, 17 October 2023 at 06:57:07 UTC, Imperatorn wrote:
 On Tuesday, 17 October 2023 at 06:46:31 UTC, Vitaliy Fadeev 
 wrote:
 Hi all!
 I want readable mixin.
 I want pass variable to string.
 I want string with named args.
 Like this:

 ```D
     enum JMP_ADDR_R = "RAX";

     mixin( format!"asm {
         jmp [JMP_ADDR_R];
    }"( JMP_ADDR_R ));    // IT NOT WORK
 ```

 Not this:

 ```D
     enum JMP_ADDR_R = "RAX";

     mixin( format!"asm {
         jmp [%s];
    }"( JMP_ADDR_R ));    // WORK, BUT NOT-READABLE
 ```

 In large code named identifiers more readable.

 In future it will used in jump table, like this:

 ```D
     mixin( format!"asm {
         lea JMP_ADDR_R, qword ptr [ JMP_I_R * PTR_SIZE + 
 TBL_ADDR ];
         jmp [JMP_ADDR_R];
     }"( JMP_I_R, PTR_SIZE, TBL_ADDR, JMP_ADDR_R ));
 ```

 Please, help me find a solution...
 or exists std.format with named args ?
 or another solution with named args ?
Without string interpolation the best you can do is https://dlang.org/library/std/conv/text.html Or write some code yourself, unfortunately
May be. "String interpolation" ```D mixin( format!"asm { lea JMP_ADDR_R, qword ptr [ JMP_I_R * PTR_SIZE + TBL_ADDR ]; jmp [JMP_ADDR_R]; }"( JMP_I_R, PTR_SIZE, TBL_ADDR, JMP_ADDR_R )); ``` vs text() ```D mixin( text( "asm { lea", JMP_ADDR_R, ", qword ptr [ ", JMP_I_R, " * ", PTR_SIZE, " + ", TBL_ADDR, " ]; jmp [", JMP_ADDR_R, "]; }")); ``` "String interpolation" more readable. "String interpolation" easier to type. "text()" syntax is highlighted, Thanks for the correct keywords: "String interpolation" - this is what I'm looking for. Is there a "string interpolation" solution?
`scriptlike` looks perfecto! ```D enum JMP_ADDR_R = ... ... import scriptlike; writeln( mixin(interp!" asm { lea ${JMP_ADDR_R}, qword ptr [ ${JMP_I_R} * ${PTR_SIZE} + ${TBL_ADDR} ]; jmp [${JMP_ADDR_R}]; } ") ); ``` produced: ``` asm { lea RAX, qword ptr [ RDX * 8 + 5594AA134290 ]; jmp [RAX]; } ``` Closed! `scriptlike` Thanks, all!
Oct 17 2023
parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Tuesday, 17 October 2023 at 07:53:28 UTC, Vitaliy Fadeev wrote:
 On Tuesday, 17 October 2023 at 07:22:41 UTC, Vitaliy Fadeev 
 wrote:
 [...]
`scriptlike` looks perfecto! ```D enum JMP_ADDR_R = ... ... import scriptlike; writeln( mixin(interp!" asm { lea ${JMP_ADDR_R}, qword ptr [ ${JMP_I_R} * ${PTR_SIZE} + ${TBL_ADDR} ]; jmp [${JMP_ADDR_R}]; } ") ); ``` produced: ``` asm { lea RAX, qword ptr [ RDX * 8 + 5594AA134290 ]; jmp [RAX]; } ``` Closed! `scriptlike` Thanks, all!
Nice
Oct 17 2023