How to convert C to D using compiler?

ManKey mensikovk817 at gmail.com
Tue Dec 7 18:03:59 UTC 2021


On Tuesday, 7 December 2021 at 16:43:21 UTC, ManKey wrote:
> I tried this
> ```
> ldc2 test.i -mixin="test.d"
> ```
> but the test.d file is empty :(

I write small script for this

```d

import std;

string replaceExt(string path, string from, string to){
	return path.dirName ~ "/" ~ path.baseName(from) ~ to;
}

int main(string[] args){
	assert(args.length == 2);

	string src_path = args[1];
	string srci_path = src_path.replaceExt(".c", ".i");
	string renamed_path = src_path.replaceExt(".c", ".d");

	void preprocess(string orig, string ifile){
		auto result = execute(["clang", "-E", orig]);
		assert(result.status == 0, result.output);
		std.file.write(ifile, result.output);
	}

	void convert(string src, string outf){
		auto result = execute(["ldc2", src, "-vcg-ast", "-o-"]);
		assert(result.status == 0, result.output);
		rename(src ~ ".cg", outf);
	}


	preprocess(src_path, srci_path);
	convert(srci_path, renamed_path);
	std.file.remove(srci_path);

	return 0;
}
```


More information about the Digitalmars-d mailing list