go2cpp : convert go to cpp using go ast

px_golang2cpp

convert go to cpp

  • convert map to std::unordered_map
  • convert array to vector
  • convert 'var test bool' to 'bool test{}'
  • convert 'var test = 2' to 'auto test = 2;'
  • convert 'var test []string' to 'vector test{};'
  • convert 'var test map[int]string' to std::unordered_map<int,string> test{};

usage

clone source code

build the project

    1. cd px_golang2cpp source directory
    1. exec the cmd: go build *.go,then execuable file main is created

generate a cpp file from go source code

    1. we have test file in directory test, we now chose the test/func.go
    1. execute the command: ./main test/func.go -o test.cc, then we get a cpp file: test.cc

build the created cpp source file

  • we now have the test.cc file, then we can execute the command like this: g++ test.cc
  • if there have no error in building the cpp file test.cc, then we get the correct cpp source form go

notice

  • the created cpp source file is just for testing, and some converted code build failed dure to lack of dependence file.

example

  • convert var in go to cpp: ./main test/var_test.go -o var_test.cc

// var_test.go

package var_test

var endings = []string{"{", ",", "}", ":"}

var test = -1
var test1 = 2

var g1, g2 = 23, "fjdd"

var (
	switchExpressionCounter = -1
	firstCase               bool
	switchLabel             string
	labelCounter            int
	iotaNumber              int // used for simple increases of iota constants
	deferCounter            int
	unfinishedDeferFunction bool
)

var globalObjectMap map[string]string

// var_test.cc (this is part of code)

std::vector<string> endings{
"{",
",",
"}",
":",
};
auto  test = (-1);
auto  test1 = 2;
auto  g1 = 23;
auto  g2 = "fjdd";
auto  switchExpressionCounter = (-1);
bool firstCase{};
string switchLabel{};
int labelCounter{};
int iotaNumber{};
int deferCounter{};
bool unfinishedDeferFunction{};

notice: cpp code not format yet(we will do it later).

  • convert go map to cpp unordered_map: ./main test/map_test.go -o map_test.cc

// map_test.go

package map_test

var includeFileMap map[string]string = make(map[string]string)

var includeMap = map[string]string{
	"std::tuple":                       "tuple",
	"std::endl":                        "iostream",
	"std::cout":                        "iostream",
	"std::string":                      "string",
	"std::size":                        "iterator",
	"std::unordered_map":               "unordered_map",
	"std::hash":                        "functional",
	"std::size_t":                      "cstddef",
	"std::int8_t":                      "cinttypes",
	"std::int16_t":                     "cinttypes",
	"std::int32_t":                     "cinttypes",
	"std::int64_t":                     "cinttypes",
	"std::uint8_t":                     "cinttypes",
	"std::uint16_t":                    "cinttypes",
	"std::uint32_t":                    "cinttypes",
	"std::uint64_t":                    "cinttypes",
	"printf":                           "cstdio",
	"fprintf":                          "cstdio",
	"sprintf":                          "cstdio",
	"snprintf":                         "cstdio",
	"std::stringstream":                "sstream",
	"std::is_pointer":                  "type_traits",
	"std::experimental::is_detected_v": "experimental/type_traits",
	"std::shared_ptr":                  "memory",
	"std::nullopt":                     "optional",
	"EXIT_SUCCESS":                     "cstdlib",
	"EXIT_FAILURE":                     "cstdlib",
	"std::vector":                      "vector",
	"std::unique_ptr":                  "memory",
	"std::runtime_error":               "stdexcept",
	"std::regex_replace":               "regex",
	"std::regex_constants":             "regex",
	"std::to_string":                   "string",
	// TODO: complex64, complex128
}

var testMap map[string]string

// map_test.cc (this is part of code)

