Re: Game and GC

2018-02-22 Thread Norm via Digitalmars-d-learn

On Friday, 23 February 2018 at 01:54:07 UTC, Leonardo wrote:

Hi, I'm new to language and games.
Many people say that GC is bad and can slow down your project 
in some moments.
What can happen if I create a game using D without worrying 
with memory management?

(using full GC)


Have a look at https://github.com/gecko0307/atrium and see how 
memory is handled there.


TBH though every game I've written I have not worried about the 
GC and just code it up. This works fine for 2d games, platformers 
etc. If it ever does bite you can always schedule the pauses 
(they are deterministic in the sense a collect will occur on 
allocation) or do pretty much what every game does in C++/C and 
allocate in pools.


Cheers,
Norm


Re: Game and GC

2018-02-22 Thread Mike Franklin via Digitalmars-d-learn

On Friday, 23 February 2018 at 01:54:07 UTC, Leonardo wrote:

Hi, I'm new to language and games.
Many people say that GC is bad and can slow down your project 
in some moments.
What can happen if I create a game using D without worrying 
with memory management?

(using full GC)


Don't let the GC prevent you from writing a game in D.  D is the 
most flexible language I have ever used; you just need to learn 
how to deal with the GC in your game.


Jonathan M. Davis gave you some good advice and explained the 
fundamental problem with the GC in real time games (the potential 
for pauses during reclamation).  You can avoid that in a number 
of ways like temporarily disabling the GC during the real-time 
part of your game, just to name one.  More can be found in the 
resources below.


https://wiki.dlang.org/Memory_Management
http://p0nce.github.io/d-idioms/#The-impossible-real-time-thread

automem (https://github.com/atilaneves/automem) also gives you 
reference counting in D (C++ style), if that will work better for 
your use case.


Mike


Re: Game and GC

2018-02-22 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 23 February 2018 at 01:54:07 UTC, Leonardo wrote:
What can happen if I create a game using D without worrying 
with memory management?

(using full GC)


It depends what kind of game it is and how sloppy your code is in 
general.


Every game I have written in D i don't think about it, and it is 
fine. But I also prefer to do simpler games (think 80's or 90's 
style more than the modern stuff) anyway.


Re: Game and GC

2018-02-22 Thread Leonardo via Digitalmars-d-learn
On Friday, 23 February 2018 at 02:16:38 UTC, Jonathan M Davis 
wrote:
The GC won't slow down your code in general (in fact, it will 
probably speed it up in comparison to reference counting), but 
whenever the GC does a collection, that means that it stops all 
threads that it manages. So, you could suddenly have everything 
stop for 100ms (the actual length of a collection is going to 
depend on how much memory the GC has to scan, and I don't know 
what the typical length of a collection is; that will depend on 
the program). For programs that can afford to occasionally stop 
like that, that's not a problem. For a game that's trying to 
maintain 60fps, that's likely a really big deal.



- Jonathan M Davis


That's what I thought for a game, but probably no one tested yet 
to see the impact. Thanks, I'll read on.


Re: Game and GC

2018-02-22 Thread Leonardo via Digitalmars-d-learn
On Friday, 23 February 2018 at 02:02:12 UTC, StickYourLeftFootIn 
wrote:

On Friday, 23 February 2018 at 01:54:07 UTC, Leonardo wrote:

Hi, I'm new to language and games.
Many people say that GC is bad and can slow down your project 
in some moments.
What can happen if I create a game using D without worrying 
with memory management?

(using full GC)


What do you think will happen? Anytime you delegate power to 
something else what can go wrong? Nothing is perfect.


The GC exists to automate a job. The job it does is not the 
problem... It does it well. The issue is when it does it. It's 
like the noisy garbage man coming in as 3AM to get your 
trash... are you ok with that? Some people are.


I understand, I'm not saying GC is bad, but I want to know if it 
would be slow to the point of being noticeable in a game. If so, 
what is the best way to do this? Placing @nogc everywhere? Thanks.


Re: Game and GC

2018-02-22 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, February 23, 2018 01:54:07 Leonardo via Digitalmars-d-learn 
wrote:
> Hi, I'm new to language and games.
> Many people say that GC is bad and can slow down your project in
> some moments.
> What can happen if I create a game using D without worrying with
> memory management?
> (using full GC)

