All Projects → elliotchance → C2go

elliotchance / C2go

Licence: mit
⚖️ A tool for transpiling C to Go.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to C2go

Ts2c
Convert Javascript/TypeScript to C
Stars: ✭ 878 (-49.42%)
Mutual labels:  transpiler
Fetlang
Fetish-themed programming language
Stars: ✭ 1,337 (-22.98%)
Mutual labels:  transpiler
Godzilla
Godzilla is a ES2015 to Go source code transpiler and runtime
Stars: ✭ 1,464 (-15.67%)
Mutual labels:  transpiler
Elchemy
Write Elixir code using statically-typed Elm-like syntax (compatible with Elm tooling)
Stars: ✭ 1,080 (-37.79%)
Mutual labels:  transpiler
Transpyle
HPC-oriented transpiler for C, C++, Cython, Fortran, OpenCL and Python.
Stars: ✭ 90 (-94.82%)
Mutual labels:  transpiler
Cs2x
Transpiles a C# subset to non .NET languages and runtimes. (Powered by Roslyn)
Stars: ✭ 97 (-94.41%)
Mutual labels:  transpiler
Pas2dart
Object Pascal (Free Pascal 3, Delphi 2007) to Dart (2.5) transpiler
Stars: ✭ 16 (-99.08%)
Mutual labels:  transpiler
Ply
Painless polymorphism
Stars: ✭ 117 (-93.26%)
Mutual labels:  transpiler
Pysimpleguidesigner
Desinger for PySimpleGUI
Stars: ✭ 95 (-94.53%)
Mutual labels:  transpiler
Pseudo Python
a restricted python to javascript / c# / go / ruby compiler
Stars: ✭ 106 (-93.89%)
Mutual labels:  transpiler
Jsweet
A Java to JavaScript transpiler.
Stars: ✭ 1,167 (-32.78%)
Mutual labels:  transpiler
Get Schwifty
Get Schwifty is a self-hosted Swift transpiler and was originally build for my WWDC scholarship application.
Stars: ✭ 89 (-94.87%)
Mutual labels:  transpiler
Powscript
transpiler written in bash: painless shellscript, indentbased, coffee for the shell with hipster-sparkles v1 BETA LANDED 🎉🎉🎉🎉 thanks fcard!
Stars: ✭ 97 (-94.41%)
Mutual labels:  transpiler
Qooxdoo Compiler
Compiler for Qooxdoo, 100% javascript
Stars: ✭ 32 (-98.16%)
Mutual labels:  transpiler
Elixirscript
Converts Elixir to JavaScript
Stars: ✭ 1,504 (-13.36%)
Mutual labels:  transpiler
Prolog Target Js
Simple Prolog to JS transpiler
Stars: ✭ 19 (-98.91%)
Mutual labels:  transpiler
Evm2wasm
[ORPHANED] Transcompiles EVM code to eWASM
Stars: ✭ 96 (-94.47%)
Mutual labels:  transpiler
Rbql
🦜RBQL - Rainbow Query Language: SQL-like language for (not only) CSV file processing. Supports SQL queries with Python and JavaScript expressions
Stars: ✭ 118 (-93.2%)
Mutual labels:  transpiler
Crossshader
⚔️ A tool for cross compiling shaders. Convert between GLSL, HLSL, Metal Shader Language, or older versions of GLSL.
Stars: ✭ 113 (-93.49%)
Mutual labels:  transpiler
Babel Preset Github
GitHub.com's Babel configuration
Stars: ✭ 103 (-94.07%)
Mutual labels:  transpiler

Build Status GitHub version Go Report Card codecov GitHub license Join the chat at https://gitter.im/c2goproject Twitter GoDoc

A tool for converting C to Go.

The goals of this project are:

  1. To create a generic tool that can convert C to Go.
  2. To be cross platform (linux and mac) and work against as many clang versions as possible (the clang AST API is not stable).
  3. To be a repeatable and predictable tool (rather than doing most of the work and you have to clean up the output to get it working.)
  4. To deliver quick and small version increments.
  5. The ultimate milestone is to be able to compile the SQLite3 source code and have it working without modification. This will be the 1.0.0 release.

Installation

c2go requires Go 1.9 or newer.

go get -u github.com/elliotchance/c2go

Usage

c2go transpile 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 transpile prime.c
go run prime.go
Enter a number
23
Prime number.

prime.go looks like:

package main

import "unsafe"

import "github.com/elliotchance/c2go/noarch"

// ... lots of system types in Go removed for brevity.

var stdin *noarch.File
var stdout *noarch.File
var stderr *noarch.File

func main() {
	__init()
	var n int
	var c int
	noarch.Printf([]byte("Enter a number\n\x00"))
	noarch.Scanf([]byte("%d\x00"), (*[1]int)(unsafe.Pointer(&n))[:])
	if n == 2 {
		noarch.Printf([]byte("Prime number.\n\x00"))
	} else {
		for c = 2; c <= n-1; func() int {
			c += 1
			return c
		}() {
			if n%c == 0 {
				break
			}
		}
		if c != n {
			noarch.Printf([]byte("Not prime.\n\x00"))
		} else {
			noarch.Printf([]byte("Prime number.\n\x00"))
		}
	}
	return
}

func __init() {
	stdin = noarch.Stdin
	stdout = noarch.Stdout
	stderr = noarch.Stderr
}

How It Works

This is the process:

  1. The C code is preprocessed with clang. This generates a larger file (pp.c), but removes all the platform-specific directives and macros.

  2. 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.

  3. Since we have all the types in the AST it's just a matter of traversing the tree in a semi-intelligent way and producing Go. Easy, right!?

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. c2go 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.

Contributing

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 successfully translate into Go.

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].