std::unordered_map<string,string> includeFileMap = {};
std::unordered_map<string,string> includeMap{
{"std::tuple" , "tuple"},
{"std::endl" , "iostream"},
{"std::cout" , "iostream"},
{"std::string" , "string"},
{"std::size" , "iterator"},
{"std::unordered_map" , "unordered_map"},
{"std::hash" , "functional"},
{"std::size_t" , "cstddef"},
{"std::int8_t" , "cinttypes"},
{"std::int16_t" , "cinttypes"},
{"std::int32_t" , "cinttypes"},
{"std::int64_t" , "cinttypes"},
{"std::uint8_t" , "cinttypes"},
{"std::uint16_t" , "cinttypes"},
{"std::uint32_t" , "cinttypes"},
{"std::uint64_t" , "cinttypes"},
{"printf" , "cstdio"},
{"fprintf" , "cstdio"},
{"sprintf" , "cstdio"},
{"snprintf" , "cstdio"},
{"std::stringstream" , "sstream"},
{"std::is_pointer" , "type_traits"},
{"std::experimental::is_detected_v" , "experimental/type_traits"},
{"std::shared_ptr" , "memory"},
{"std::nullopt" , "optional"},
{"EXIT_SUCCESS" , "cstdlib"},
{"EXIT_FAILURE" , "cstdlib"},
{"std::vector" , "vector"},
{"std::unique_ptr" , "memory"},
{"std::runtime_error" , "stdexcept"},
{"std::regex_replace" , "regex"},
{"std::regex_constants" , "regex"},
{"std::to_string" , "string"},
};
std::unordered_map<string,string> testMap{};

notice: not format yet

Similar Resources

A multi-pass compiler written in Go comprised of scanner, recursive-descent parser, generation of AST, intermediate representation (ILOC), and code generation (Armv8).

GoLite Project - Go Huskies! This is a project conducted and led in the course MPCS 51300 Compilers at the University of Chicago. In a group of two, w

Jan 10, 2022

Generate Equal() methods from AST

Generate Equal() methods from AST

Nov 25, 2022

Interpreted Programming Language built in Go. Lexer, Parser, AST, VM.

Gago | Programming Language Built in Go if you are looking for the docs, go here Gago is a interpreted programming language. It is fully written in Go

May 6, 2022

Read data from rss, convert in pdf and send to kindle. Amazon automatically convert them in azw3.

Kindle-RSS-PDF-AZW3 The Kindle RSS PDF AZW3 is a personal project. The Kindle RSS PDF AZW3 is a personal project. I received a Kindle for Christmas, a

Jan 10, 2022

Convert images to computer generated art using delaunay triangulation.

Convert images to computer generated art using delaunay triangulation.

▲ Triangle is a tool for generating triangulated image using delaunay triangulation. It takes a source image and converts it to an abstract image comp

Dec 29, 2022

Dredger is a utility to help convert helm charts to Terraform modules using kubernetes provider.

dredger Dredger is a utility to help convert helm charts to Terraform modules using kubernetes provider. Dredger is made of dark magic and cannot full

Aug 25, 2022

golang function that download a video from youtube, and convert it to a mp3 file using ffmpeg

