A tool for transpiling C code to Go code.
Milestones of the project:
Notes:
- Transpiler works on linux machines
- Need to have installed
clang
. See llvm download page
c4go
requires Go 1.9 or newer.
go get -u github.com/Konstantin8105/c4go
# Change your location to the folder with examples:
cd $GOPATH/src/github.com/Konstantin8105/c4go/examples/
# Transpile one file from the C example folder:
c4go transpile prime.c
# Look at the result
nano prime.go
# Check the result:
go run prime.go
# Enter a number
# 13
# The number is: 13
# Prime number.
C code of file prime.c
:
#include <stdio.h>
int main()
{
int n, c;
printf("Enter a number\n");
// get value
scanf("%d", &n);
printf("The number is: %d\n", 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;
}
Go code of file prime.go
:
//
// Package main - transpiled by c4go
//
// If you have found any issues, please raise an issue at:
// https://github.com/Konstantin8105/c4go/
//
package main
import "unsafe"
import "github.com/Konstantin8105/c4go/noarch"
import "fmt"
// main - transpiled function from $GOPATH/src/github.com/Konstantin8105/c4go/examples/prime.c:3
func main() {
var n int
var c int
fmt.Printf("Enter a number\n")
noarch.Scanf([]byte("%d\x00"), (*[100000000]int)(unsafe.Pointer(&n))[:])
// get value
//
noarch.Printf([]byte("The number is: %d\n\x00"), n)
if n == 2 {
fmt.Printf("Prime number.\n")
// -------
//
} else {
for c = 2; c <= n-1; c++ {
if n%c == 0 {
break
}
}
if c != n {
fmt.Printf("Not prime.\n")
} else {
fmt.Printf("Prime number.\n")
}
}
return
}
func init() {
}
assert.h 1/1 100%
ctype.h 14/14 100%
errno.h 0/1 0%
float.h undefined
iso646.h undefined
limits.h undefined
locale.h 0/3 0%
math.h 40/58 69%
setjmp.h 0/3 0%
signal.h 0/3 0%
stdarg.h 4/4 100%
stddef.h 2/6 33.3%
stdio.h 33/46 71.7%
stdlib.h 32/47 68.1%
string.h 12/24 50%
time.h 8/15 53.3%
wchar.h 0/68 0%
wctype.h 0/22 0%
Feel free to submit PRs or open issues. Main information from: http://en.cppreference.com/w/c
By default, only unit tests are run with go test
. You can also include the
integration tests:
go test -tags=integration ./...
Integration tests in the form of complete C programs that can be found in the tests directory.
Integration tests work like this:
- Clang compiles the C to a binary as normal.
- c4go converts the C file to Go.
- The Go is built to produce another binary.
- Both binaries are executed and the output is compared. All C files will contain some output so the results can be verified.