All Projects → xyproto → go2cpp

xyproto / go2cpp

Licence: MIT License
Go to C++20 transpiler

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to go2cpp

habrlang
Step by Step guide how to make your own programming language
Stars: ✭ 20 (-60.78%)
Mutual labels:  transpiler
Headache
Programming Language that compiles to 8 Bit Brainfuck
Stars: ✭ 59 (+15.69%)
Mutual labels:  transpiler
grizzly
A Python-to-SQL transpiler as replacement for Python Pandas
Stars: ✭ 27 (-47.06%)
Mutual labels:  transpiler
LunarML
A Standard ML compiler that produces Lua/JavaScript
Stars: ✭ 127 (+149.02%)
Mutual labels:  transpiler
sugartex
SugarTeX is a more readable LaTeX language extension and transcompiler to LaTeX. Fast Unicode autocomplete in Atom editor via https://github.com/kiwi0fruit/atom-sugartex-completions
Stars: ✭ 74 (+45.1%)
Mutual labels:  transpiler
js-slang
Implementations of the Source languages, which are small sublanguages of JavaScript designed for SICP JS
Stars: ✭ 41 (-19.61%)
Mutual labels:  transpiler
coffee-to-ts
[NOT ACTIVELY MAINTAINED] Convert CoffeeScript to TypeScript
Stars: ✭ 34 (-33.33%)
Mutual labels:  transpiler
go2hx
Go -> Haxe -> JS Java C# C++ C Python Lua
Stars: ✭ 49 (-3.92%)
Mutual labels:  transpiler
escapin
Escapin is a JS/TS transpiler for escaping from complicated usage of cloud services and APIs
Stars: ✭ 20 (-60.78%)
Mutual labels:  transpiler
sqlglot
Python SQL Parser and Transpiler
Stars: ✭ 310 (+507.84%)
Mutual labels:  transpiler
json-sql-builder2
Level Up Your SQL-Queries
Stars: ✭ 59 (+15.69%)
Mutual labels:  transpiler
ShenScript
Shen for JavaScript
Stars: ✭ 40 (-21.57%)
Mutual labels:  transpiler
bck2brwsr
Bck2Brwsr VM to transpile Java bytecode to JavaScript
Stars: ✭ 93 (+82.35%)
Mutual labels:  transpiler
War3Net
A .NET implementation of Warcraft III related libraries.
Stars: ✭ 76 (+49.02%)
Mutual labels:  transpiler
rewrite-imports
Rewrite `import` statements as `require()`s; via RegExp
Stars: ✭ 31 (-39.22%)
Mutual labels:  transpiler
preact-codemod
🍧 Shave some bytes by using Preact.
Stars: ✭ 39 (-23.53%)
Mutual labels:  transpiler
boost beast websocket echo
A collection of Demo applications to try to help you understand how Asio and Beast work
Stars: ✭ 12 (-76.47%)
Mutual labels:  cxx20
Go2SourcePawn
Go2SourcePawn is a transpiler that transforms a subset of Golang-like code to equivalent SourcePawn.
Stars: ✭ 13 (-74.51%)
Mutual labels:  transpiler
BashClass
BashClass is an Object Oriented Programming language that compiles to BASH 4.4
Stars: ✭ 40 (-21.57%)
Mutual labels:  transpiler
qaffeine
Decaffeinate your JS-powered CSS stylesheets
Stars: ✭ 22 (-56.86%)
Mutual labels:  transpiler

go2cpp

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 implement import is needed.

Features and limitations

  • Pretty fast.
  • Simple to use.
  • Few dependencies (for compiling go2cpp, only the go compiler is needed).
  • Low complexity.
  • Short source code.
  • g++ is used for compiling the generated C++ code.
  • clang-format is used for formatting the generated C++ code.

Usage

Compile to executable:

go2cpp main.go -o main

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

go2cpp main.go

Requirements

  • g++ with support for C++20
  • clang-format

Example transformations

Go input:

// Multiple return
package main

import (
    "fmt"
)

func addsub(x int) (a, b int) {
    return x + 2, x - 2
}

func main() {
    y, z := addsub(4)
    fmt.Println("y =", y)
    fmt.Println("z =", z)
}

C++ output:

#include <iostream>
#include <tuple>

// Multiple return

auto addsub(int x) -> std::tuple<int, int>
{
    return std::tuple<int, int>{ x + 2, x - 2 };
}

auto main() -> int
{
    auto [y, z] = addsub(4);
    std::cout << "y ="
              << " " << y << std::endl;
    std::cout << "z ="
              << " " << z << std::endl;
    return 0;
}

Go input:

package main

import (
	"fmt"
)

func main() {
	m := map[string]string{"first": "hi", "second": "you", "third": "there"}
	first := true
	for k, v := range m {
		if first {
			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)
{
    if constexpr (std::is_same<T, bool>::value) {
        out << std::boolalpha << x << std::noboolalpha;
    } else if constexpr (std::is_integral<T>::value) {
        out << static_cast<int>(x);
    } else {
        out << x;
    }
}

auto main() -> int
{
    std::unordered_map<std::string, std::string> m{ { "first", "hi" }, { "second", "you" },
        { "third", "there" } };
    auto first = true;
    for (const auto& [k, v] : m) {
        if (first) {
            first = false;
        } else {
            std::cout << " ";
        }

        _format_output(std::cout, k + v);
    }

    std::cout << std::endl;
    return 0;
}

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.

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