Re: ELIZA Chatbot Implementation From C to D Lang

2023-03-01 Thread ron77 via Digitalmars-d-learn

On Friday, 17 February 2023 at 17:03:34 UTC, ron77 wrote:
Hello, I succeeded in converting an ELIZA code from C to D, and 
here are the results. although I'm sure there are better ways 
to code it or to convert it...


```d
/* eliza.c
 * ys
 * original code by Weizenbaum, 1966
 * this rendition based on Charles Hayden's Java implementation 
from http://chayden.net/eliza/Eliza.html

 *
 * Note: There are certainly far more optimal and elegant ways 
to code this... we kept this

 * structure to be faithful to the original.  -scaz
 */

//#include 
import std.stdio;
import std.string;
import core.stdc.string;
import core.stdc.ctype;
//import std.stdlib
// #include 
//#include 

const NUMKEYWORDS =37;
const MAXLINELEN = 120;
const NUMSWAPS = 14;


const char*[] keywords= [
"CAN YOU","CAN I","YOU ARE","YOURE","I DONT","I FEEL",
"WHY DONT YOU","WHY CANT I","ARE YOU","I CANT","I AM","IM ",
"YOU ","I WANT","WHAT","HOW","WHO","WHERE",
"WHEN","WHY",
"NAME","CAUSE","SORRY","DREAM","HELLO","HI ","MAYBE",
" NO","YOUR","ALWAYS","THINK","ALIKE","YES","FRIEND",
"COMPUTER","CAR","NOKEYFOUND"];

const char*[2][NUMSWAPS] SWAPS = [
["ARE","AM"],
["WERE", "WAS"],
["YOU","I"],
["YOUR", "MY"],
["IVE", "YOU'VE"],
["IM", "YOU'RE"],
["YOU", "ME"],
["ME", "YOU"],
["AM","ARE"],
["WAS", "WERE"],
["I","YOU"],
["MY", "YOUR"],
["YOUVE", "I'VE"],
["YOURE", "I'M"]
];

int[NUMKEYWORDS] ResponsesPerKeyword= [
3,2,4,4,4,3,
3,2,3,3,4,4,
3,5,9,9,9,9,
9,9,
2,4,4,4,1,1,5,
5,2,4,3,7,3,6,
7,5,6];

const char *[9][NUMKEYWORDS] responses= [
[   "DON'T YOU BELIEVE THAT I CAN*",
"PERHAPS YOU WOULD LIKE TO BE ABLE TO*",
"YOU WANT ME TO BE ABLE TO*"],
[   "PERHAPS YOU DON'T WANT TO*",
"DO YOU WANT TO BE ABLE TO*"],
[   "WHAT MAKES YOU THINK I AM*",
"DOES IT PLEASE YOU TO BELIEVE I AM*",
"PERHAPS YOU WOULD LIKE TO BE*",
"DO YOU SOMETIMES WISH YOU WERE*"],
[   "WHAT MAKES YOU THINK I AM*",
"DOES IT PLEASE YOU TO BELIEVE I AM*",
"PERHAPS YOU WOULD LIKE TO BE*",
"DO YOU SOMETIMES WISH YOU WERE*"],
[   "DON'T YOU REALLY*",
"WHY DON'T YOU*",
"DO YOU WISH TO BE ABLE TO*",
"DOES THAT TROUBLE YOU?"],
[   "TELL ME MORE ABOUT SUCH FEELINGS.",
"DO YOU OFTEN FEEL*",
"DO YOU ENJOY FEELING*"],
[   "DO YOU REALLY BELIEVE I DON'T*",
"PERHAPS IN GOOD TIME I WILL*",
"DO YOU WANT ME TO*"],
[   "DO YOU THINK YOU SHOULD BE ABLE TO*",
"WHY CAN'T YOU*"],
[   "WHY ARE YOU INTERESTED IN WHETHER OR NOT I AM*",
"WOULD YOU PREFER IF I WERE NOT*",
"PERHAPS IN YOUR FANTASIES I AM*"],
[   "HOW DO YOU KNOW YOU CAN'T*",
"HAVE YOU TRIED?",
"PERHAPS YOU CAN NOW*"],
[   "DID YOU COME TO ME BECAUSE YOU ARE*",
"HOW LONG HAVE YOU BEEN*",
"DO YOU BELIEVE IT IS NORMAL TO BE*",
"DO YOU ENJOY BEING*"],
[   "DID YOU COME TO ME BECAUSE YOU ARE*",
"HOW LONG HAVE YOU BEEN*",
"DO YOU BELIEVE IT IS NORMAL TO BE*",
"DO YOU ENJOY BEING*"],
[   "WE WERE DISCUSSING YOU-- NOT ME.",
"OH, I*",
"YOU'RE NOT REALLY TALKING ABOUT ME, ARE YOU?"],
[   "WHAT WOULD IT MEAN TO YOU IF YOU GOT*",
"WHY DO YOU WANT*",
"SUPPOSE YOU SOON GOT*",
"WHAT IF YOU NEVER GOT*",
"I SOMETIMES ALSO WANT*"],
[   "WHY DO YOU ASK?",
"DOES THAT QUESTION INTEREST YOU?",
"WHAT ANSWER WOULD PLEASE YOU THE MOST?",
"WHAT DO YOU THINK?",
"ARE SUCH QUESTIONS ON YOUR MIND OFTEN?",
"WHAT IS IT THAT YOU REALLY WANT TO KNOW?",
"HAVE YOU ASKED ANYONE ELSE?",
"HAVE YOU ASKED SUCH QUESTIONS BEFORE?",
"WHAT ELSE COMES TO MIND WHEN YOU ASK THAT?"],
[   "WHY DO YOU ASK?",
"DOES THAT QUESTION INTEREST YOU?",
"WHAT ANSWER WOULD PLEASE YOU THE MOST?",
"WHAT DO YOU THINK?",
"ARE SUCH QUESTIONS ON YOUR MIND OFTEN?",
"WHAT IS IT THAT YOU REALLY WANT TO KNOW?",
"HAVE YOU ASKED ANYONE ELSE?",
"HAVE YOU ASKED SUCH QUESTIONS BEFORE?",
"WHAT ELSE COMES TO MIND WHEN YOU ASK THAT?"],
[   "WHY DO YOU ASK?",
"DOES THAT QUESTION INTEREST YOU?",
"WHAT ANSWER WOULD PLEASE YOU THE MOST?",
"WHAT DO YOU THINK?",
"ARE SUCH QUESTIONS ON YOUR MIND OFTEN?",
"WHAT IS IT THAT YOU REALLY WANT TO KNOW?",
"HAVE YOU ASKED ANYONE ELSE?",
"HAVE YOU ASKED SUCH QUESTIONS BEFORE?",
"WHAT ELSE COMES TO MIND WHEN YOU ASK THAT?"],
[   "WHY DO YOU ASK?",
"DOES THAT QUESTION INTEREST YOU?",
"WHAT ANSWER WOULD PLEASE YOU THE MOST?",
"WHAT DO YOU THINK?",

Re: foreach with assoc. array

2023-03-01 Thread Paul Backus via Digitalmars-d-learn

On Wednesday, 1 March 2023 at 19:05:10 UTC, DLearner wrote:

(1) & (2) compile and run with the expected results.
But (3) fails with:
```
Error: variable `wk_Idx` is shadowing variable 
`for3.main.wk_Idx`

