Skip to content

Commit

Permalink
Remove TranslationSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
dkorpel committed May 24, 2024
1 parent 601f893 commit d8f5cdd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 45 deletions.
39 changes: 25 additions & 14 deletions source/ctod/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,54 @@ module ctod.app;
import tree_sitter.api;

import std.stdio;
import std.path: withExtension;
import std.path : withExtension;
import std.file;
import std.file: writeFile = write;
import std.file : writeFile = write;
import std.path;

import ctod.translate;

private:

version(unittest) {} else
int main(string[] args) {
if (args.length < 2) {
version (unittest) { } else
int main(string[] args)
{
if (args.length < 2)
{
stderr.writeln("give at least one file argument");
return -1;
}
try {
foreach(i; 1..args.length) {
TranslationSettings settings;
if (args[i] == "--help") {
try
{
foreach (i; 1 .. args.length)
{
if (args[i] == "--help")
{
printHelp(args[0]);
} else {
}
else
{
const fname = args[i];
if (!(fname.extension == ".c" || fname.extension == ".h")) {
if (!(fname.extension == ".c" || fname.extension == ".h"))
{
stderr.writeln("file shoud have .c or .h extension, not ", fname.extension);
return -1;
}
const source = cast(string) read(fname);
const moduleName = fname.baseName.stripExtension;
writeFile(fname.withExtension(".d"), translateFile(source, moduleName, settings));
writeFile(fname.withExtension(".d"), translateFile(source, moduleName));
}
}
} catch (Exception e) {
}
catch (Exception e)
{
stderr.writeln(e.msg);
return -1;
}
return 0;
}

void printHelp(string name) {
void printHelp(string name)
{
writeln("Usage: ", name, " [FILES]\nOptions:\n --strip strip comments");
}
6 changes: 3 additions & 3 deletions source/ctod/test.d
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ module ctod.test;
@safe:

import ctod.translate;
import ctod.util;

private void test(string c, string d)
{
TranslationSettings settings;
settings.includeHeader = false;
const actual = translateFile(c, "testmodule", settings);
const actual = translateFile(c, "testmodule")[56 .. $];

if (actual != d)
{
import std.stdio: writeln;
Expand Down
46 changes: 18 additions & 28 deletions source/ctod/translate.d
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ private template HasVersion(string versionId) {
}
`;

struct TranslationSettings
{
bool includeHeader = true;
}

/// Returns: C language parser for tree-sitter
extern (C) void* tree_sitter_c();

Expand All @@ -35,12 +30,22 @@ private TSParser* getCParser() @trusted
return parser;
}

package void headerString(ref OutBuffer result, string moduleName)
{
if (moduleName.length > 0)
{
result ~= "module ";
result ~= moduleName;
result ~= ";\n";
}
result ~= "@nogc nothrow:\n" ~ "extern(C): __gshared:\n";
}

/// Params:
/// source = C source code
/// moduleName = name for the `module` declaration on the D side
/// settings = translation settings
/// Returns: `source` translated from C to D
string translateFile(string source, string moduleName, ref TranslationSettings settings)
string translateFile(string source, string moduleName)
{
auto parser = getCParser();
// scope(exit) ts_parser_delete(parser);
Expand All @@ -63,38 +68,23 @@ string translateFile(string source, string moduleName, ref TranslationSettings s
translateNode(ctx, *root);

OutBuffer result;

if (settings.includeHeader)
{
if (moduleName.length > 0)
{
result ~= "module ";
result ~= moduleName;
result ~= ";\n";
}
result ~= "@nogc nothrow:\n" ~ "extern(C): __gshared:\n";
}
headerString(result, moduleName);

if (ctx.needsHasVersion)
{
result ~= hasVersion;
}

if (ctx.needsClong)
{
result ~= "import core.stdc.config: c_long, c_ulong;\n";
}

if (ctx.needsWchar)
{
result ~= "import core.stdc.stddef: wchar_t;\n";
}

if (ctx.needsInt128)
{
result ~= "import core.int128;\n";
}

if (ctx.needsCbool)
{
result ~= "alias c_bool = int;\n";
}

// white space leading up to the first AST element is not included in the AST, so add it
result ~= source[0 .. root.start];
result ~= root.output();
Expand Down

0 comments on commit d8f5cdd

Please sign in to comment.