The GC won't slow down your code in general (in fact, it will probably speed
it up in comparison to reference counting), but whenever the GC does a
collection, that means that it stops all threads that it manages. So, you
could suddenly have everything stop for 100ms (the actual length of a
collection is going to depend on how much memory the GC has to scan, and I
don't know what the typical length of a collection is; that will depend on
the program). For programs that can afford to occasionally stop like that,
that's not a problem. For a game that's trying to maintain 60fps, that's
likely a really big deal.

There are a number of ways to handle it, though the biggest is to simply
minimize how much you allocate on the GC heap and how much memory has to be
scanned for pointers that refer to GC-allocated memory. Other stuff includes
disabling the GC while critical pieces of code are running and having
critical threads not be managed by the GC or use GC-allocated memory.

I would suggest that you read this series of articles on the official D
blog:

https://dlang.org/blog/the-gc-series/

- Jonathan M Davis



Re: Game and GC

2018-02-22 Thread StickYourLeftFootIn via Digitalmars-d-learn

On Friday, 23 February 2018 at 01:54:07 UTC, Leonardo wrote:

Hi, I'm new to language and games.
Many people say that GC is bad and can slow down your project 
in some moments.
What can happen if I create a game using D without worrying 
with memory management?

(using full GC)


What do you think will happen? Anytime you delegate power to 
something else what can go wrong? Nothing is perfect.


The GC exists to automate a job. The job it does is not the 
problem... It does it well. The issue is when it does it. It's 
like the noisy garbage man coming in as 3AM to get your trash... 
are you ok with that? Some people are.


Game and GC

2018-02-22 Thread Leonardo via Digitalmars-d-learn

Hi, I'm new to language and games.
Many people say that GC is bad and can slow down your project in 
some moments.
What can happen if I create a game using D without worrying with 
memory management?

(using full GC)



Re: How to use strip or stripRight on char[len] ? Thanks.

2018-02-22 Thread FrankLike via Digitalmars-d-learn
On Thursday, 22 February 2018 at 18:02:11 UTC, Adam D. Ruppe 
wrote:


You don't strip that at all, the function writes a 
zero-terminated string to the buffer.


Thank you very much ! I forgot it.



Re: Function overloading between modules

2018-02-22 Thread ketmar via Digitalmars-d-learn

JN wrote:


same idea?


absolutely the same. non-qualified imports (be it template, or function) 
won't take part in overload resoultion.


Re: Function overloading between modules

2018-02-22 Thread JN via Digitalmars-d-learn

On Thursday, 22 February 2018 at 21:19:12 UTC, ketmar wrote:


yes. this is done so unqualified won't silently "steal" your 
functions. this can cause some unexpected (and hard to find) 
bugs.


if you want it to work, you can either do qualified import

import bar : foo;

or manuall bring overloads from `bar` with

alias foo = bar.foo;


I see, how about this one:

bar.d
---
void foo(T)(T t)
{
}
app.d
---
import std.stdio;
import bar;
void foo(T : string)(T t)
{
}
void main()
{
   foo(123);
};

same idea?


Re: Function overloading between modules

2018-02-22 Thread ketmar via Digitalmars-d-learn

JN wrote:


Is this expected behaviour?

bar.d
---
void foo(string s)
{
}


app.d
---

import std.stdio;
import bar;

void foo(int x)
{
}

void main()
{
   foo("hi");
};


===
Error: function app.foo (int x) is not callable using argument types 
(string)


yes. this is done so unqualified won't silently "steal" your functions. 
this can cause some unexpected (and hard to find) bugs.


if you want it to work, you can either do qualified import

import bar : foo;

or manuall bring overloads from `bar` with

alias foo = bar.foo;


Function overloading between modules

2018-02-22 Thread JN via Digitalmars-d-learn

Is this expected behaviour?

bar.d
---
void foo(string s)
{
}


app.d
---

import std.stdio;
import bar;

void foo(int x)
{
}

void main()
{
  foo("hi");
};


===
Error: function app.foo (int x) is not callable using argument 
types (string)


Re: array/Array: "hard" bounds checking

2018-02-22 Thread TheFlyingFiddle via Digitalmars-d-learn

On Thursday, 22 February 2018 at 12:50:43 UTC, ag0aep6g wrote:

On 02/22/2018 10:39 AM, bauss wrote:
On Thursday, 22 February 2018 at 05:22:19 UTC, TheFlyingFiddle 
wrote:


Eg:

uint a = 3;
int b = -1;

assert(a > b); //No idea what should happen here.


This is what happens:

assert(cast(int)a > b);


Nope. It's `assert(a > cast(uint)b);`.


