WinDbg, 449 388 bytes
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 bytes, definindo alias para código repetido
Inspirado pelo uso de LambdaBeta#define
. Essa abordagem modifica ligeiramente a sintaxe do WordMath ( ,
e .
deve ser delimitada por espaço como as outras palavras, e ,
não segue repeat
) e cria um alias para que a sintaxe do WordMath modificada seja um código WinDbg válido. A última linha faz o que a pergunta pede e transpila, convertendo a entrada na sintaxe modificada.
A entrada é obtida definindo uma sequência em um endereço de memória e configurando o pseudo-registro $t0
para esse endereço. Nota: isso substituirá o int
at 0x2000000
, portanto, se você iniciar sua string lá, ela será parcialmente substituída. $t0
também será substituído.
Como ele cria aliases, dependendo se esse código foi executado antes ou depois de definir a sequência, o código de saída será diferente (alias ou não). Infelizmente, não encontrei uma maneira de fazer com que os aliases se expandissem corretamente sem serem delimitados por espaços em branco (o que significa que o script do WordMath não poderia ser executado diretamente diretamente sem ser transformado primeiro).
Como funciona:
* $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 " ."
Exemplo de saída, inserindo a string antes de executar este código uma vez (o programa resultante se assemelha ao 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
Exemplo de saída, inserindo a sequência após a execução desse código uma vez (os aliases são expandidos ao inserir a sequência, para que o programa resultante não seja tão bonito):
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
Um pouco mais de exemplo de saída, apenas usando a sintaxe do WordMath ligeiramente modificada:
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