echedwnmp3 echedwnmp3 is a function that download a video from youtube, and convert it to a mp3 file using ffmpeg example package main import(echedwn

Dec 7, 2021

lmmp3 is a little golang library that download a video from youtube, and convert it to a mp3 file using ffmpeg

lmmp3 lmmp3 is a function that download a video from youtube, and convert it to a mp3 file using ffmpeg You need to have installed ffmpeg in your syst

Aug 12, 2022

timestamp convert & compare tool. 时间戳转换与对比工具

ts timestamp convert & compare tool Install Shell Install support Linux & MacOS # binary will be $(go env GOPATH)/bin/ts $: curl -sfL https://raw.gith

Sep 26, 2022

Query, update and convert data structures from the command line. Comparable to jq/yq but supports JSON, TOML, YAML, XML and CSV with zero runtime dependencies.

Query, update and convert data structures from the command line. Comparable to jq/yq but supports JSON, TOML, YAML, XML and CSV with zero runtime dependencies.

dasel Dasel (short for data-selector) allows you to query and modify data structures using selector strings. Comparable to jq / yq, but supports JSON,

Jan 2, 2023

Convert string to duration in golang

Go String To Duration (go-str2duration) This package allows to get a time.Duration from a string. The string can be a string retorned for time.Duratio

Dec 7, 2022

:foggy: Convert image to ASCII

:foggy: Convert image to ASCII

🌁 Image2ascii Image2ASCII is a library that converts images into ASCII images and provides command-line tools for easy use. Installation go get githu

Jan 8, 2023

Convert struct, slice, array, map or others for Golang

XConv zh-CN XConv is a golang type convertor. It convert any value between types (base type, struct, array, slice, map, etc.) Features Convert between

Dec 8, 2022

Query, update and convert data structures from the command line. Comparable to jq/yq but supports JSON, TOML, YAML, XML and CSV with zero runtime dependencies.

Query, update and convert data structures from the command line. Comparable to jq/yq but supports JSON, TOML, YAML, XML and CSV with zero runtime dependencies.

dasel Dasel (short for data-selector) allows you to query and modify data structures using selector strings. Comparable to jq / yq, but supports JSON,

Jan 2, 2023

Convert Golang Struct To GraphQL Object On The Fly

Straf Convert Golang Struct To GraphQL Object On The Fly Easily Create GraphQL Schemas Example Converting struct to GraphQL Object type UserExtra stru

Oct 26, 2022

⚙️ Convert HTML to Markdown. Even works with entire websites and can be extended through rules.

⚙️ Convert HTML to Markdown. Even works with entire websites and can be extended through rules.

html-to-markdown Convert HTML into Markdown with Go. It is using an HTML Parser to avoid the use of regexp as much as possible. That should prevent so

Jan 6, 2023

Convert xml and json to go struct

xj2go The goal is to convert xml or json file to go struct file. Usage Download and install it: $ go get -u -v github.com/wk30/xj2go/cmd/... $ xj [-t

Oct 23, 2022

Convert scanned image PDF file to text annotated PDF file

Convert scanned image PDF file to text annotated PDF file

Jisui (自炊) This tool is PoC (Proof of Concept). Jisui is a helper tool to create e-book. Ordinary the scanned book have not text information, so you c

Dec 11, 2022

Convert data exports from various services to a single SQLite database

Convert data exports from various services to a single SQLite database

Bionic Bionic is a tool to convert data exports from web apps to a single SQLite database. Bionic currently supports data exports from Google, Apple H

Dec 9, 2022
golang AST matcher

goastch (GO AST matCH) Introduction Inspired by ast matcher. There are four different basic categories of matchers: Node Matchers: Matchers that match

Nov 11, 2022
Transform Go code into it's AST

Welcome to go2ast ?? Transform Go code into it's AST Usage echo "a := 1" | go run main.go Example output []ast.Stmt { &ast.AssignStmt {

Dec 13, 2022
Convert Go values to their AST

valast - convert Go values to their AST Valast converts Go values at runtime into their go/ast equivalent,

Dec 21, 2022
Golang->Haxe->CPP/CSharp/Java/JavaScript transpiler

TARDIS Go -> Haxe transpiler Haxe -> C++ / C# / Java / JavaScript Project status: a non-working curiosity, development currently on-ice The advent of

Dec 30, 2022
800k lines switch case cpp challenge
800k lines switch case cpp challenge

800k lines cpp compile challenge 80万行cpp编译挑战 800k lines cpp 编译 challenge generate 生成Cpp代码 go run generator.go compile 编译Cpp代码 g++ -m64 main.cpp binary

Oct 27, 2021
A go wrapper around the rwkv.cpp library

AI without python (1) go-rwkv.cpp gowrkv.go is a wrapper around rwkv-cpp, which is an adaption of ggml.cpp. (1) Python required for training and conve

May 10, 2023
Using NFP (Number Format Parser) you can get an Abstract Syntax Tree (AST) from Excel number format expression

NFP (Number Format Parser) Using NFP (Number Format Parser) you can get an Abstract Syntax Tree (AST) from Excel number format expression. Installatio

Feb 4, 2022
Golang AST visualizer
Golang AST visualizer

GoAst Viewer Golang AST visualizer. Demo GoAst Viewer Demo Usage You just need to run a http server and open your browser to index.html Installation T

Dec 29, 2022
golang AST matcher

goastch (GO AST matCH) Introduction Inspired by ast matcher. There are four different basic categories of matchers: Node Matchers: Matchers that match

Nov 11, 2022
Transform Go code into it's AST

Welcome to go2ast ?? Transform Go code into it's AST Usage echo "a := 1" | go run main.go Example output []ast.Stmt { &ast.AssignStmt {

Dec 13, 2022