These two posts kind of proved my point :D. And that is why you 
should never mix signed and unsigned integers. A good thing is 
that dscanner static analysis will warn you about this stuff (in 
simple cases at-least).


Re: How to use strip or stripRight on char[len] ? Thanks.

2018-02-22 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 22 February 2018 at 17:44:53 UTC, FrankLike wrote:

SHGetSpecialFolderPath(NULL,cast(char*)Path.ptr,CSIDL_DESKTOP,0);


You don't strip that at all, the function writes a 
zero-terminated string to the buffer. So either: use the pointer 
as-is to other C functions, or  call the `fromStringz` function 
in Phobos to convert it and copy it if you want to store it.


http://dpldocs.info/experimental-docs/std.string.fromStringz.html

import std.string;
string s = fromStringz(abc.ptr).idup; // the idup copies it out


Re: Tuts/Aritcles: Incrementasl C++-to-D conversion?

2018-02-22 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Feb 22, 2018 at 10:43:24AM +0200, ketmar via Digitalmars-d-learn wrote:
> Nick Sabalausky (Abscissa) wrote:
> 
> > Are there any tutorials or articles out there for "getting started
> > with converting a C++ codebase to D one module at a time?" Or at the
> > very least: tips, tricks, lessions learned, from those who have come
> > before.
>
> from my experience (various codebases up to middle size, mostly C,
> some C++): fsck the "one module at a time" idea! even in D modules are
> interwined, and in C and C++ they're even more so. besides, converting
> tests is tedious, it is much funnier to have something working.

I'm in the middle of the process of converting an old C++ project right
now (it's so old that most of it dates back to C++98, and needs to be
compiled with options to suppress C++11 deprecations and errors).  I
started out with the idea of incremental conversion, and managed to get
as far as converting main() to D via shims to transition between the
D/C++ boundary.  The straw that broke the camel's back, though: C++
exceptions that need to percolate all the way to the top-level.  I know
people have mentioned Calypso, etc., but I'm mainly using dmd right now,
and I really don't like the idea of putting a bunch of effort into using
a different toolchain just to be able to incrementally migrate a lousy
C++ project.

So I'm starting to agree with ketmar: just jump into D cold turkey and
convert the whole dang thing in one go.

Actually, what I ended up doing right now is no longer converting, but
rewriting individual modules in D.  It's more effort, but I'm aiming for
more idiomatic D code, and also the whole reason I started out on this
task is because I want to refactor the code to fix some fundamental
design flaws, but I really do not want to work with C++ anymore.  So it
doesn't make sense to me to struggle with making C++ code compile in D,
only to immediately afterwards rewrite it anyway.  Why not rewrite it
right now, using the C++ code as a reference rather than a direct
source.


[...]
> tip: try to not rewrite code in any way until it works. i know how
> tempting it to replace "just this tiny thing, it is so ugly, and in D
> we have a nice idiom!" NEVAR. this is by far the most important thing
> to remember (at least for me), so i'll repeat it again: no code
> modifications until it works!

So I'm taking the wrong approach, perhaps. :-D  But basically, I'm
starting out with the leaf node modules and rewriting it in D, complete
with unittests and what-not, to ensure it's semantically correct.
Perhaps when I get to the main code I may have to compromise on my
rewrite approach, because it will be too big to rewrite in one go.  But
we'll see.

One thing that's *really* helping is D's built-in unittests.  With this
approach, I can actually just comment out the C++ code, then gradually
uncomment individual functions one at a time, translate to D, then add
unittests to make sure it works correctly.  This is extremely helpful
because I don't need to have a working main() to test individual
translated functions -- that wouldn't work because too many things
depend on too many other things, so requiring a functional main() means
rewriting the entire codebase before you get something that's testable.
There's just too much room for human error there.  Being able to
translate C++ functions one at a time and have unittests to ensure I
didn't introduce any bugs, is a HUGE help.  It helps build confidence
that I'm actually making progress (instead of writing a huge amount of
code that ultimately doesn't work!).


[...]
> otherwise, sweat and blood, and patience.

+1. Yep, it's a lot of work.  But I'm chugging away at it.  Can't wait
till the thing is finally running in D, and I can kill off the old C++
sources forever.  Just too much unmaintainable ugliness there that I'd
rather not remember.


T

-- 
"I'm not childish; I'm just in touch with the child within!" - RL


Re: How to use strip or stripRight on char[len] ? Thanks.

2018-02-22 Thread FrankLike via Digitalmars-d-learn
On Thursday, 22 February 2018 at 16:59:40 UTC, Adam D. Ruppe 
wrote:

On Thursday, 22 February 2018 at 16:55:14 UTC, FrankLike wrote:

 char[100] abc ="aabc";
 auto abcaa = ((abc).dup).stripRight;


try:

auto abcaa = stripRight(abc[])


Now,I want to get the result:

 char[100] Path;
 writeln("will get path ");
 SHGetSpecialFolderPath(NULL,cast(char*)Path.ptr,CSIDL_DESKTOP,0);

if use char[] Path;

It not get the result.

How to strip the Path? Thanks.


Re: How to use strip or stripRight on char[len] ? Thanks.

2018-02-22 Thread FrankLike via Digitalmars-d-learn

On Thursday, 22 February 2018 at 17:08:14 UTC, FrankLike wrote:
On Thursday, 22 February 2018 at 16:59:40 UTC, Adam D. Ruppe 
wrote:

On Thursday, 22 February 2018 at 16:55:14 UTC, FrankLike wrote:


It is simply that these functions require a slice so it can 
resize it and you can't resize a static array.



char[100] abc ="aabc";
char[] p = abc;
string aa = to!string(p);
 string aaa = aa.stripRight;


 It get the same result too.




Re: How to use strip or stripRight on char[len] ? Thanks.

2018-02-22 Thread FrankLike via Digitalmars-d-learn
On Thursday, 22 February 2018 at 16:59:40 UTC, Adam D. Ruppe 
wrote:

On Thursday, 22 February 2018 at 16:55:14 UTC, FrankLike wrote:


It is simply that these functions require a slice so it can 
resize it and you can't resize a static array.


   char[100] abc ="aabc";
   string aa = to!string(abc);
   string aaa = aa.stripRight;


It get the same result.










Re: How to use strip or stripRight on char[len] ? Thanks.

2018-02-22 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 22 February 2018 at 16:55:14 UTC, FrankLike wrote:

 char[100] abc ="aabc";
 auto abcaa = ((abc).dup).stripRight;


try:

auto abcaa = stripRight(abc[])

just make sure you do NOT return that abcaa from the function or 
store it in an object. It still refers to the static array that 
does not outlive that function.


It is simply that these functions require a slice so it can 
resize it and you can't resize a static array.


How to use strip or stripRight on char[len] ? Thanks.

2018-02-22 Thread FrankLike via Digitalmars-d-learn

Hi,everyone,
How to use strip or stripRight on char[len]?

For example:

string abcs ="aabc";
auto abcsaa = abcs.stripRight;
writeln(abcsaa);
writeln("---abcsaa--stripRight ok ");


 char[100] abc ="aabc";
 auto abcaa = ((abc).dup).stripRight;
 writeln(abcaa);
writeln("stripRight error- ");

Where is error? Thanks.


Re: Negative index range violation

2018-02-22 Thread SrMordred via Digitalmars-d-learn
On Thursday, 22 February 2018 at 02:41:30 UTC, Steven 
Schveighoffer wrote:

Hah! I never thought of doing a slice with negative indexes ;)


Maybe is my past of python:

arr[-3:] to get the last 3 elements for eg.

:)


Re: Negative index range violation

2018-02-22 Thread Timon Gehr via Digitalmars-d-learn

On 22.02.2018 01:26, Adam D. Ruppe wrote:

On Thursday, 22 February 2018 at 00:13:43 UTC, SrMordred wrote:

string x = "123";
auto c = x.ptr;
c++;
writeln(c[-1]); // 1


That's only happening because pointers bypass range checks.


writeln(c[-1..0]); //BOOM Range violation


But with a slice negative indexes are never allowed, even on a pointer.


Actually, it's slightly more complicated than that. E.g. c[-2..-1] does 
not trigger the range check and will behave as expected. For slices 
c[l..r] there is a check whether cast(size_t)l<=cast(size_t)r. The range 
violation happens because -1 is larger than 0 as an unsigned integer.


Re: Understanding the AST...

2018-02-22 Thread joe via Digitalmars-d-learn

On Thursday, 22 February 2018 at 14:53:11 UTC, Seb wrote:

On Tuesday, 6 February 2018 at 12:03:06 UTC, joe wrote:

Hello everybody!

Last week end I found this post ( 
https://dlang.org/blog/2017/08/01/a-dub-case-study-compiling-dmd-as-a-library/ ) on the Blog and thought to myself awesome.


[...]



BTW I know it's not as powerful as DMD (and not the real 
thing), but often the AST XML dump from DScanner helps to 
deepen the understanding:


https://github.com/dlang-community/D-Scanner#ast-dump

You can even play with libdparse on the web:

https://run.dlang.io/is/qZsGDD


Hello Seb,

I had a look at the resources you provided and they are quite 
useful. Thank you.


However, while technically a lexer would be enough to solve the 
problem at hand, I think I'm going to want that full front-end a 
bit later.


The more information, the better. I rather have the option to 
ignore something I don't need than to need something I don't have 
:)





Re: Understanding the AST...

2018-02-22 Thread Seb via Digitalmars-d-learn

On Tuesday, 6 February 2018 at 12:03:06 UTC, joe wrote:

Hello everybody!

Last week end I found this post ( 
https://dlang.org/blog/2017/08/01/a-dub-case-study-compiling-dmd-as-a-library/ ) on the Blog and thought to myself awesome.


[...]



BTW I know it's not as powerful as DMD (and not the real thing), 
but often the AST XML dump from DScanner helps to deepen the 
understanding:


https://github.com/dlang-community/D-Scanner#ast-dump

You can even play with libdparse on the web:

https://run.dlang.io/is/qZsGDD


Re: Understanding the AST...

2018-02-22 Thread joe via Digitalmars-d-learn

On Thursday, 22 February 2018 at 13:44:51 UTC, RazvanN wrote:

On Thursday, 22 February 2018 at 13:21:04 UTC, joe wrote:

[...]


Indeed, @Stefan is right. The ParseTimeVisitor only contains 
information available at parse time. If you are interested in 
the parent you have 2 options: either (1) use the 
ParseTimeVisitor and implement the AST traversal logic yourself 
or (2) you can use the SemanticTimeTransitiveVisitor in which 
case the parent is not going to be null. In the case of (2) you 
need to also do some semantic analysis (so you need the whole 
dmd library, not just the parsing one). Here's an example on 
using the dmd library (including semantic) [1]. You can copy 
paste that example and add a few lines of code where you 
instantiate your visitor (which will inherit 
SemanticTimeTransitiveVisitor).


[...]


awesome, that helps a lot!

Thanks both of you :)


Re: How to use Com object (it comes from other dll) in D? Thanks.

2018-02-22 Thread FrankLike via Digitalmars-d-learn
On Thursday, 22 February 2018 at 13:15:11 UTC, rikki cattermole 
wrote:

On 23/02/2018 2:12 AM, FrankLike wrote:



 IShellLink* pLink;
 IPersistFile* ppf;


Reminder classes in D are already references, no need for 
pointers to them.


Ok,I delete the pointers ,It's ok!

Thank you very much!


Re: Negative index range violation

2018-02-22 Thread joe via Digitalmars-d-learn
On Thursday, 22 February 2018 at 02:41:30 UTC, Steven 
Schveighoffer wrote:

On 2/21/18 7:30 PM, SrMordred wrote:
But with a slice negative indexes are never allowed, even on 
a pointer.



youd have to do
(c-1)[0 .. 1];


Nice!
Thank you both!

In D Slice article it says "You can even use negative 
indexes!" so I thought

that the [-1..x] should work too :)




Hah! I never thought of doing a slice with negative indexes ;)

/SNIP

-Steve


At night I dream about doing something like this

auto pos = haystack.find(needle);
auto something = haystack[pos..+3];  // meaning [pos..pos+3]
auto somethingElse = haystack[pos..-3];  // and [pos..pos-3] 
respectively


:)


Re: Understanding the AST...

2018-02-22 Thread RazvanN via Digitalmars-d-learn

On Thursday, 22 February 2018 at 13:21:04 UTC, joe wrote:

On Monday, 12 February 2018 at 08:47:58 UTC, RazvanN wrote:

Hi Joe,

/SNIP

On Tuesday, 6 February 2018 at 12:03:06 UTC, joe wrote:

[...]


The FuncDeclaration node contains all the information for that.
For example, you can access fd.parent to see if the function is
declared at top-level (in which case, the parent is going to 
be a module
declaration ) or if it is a nested function (in a class, in a 
struct, in a function).
Every AST node contains information about the position in the 
AST, all you
have to do is find how to get that information: which field to 
access or which

member function to call.

/SNIP

Cheers,
RazvanN


Follow up question...

Why is *.parent always null?
e.g.:

