GridProtectionAlliance / go2cs

Golang to C# Converter
https://go2cs.net
MIT License
362 stars 50 forks source link

Nice work! #1

Open gengle opened 5 years ago

gengle commented 5 years ago

I am pleasantly impressed by all your work on this effort. Let me know if there is anything I can help with.

ritchiecarroll commented 5 years ago

It's a pet project. I haven't had much time to poke at it in the last few months, anything specific you would like to tackle? :-)

atErik commented 4 years ago

Hi, yep very good+helpful project indeed.
is there any pointer/project/tool to convert Go/Golang into C ?
thanks in advance.

ritchiecarroll commented 4 years ago

I think "cgo" allows this already, basically you can use your Go functions in C/C++:

https://golang.org/cmd/cgo/#hdr-C_references_to_Go

atErik commented 4 years ago

Thanks.
i think, "cgo" allows to enter/use "C" code inside "Go" code, but...
i do not want any "Go" code remaining in transpiled output or anywhere , i want complete conversion into "C" or "C++".

i posted link to this project here: https://stackoverflow.com/questions/64180191/ ( if you find/consider it useful or if it may be useful to others , then please UPVOTE for both Q+A , or please vote on "Converting/transpiling..." to UNDELETE it ).
( by the way, Transpiler-List is now here or here ( or find the project from my repo(s) here ).

EDIT : i have removed few words, etc.
EDIT : added a request to upvote both Q+A.
EDIT : added links for Transpiler-List new page.

ritchiecarroll commented 4 years ago

I upvoted your post, but looks like topic was closed.

FYI, given my experience with this project so far, i.e., converting from Golang to C#, I think a converter to C at a source code level is certainly possible. I disagree (given my experience here) with the guy who responded to your initial post that "goroutines / channels would very hard to express in C". These types of things would actually be the easy part, the harder parts are like these mentioned here: https://go2cs.net/ConversionStrategies.html, for which workable strategies already exist.

A lot of work, but very doable.

Thanks, Ritchie

ritchiecarroll commented 4 years ago

BTW - most of those other tools, like the ones that go directly from Go to C or C++ are way short of being able to compile day-to-day Go code. Even the code I've put together here still has a ways to go - however, I have already produced a half-way decent conversion to the entire Go standard library source code:

https://github.com/GridProtectionAlliance/go2cs/tree/master/src/go-src-converted

I feel I'm less than 20% away from a reliable transpiler, although that last 5% may be hard won - with all the "edge" cases that will need to be handled.

atErik commented 4 years ago

Hi Ritchie,

i appreciate your upvote in SO Q+A , thank you.

EXTRA-INFO , PLEASE SKIP , WHEN YOU DO NOT HAVE SUFFICIENT TIME TO READ:
There are "Go"/"Golang" & "Google" PUSHERS, who up-votes SO Q/A on C/C++ into Go based transpilers or conversion process, but when it comes to "Go" into something else like C/C++, then they down-votes .
If you look at the person who edited (and closed) my Question and also down-voted my answer, have extensive connections with Go (and Google), etc . ( Google Crimes1, 2, 3, 4, 5, 6. Problems in "Go"1, 2, 3, 4, 5, 6, 7 ).

Question was simple, which transpiler tool can do better Go-to-C transpilation , but asking Q about suggestion on tools/software is not allowed in SO !! so i have changed it, which transpiler out of specific four, can do Go-to-C (Go2C) conversion better, etc . Q is about TRANSPILER & can a transpiler keep algorithms/structures used in source fairly or accurately same in output or not . But other answer-er has taken it to different directions : Learning "Go" language, when to transpile & when to not, etc, etc, ( so i have down-voted it because as it is not-correct answer, for the Questions that i have asked . It is a good answer for another type of question (for-example : Should i transpile "Go" And Should i learn "Go" ? ) . Any answer must focus on what i have asked-for, first : answer must analysis specific four TRANSPILER sets i have mentioned (and provided links), & answer must post result, & show which one/set is better, etc ).

You're right, the other person in comment area, even said there is no-tool !! i knew of few transpilers (but all were supporting only subset), & then my search began to show even more , though those search-results were better than what i knew , but none were supporting full or close to full support of source/input language , these type of (only subset supported) tools need lots of improvements , and those shortcomings you have also pointed out in your message in above . Thanks for pointing those out, but yeah, sad.

After i compared few features of other transpilers including yours , i have immediately realized your transpiler is much much better & more mature in compare to others , so i have kept it in my "solutions" as top two options.

i guess programing is an art, too many ways to program , it takes long time (so very hard) to collect & put all various types of coding "patterns" & "variations" into workable detection engines, & spit out something very close/equivalent, in another language . More volunteers can obviously speed it up.

I'm still learning & grasping with manual Go-to-C++ transpilation, for a project conversion,
i still do lots of regex based detection & replace with my own code.
And i still also have to learn few other languages (Python, C#, Rust, etc) sufficiently well.
I understand C, Assembly, etc and not-all-of C++, etc , Others not so well , Golang is much newer to me ( but as some users keep pointing out its close to C, shouldn't be hard ).
So I will have to manage/find more time, to learn those better.

QUESTION:
i have already asked about "Go"-to-"C" earlier, so this time, What you think is a fairly good "C#"-to-"C" transpiler ? or "C#"-to-"C++" transpiler ?

EDIT : removed few sentences/sections, improved other responses, etc.
EDIT : added links on "Go"-problems, etc.

ritchiecarroll commented 4 years ago

I'm not aware of any "good" options for actual source conversion from C# to C++. There was a project, like 5 years back, that attempted this https://github.com/AlexAlbala/Alter-Native. This project appears dead, but perhaps you can find some some forks with some life in them. One of the stated goals of the Alter-Native project was the "WORA (Write Once, Run Everywhere) philosophy", but this goal has now been fully subsumed by .NET Core.

There's also a commercial option - see here. Note that this commercial option is not .NET Core based, its highest supported .NET version is NET Framework v4.6.1. Also the conversions seem to be focused on "language conversion", the tool seems limited in terms of .NET class library conversions. Here's a quote from the main page:

"C++ to C# Converter is intended to reduce the amount of work you'll have to do to convert code to C#, but it is just the first step."

The .NET class libraries are the primary issue. A source code conversion from C# to any other language target for the purposes of transpilation is made difficult by the fact that the .NET class libraries are so extensive. I guess someone could make the same argument about the Go core libraries where I am already making progress, but the Go language is minimalistic and very simple as compared to C#.

I know there are several commercial options that say they will go from C# to Java (maybe even some open source ones), but I suspect none of these tools have been able to keep pace with the rapid language feature and .NET Core updates that have been happening over the last few years.

I think C# is starting to stand alone in the field of programming languages - especially in terms of language features, depth of class libraries and runtime speed. However, with this comes new levels of complexity that make truly learning C# more difficult. It has always been simple to get started with C#, but it takes time to master. I think this is why so many new programmers are attracted to Go, the language is simple - it's simple to get started, and quick to master. It is because of this that I am developing go2cs, i.e., it will allow the true "transpiler" option: you can code in Go and compile to C# (should one need to interact with .NET). Guess we'll see how well that ends up.

For something close to transpilation of C# to C++, I am aware of the "CoreRT" project (https://github.com/dotnet/corert) that offers real native compilation of .NET, to the point where it produces native libraries that can be linked into C/C++ applications. This would basically allow you take your C# code and use it along with other native C++ applications. The issue here is that this project is still in "experimental" mode.

That said, if you don't need "lib" files that can be linked with C++ projects, .NET already compiles to native code with an option called "NET Native". In Visual Studio, for example, you can set this up with a folder-based publish target that will allow you to specify a native OS and platform target, e.g., Linux x64. This will produce native code directly from C# that is more in-line with typical C++ application runtime speeds.

Lastly, all these options not to be confused with the "crossgen" project. This project keeps your code as portable IL, but on the target platform basically performs a pre-compile of the code. This is more akin to normal .NET runtime operation but with an up-front compile instead of using just-in-time (JIT) compile. However, this generally only buys you faster startup times by eliminating the JIT processing and will still fallback on JIT if a native binary cannot be found. Also, you won't get the same overall runtime speed benefits as you do with .NET Native compiles.

None of these native options gets you to "translated source code", but they do provide native compiled applications from C# code with the runtime speed benefits. So in the end, I guess it's all about what you would like to accomplish.

atErik commented 4 years ago

Hi Ritchie , THANK YOU for your VERY HELPFUL if not best analysis.

need both : speedy conversion from Go into C/C++,
and also high-quality source-to-source code conversion with all high-level structures of input remaining fairly accurate in output source.
i think thats still far now for Go-to-C/C++ conversion.

so i will settle in middle now, need better quality C or C++ output, with Go-to-C or Go-to-C++ fast conversion.

two requests:
i have created new page for Transpiler List, here ( or find it from [here](https://github.com/atErik/ or here) . SO's "do-evil" Google-PUSHER mods have deleted the SO Q+A here , but a vote to UNDELETE would be very helpful ( it needs 5 undelete votes to undelete) , if you want to then please help.

i found another transpiler "Hurley" , it appears able to do C# to C++ conversion.

if you have time to inspect it little bit, what you think of C#-to-C++ conversion quality of Hurley ?

i doubt it can do better than CoreRT , but might be better than cs2cpp ?
other option for C#-to-C++ is Alter-Native, but its dead for now, as you've pointed out.

if "Hurley" can do HQ "cs-to-cpp" conversion like your this "go2cs" project , then my objective of HQ (high-quality) conversion of "Go-to-C" or "Go-to-C++" is almost achieved , by using only two conversions : go2cs -> hurley.

The "go2c(mukadr)" transpiler's conversion quality is not high :( now , bcoz it supports only subset of Go, or else, i would/could have used it continuously.

CrossGen is very interesting.
Yes, i need the (Go-source) transpiled (into C/C++) source working in multiple platform (macOS & Debian-GNU/Linux & Windows, etc).
As none of the transpilers were suitable for my "go-2-c"/"go-2-cpp" conversion need , i began to look into Go to Assembly conversion (and then from Assembly to C/C++, i know this process would lost almost all high-level structures in "Go" source, but it would be a faster solution) , & So, i found-out that "Go" tools itself can convert it into "Assembly", but its a "Golang" flavored "Assembly" , not GNU/GAS/AT&T-Assembly , not NASM-Assembly, not-MASM, etc.
So i'm trying to understand if any tool from GoDevTool (http://www.GoDevTool.com/) can do this Go-"Assembly" to GNU/NASM/MASM-Assembly conversion . As i want to avoid compiling the Go source into machine code & then machine-code to Assembly->C/C++ with "Boomerang" (https://sourceforge.net/projects/boomerang/), etc . It appears Boomerang can also do Assembly source-code to C/C++ source-code decompilation.

EDIT : added links (created new page for Transpiler List).
EDIT : found-out that "GoDevTool" cannot do "Golang"-"Assembly" into other Assembly conversion, so back to research again.