Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ public static void Main(string[] args)
Console.WriteLine(""Hello, world!"");
}
}";
const string expectedResult = @"class Program
const string expectedResult = @"#include <iostream>
#include <string>

class Program
{
public: static void Main(std::string args[])
{
Expand All @@ -35,5 +38,63 @@ public static void Main(string[] args)
var actualResult = transformer.Transform(helloWorldCode);
Assert.Equal(expectedResult, actualResult);
}

[Fact]
public void BooleanTypeTransformationTest()
{
const string booleanCode = @"namespace fuzzbuzz {
class Program {
public static Boolean func(int x1, int x2, int y1, int y2) {
Boolean first = true;
if(x1*x1 + y1*y1 > x2*x2 + y2*y2) {
first = false;
}
return first;
}

public static void Main(string[] args) {
Console.WriteLine(""Test"");
}
}
}";
const string expectedResult = @"namespace fuzzbuzz {
class Program {
public: static bool func(std::int32_t x1, std::int32_t x2, std::int32_t y1, std::int32_t y2) {
bool first = true;
if(x1*x1 + y1*y1 > x2*x2 + y2*y2) {
first = false;
}
return first;
}

public: static void Main(std::string args[]) {
printf(""Test\n"");
}
}
}";
var transformer = new CSharpToCppTransformer();
var actualResult = transformer.Transform(booleanCode);
Assert.Equal(expectedResult, actualResult);
}

[Fact]
public void InputOutputTransformationTest()
{
const string inputOutputCode = @"using System;
class Program
{
public static void Main(string[] args)
{
string line = Console.ReadLine();
int number = int.Parse(line);
Console.WriteLine(number);
}
}";
var transformer = new CSharpToCppTransformer();
var actualResult = transformer.Transform(inputOutputCode);
// Debug output to see what we get currently
System.Console.WriteLine("INPUT/OUTPUT ACTUAL:");
System.Console.WriteLine(actualResult);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ public class CSharpToCppTransformer : TextTransformer
// ulong
// std::uint64_t
(new Regex(@"(?<before>\W)((System\.)?UInt64|ulong)(?!\s*=|\()(?<after>\W)"), "${before}std::uint64_t${after}", 0),
// Boolean
// bool
(new Regex(@"(?<before>\W)((System\.)?Boolean)(?!\s*=|\()(?<after>\W)"), "${before}bool${after}", 0),
// char*[] args
// char* args[]
(new Regex(@"([_a-zA-Z0-9:\*]?)\[\] ([a-zA-Z0-9]+)"), "$1 $2[]", 0),
Expand All @@ -266,7 +269,10 @@ public class CSharpToCppTransformer : TextTransformer
// double.MaxValue
// std::numeric_limits<float>::max()
(new Regex(@"(?<before>\W)(?<type>std::[a-z0-9_]+|float|double)\.MaxValue(?<after>\W)"), "${before}std::numeric_limits<${type}>::max()${after}", 0),
// using Platform.Numbers;
// using System;
// #include <iostream>
(new Regex(@"([\r\n]{2}|^)\s*?using System;\s*?$"), "$1#include <iostream>" + Environment.NewLine + "#include <string>" + Environment.NewLine, 0),
// using Platform.Numbers; (other usings)
//
(new Regex(@"([\r\n]{2}|^)\s*?using [\.a-zA-Z0-9]+;\s*?$"), "", 0),
// class SizedBinaryTreeMethodsBase : GenericCollectionMethodsBase
Expand Down Expand Up @@ -347,6 +353,18 @@ public class CSharpToCppTransformer : TextTransformer
// Console.WriteLine("...")
// printf("...\n")
(new Regex(@"Console\.WriteLine\(""([^""\r\n]+)""\)"), "printf(\"$1\\n\")", 0),
// Console.WriteLine(variable)
// std::cout << variable << std::endl
(new Regex(@"Console\.WriteLine\(([^""\r\n\)]+)\)"), "std::cout << $1 << std::endl", 0),
// std::string variable = Console.ReadLine();
// std::string variable; std::getline(std::cin, variable);
(new Regex(@"(std::string)\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*Console\.ReadLine\(\);"), "$1 $2;" + Environment.NewLine + " std::getline(std::cin, $2);", 0),
// variable = Console.ReadLine(); (when already declared)
// std::getline(std::cin, variable);
(new Regex(@"([a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*Console\.ReadLine\(\);"), "std::getline(std::cin, $1);", 0),
// int.Parse(variable) or std::int32_t.Parse(variable)
// std::stoi(variable)
(new Regex(@"(std::)?int(32_t)?\.Parse\(([^)]+)\)"), "std::stoi($3)", 0),
// TElement Root;
// TElement Root = 0;
(new Regex(@"(?<before>\r?\n[\t ]+)(?<access>(private|protected|public)(: )?)?(?<type>[a-zA-Z0-9:_]+(?<!return)) (?<name>[_a-zA-Z0-9]+);"), "${before}${access}${type} ${name} = 0;", 0),
Expand Down
Loading