elliotchance / c2go
- воскресенье, 16 апреля 2017 г. в 03:11:38
Go
⚖️ A tool for converting C to Go.
A tool for converting C to Go.
The goals of this project are:
go get -u github.com/elliotchance/c2go
c2go myfile.c
The c2go
program processes a single C file and outputs the translated code
in Go. Let's use an included example,
prime.c:
#include <stdio.h>
int main()
{
int n, c;
printf("Enter a number\n");
scanf("%d", &n);
if ( n == 2 )
printf("Prime number.\n");
else
{
for ( c = 2 ; c <= n - 1 ; c++ )
{
if ( n % c == 0 )
break;
}
if ( c != n )
printf("Not prime.\n");
else
printf("Prime number.\n");
}
return 0;
}
c2go prime.c > prime.go
go run prime.go
Enter a number
23
Prime number.
prime.go
looks like:
package main
import (
"fmt"
)
// ... lots of system types in Go removed for brevity.
func main() {
var n int
var c int
fmt.Printf("Enter a number\n")
fmt.Scanf("%d", &n)
if n == 2 {
fmt.Printf("Prime number.\n")
} else {
for c = 2; c <= n - 1; c += 1 {
if n % c == 0 {
break
}
}
if c != n {
fmt.Printf("Not prime.\n")
} else {
fmt.Printf("Prime number.\n")
}
}
return
}
This table represents what is supported. If you see anything missing (there is a lot missing!) please add it with a pull request.
Function | Supported? | Notes |
---|---|---|
assert.h | Yes | |
assert | Yes | This is actually a macro. |
math.h | Partly | All of the C99 functions. |
acos | Yes | |
asin | Yes | |
atan | Yes | |
atan2 | Yes | |
ceil | Yes | |
cos | Yes | |
cosh | Yes | |
exp | Yes | |
fabs | Yes | |
floor | Yes | |
fmod | Yes | |
ldexp | Yes | |
log | Yes | |
log10 | Yes | |
pow | Yes | |
sin | Yes | |
sinh | Yes | |
sqrt | Yes | |
tan | Yes | |
tanh | Yes | |
stdio.h | Partly | |
printf | Yes | |
scanf | Yes |
This is the process:
The C code is preprocessed with clang. This generates a larger file (pp.c
),
but removes all the platform specific directives and macros.
pp.c
is parsed with the clang AST and dumps it in a colourful text format
that
looks like this.
Apart from just parsing the C and dumping an AST, the AST contains all of the
resolved information that a compiler would need (such as data types). This means
that the code must compile successfully under clang for the AST to also be
usable.
Since we have all the types in the AST it's just a matter of traversing the tree is a semi-intelligent way and producing Go. Easy, right!?
Testing is done with a set of integrations tests in the form of complete C programs that can be found in the tests directory.
For each of those files:
The test suite is run with run-tests.sh.
As I said it is still very early days (sorry for all the hacky Python). And eventually the build chain can be converted to pure Go since we don't need any clang APIs.
Contributing is done with pull requests. There is no help that is too small! :) If you're looking for where to start I can suggest finding a simple C program (like the other examples) that does not successful translate into Go and fixing up the Python so that it does.
Or, if you don't want to do that you can submit it as an issue so that it can be picked up by someone else.