extern(C++) class MyVisitor(AST): ParseTimeTransitiveVisitor!AST
{
  override void visit(AST.Import i)
  {
assert(i.parent is null); // always true
  }

  override void visitFuncBody(AST.FuncDeclaration f)
  {
assert(f.parent is null); // always true
  }
}


Indeed, @Stefan is right. The ParseTimeVisitor only contains 
information available at parse time. If you are interested in the 
parent you have 2 options: either (1) use the ParseTimeVisitor 
and implement the AST traversal logic yourself or (2) you can use 
the SemanticTimeTransitiveVisitor in which case the parent is not 
going to be null. In the case of (2) you need to also do some 
semantic analysis (so you need the whole dmd library, not just 
the parsing one). Here's an example on using the dmd library 
(including semantic) [1]. You can copy paste that example and add 
a few lines of code where you instantiate your visitor (which 
will inherit SemanticTimeTransitiveVisitor).


[1] 
https://github.com/dlang/dmd/blob/master/test/dub_package/frontend.d


RazvanN


Re: How to use Com object (it comes from other dll) in D? Thanks.

2018-02-22 Thread FrankLike via Digitalmars-d-learn
On Thursday, 22 February 2018 at 13:15:11 UTC, rikki cattermole 
wrote:


Reminder classes in D are already references, no need for 
pointers to them.


Thank you,but get the same error.

[D CODE]

if(lpszLnkFileDir is null) return;
HRESULT hr;
IShellLink pLink;
IPersistFile ppf;

	hr = CoCreateInstance(CLSID_ShellLink,NULL, 
CLSCTX_INPROC_SERVER,IID_IShellLinkA,pLink);//cast(PVOID*)


DisplayInfoWork("CoCreateInstance ");
if(FAILED(hr))
{
DisplayInfoWork("FAILED(hr) ");
return ;
}
DisplayInfoWork("  pLink is "~pLink.to!string);
DisplayInfoWork("will QueryInterface hr is "~hr.to!string);
DisplayInfoWork(IID_IPersistFile.to!string);
hr = pLink.QueryInterface(IID_IPersistFile, ppf);// err <-
DisplayInfoWork("pLink.QueryInterface ");

---log info--
CoCreateInstance
  pLink is 5E9954
will QueryInterface hr is 0
const(GUID)(267, 0, 0, [192, 0, 0, 0, 0, 0, 0, 70])
Access Violation

---end
Thanks.


Re: Understanding the AST...

2018-02-22 Thread Stefan Koch via Digitalmars-d-learn

On Thursday, 22 February 2018 at 13:21:04 UTC, joe wrote:

On Monday, 12 February 2018 at 08:47:58 UTC, RazvanN wrote:

[...]


Follow up question...

Why is *.parent always null?
e.g.:

extern(C++) class MyVisitor(AST): ParseTimeTransitiveVisitor!AST
{
  override void visit(AST.Import i)
  {
assert(i.parent is null); // always true
  }

  override void visitFuncBody(AST.FuncDeclaration f)
  {
assert(f.parent is null); // always true
  }
}


I think parent is only set after sema.
and you are overriding the parsetime visitor.


Re: Understanding the AST...

2018-02-22 Thread joe via Digitalmars-d-learn

On Monday, 12 February 2018 at 08:47:58 UTC, RazvanN wrote:

Hi Joe,

/SNIP

On Tuesday, 6 February 2018 at 12:03:06 UTC, joe wrote:

[...]


The FuncDeclaration node contains all the information for that.
For example, you can access fd.parent to see if the function is
declared at top-level (in which case, the parent is going to be 
a module
declaration ) or if it is a nested function (in a class, in a 
struct, in a function).
Every AST node contains information about the position in the 
AST, all you
have to do is find how to get that information: which field to 
access or which

member function to call.

/SNIP

Cheers,
RazvanN


Follow up question...

Why is *.parent always null?
e.g.:

extern(C++) class MyVisitor(AST): ParseTimeTransitiveVisitor!AST
{
  override void visit(AST.Import i)
  {
assert(i.parent is null); // always true
  }

  override void visitFuncBody(AST.FuncDeclaration f)
  {
assert(f.parent is null); // always true
  }
}


Re: How to use Com object (it comes from other dll) in D? Thanks.

2018-02-22 Thread rikki cattermole via Digitalmars-d-learn

On 23/02/2018 2:12 AM, FrankLike wrote:

Hi,everyone,
  I want use the Com object (it comes from other dll) in D,but the 
core.sys.windows.objidl:QueryInterface Fuction not work.

For example:


