Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Go to C++20 transpiler

License

NotificationsYou must be signed in to change notification settings

xyproto/go2cpp

Repository files navigation

Compiles Go to native executables via C++20.

One of the goals is for the compiler to be able to compile itself.

The intended use is not to convert entire existing Go programs to C++, but to help port parts of it to C++, or perhaps write programs from scratch and continually check that the program can be converted and compiled as C++.

Known issues

  • Only works with simple code samples, for now.
  • Very few functions from the Go standard library are implemented. The ideal would be to be able to compile the official Go standard library.
  • A good plan for how to implementimport is needed.

Features and limitations

  • Pretty fast.
  • Simple to use.
  • Few dependencies (for compilinggo2cpp, only the go compiler is needed).
  • Low complexity.
  • Short source code.

Required dependencies

  • g++ with support for C++20 is used for compiling the generated C++ code.
  • clang-format is used for formatting the generated C++ code.

Installation

go install github.com/xyproto/go2cpp@latest

Then~/go/bin/go2cpp should be available (unless GOPATH points somewhere else).

Usage

Compile to executable:

go2cpp main.go -o main

Output what the intermediate C++20 code looks like:

go2cpp main.go

Example transformations

Go input:

// Multiple returnpackage mainimport ("fmt")funcaddsub(xint) (a,bint) {returnx+2,x-2}funcmain() {y,z:=addsub(4)fmt.Println("y =",y)fmt.Println("z =",z)}

C++ output:

#include<iostream>#include<tuple>// Multiple returnautoaddsub(int x) -> std::tuple<int, int>{return std::tuple<int,int>{ x +2, x -2 };}automain() -> int{auto [y, z] =addsub(4);    std::cout <<"y ="              <<"" << y << std::endl;    std::cout <<"z ="              <<"" << z << std::endl;return0;}

Go input:

package mainimport ("fmt")funcmain() {m:=map[string]string{"first":"hi","second":"you","third":"there"}first:=truefork,v:=rangem {iffirst {first=false        }else {fmt.Print(" ")        }fmt.Print(k+v)    }fmt.Println()}

C++ output:

#include<iostream>#include<string>#include<unordered_map>template<typename T>void_format_output(std::ostream& out, T x){ifconstexpr (std::is_same<T,bool>::value) {        out << std::boolalpha << x << std::noboolalpha;    }elseifconstexpr (std::is_integral<T>::value) {        out <<static_cast<int>(x);    }else {        out << x;    }}automain() -> int{    std::unordered_map<std::string, std::string> m{ {"first","hi" }, {"second","you" },        {"third","there" } };auto first =true;for (constauto& [k, v] : m) {if (first) {            first =false;        }else {            std::cout <<"";        }_format_output(std::cout, k + v);    }    std::cout << std::endl;return0;}

General info

  • Version: 0.4.0
  • License: MIT

TODO

Syntactic elements

  • backtick quoted strings:` (one level deep only)
  • iota

Keywords

  • break
  • case
  • chan
  • const
  • continue
  • default
  • defer
  • else
  • fallthrough
  • for
  • func
  • go
  • goto
  • if
  • import (partially)
  • interface
  • map (needs more testing)
  • package (partially)
  • range
  • return
  • select
  • struct (needs more testing)
  • switch
  • type (needs more testing)
  • var

Standard library

  • fmt.Println
  • fmt.Print
  • fmt.Printf (partially)
  • fmt.Sprintf
  • strings.Contains
  • strings.HasPrefix
  • strings.HasSuffix
  • strings.Index
  • strings.Join
  • strings.NewReader
  • strings.Replace
  • strings.Split
  • strings.SplitN
  • strings.TrimSpace
  • All the rest

One goal is that all code in the standard library should transpile correctly to C++20.

About

Go to C++20 transpiler

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp