Re: Multithreading in D
What I imagine as solution (I know it won't work this way, but to give you a better idea): for(int i = 0; i #threads; i++){ runInThread(generateTerrain(...)); } Are you looking for parallel? http://dlang.org/library/std/parallelism/parallel.html
Re: 'write' crashes without extra window
On Tuesday, 7 October 2014 at 23:41:14 UTC, Joel wrote: I have a program that runs at Windows 7 login, each time. But it had been opening with a command prompt, so I got rid of the prompt and now it some times crashes. I've noticed it before, using 'write' without the prompt. Can you post the code and the error message (from crash) you're getting?
coding practices: include whole module or only the needed function
Which practice do you use: if you need only one or two functions from a module: import myModule : func, func2; or (import whole module, assuming no function name conflits of course) import myModule; any words why one over the other are welcome. I like the first one why it explicitly show why I'm importing such a module and I think (personally) it make code more easy to read/understand/maitain. Also it's very useful inside functions (local imports) where we can overload the function like in: int f(int arg) { import foo : f; return f(somethingElse, arg); } I used it recently.
How do I check if a function got CTFE?
I'd like to check if a function got CTFE, ie, the compiler was able to replace my foo(s); by the computed value at compile-time. I'm trying to convert the binary executable to assembly by using objconv tool but I'm finding it very diffucult to find anything in there, since some converters I've used which does ELF to ASM keep the function name, e.g, foo() function is a foo label somewhere in the file but this convert doesn't and use some numbers instead of. I don't know if it's related how is the windows object file format designed.
Re: How do I check if a function got CTFE?
On Thursday, 2 October 2014 at 18:02:30 UTC, Adam D. Ruppe wrote: On Thursday, 2 October 2014 at 17:56:29 UTC, AsmMan wrote: I'd like to check if a function got CTFE, ie, the compiler was able to replace my foo(s); by the computed value at compile-time. You have to explicitly force ctfe with context, it is never done automatically, and if it fails, the build will fail and you get a compile time error. That's the point. I thought the compiler did it by checking things like constant arguments + function purity or so. This was exactly my issue. Thanks!
Re: How do I check if a function got CTFE?
On Thursday, 2 October 2014 at 18:17:12 UTC, anonymous wrote: On Thursday, 2 October 2014 at 17:56:29 UTC, AsmMan wrote: I'd like to check if a function got CTFE, ie, the compiler was able to replace my foo(s); by the computed value at compile-time. I'm trying to convert the binary executable to assembly by using objconv tool but I'm finding it very diffucult to find anything in there, since some converters I've used which does ELF to ASM keep the function name, e.g, foo() function is a foo label somewhere in the file but this convert doesn't and use some numbers I was thiking the dmd compiler did CTFE without someone ask for this, in the way as I've mentioned, checking for constant arguments + function's purity and if all this is true, it did the CTFE rather than generate code to compute it at run-time. In the case of it did happen, I just wanted to know. It was my misunderstsooding how it does works in dmd.
out parameter with default value
Why it does works: void f(out int c) { if(some_cond) c = 10; } but it doesn't? void f(out int c = 1) { if(some_cond) c = 10; } it give compiler error: Error: constant 1 is not an lvalue
Re: out parameter with default value
My question is why it doesn't works and if there's a workaround
Re: out parameter with default value
I had just forget out and ref are same as pointers... thanks guys
Re: Can I make a variable public and readonly (outside where was declared) at same time?
On Friday, 26 September 2014 at 18:18:45 UTC, Steven Schveighoffer wrote: On 9/26/14 1:36 PM, Marc =?UTF-8?B?U2Now7x0eiI=?= schue...@gmx.net wrote: Alternatively, you could create a union with a private and a public member with the same types, but I wouldn't recommend it. Besides, the members would need to have different names: class Foo { union { private int a; public int b; } } Hm.. that doesn't provide readonly access to either a or b. But it gave me an idea: class Foo { union { private int _a; public const int a; } void setA(int x) { _a = x; } } Hot damn! It works too :) Can't access _a from outside the module, can access a, but can't write it (even from within Foo). It's like an auto-inlined property function. I don't know how it would affect the optimizer, or the GC scanner. Unions are ugly things... -Steve This is really a loot cool and works. Thanks. If private in D had same behavior like in C#/C++, ie, private to scope of where class was declared and not public to the entire module, I guess we could even do: class Foo { union { private int a_; public @property int a() { return a_; } private @property void a(int value) { a_ = value; } } //no one need knows the 'a_' (ugly?) identifier void setValue(int x) { a = x; } } And then Foo f = new Foo(); f.a = 10; // give a compile error becaus it is private and acessible within Foo class only BTW: I'm not sure about memory usage where using properties. But it is still cool.
Can I get caller name?
for debugging purposes, I need to get the caller name. Is it possible? if so, how do I do this? void foo() { baa(); } void baa() { wrilten(caller_name()); // foo } I know I could create an extra parameter and pass the current function name as argument but it isn't a possibility for now.
Re: Can I get caller name?
Thanks guys!
Can I make a variable public and readonly (outside where was declared) at same time?
I know I can combine it by making an extra variable plus a property like this: class Foo { private int a_; void do_something1() { a_ = baa(); } void do_something2() { if(cond) a_ = baa2(); } @property int a() { return a; } } This is the C#'s to do which I'm translated to D within my limited knowledge. I don't do much OOP, maybe it's possible and I don't know. I'm using @property to make 'a' accessible and readonly at same time but I wanted to do that without this a_ extra variable, i.e, only the methods within the Foo class can assign a new value to a but a instance of Foo can't. An imaginary code example: class Foo { public MAGIC_HERE int a; void do_something1() { a = baa(); } void do_something2() { if(cond) a = baa2(); } } And then: Foo f = new Foo(); writeln(f.a); // output value of a f.a = 10; // compile error: a is readonly outside Foo's methods. I hope it's clear (sorry for por English)
Re: Does D has C#'s string.Empty?
On Thursday, 25 September 2014 at 06:41:03 UTC, SlomoTheBrave wrote: On Thursday, 25 September 2014 at 05:29:37 UTC, AsmMan wrote: Does D has C#'s string.Empty? string.init ? string a; a = string.init; assert( a == ); does the job for the type string at least. Thanks.:)
Re: Does D has C#'s string.Empty?
On Thursday, 25 September 2014 at 12:43:57 UTC, Steven Schveighoffer wrote: On 9/25/14 2:41 AM, SlomoTheBrave wrote: On Thursday, 25 September 2014 at 05:29:37 UTC, AsmMan wrote: Does D has C#'s string.Empty? string.init ? string a; a = string.init; assert( a == ); does the job for the type string at least. null also works. In fact, in the above, a is already string.init or null before assigning (D always initializes variables unless asked not to). a = null; // same as a = string.init; -Steve It made me a bit confusing. How is the implementation of string comparasion in D? (if someone could point to actual code used in these comparasion would be really great otherwise I'll check out assembly output, maybe) in no language I know of (including C#) == null is true
Re: Does D has C#'s string.Empty?
On Friday, 26 September 2014 at 00:53:24 UTC, ketmar via Digitalmars-d-learn wrote: On Fri, 26 Sep 2014 00:24:27 + AsmMan via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: It made me a bit confusing. How is the implementation of string comparasion in D? has length of 0. null has length of 0. two strings without content are essentialy the same. but null has length? what's null in D?
Does D has C#'s string.Empty?
Does D has C#'s string.Empty?
Re: Does remove immutability using cast to pass in a function make sense?
On Tuesday, 23 September 2014 at 03:34:22 UTC, Ali Çehreli wrote: If it still doesn't work for you please show us a minimal program that demonstrates the problem. Ali Ok, the case is the follow, I updated my dmd compiler some days ago (after my mono crashed and I lost some of D files, I posted on this forum) and I can't reproduce the error anymore. What I remember it was about a template error and when I removed the immutable keyword it compiled. I tried really, but it doesn't give any error anymore and your code compile. :/
Does remove immutability using cast to pass in a function make sense?
I have this array: static immutable string[] months = [jan, fev, ...]; I need to pass it into canFind(). But it doesn't works with immutables so I need to cast it like in canFind(cast(string[]) months, month) to work. There's a reason related to design why it doesn't work with immutables or a function just wasn't written? What I want to know is if I'm doing something wrong in casting it to work. I know from C which such casts should be done carefully
put string[] into a appender without loop?
I'd like to copy an array string into a appender!string() but I can't see how to do this without loop myself over the string array. Is there a native function or should I write it myself?
Re: put string[] into a appender without loop?
On Sunday, 21 September 2014 at 23:41:58 UTC, AsmMan wrote: I'd like to copy an array string into a appender!string() but I can't see how to do this without loop myself over the string array. Is there a native function or should I write it myself? call: auto app = appender!string(); string[] s = [foo, baa]; app.put(s); give a: \src\phobos\std\conv.d(9,9): Error: static assert immutable(char) cannot be emplaced from a string. (b) How do I fix it?
Re: put string[] into a appender without loop?
On Monday, 22 September 2014 at 00:09:22 UTC, Vladimir Panteleev wrote: On Sunday, 21 September 2014 at 23:48:59 UTC, AsmMan wrote: On Sunday, 21 September 2014 at 23:41:58 UTC, AsmMan wrote: I'd like to copy an array string into a appender!string() but I can't see how to do this without loop myself over the string array. Is there a native function or should I write it myself? call: auto app = appender!string(); string[] s = [foo, baa]; app.put(s); give a: \src\phobos\std\conv.d(9,9): Error: static assert immutable(char) cannot be emplaced from a string. (b) How do I fix it? put(app, s); This is not an ideal solution, since Appender may reallocate several times when appending the array items. Ideally, appender itself should take a range of strings, so that it can preallocate memory for them only once. this give undefined identifier: 'put' error. (std.array is already included, buffer.put(string) doesn't give same error) The copy of an array doesn't happen often as string but do suggest to I want something else instead of appender?
Re: put string[] into a appender without loop?
On Monday, 22 September 2014 at 00:30:44 UTC, Vladimir Panteleev wrote: On Monday, 22 September 2014 at 00:18:03 UTC, AsmMan wrote: this give undefined identifier: 'put' error. (std.array is already included, buffer.put(string) doesn't give same error) You need to import std.range. Thanks, I was thinking std.array is enough The copy of an array doesn't happen often as string but do suggest to I want something else instead of appender? No, I was just thinking aloud about how Appender could be improved in the future. To minimize allocations right now, you could write your own put-like function which calls Appender.reserve. this is not critical (for now, I'm not doing any optmization) but I'll save the idea.
Re: pop popFront combined
On Saturday, 20 September 2014 at 18:59:03 UTC, Nordlöw wrote: Is there a reason why popFront doesn't automatically return what front does? If so I'm still missing a combined variant of pop and popFront in std.range. Why isn't such a common operation in Phobos already? So far I know isn't common use return value from popFront() at same time it's called. For example, checkout how is: int[] a = [1,2,3]; foreach(int n; a) {} is translated: for(auto r = a; !r.empty; r.popFront()) { int n = r.front; } to return same as value in front by popFront() save previously value is needed: int popFrontInt(out int[] arr) { int current = arr[0]; // or arr.front arr = arr[1 .. $]; return current; } (this isn't exactly like Phobos implementation and is int[]-only, btw) the cost of the 'current' variable may be a bit expansive (one extra register use per function call) and useless, since it isn't used and a common use is one like the loop. I think it's well-designed, IMHO... On Saturday, 20 September 2014 at 18:59:03 UTC, Nordlöw wrote: If you want move semantics, use `moveFront`. Is this function part of phobos library? if so, where?
Re: regex problems
On Saturday, 20 September 2014 at 15:28:54 UTC, seany wrote: consider this: import std.conv, std.algorithm; import core.vararg; import std.stdio, std.regex; void main() { string haystack = ID : generateWorld; Position : { {ID : \ absolute ; Coordinate : , NULL OMEGA;} {ID : \ inclusion ; Coordinate : UNDEF;} {ID : \ subarc; Coordinate : , NULL OMEGA; } }; ID : ; // thus, something like *{B}* can not end here, // but something like X can start here. string needle = (?!(([.\n\r])*(\\{)([.\n\r])*))(ID(\\p{White_Space})*:(\\p{White_Space})*)(?!(([.\n\r])*(\\})([.\n\r])*)); auto r = regex(needle, g); auto m = matchAll(haystack, r); foreach (c; m) writeln(c.hit); } So let us break up needle: ( ?! ( ([.\n\r])*(\\{)([.\n\r])* ) ) Do not match somthing, that may contain a *{* as a leading match, * this time means any character, including \n and \r (ID(\\p{White_Space})*:(\\p{White_Space})*) however, look for the form : ID few blank spaces : more blank spaces (?!(([.\n\r])*(\\})([.\n\r])*)) but no trailing *}* as a trailing match. In haystack, there are two such ID : -s. once at the beginning, ID : generateWorld. and then the final, last ID However, this is returning all 5 ID-s as match what am I doing wrong? Is this string a JSON string? if so, why not use a proper JSON parsing library? as other already mentioned, this kind of data isn't good to parse using regex... write small routines to parse that data instead of. It isn't more hard than make it working using regexp. Seriously.
Re: std.utf.decode(dlang, 1)
On Thursday, 18 September 2014 at 06:09:54 UTC, Algo wrote: void main() { import std.utf; decode(dlang, 1); } Error: template std.utf.decode cannot deduce function from argument types !()(string, int), candidates are: D:\msc\D\dmd2\windows\bin\..\..\src\phobos\std\utf.d(924): std.utf.decode(S)(auto ref S str, ref size_t index) if (!isSomeString!S isRandomAccessRange!S hasSlicing!S hasLength!S isSomeChar!(ElementType!S)) D:\msc\D\dmd2\windows\bin\..\..\src\phobos\std\utf.d(942): std.utf.decode(S)(auto ref S str, ref size_t index) if (isSomeString!S) why doesn't decode(S)(auto ref S str, ref size_t index) if (isSomeString!S) correspond? I was having same issue a while ago. But I realized I was using function in the wrong way with string type and passing a size_t in the 2th argument. You can see use of decode(val, size) in the standard library but call it in your code result in an error. it worked: string s = readText(file); decode(cast(ubyte[]) s); I'm not sure why you're trying to pass a string literal as argument because it does need a memory location to change to new memory location, where bytes skiped end. That's why it does use ref.
Re: dub can't read files from cache
On Thursday, 18 September 2014 at 16:51:06 UTC, ketmar via Digitalmars-d-learn wrote: On Thu, 18 Sep 2014 16:31:08 + Ilya Yaroshenko via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: one ring to rule them all UTF-8 = Lord of the encodings. i want 42th symbol from the string. what? what do you mean saying that i must scan the whole string from the beginning to get it? oh, High Lord, this one Lord is fake! That's why a while ago I was considering convert a string from UTF-8 to UTF-32. UTF-32 is nice I don't understand when people say there are no any advantage to use it. Indexing is just possible. Memory size isn't much an issue. I needed to extend support for UTF-8 in a program where I had some routines where I could move forward and backward very easily just indexing but using UTF-8 it isn't possible so I needed to make my own an iterator when I need to save a pointer instead of a index. In memory usage it isn't so bad since a size of that index is same as pointer but the structure of the program was a bit ugly, a kind of hack, IMHO.
Re: dub can't read files from cache
On Thursday, 18 September 2014 at 16:49:14 UTC, ketmar via Digitalmars-d-learn wrote: On Thu, 18 Sep 2014 16:24:17 + Ilya Yaroshenko via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: You can choice encoding for console in Linux yes. and i chose koi8. yet many utilities tend to ignore my locale when reading files (hey, D compiler, i'm talking about you!). i don't care about localized messages (i'm using English messages anyway), but trying to tell me that my text file is invalid utf-8, or my filename is invalid utf-8, or spitting utf-8 encoded messages to my terminal drives me mad. what is so wrong with locale detection that virtually nobody does that? we have iconv, it's readily available on any decent GNU/Linux platform, yet it's still so hard to detect that stinky locale and convert that stinky utf-8 to it? BS. (hey, phobos, i'm talking about your stdout.write() here too!) the whole utf-8 or die attitude has something very wrong in it. I didn't know about this encoding. Why should you use KOI8-R instead of UTF-8? what does it conver that UTF-8 didn't? I used to think UTF-8 does conver all the alphabets around, japonese people does use it, isn't?
Re: commercial application with D
On Monday, 15 September 2014 at 20:45:38 UTC, Ali Çehreli wrote: On 09/15/2014 01:02 PM, Andrey wrote: Can I develop commercial application with D programming language? Here is a short list of companies that do that: http://wiki.dlang.org/Current_D_Use Ali Good list.
Re: how to transform decial point 3.15 to 3,15 comma?
On Monday, 15 September 2014 at 22:45:50 UTC, Cassio Butrico wrote: how to transform decial point 3.15 to 3,15 comma? Hello everyone, I am making a registry of real amounts, and need trasformar fractional numbers, so print coretamente. there is some routine that do this? Is the , (comma) the system decimal separator? if so, you can use C intero and include locale.h header (in D, the respective module) call setlocale(LC_NUMERIC, ) function and then printf(%g, 3.5) will output (if decimal separator is the comma): 3,5 I haven't tested it in D but I think D's writefln() will behave exactly same as C's printf().. but it didn't you're free to call C's printf()
Re: how to transform decial point 3.15 to 3,15 comma?
On Monday, 15 September 2014 at 23:17:51 UTC, AsmMan wrote: On Monday, 15 September 2014 at 22:45:50 UTC, Cassio Butrico wrote: how to transform decial point 3.15 to 3,15 comma? Hello everyone, I am making a registry of real amounts, and need trasformar fractional numbers, so print coretamente. there is some routine that do this? Is the , (comma) the system decimal separator? if so, you can use C intero and include locale.h header (in D, the respective module) call setlocale(LC_NUMERIC, ) function and then printf(%g, 3.5) will output (if decimal separator is the comma): 3,5 I haven't tested it in D but I think D's writefln() will behave exactly same as C's printf().. but it didn't you're free to call C's printf() The code is the following: import std.stdio; import std.c.locale; void main() { setlocale(LC_NUMERIC, ); writeln(3.5); // 3,5 } You can change/see current decimal separator using (assuming Windows) http://www.softwareok.com/?seite=faq-Win-7faq=78
How do I properly exit from a D program (outside main)?
Someone said somewhere that call std.c.process.exit() isn't the proper way to exit from a D program since it doesn't terminate some phobos stuff. So what should I use instead of? or there's no a replacement?
Re: core.exception.UnicodeException@src\rt\util\utf.d(400): illegal UTF-16 value
On Monday, 15 September 2014 at 23:57:59 UTC, Ali Çehreli wrote: On 09/15/2014 04:36 PM, notna wrote: WCHAR lpwszUsername[254]; debug writefln(lpwszUsername.sizeof is %s, WCHAR.sizeof is %s, lpwszUsername.sizeof, WCHAR.sizeof); // DWORD dUsername2 = lpwszUsername.sizeof / WCHAR.sizeof; DWORD dUsername2 = 254; GetUserNameW(lpwszUsername.ptr, dUsername2); You must make use of the returned value to slice your wstring. Something like this (not compiled): auto actualLength = GetUserNameW(lpwszUsername.ptr, dUsername2); auto userName = lpwszUsername[0..actualLength]; Otherwise, D knows that lpwszUsername is a 254-char string and will try to print all of it. Ali GetUserNameW() return a zero on error and non-zero on success. The actual number of bytes copied into lpwszUsernam is in dUsername2. auto userName = lpwszUsername[0..dUsername2];
Re: how to transform decial point 3.15 to 3,15 comma?
On Monday, 15 September 2014 at 23:43:47 UTC, Cassio Butrico wrote: On Monday, 15 September 2014 at 23:24:13 UTC, AsmMan wrote: On Monday, 15 September 2014 at 23:17:51 UTC, AsmMan wrote: On Monday, 15 September 2014 at 22:45:50 UTC, Cassio Butrico wrote: how to transform decial point 3.15 to 3,15 comma? Hello everyone, I am making a registry of real amounts, and need trasformar fractional numbers, so print coretamente. there is some routine that do this? Is the , (comma) the system decimal separator? if so, you can use C intero and include locale.h header (in D, the respective module) call setlocale(LC_NUMERIC, ) function and then printf(%g, 3.5) will output (if decimal separator is the comma): 3,5 I haven't tested it in D but I think D's writefln() will behave exactly same as C's printf().. but it didn't you're free to call C's printf() The code is the following: import std.stdio; import std.c.locale; void main() { setlocale(LC_NUMERIC, ); writeln(3.5); // 3,5 } Okay, Thanks everyone, that was it you're welcome! :)
Re: How do I properly exit from a D program (outside main)?
On Monday, 15 September 2014 at 23:52:25 UTC, H. S. Teoh via Digitalmars-d-learn wrote: On Mon, Sep 15, 2014 at 11:36:54PM +, AsmMan via Digitalmars-d-learn wrote: Someone said somewhere that call std.c.process.exit() isn't the proper way to exit from a D program since it doesn't terminate some phobos stuff. So what should I use instead of? or there's no a replacement? AFAIK, there is currently no replacement. I personally use an ExitException and put a catch block in main(): class ExitException : Exception { int status; this(int _status=0, string file=__FILE__, size_t line=__LINE__) { super(Program exit, file, line); status = _status; } } void exit(int status=0) { throw new ExitException(status); } ... int main() { try { ... } catch(ExitException e) { return e.status; } return 0; } The catch is that this may or may not work correctly in multithreaded programs, because the exception may happen in a different thread than the one main() is running in, and there isn't any nice way to terminate other still-running threads after catching such an exception. There has some discussion as to how to implement this, but AFAIK no good solution was found. See also: https://issues.dlang.org/show_bug.cgi?id=3462 But at least, for single-threaded programs, the above ExitException should work reasonably well. T Thanks! I'll use it.
Re: How do I properly exit from a D program (outside main)?
On Monday, 15 September 2014 at 23:42:02 UTC, notna wrote: how about return? :) there is also assert... and pls note, scope could also be your friend :O On Monday, 15 September 2014 at 23:36:56 UTC, AsmMan wrote: Someone said somewhere that call std.c.process.exit() isn't the proper way to exit from a D program since it doesn't terminate some phobos stuff. So what should I use instead of? or there's no a replacement? Neither assert or return will help. Check out this code example: void main() { f(); } void f() { if(!foo) exit(1); do_something(); }
when should I use a function call without parenteses?
In which context do you use a function call without paranteses? (considering the function doesn't has arguments or they're default, of couse) I want to know when/why should I use or if it depends only to programmer's coding style.. f / baa.foo versus f() / baa.foo() personally, to me, which came from C and C# world I find a bit strange use f symbol being f a function but I can get used to it very easily.
Re: String Theory Questions
On Saturday, 13 September 2014 at 23:21:09 UTC, David Nadlinger wrote: On Saturday, 13 September 2014 at 22:41:39 UTC, AsmMan wrote: D string are actullay C-strings? No. But string *literals* are guaranteed to be 0-terminated for easier interoperability with C code. David ah makes sense. On Sunday, 14 September 2014 at 12:07:16 UTC, Marc Schütz wrote: On Sunday, 14 September 2014 at 09:07:26 UTC, Kagamin wrote: On Sunday, 14 September 2014 at 00:34:56 UTC, WhatMeWorry wrote: So is one form (Empty strings versus null strings) considered better than the other? Or does it depend on the context? For all practical purposes they should be equivalent in D code. I suppose the distinction exists because somebody claimed he can make sense of it. Some API may rely on distinction between null and empty string, like XML DOM, though I don't think such interface is very useful. Also for some reason boolean value of a string is derived from ptr instead of length... meh. Which makes sense given the distinction exists, IMO. Compare for example with Ruby, where empty strings and `0` integers also evaluate to true, but only `nil` and `false` evaluated to false. That's why I don't like most of dynamic languages... type system is a mess. I don't like even the fact one can do: x = abc; f(x) x = 10; g(x); and it work
Re: String Theory Questions
On Saturday, 13 September 2014 at 17:31:18 UTC, ketmar via Digitalmars-d-learn wrote: On Sat, 13 Sep 2014 17:09:56 + WhatMeWorry via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: I guess I was expecting them to be equivalent. I can understand why both lengths are zero. But what is emptyStr.ptr doing with the 42F080 value? I presume this is a address? If so, what does this address contain and what is it used for? it's used to keep empty string. ;-) note that null string and empty string aren't same things. arrays are reference types and compiler magically knows that null-arrays are just empty arrays (and you can assign 'null' to array to clear it). but strings are special in one funny way: when compiler sees string literal (i.e. quoted string) in source code, it actually generates C-like zero-terminated string. this is to ease C interop, so we can call C functions like this: `printf(my string!\n);` instead of this: `printf(my string!\n.toStringz);`. D string are actullay C-strings?
Re: Is º an unicode alphabetic character?
On Friday, 12 September 2014 at 04:04:22 UTC, Ali Çehreli wrote: On 09/11/2014 08:04 PM, AsmMan wrote: what's an unicode alphabetic character? Alphabetic is defined as Lu + Ll + Lt + Lm + Lo + Nl + Other_Alphabetic, all of which are explained here: http://www.unicode.org/Public/5.1.0/ucd/UCD.html#General_Category_Values I misunderstood isAlpha(), I used to think it's to validate letters like a, b, è, é .. z etc but isAlpha('º') from std.uni module return true. º happens to be in the Letter, Lowercase category so yes, it is isAlpha(). How can I validate only the letters of an unicode alphabet in D or should I write one? There are so many alphabets in the world. It is likely that a Unicode character will be a part of one. I know I can do: bool is_id(dchar c) { return c = 'a' c = 'z' || c = 'A' c = 'z' || c = 0xc0; } There is a misunderstanding. There are so many Unicode characters that are = 0xc0 but not a part of the Alphabetic category. For example: ← (U+2190 LEFTWARDS ARROW). Ali If I want ASCII and latin only alphabet which range should I use? ie, how should I rewrite is_id() function?
Re: Is º an unicode alphabetic character?
On Friday, 12 September 2014 at 07:57:43 UTC, Ali Çehreli wrote: On 09/11/2014 11:38 PM, AsmMan wrote: If I want ASCII and latin only alphabet which range should I use? ie, how should I rewrite is_id() function? This seems to be it: import std.stdio; import std.uni; void main() { alias latin = unicode.script.latin; assert('ç' in latin); assert('7' !in latin); writeln(latin); } Ali Sorry, I shouldn't asked for latin but an alphabet like French instead of: http://www.importanceoflanguages.com/Images/French/FrenchAlphabet.jpg (including the diacritics, of course) As you mentioned, º happend to be a letter so it still pass in: assert('º' in latin); so isn't different from isAlpha(). Is the UTF-8 table organized so that I can use a range (like we do for ASCII ch = 'a' ch = 'z' || ch = 'A' ch = 'Z') or should I put these alpha characters myself on table and then do look up?
Re: Is º an unicode alphabetic character?
Thanks Ali, I think I get close: bool is_id(dchar c) { return c = 'a' c = 'z' || c = 'A' c = 'Z' || c = 0xc0 c = 0x0d || c = 0xd8 c = 0xf6 || c = 0xf8 c = 0xff; } this doesn't include some math symbols. like c = 0xc0 did.
Is º an unicode alphabetic character?
what's an unicode alphabetic character? I misunderstood isAlpha(), I used to think it's to validate letters like a, b, è, é .. z etc but isAlpha('º') from std.uni module return true. How can I validate only the letters of an unicode alphabet in D or should I write one? I know I can do: bool is_id(dchar c) { return c = 'a' c = 'z' || c = 'A' c = 'z' || c = 0xc0; } but I'm looking for a native, if any
Re: Novice web developer trying to learn D
On Sunday, 7 September 2014 at 21:06:48 UTC, zuzuleinen wrote: Hello, First, here is my Linkedin profile http://www.linkedin.com/in/andreiboar in order to make an image of my professional background. I do realise here are really good programmers for which this background might sound like a joke, but this is what I did so far. After watching some presentantions from DConf, and trying the language I decided to give it a try in the future. Currrently I'm reading the Programming in D book by Ali Çehreli, and then The D Programming Language by Andrei Alexandrescu in order to learn more. The reason I post this is to ask you what other books do you think I should try in order to become hireable in the next 2 years? As a web developer I know I lack a lot of information, but I'm willing to do the hard work. So if anyone has any other books/things I need to know and is willing to make me like a small roadmap to become a good D developer I would really appreciate. Thanks a lot, Andrei Before go to D I recomend you to take a look at the C programming language (as Gary Willoughby already mentioned). I think it's really fundamental.
Re: Allowing Expressions such as (low value high)
On Thursday, 4 September 2014 at 20:33:45 UTC, Nordlöw wrote: On Thursday, 4 September 2014 at 20:25:52 UTC, monarch_dodra wrote: In the case of D, it's a C compatibility thing. Other languages I don't know. FYI, auto x = 1 2 3; as C++ is accepted (but warned about) by GCC as x.cpp:19:20: warning: comparisons like ‘X=Y=Z’ do not have their mathematical meaning [-Wparentheses] auto x = 1 2 3; Clang gives no warning. Very surprising clang doesn't. But it willn't take so long to do so.
Register allocation algorithm
I'm working on small compiler to understand these stuff and maybe get involved with the D compiler. I wrote a front-end to a C-like language and now I'm working on the code generator. To be more specific, in the register allocation phase. I was using a old and one where I put everything on stack because I thought it could be too complex for now but then I found something that seems a beginner like me would implement. From wikipedia articles I was able to implement below algorithm. I did some search, but it didn't provide much useful contents to myself, much probably because I don't have a math background and compiler development baggage. From what I understood how algorithms works I've implemented but I have a couple of questions. There's a lot of people here that knows a lot about compilers. Questions: Data: the code generator has only two registers available, they are RO and R1. Numbers are 32-bit. operations: load, push, pop, add, sub, div, mul. They are x86-like instructions. I think that's all. How should I design the get_reg() function? I was using a stack-based to hold registers, but changed to current one (very very simple): it does keep the previously register returned in the function and return the inverse of, eg, if previously was R0 does return R1 if R1 does return R0. It does init always with R0 register to result of expression always be in R0. But if I run out of registers, one register is pushed on stack and result might don't live in R0. So, how do I chose the register to result expression always be in it? A working code example could be very great! I also want to someone tell me if is correct my label()/sethiUllman() implementation. I translated directly this code from C/C++(since post non-D code one could say it doesn't make much sense) that's the language I'm using and keep it in the C-way as possible, ie, not using too much D features because I want to translate it back to my C one when I get it working. It's because D compiler is written in C++ and then I want to be able read dmd compiler source code, in case I get involved to, what I really want to. Thanks in advance. Here's the code: http://pastebin.com/vP0XtyVi (pastebin version, in case of found it's more readable) import std.stdio; enum Type { number, id, add, sub, mul, div, push, pop, none } enum Reg { none, r0, r1 } class AST { AST left; AST right; Type type; Reg reg; int n; this(AST l, AST r, Type t) { left = l; right = r; type = t; n = 0; } } class BinExpression : AST { this(AST l, AST r, Type t) { super(l, r, t); } } class Identifier : AST { string name; this(string nm) { super(null, null, Type.id); name = nm; } } class Number : AST { int number; this(int n) { super(null, null, Type.number); number = n; } } Reg[] regs = [ Reg.r0, Reg.r1 ]; const int regs_num = regs.sizeof / int.sizeof; int C = 0; void main() { // ((2 + 2) + (2 + 2)) + ((2 + 2) + (2 + 2)) // t1 = 2 + 2 // t2 = 2 + 2 // t3 = t1 + t2 // t4 = 2 + 2 // t5 = 2 + 2 // t6 = t4 + t5 // t7 = t3 + t6 Number a = new Number(2); Number b = new Number(2); Number c = new Number(2); Number d = new Number(2); Number e = new Number(2); Number f = new Number(2); Number g = new Number(2); Number h = new Number(2); // t1 = 2 + 2 BinExpression t1 = new BinExpression(a, b, Type.add); // t2 = 2 + 2 BinExpression t2 = new BinExpression(c, d, Type.add); // t3 = t1 + t2 BinExpression t3 = new BinExpression(t1, t2, Type.add); // t4 = 2 + 2 BinExpression t4 = new BinExpression(e, f, Type.add); // t5 = 2 + 2 BinExpression t5 = new BinExpression(g, h, Type.add); // t5 = t3 + t4 BinExpression t6 = new BinExpression(t4, t5,Type.add); BinExpression t7 = new BinExpression(t3, t6, Type.add); label(t7); gen(t7); } Reg get_reg() { if(C == regs.sizeof / int.sizeof) { //writeln(out of registers!); C = 0; } return regs[C++]; } void gen(AST ast) { if(ast.left !is null ast.right !is null) { int l = ast.left.n; int r = ast.right.n; if(l = regs_num r = regs_num) { gen(ast.right); ast.n -= 1; //Reg r2 = ast.right.reg; emit_operation(Type.push, ast.right.reg);