```
Why is this usage wrong?


With `foreach`, you can't reuse an existing variable as the loop 
variable. It always declares a new one. If you want to reuse an 
existing variable for your loop, you have to use `for`.


foreach with assoc. array

2023-03-01 Thread DLearner via Digitalmars-d-learn

Hi

Please consider (1):
```
void main() {
   import std.stdio;

   int wk_Idx;

   int[2] IntArr;

   IntArr[0] = 1;
   IntArr[1] = 2;

   for (wk_Idx = 0; wk_Idx <= 1; wk_Idx = wk_Idx + 1) {

  writeln("wk_Idx = ", wk_Idx, " IntArr = ", IntArr[wk_Idx]);
   }
}
```

Now consider (2), which is (1) via assoc. array:
```
void main() {
   import std.stdio;

   int wk_Idx;

   int[int] IntArr;

   IntArr[0] = 1;
   IntArr[1] = 2;

   for (wk_Idx = 0; wk_Idx <= 1; wk_Idx = wk_Idx + 1) {

  writeln("wk_Idx = ", wk_Idx, " IntArr = ", IntArr[wk_Idx]);
   }
}
```

And finally (3) which is (2) via a foreach:
```
void main() {
   import std.stdio;

   int wk_Idx;

   int[int] IntArr;

   IntArr[0] = 1;
   IntArr[1] = 2;

   foreach (wk_Idx; IntArr.keys) {

  writeln("wk_Idx = ", wk_Idx, " IntArr = ", IntArr[wk_Idx]);
   }
}
```

(1) & (2) compile and run with the expected results.
But (3) fails with:
```
Error: variable `wk_Idx` is shadowing variable `for3.main.wk_Idx`
```
Why is this usage wrong?



Terminating the process of a running LDC2 compiler.

2023-03-01 Thread realhet via Digitalmars-d-learn

Hello,

Is it safe to kill an ongoing LDC2 process on Windows?

My situation is this:
- I launch 8 LDC2 compilation command lines on 8 DLang source 
files.

- One of them has a compilation error and quits.
- At this point I wait the completion of the other threads, but 
it would be faster to kill all those threads, fix the error, and 
run the multithreaded compilation again.


Is it safe to kill those processes, or would it be unreliable 
(because of the integrity of environment variables, and/or cached 
temp files)?


Thank You!


Re: How would the equivalent C type be in D?

2023-03-01 Thread rempas via Digitalmars-d-learn

On Wednesday, 1 March 2023 at 08:26:07 UTC, FeepingCreature wrote:


11 is SIGSEGV. A segfault, or access violation, happens when 
you try to access unallocated memory. In this case, let me 
annotate your code so it's easier to see what's happening:


```d
// null is the default value for a pointer
uint* value = null;
// because `value` is null, the first index also lies at null.
assert([0] is null);
// So we try to store screen.black_pixel at memory address 
null, which is unallocated.

value[0] = screen.black_pixel;
xcb_create_gc(connection, black, win, mask, value);
```

As there is no memory segment allocated at address null, the 
CPU indicates a segmentation fault, which terminates the 
program.


So yes, `xcb_create_gc` wants a `uint*` parameter, but not just 
any `uint*` will do: it has to point to valid memory. Going 
back to the first snippet, what's happening here is that in C, 
arrays implicitly convert to pointers, because C doesn't have a 
notion of array types as distinct from pointer types. So you 
can have a variable declared as `uint[1]`, but pass it to a 
parameter that expects `uint*`, and the value that is passed 
will just be the address of the first field of the array. 
However, even in C, if you try to define `value` as `uint*`, it 
will segfault in the same way. Instead, in D, you need to tell 
the compiler to define an array of size 1, and then pass a 
pointer to the array's first member explicitly:


```d
uint32_t[1] value;
value[0] = screen.black_pixel;
// this is what C does under the hood
xcb_create_gc(connection, black, win, mask, [0]);
```

Or shorter, but with the same effect:

```d
uint32_t[1] value;
value[0] = screen.black_pixel;
xcb_create_gc(connection, black, win, mask, value.ptr);
```


Thank you! You are amazing for explaining it! I was so focused on 
thinking that I'm doing something wrong with the type that I 
didn't noticed that the pointers, points to nowhere so the 
function obviously has nowhere to write to. Like... OMG! And I 
want to make a fully fledged compiler when making stupid mistakes 
like that. Btw, When I executed the program, I got "Error Program 
exited with code -11". You said that the code was "11". What 
about that dash? If it is not a "minus" and it's just the dash 
symbol, then what's the idea?


Re: Linking phobos64.lib errors

2023-03-01 Thread Elfstone via Digitalmars-d-learn

On Wednesday, 1 March 2023 at 07:15:36 UTC, Elfstone wrote:

https://forum.dlang.org/post/edwmddiqcsnwpliyo...@forum.dlang.org

On Wednesday, 3 September 2014 at 09:13:52 UTC, Szymon Gatner 
wrote:
On Wednesday, 3 September 2014 at 08:47:35 UTC, Rikki 
Cattermole wrote:

[...]


Hey, for some reason I missed this:

http://forum.dlang.org/thread/ji93fh$15ph$1...@digitalmars.com

The fix is to add main() function to D library. Don't know why 
it is needed for static lib but it sure helps to resolve link 
issues.


Strangely I just got similar link errors (phobos64.lib ... 
unresolved ...) building dynamic lib on Win11 (no errors on 
Win10, or with ldc). I found this thread and adding main() 
saved me.


Any expert can explain why and when main() is needed for 
dynamic, or static lib?


Oh, I'm not saved. Because now I get "only one `main` allowed" 
error when I link the lib.


Re: How would the equivalent C type be in D?

2023-03-01 Thread FeepingCreature via Digitalmars-d-learn

On Wednesday, 1 March 2023 at 08:26:07 UTC, FeepingCreature wrote:

```d
uint32_t[1] value;
value[0] = screen.black_pixel;
xcb_create_gc(connection, black, win, mask, value.ptr);
```


To expand on this:

```d
uint32_t[2] value;
uint32_t* value_ptr = value.ptr;
// We are allowed to access the pointer at index 0
// because we declared the value it points to, to be size 2.
value_ptr[0] = 0;
// This is also allowed, because `value_ptr` is
// a pointer to two sequential `uint32_t` in memory.
value_ptr[1] = 0;
// But this access would segfault, because it's trying to write
// to the third element of a two-element array:
value_ptr[2] = 0;
```

Note: I just lied; `value_ptr[2]` would not segfault, for 
somewhat technical reasons; but it *would* corrupt your program's 
memory. At any rate, it's an invalid operation.




Re: How would the equivalent C type be in D?

2023-03-01 Thread FeepingCreature via Digitalmars-d-learn

On Wednesday, 1 March 2023 at 08:12:05 UTC, rempas wrote:
I'm looking into 
[this](https://www.x.org/releases/X11R7.7/doc/libxcb/tutorial/index.html) tutorial to learn XCB and I'm trying to write the code in D with betterC. In the section 9.1 (sorry, I cannot give a section link, the article does not give us this ability), I'm facing a problem and my program exits with the exit code: "-11". I suspect that this happens because I haven't translated the following code the right way:


```d
uint32_t value[1];
value[0] = screen.black_pixel;
xcb_create_gc(connection, black, win, mask, value);
```

I originally tried to translate this as:

```d
uint[1] value;
value[0] = screen.black_pixel;
xcb_create_gc(connection, black, win, mask, value);
```

But then, when I tried to compile the program, I got the 
following error:


```
Error: function `headers.xcb_create_gc(xcb_connection_t* c, 
uint cid, uint drawable, uint value_mask, const(void)* 
value_list)` is not callable using argument types 
`(xcb_connection_t*, uint, uint, uint, uint[1])`
src/draw.d(18,16):cannot pass argument `value` of type 
`uint[1]` to parameter `const(void)* value_list`