import core.sys.windows.windef;
import core.sys.windows.basetyps;
import core.sys.windows.uuid;
import core.sys.windows.com;
import 
core.sys.windows.objbase:CoInitialize,CoUninitialize,CoCreateInstance;

import core.sys.windows.objidl;//:QueryInterface
import core.sys.windows.shlobj;//:IShellLink
import core.sys.windows.unknwn;

 if(lpszLnkFileDir is null) return;
 HRESULT hr;
 IShellLink* pLink;
 IPersistFile* ppf;

 hr = CoCreateInstance(&CLSID_ShellLink,NULL, 
CLSCTX_INPROC_SERVER,&IID_IShellLinkA,cast(PVOID*)&pLink);//cast(PVOID*)


 writeln("CoCreateInstance ");
 if(FAILED(hr))
 {
     writeln("FAILED(hr) ");
     return ;
 }
 writeln("  pLink is "~pLink.to!string);
 writeln("will QueryInterface hr is "~hr.to!string);
 writeln(IID_IPersistFile.to!string);
 hr = pLink.QueryInterface(&IID_IPersistFile, cast(PVOID*)&ppf);// 
err <-

 writeln("pLink.QueryInterface ");

-
It stops in 'hr = pLink.QueryInterface(&IID_IPersistFile, 
cast(PVOID*)&ppf);'

But  the same c++ code is ok.
Who can help me?
Thanks.


Reminder classes in D are already references, no need for pointers to them.



How to use Com object (it comes from other dll) in D? Thanks.

2018-02-22 Thread FrankLike via Digitalmars-d-learn

Hi,everyone,
 I want use the Com object (it comes from other dll) in D,but the 
core.sys.windows.objidl:QueryInterface Fuction not work.

For example:


import core.sys.windows.windef;
import core.sys.windows.basetyps;
import core.sys.windows.uuid;
import core.sys.windows.com;
import 
core.sys.windows.objbase:CoInitialize,CoUninitialize,CoCreateInstance;

import core.sys.windows.objidl;//:QueryInterface
import core.sys.windows.shlobj;//:IShellLink
import core.sys.windows.unknwn;

if(lpszLnkFileDir is null) return;
HRESULT hr;
IShellLink* pLink;
IPersistFile* ppf;

	hr = CoCreateInstance(&CLSID_ShellLink,NULL, 
CLSCTX_INPROC_SERVER,&IID_IShellLinkA,cast(PVOID*)&pLink);//cast(PVOID*)


writeln("CoCreateInstance ");
if(FAILED(hr))
{
writeln("FAILED(hr) ");
return ;
}
writeln("  pLink is "~pLink.to!string);
writeln("will QueryInterface hr is "~hr.to!string);
writeln(IID_IPersistFile.to!string);
	hr = pLink.QueryInterface(&IID_IPersistFile, 
cast(PVOID*)&ppf);// err <-

writeln("pLink.QueryInterface ");

-
It stops in 'hr = pLink.QueryInterface(&IID_IPersistFile, 
cast(PVOID*)&ppf);'

But  the same c++ code is ok.
Who can help me?
Thanks.



Re: array/Array: "hard" bounds checking

2018-02-22 Thread ag0aep6g via Digitalmars-d-learn

On 02/22/2018 10:39 AM, bauss wrote:

On Thursday, 22 February 2018 at 05:22:19 UTC, TheFlyingFiddle wrote:


Eg:

uint a = 3;
int b = -1;

assert(a > b); //No idea what should happen here.


This is what happens:

assert(cast(int)a > b);


Nope. It's `assert(a > cast(uint)b);`.


Re: Understanding the AST...

2018-02-22 Thread joe via Digitalmars-d-learn

On Monday, 12 February 2018 at 08:47:58 UTC, RazvanN wrote:

Hi Joe,

I suggest you watch this video which explains how the parse 
time visitors work: https://www.youtube.com/watch?v=tK072jcoWv4 
.


On Tuesday, 6 February 2018 at 12:03:06 UTC, joe wrote:

[...]


The FuncDeclaration node contains all the information for that.
For example, you can access fd.parent to see if the function is
declared at top-level (in which case, the parent is going to be 
a module
declaration ) or if it is a nested function (in a class, in a 
struct, in a function).
Every AST node contains information about the position in the 
AST, all you
have to do is find how to get that information: which field to 
access or which

member function to call.


[...]


The function average length visitor inherits a transitive 
visitor
which means that the AST traversal logic is already implemented 
for you.
All you have to do is override the visiting methods of interest 
and do
whatever suits you : print stuff, alter the ast, stop the 
visitation or

