I'm doing some shader programming in D, and what I want to do is write my shaders in text files, but during compilation have the text files be put into the executable / library, just like a normal string would.
Is there a way to do this in D?
You can use Import Expressions.
string shaderText = import("shader.txt");
Make sure you use the -J compiler switch to supply the directory of the shader.
Related
I would like to include part of a C header or a C file into my sphinx doc.
using literalinclude gets the whole file but I would like to get only some part but based on functions, struct names etc...
I don't want to use lines or start-with options as I would like to make it fail when the C idiom is not found.
I don't see any option or extension to do that.
I am building a build system and have .c file which contains version string. const char version[] = "V01.00.00";
I need to get that line by regex into make variable to use for further binary making.
Can you suggest the best way to do that? Would appreciate native make solution.
It's a bad idea to try to parse .c files by yourself to extract information from them. Only the C compiler, for example gcc should be used to parse .c files. You should not be doing it, leave it to the compiler.
The reason is, that if someone changes the .c file you extracted information from, your extraction code is likely to break. Basically, your extraction code would have to be compatible with any .c syntax, which means, you would end up to be a "compiler".
Otherwise, you would have to publish some documentation for other programmers, to tell them to keep the .c code the way you want it, for your code not to break. So you would end up restricting the C language for them.
If you want to get such information:
define a function in the .c file to return that information
compile that file to a library
make an executable that links with the library and calls that function
call that executable from the Makefile
I am trying to add some support for D programming language to my vim config. For autocompletion I need to detect packages that are included. This is not exactly hard to do in simple case:
import std.stdio;
import std.conv;
My config:
set include=^\\s*import
set includeexpr=substitute(v:fname,'\\.','/','g')
Works great.
However, imports can have more complicated format, for example:
package import std.container, std.stdio = io, std.conv;
I was not able to find a simple way to parse this with include and includeexpr.
Also there is a second problem: import can have different access modifiers, like public and private. VIM scans included files recursively, import statements from included files are parsed too. But I need to distinguish between the file I am working with now and files which are scanned automatically: in current file all imports should be detected, but in other files only public import statements should add more files to the search.
Thanks for help.
Update
It's a shame if this can not be done without full parsers. Essentially, I only need two things:
ability to return an array from includeexpr instead of one file name
ability to distinguish between includes in current and other files
I think only way to do it reliably is to use complete parser and semantic analyzer. D Completion Daemon (https://github.com/Hackerpilot/DCD/tree/master/editors/vim ) has vim plugin and is not very resource-hungry.
Vim's include mechanism and 'includeexpr' are heavily influenced by the C programming language and only work for single files. You cannot return a list of filenames, so it won't be possible to support D's complex include mechanism with Vim. Use an IDE that is fully tailored to support the programming language, not a general-purpose text editor.
that is:
#include "mySnippet.h"
Does D even have such thing ?
Using import "myInclude.d" procuces:
test.d(5): Error: Identifier expected following import
test.d(5): Error: ';' expected
The only thing that is remotely similar to the C/C++ #include statement is the in-place import of a file, and then mixing that into the source:
mixin(import("mySnippet.d"));
D is a modular programming language and the import statement is your friend when you do D programming.
In D you really do not need anything like #pragma once. This extension, and include-guards are there because C and C++ do not care about these things. D does.
However, I assume you are probably wondering how you give the interface to your clients in case you create a library and you do not want to expose the internals. For this purpose the DPL team came up with the D interface files in order to solve this problem. So, you simply give the interface files to your client along with your library, and job is done.
See the reference file at: http://dlang.org/module.html
The imports are done like this (extracted from there):
import std.stdio; // import module stdio from package std
import foo, bar; // import modules foo and bar
void main() {
writefln("hello!\n"); // calls std.stdio.writefln
}
And modules are created like this:
module A;
void foo();
void bar();
The D style is:
import myinclude;
D doesn't have a textual include like C.
D doesn't include files in the same way as C/C++.
C/C++ actually copies the contents of the included file at the location of the #include, which is why the '#pragma once' or other inclusion guard is required, because the code will really be copied for each time it is in a #include line.
D doesn't bother with copying the source around, and compiles each source file into its own object file. The 'import' command in D asks for the symbols (or the names of each function, class, struct, enum, and variable) found in the module that is asked for. The compiler then just has to connect these object files together into the executable. Well, there's a bit more to it than that, but that should be accurate enough for this.
Also, as Mr Ruppe said, there is no need to enclose the name of the source file in quotes, nor do you need to add the file extension to the name.
If, for example, you have your main() function in main.d, and you want to include a source file called foo.d, that is in the same folder as main.d, then you type the following into main.d;
import foo;
and in foo, you would have;
module foo;
<other code here>
Hope this helps. Most of this is also on http://dlang.org, on their 'modules' page under the language definitions.
I just discovered that some parts of the code I am working on incorrectly uses writeable static data where it could/should use constant data.
Short of doing a dumb search-and-replace for "static" -> "static const", is there any way to preventing all 'static' data from being writeable, much like how constant string data can be made explicitly writeable?
I am using the GCC toolchain, development target is x86.
There's probably writable static data in some of the libraries you use. (Such as the standard C and C++ libraries). Making that const would be bad.
It's probably better to go through your code and change things manually.
You can use nm to get a list of symbols in your .o files. In the nm output, the first column gives the type of symbol; the letters B, C, D, G or S indicate writable data. The last column gives the (mangled) variable name. It's possible to write a little script to parse the nm output and look for these.
I guess the better way is adding the "const" to all variables you have. You could use a "#define static static const" (note that it would break wherever you already changed it) but I don't recommend doing so (will make your code much less readable and possibly will break some things, and you won't be able to declare static functions).