```

So I thought of doing the following:

```d
uint* value;
value[0] = screen.black_pixel;
xcb_create_gc(connection, black, win, mask, value);
```

Now the program complies but I get the "-11" exit code. Another 
thing I thought (which is probably the same thing under the 
hood but done a different way):


```d
const(void)* value;
(cast(ubyte*)value)[0] = screen.black_pixel;
xcb_create_gc(connection, black, win, mask, value);
```

Same results. Any ideas?


11 is SIGSEGV. A segfault, or access violation, happens when you 
try to access unallocated memory. In this case, let me annotate 
your code so it's easier to see what's happening:


```d
// null is the default value for a pointer
uint* value = null;
// because `value` is null, the first index also lies at null.
assert([0] is null);
// So we try to store screen.black_pixel at memory address null, 
which is unallocated.

value[0] = screen.black_pixel;
xcb_create_gc(connection, black, win, mask, value);
```

As there is no memory segment allocated at address null, the CPU 
indicates a segmentation fault, which terminates the program.


So yes, `xcb_create_gc` wants a `uint*` parameter, but not just 
any `uint*` will do: it has to point to valid memory. Going back 
to the first snippet, what's happening here is that in C, arrays 
implicitly convert to pointers, because C doesn't have a notion 
of array types as distinct from pointer types. So you can have a 
variable declared as `uint[1]`, but pass it to a parameter that 
expects `uint*`, and the value that is passed will just be the 
address of the first field of the array. However, even in C, if 
you try to define `value` as `uint*`, it will segfault in the 
same way. Instead, in D, you need to tell the compiler to define 
an array of size 1, and then pass a pointer to the array's first 
member explicitly:


```d
uint32_t[1] value;
value[0] = screen.black_pixel;
// this is what C does under the hood
xcb_create_gc(connection, black, win, mask, [0]);
```

Or shorter, but with the same effect:

```d
uint32_t[1] value;
value[0] = screen.black_pixel;
xcb_create_gc(connection, black, win, mask, value.ptr);
```



How would the equivalent C type be in D?

2023-03-01 Thread rempas via Digitalmars-d-learn
I'm looking into 
[this](https://www.x.org/releases/X11R7.7/doc/libxcb/tutorial/index.html) tutorial to learn XCB and I'm trying to write the code in D with betterC. In the section 9.1 (sorry, I cannot give a section link, the article does not give us this ability), I'm facing a problem and my program exits with the exit code: "-11". I suspect that this happens because I haven't translated the following code the right way:


```d
uint32_t value[1];
value[0] = screen.black_pixel;
xcb_create_gc(connection, black, win, mask, value);
```

I originally tried to translate this as:

```d
uint[1] value;
value[0] = screen.black_pixel;
xcb_create_gc(connection, black, win, mask, value);
```

But then, when I tried to compile the program, I got the 
following error:


```
Error: function `headers.xcb_create_gc(xcb_connection_t* c, uint 
cid, uint drawable, uint value_mask, const(void)* value_list)` is 
not callable using argument types `(xcb_connection_t*, uint, 
uint, uint, uint[1])`
src/draw.d(18,16):cannot pass argument `value` of type 
`uint[1]` to parameter `const(void)* value_list`

```

So I thought of doing the following:

```d
uint* value;
value[0] = screen.black_pixel;
xcb_create_gc(connection, black, win, mask, value);
```

Now the program complies but I get the "-11" exit code. Another 
thing I thought (which is probably the same thing under the hood 
but done a different way):


```d
const(void)* value;
(cast(ubyte*)value)[0] = screen.black_pixel;
xcb_create_gc(connection, black, win, mask, value);
```

Same results. Any ideas?