continue the visitation (by calling super.visit(ASTnode)).


[...]


I know that my explanations might not be very explicit, but if 
you have an example please post it and we can work on it.


Cheers,
RazvanN


Hello RazvanN,

thank you very much for taking the time to reply and also your 
effort in making this happen.


I watched the video you linked and read your reply over and over, 
yet I still have a hard time to wrap my head around this idea.


Like for example DHTML DOM is very easy for me to grasp. It's 
like riding the car down the country road and I know where I am 
and which town I'm going to be next, etc.


This AST thing is more like a teleporter room on the Enterprise. 
Scotty activates the teleporter and a canister appears an a spot 
labeled imports. He repeats and a canister appears on a spot 
labeled functions, etc.


I will try again...


Re: array/Array: "hard" bounds checking

2018-02-22 Thread bauss via Digitalmars-d-learn
On Thursday, 22 February 2018 at 05:22:19 UTC, TheFlyingFiddle 
wrote:


Eg:

uint a = 3;
int b = -1;

assert(a > b); //No idea what should happen here.


This is what happens:

assert(cast(int)a > b);


Re: Tuts/Aritcles: Incrementasl C++-to-D conversion?

2018-02-22 Thread Timothee Cour via Digitalmars-d-learn
also related: https://github.com/Syniurge/Calypso/issues/85
(feasibility of extending calypso to do source to source translation
(eg C++ => D or C=>D) )


On Thu, Feb 22, 2018 at 12:43 AM, ketmar via Digitalmars-d-learn
 wrote:
> Nick Sabalausky (Abscissa) wrote:
>
>> Are there any tutorials or articles out there for "getting started with
>> converting a C++ codebase to D one module at a time?" Or at the very least:
>> tips, tricks, lessions learned, from those who have come before.
>
>
> from my experience (various codebases up to middle size, mostly C, some
> C++): fsck the "one module at a time" idea! even in D modules are
> interwined, and in C and C++ they're even more so. besides, converting tests
> is tedious, it is much funnier to have something working.
>
> so, i'm usually converting alot of code, up to the whole codebase. it is not
> fun when compler spits 100500 errors, but when it finally stops... oh, joy!
>
> trick: use 'sed' (or your favorite regexp search-and-replace tool) alot.
> basically, before HDD crash i almost had a set of scripts that does 80-90
> percents of work translating C to D with sed. ;-) then use editor with "jump
> to error line" support, and simply compile your code, fixing errors one by
> one.
>
> tip: try to not rewrite code in any way until it works. i know how tempting
> it to replace "just this tiny thing, it is so ugly, and in D we have a nice
> idiom!" NEVAR. this is by far the most important thing to remember (at least
> for me), so i'll repeat it again: no code modifications until it works!
>
> personal memories: C code often using things like `a == &arr[idx]`, where
> idx can go just past the last array element. it got me when i was doing enet
> conversion. nasty trick.
>
> otherwise, sweat and blood, and patience.


Re: Tuts/Aritcles: Incrementasl C++-to-D conversion?

2018-02-22 Thread ketmar via Digitalmars-d-learn

Nick Sabalausky (Abscissa) wrote:

Are there any tutorials or articles out there for "getting started with 
converting a C++ codebase to D one module at a time?" Or at the very 
least: tips, tricks, lessions learned, from those who have come before.


from my experience (various codebases up to middle size, mostly C, some 
C++): fsck the "one module at a time" idea! even in D modules are 
interwined, and in C and C++ they're even more so. besides, converting 
tests is tedious, it is much funnier to have something working.


so, i'm usually converting alot of code, up to the whole codebase. it is 
not fun when compler spits 100500 errors, but when it finally stops... oh, 
joy!


trick: use 'sed' (or your favorite regexp search-and-replace tool) alot. 
basically, before HDD crash i almost had a set of scripts that does 80-90 
percents of work translating C to D with sed. ;-) then use editor with 
"jump to error line" support, and simply compile your code, fixing errors 
one by one.


tip: try to not rewrite code in any way until it works. i know how tempting 
it to replace "just this tiny thing, it is so ugly, and in D we have a nice 
idiom!" NEVAR. this is by far the most important thing to remember (at 
least for me), so i'll repeat it again: no code modifications until it works!


personal memories: C code often using things like `a == &arr[idx]`, where 
idx can go just past the last array element. it got me when i was doing 
enet conversion. nasty trick.


otherwise, sweat and blood, and patience.