Giter VIP home page Giter VIP logo

c4go's Introduction

Build Status Go Report Card codecov GitHub license GoDoc Maintainability

A tool for transpiling C code to Go code.

Milestones of the project:

  1. Transpiling project GNU GSL.
  2. Transpiling project GTK+.

Notes:

Installation

c4go requires Go 1.9 or newer.

go get -u github.com/Konstantin8105/c4go

Usage example

# 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() {
}

C standard library implementation

            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%

Contributing

Feel free to submit PRs or open issues. Main information from: http://en.cppreference.com/w/c

Testing

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:

  1. Clang compiles the C to a binary as normal.
  2. c4go converts the C file to Go.
  3. The Go is built to produce another binary.
  4. Both binaries are executed and the output is compared. All C files will contain some output so the results can be verified.

c4go's People

Contributors

elliotchance avatar konstantin8105 avatar corebreaker avatar x-tyger avatar yulvil avatar zoeyfyi avatar jorbs avatar tjyang avatar pravj avatar alebcay avatar jnjackins avatar rjalfa avatar xoviat avatar xyproto avatar souvikmaji avatar bradleyjkemp avatar crgimenes avatar lestrrat avatar tboztuna avatar utkarshgupta-cs avatar crvv avatar timothygk avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.