WinDbg, 449 388 byte
as, }@$t1
as. }0;?@$t0
asThink n10;ed8<<22;r$t0=dwo(8<<22);r$t1=0;.do{
aSa " "
asQ .printf";r$t1=1;.do{r$t1=@$t1-1;r$t0=@$t0
as/c add Q +"
aSby " "
as/c divide Q /"
asfrom 0;r$t0=-@$t0+
as/c multiply Q *"
aSnumber " "
aSof " "
asrepeat +1
as/c subtract Q -"
.for(r$t9=1;by(@$t0);r$t0=@$t0+1){j44!=by(@$t0) .printf"%c",by(@$t0);.if116!=by(@$t0-1){.printf" , "}};.printf"\b ."
-61 byte definendo l'alias per il codice ripetuto
Ispirato dall'uso di LambdaBeta di #define
. Questo approccio modifica leggermente la sintassi di WordMath ( ,
e .
deve essere delimitata da spazi come le altre parole e ,
non seguerepeat
) e crea un alias tale che la sintassi di WordMath modificata sia un codice WinDbg valido. L'ultima riga fa ciò che la domanda pone e traspila convertendo l'input nella sintassi modificata.
L'input viene preso impostando una stringa su un indirizzo di memoria e impostando lo pseudo-registro $t0
su quell'indirizzo. Nota: questo sovrascriverà int
at 0x2000000
, quindi se inizi la tua stringa lì, sarà parzialmente sovrascritto.$t0
verrà anche sovrascritto.
Poiché crea alias, a seconda che questo codice sia stato eseguito prima o dopo l'impostazione della stringa, il codice di output sarà diverso (alias o no). Sfortunatamente, non ho trovato il modo di far espandere correttamente gli alias senza essere delimitati da spazi bianchi (il che significa che lo script WordMath non poteva essere eseguito direttamente senza essere prima trasformato).
Come funziona:
* $t1 is used for repeating and $t0 is used to read the input and hold the accumulator
* Alias , to }@$t1 -- closing do-while loop and allowing repeat
as , }@$t1
* Alias . to }0;?@$t0 -- close do-while loop and evaluate $t0 (accumulator)
as . }0;?@$t0
* Alias Think to (note this is one line)
as Think n10; * Set base 10
ed 8<<22; * Read ints to address 0x2000000. Enter nothing to exit input mode
r$t0 = dwo(8<<22); * Set $t0 = first int
r$t1=0;.do{ * Open do-while
* Alias a to nothing
aS a " "
* Alias add to (note one line):
as add ; * Close previous statement
r$t1=1;.do{r$t1=@$t1-1; * Open do-while (once) loop
r$t0=@$t0+ * Add number to $t0
* Alias by to nothing
aS by " "
* Alias divide to (note one line):
as divide ; * Close previous statement
r$t1=1;.do{r$t1=@$t1-1; * Open do-while (once) loop
r$t0=@$t0/ * Divide next number from $t0
* Alias from to (note one line):
as from 0; * Preceding subtract statement subtracts 0
r$t0=-@$t0+ * Subtract $t0 from next number
* Alias multiply to (note one line):
as multiply ; * Close previous statement
r$t1=1;.do{r$t1=@$t1-1; * Open do-while (once) loop
r$t0=@$t0* * Multiply next number with $t0
* Alias number to nothing
aS number " "
* Alias of to nothing
aS of " "
* Alias repeat to +1 making do-while (once) loops into do-while (once)+1
as repeat +1
* Alias subtract to (note one line):
as subtract ; * Close previous statement
r$t1=1;.do{r$t1=@$t1-1; * Open do-while (once) loop
r$t0=@$t0- * Subtract next number from $t0
.for (r$t9=1; by(@$t0); r$t0=@$t0+1) * Enumerate the string
{
j 44!=by(@$t0) * If not comma
.printf "%c",by(@$t0); * Print the char
* implicit else
.if 116!=by(@$t0-1) * Else if the previous char is not t
{
.printf " , " * Print the comma with spaces around it
}
};
.printf "\b ." * Replacing ending "." with " ."
Output di esempio, immettendo la stringa prima di eseguire questo codice una volta (il programma risultante è simile a WordMath):
0:000> r$t0=8<<22
0:000> eza8<<22"Think of a number, add 5, add 10, multiply by 2, subtract 15, repeat, divide by 2."
0:000> as, }@$t1
0:000> as. }0;?@$t0
0:000> asThink n10;ed8<<22;r$t0=dwo(8<<22);r$t1=0;.do{
0:000> aSa " "
0:000> asadd ;r$t1=1;.do{r$t1=@$t1-1;r$t0=@$t0+
0:000> aSby " "
0:000> asdivide ;r$t1=1;.do{r$t1=@$t1-1;r$t0=@$t0/
0:000> asfrom 0;r$t0=-@$t0+
0:000> asmultiply ;r$t1=1;.do{r$t1=@$t1-1;r$t0=@$t0*
0:000> aSnumber " "
0:000> aSof " "
0:000> asrepeat +1
0:000> assubtract ;r$t1=1;.do{r$t1=@$t1-1;r$t0=@$t0-
0:000> .for(r$t9=1;by(@$t0);r$t0=@$t0+1){j44!=by(@$t0) .printf"%c",by(@$t0);.if116!=by(@$t0-1){.printf" , "}};.printf"\b ."
Think of a number , add 5 , add 10 , multiply by 2 , subtract 15 , repeat divide by 2 }0;?@$t0
0:000> Think of a number , add 5 , add 10 , multiply by 2 , subtract 15 , repeat divide by 2 }0;?@$t0
base is 10
02000000 6e696854 18
18
02000004 666f206b
Evaluate expression: 18 = 00000012
Output di esempio, immettendo la stringa dopo che questo codice è stato eseguito una volta (gli alias vengono espansi quando si inserisce la stringa in modo che il programma risultante non sia così carino):
0:000> r$t0=8<<22
0:000> eza8<<22"Think of a number, add 5, add 10, multiply by 2, subtract 15, repeat, divide by 2."
0:000> as, }@$t1
0:000> as. }0;?@$t0
0:000> asThink n10;ed8<<22;r$t0=dwo(8<<22);r$t1=0;.do{
0:000> aSa " "
0:000> asadd ;r$t1=1;.do{r$t1=@$t1-1;r$t0=@$t0+
0:000> aSby " "
0:000> asdivide ;r$t1=1;.do{r$t1=@$t1-1;r$t0=@$t0/
0:000> asfrom 0;r$t0=-@$t0+
0:000> asmultiply ;r$t1=1;.do{r$t1=@$t1-1;r$t0=@$t0*
0:000> aSnumber " "
0:000> aSof " "
0:000> asrepeat +1
0:000> assubtract ;r$t1=1;.do{r$t1=@$t1-1;r$t0=@$t0-
0:000> .for(r$t9=1;by(@$t0);r$t0=@$t0+1){j44!=by(@$t0) .printf"%c",by(@$t0);.if116!=by(@$t0-1){.printf" , "}};.printf"\b ."
n10;ed8<<22;r$t0=dwo(8<<22);r$t1=0;.do{ number , ;r$t1=1;.do{r$t1=@$t1-1;r$t0=@$t0+ 5 , ;r$t1=1;.do{r$t1=@$t1-1;r$t0=@$t0+ 10 , ;r$t1=1;.do{r$t1=@$t1-1;r$t0=@$t0* 2 , ;r$t1=1;.do{r$t1=@$t1-1;r$t0=@$t0- 15 , repeat ;r$t1=1;.do{r$t1=@$t1-1;r$t0=@$t0/ 2 }0;?@$t0
0:000> n10;ed8<<22;r$t0=dwo(8<<22);r$t1=0;.do{ number , ;r$t1=1;.do{r$t1=@$t1-1;r$t0=@$t0+ 5 , ;r$t1=1;.do{r$t1=@$t1-1;r$t0=@$t0+ 10 , ;r$t1=1;.do{r$t1=@$t1-1;r$t0=@$t0* 2 , ;r$t1=1;.do{r$t1=@$t1-1;r$t0=@$t0- 15 , repeat ;r$t1=1;.do{r$t1=@$t1-1;r$t0=@$t0/ 2 }0;?@$t0
base is 10
02000000 3b30316e 26
26
02000004 3c386465
Evaluate expression: 26 = 0000001a
Qualche ulteriore output di esempio, usando semplicemente la sintassi di WordMath leggermente modificata:
0:000> Think of a number , add 1 , repeat repeat repeat divide by 3 .
base is 10
02000000 0000001a 3
3
02000004 3c386465
Evaluate expression: 2 = 00000002
0:000> Think of a number , divide by 5 , subtract from 9 .
base is 10
02000000 00000003 29
29
02000004 3c386465
Evaluate expression: 4 = 00000004