• Stars
    star
    304
  • Rank 137,274 (Top 3 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created over 2 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Convert C to Go

c2go - Convert C to Go

Build Status Go Report Card GitHub release Coverage Status GoDoc Language

This is a subproject of the Go+ project. Its goal is converting any C project into Go without any human intervention and keeping performance close to C.

  • Make it passed. Stage: Done Almost. We have compiled libc (in progress) and sqlite3 (done).
  • Make it correct. Stage: Doing. We ported libc-test to test c2go and libc. Coverage: Coverage Status
  • Make it fast. Stage: Planning.

Screen Shot1

How to run examples?

Run an example:

  • Build c2go tools: go install -v ./...
  • Go testdata/xxx directory, and run c2go .

Run/Test multiple examples:

  • Run examples: c2go ./...
  • Test examples: c2go -test ./...

How c2go is used in Go+

Here is an example to show how Go+ interacts with C.

import "C"

C.printf C"Hello, c2go!\n"
C.fprintf C.stderr, C"Hi, %7.1f\n", 3.14

Here we use import "C" to import libc. It's an abbreviation for import "C/github.com/goplus/libc". It is equivalent to the following code:

import "C/github.com/goplus/libc"

C.printf C"Hello, c2go!\n"
C.fprintf C.stderr, C"Hi, %7.1f\n", 3.14

In this example we call two C standard functions printf and fprintf, passing a C variable stderr and two C strings in the form of C"xxx" (a Go+ syntax to represent C style strings).

Run gop run . to see the output of this example:

Hello, c2go!
Hi,     3.1

What's our plan?

  • First, support most of the syntax of C. Stage: Done Almost, see supported C syntax.
  • Second, compile sqlite3 to fix c2go bugs and get list of its dependent C standard libary fuctions. Stage: Done, see github.com/goplus/sqlite and its dependent fuctions.
  • Third, support most of C standard library functions (especially that used by sqlite3), and can import them by Go+. Stage: Doing, see detailed progress here.
  • Last, support all custom libraries, especially those well-known open source libraries. Stage: Planning.

Tested Platform

  • MacOS: 11.x
  • Linux: ubuntu-20.04 (temporarily skip testdata/qsort)
  • Windows

Supported C syntax

Data structures

  • Void: void
  • Boolean: _Bool, bool
  • Integer: [signed/unsigned] [short/long/long long] int
  • Enum: enum
  • Float: float, double, long double
  • Character: [signed/unsigned] char
  • Wide Character: wchar_t
  • Large Integer: [signed/unsigned] __int128
  • Complex: _Complex float/double/long double
  • Typedef: typedef
  • Pointer: *T, T[]
  • Array: T[N], T[]
  • Array Pointer: T(*)[N]
  • Function Pointer: T (*)(T1, T2, ...)
  • Struct: struct
  • Union: union
  • BitField: intType :N

Operators

  • Arithmetic: a+b, a-b, a*b, a/b, a%b, -a, +a
  • Increment/Decrement: a++, a--, ++a, --a
  • Comparison: a<b, a<=b, a>b, a>=b, a==b, a!=b
  • Logical: a&&b, a||b, !a
  • Bitwise: a|b, a&b, a^b, ~a, a<<n, a>>n
  • Pointer Arithmetic: p+n, p-n, p-q, p++, p--
  • Assignment: =
  • Operator Assignment: a<op>=b
  • BitField Assignment: =
  • BitField Operator Assignment: a<op>=b
  • Struct/Union/BitField Member: a.b
  • Array Member: a[n]
  • Pointer Member: &a, *p, p[n], n[p], p->b
  • Comma: a,b
  • Ternary Conditional: cond?a:b
  • Function Call: f(a1, a2, ...)
  • Conversion: (T)a
  • Sizeof: sizeof(T), sizeof(a)
  • Offsetof: __builtin_offsetof(T, member)

Literals

  • Boolean, Integer
  • Float, Complex Imaginary
  • Character, String
  • Array: (T[]){ expr1, expr2, ... }
  • Array Pointer: &(T[]){ expr1, expr2, ... }
  • Struct: struct T{ expr1, expr2, ... }

Initialization

  • Basic: T a = expr
  • Array: T a[] = { expr1, expr2, ... }, T a[N] = { expr1, expr2, ... }
  • Struct: struct T a = { expr1, expr2, ... }, struct T a = { .a = expr1, .b = expr2, ... }
  • Union: union T a = { expr }, union T a = { .a = expr }
  • Array in Struct: struct { T a[N]; ... } v = { { expr1, expr2, ... }, ... }, struct { T a[N]; ... } v = { { [0].a = expr1, [1].a = expr2, ... }, ... }

Control structures

  • If: if (cond) stmt1 [else stmt2]
  • Switch: switch (tag) { case expr1: stmt1 case expr2: stmt2 default: stmtN }
  • For: for (init; cond; post) stmt
  • While: while (cond) stmt
  • Do While: do stmt while (cond)
  • Break/Continue: break, continue
  • Goto: goto label

Functions

  • Parameters
  • Variadic Parameters
  • Variadic Parameter Access
  • Return

More Repositories

1

gop

The Go+ programming language is designed for engineering, STEM education, and data science
Go
8,682
star
2

bpl

Binary Processing Language
Go
149
star
3

gox

Code generator for the Go language
Go
139
star
4

spx

spx - A Scratch Compatible Go/Go+ 2D Game Engine for STEM education
Go
97
star
5

igop

The Go/Go+ Interpreter
Go
97
star
6

tutorial

Tutorials for GoPlus (The Go+ Language)
Go
38
star
7

hdq

HTML DOM Query Language for Go+
Go
38
star
8

reflectx

Golang reflect package hack tools
Assembly
28
star
9

yaigop

Yet Another Go+ interpreter (still in beta version)
Go
26
star
10

www

Source code of https://goplus.org/
Go
26
star
11

pandas

Flexible and powerful data analysis / manipulation library for Go+, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
25
star
12

builder

Go+ Builder
Vue
25
star
13

libc

Porting libc from C to Go
Go
24
star
14

pyg

Python to Go tools
Go
13
star
15

community

Go+ Community written in Go+
JavaScript
11
star
16

llgo

A Go compiler based on LLVM in order to better integrate Go with the C ecosystem
Go
9
star
17

ispx

The Interpreter of Go+ spx engine
Go
8
star
18

yap

Yet Another Go/Go+ HTTP Web Framework
Go
8
star
19

sqlite

sqlite for Go+
Go
7
star
20

spx-editor

Spx editor
JavaScript
6
star
21

igo

Yet another interpreter for the Go language
Go
6
star
22

mod

Module support for Go/Go+
Go
6
star
23

canvas

HTML Canvas 2D Context API for mobile, desktop and web
Go
5
star
24

FlappyCalf

FlappyCalf - a game powered by Go+ spx game engine
Shell
3
star
25

goplus.github.io

Thoughts on Go+
2
star
26

ipkg

Go
2
star
27

setup-goplus

Set up your GitHub Actions workflow with a specific version of Go+
TypeScript
2
star
28

installer

Go+ Installer for Windows, MacOS, etc.
Go
2
star
29

gopdlv

gopdlv is a debugger for the Go/Go+ programming language
Go
2
star
30

llama2

LLGo support for Inference Llama 2
LLVM
2
star
31

MazePlay

MazePlay - a game powered by Go+ Builder
Shell
1
star
32

AircraftWar

AircraftWar - a game powered by Go+ spx game engine
Shell
1
star
33

xtypes

Golang types convert utils
Go
1
star
34

classfile-project-template

A template for starting a new classfile project
1
star
35

expect

Expect for Go+
Go
1
star
36

show

show.goplus.org
Go
1
star
37

xtools

Extended tools for Go+
Go
1
star
38

run

run.goplus.org
Go
1
star
39

llgoexamples

LLGo Examples
Rust
1
star
40

account

Go+ Account System
1
star
41

goxls

The Go/Go+ Language Server
Go
1
star