SlideShare a Scribd company logo
1 of 33
Download to read offline
All I know about rsc.io/c2go
Moriyoshi Koizumi
Open Collector, Inc.
Hey
I can be found amongst CONTRIBUTORS :)
rsc.io/c2go
C to Go translation tool behind the Go's self-hosting effort.
Written by rsc.
Apparently not intended to be a generic translation tool.
$ go get rsc.io/c2go
こんにちは、世界
void print(char *fmt, ...);
void main()
{
print("Hello, %s.n", "world");
}
⬇️ bin/c2go -dst $GOPATH hello.c
package main
func main() {
fmt.Printf("Hello, %s.n", "world")
}
(generated under $GOPATH/src/main)
Quicksort1
Quicksort1
Involves pointer arithmetics, compound types, and function pointers.
Quicksort1 (code)
int print(const char *fmt, ...);
typedef struct Data {
int v;
} Data;
void swap(Data *a, Data *b)
{
Data v = *a;
*a = *b;
*b = v;
}
int cmp(Data *a, Data *b)
{
if (a->v < b->v)
return -1;
else if (a->v > b->v)
return 1;
else
return 0;
}
...
Quicksort1 (code)
...
void _qsort(Data *d, int l, int (*cmp)(Data *a, Data *b))
{
int pivot, le, i, j;
Data pv;
if (l == 0)
return;
le = l - 1;
pivot = l / 2;
pv = d[pivot];
swap(&d[pivot], &d[le]);
i = 0;
for (j = 0; j < le; j++) {
if (cmp(&d[j], &pv) < 0) {
swap(&d[i], &d[j]);
i++;
}
}
swap(&d[i], &d[le]);
_qsort(d, i, cmp);
if (l > i)
_qsort(&d[i + 1], l - (i + 1), cmp);
}
...
Quicksort1 (code)
...
void main() {
int i;
Data values[] = { { 1 }, { 9 }, { 5 }, { 2 }, { 4 }, { 3 }, { 8 }, { 6 }, { 7 } };
_qsort(values, 9, cmp);
for (i = 0; i < 9; i++)
print("%dn", values[i].v);
}
Quicksort1
Compile:
$ cc -o qsort1 qsort1.c $GO_1_4_ROOT/pkg/obj/darwin_amd64/lib9.a
Run:
$ ./qsort1
1
2
3
4
5
6
7
8
9
Quicksort1
Translate:
$ c2go -dst $GOPATH qsort1.c
No worry about the message; this is a warning.
main/qsort1.go: cannot find copyright notice in C file qsort1.c
Generated code:
package main
type Data struct {
v int
}
func swap(a *Data, b *Data) {
var v Data = *a
*a = *b
*b = v
}
...
Quicksort1
...
func _qsort(d *Data, l int, cmp func(*Data, *Data) int) {
var pivot int
var le int
var i int
var j int
var pv Data
if l == 0 {
return
}
le = l - 1
pivot = l / 2
pv = d[pivot]
swap(&d[pivot], &d[le])
...
Looks like it went well so far, but Aw, Snap! #
src/main/qsort1.go:24: invalid operation: d[pivot] (type *Data does not support indexing)
src/main/qsort1.go:25: invalid operation: d[pivot] (type *Data does not support indexing)
src/main/qsort1.go:25: invalid operation: d[le] (type *Data does not support indexing)
...
Quicksort2
Quicksort2
int print(const char *fmt, ...);
typedef struct Data {
int v;
} Data;
typedef struct DataSlice {
Data *p;
int len;
int cap;
} DataSlice;
Quicksort2
...
void swap(Data *a, Data *b)
{
Data v = *a;
*a = *b;
*b = v;
}
int cmp(Data *a, Data *b)
{
if (a->v < b->v)
return -1;
else if (a->v > b->v)
return 1;
else
return 0;
}
...
Quicksort2
...
void _qsort(DataSlice *ds, int (*cmp)(Data *a, Data *b))
{
int pivot, le, i, j;
Data pv;
if (ds->len == 0)
return;
le = ds->len - 1;
pivot = ds->len / 2;
pv = ds->p[pivot];
swap(&ds->p[pivot], &ds->p[le]);
i = 0;
for (j = 0; j < le; j++) {
if (cmp(&ds->p[j], &pv) < 0) {
swap(&ds->p[i], &ds->p[j]);
i++;
}
}
swap(&ds->p[i], &ds->p[le]);
...
Quicksort2
...
{
DataSlice ns = { ds->p, i, ds->cap };
_qsort(&ns, cmp);
}
if (ds->len > i) {
DataSlice ns = { &ds->p[i + 1], ds->len - (i + 1), ds->cap - (i + 1) };
_qsort(&ns, cmp);
}
}
void main() {
int i;
Data values[] = { { 1 }, { 9 }, { 5 }, { 2 }, { 4 }, { 3 }, { 8 }, { 6 }, { 7 } };
DataSlice s = { values, 9, 9 };
_qsort(&s, cmp);
for (i = 0; i < 9; i++)
print("%dn", values[i].v);
}
Quicksort2
my.cfg:
slice DataSlice.p DataSlice.len DataSlice.cap
diff {
- var ns = DataSlice{ds.p, i, cap(ds.p)}
+ var ns = DataSlice{ds.p[0:i]}
}
diff {
- var ns = DataSlice{&ds.p[i+1], len(ds.p) - (i + 1), cap(ds.p) - (i + 1)}
+ var ns = DataSlice{ds.p[i+1:]}
}
diff {
- var s = DataSlice{values, 9, 9}
+ var s = DataSlice{values}
}
Quicksort2
Translate:
c2go -c my.cfg -dst $GOPATH qsort2.c
Forget all the unfamiliar messages; these are warnings:
qsort2.c:39: too many fields in braced initializer of DataSlice
qsort2.c:43: too many fields in braced initializer of DataSlice
qsort2.c:61: too many fields in braced initializer of DataSlice
main/qsort2.go: cannot find copyright notice in C file qsort2.c
Quicksort2
Generated Code:
...
func _qsort(ds *DataSlice, cmp func(*Data, *Data) int) {
var pivot int
var le int
var i int
var j int
var pv Data
if len(ds.p) == 0 {
return
}
le = len(ds.p) - 1
pivot = len(ds.p) / 2
pv = ds.p[pivot]
swap(&ds.p[pivot], &ds.p[le])
i = 0
for j = 0; j < le; j++ {
if cmp(&ds.p[j], &pv) < 0 {
...
Looks pretty promising!
Quicksort2
$ go run $GOPATH/src/main/qsort2.go
# command-line-arguments
src/main/qsort2.go:66: undefined: fmt in fmt.Printf
Seems we had to add import "fmt" manually $
package main
import "fmt" // here
type Data struct {
v int
}
func swap(a *Data, b *Data) {
...
Quicksort2
Let's give it a second try:
$ go run $GOPATH/src/main/qsort2.go
1
2
3
4
5
6
7
8
9
WOW %
Quicksort2
The configuration file gives more control over the generated code.
Its directives are not totally demystified yet.
Quicksort3
Quicksort3
This is CHEATING
void qsort(void *d, int l, int s, int(*cmp)(void *, void *));
int print(char *fmt, ...);
typedef struct Data {
int v;
} Data;
int cmp(void *a, void *b)
{
Data *_a, *_b;
_a = a;
_b = b;
if (_a->v < _b->v)
return -1;
else if (_a->v > _b->v)
return 1;
else
return 0;
}
...
Quicksort3
...
void main() {
int i;
Data values[] = { { 1 }, { 9 }, { 5 }, { 2 }, { 4 }, { 3 }, { 8 }, { 6 }, { 7 } };
qsort(values, 9, sizeof(values[0]), cmp);
for (i = 0; i < 9; i++)
print("%dn", values[i].v);
}
Quicksort3
Translate:
$ c2go -dst $GOPATH qsort3.c
Generated code:
package main
type Data struct {
v int
}
type cmp []Data
func (x cmp) Len() int {
return len(x)
}
func (x cmp) Swap(i, j int) {
x[i], x[j] = x[j], x[i]
}
...
Quicksort3
...
func (x cmp) Less(i, j int) bool {
var _a *Data
var _b *Data
_a = &x[i]
_b = &x[j]
if _a.v < _b.v {
return true
} else if _a.v > _b.v {
return 1 < 0
} else {
return false
}
}
func main() {
var i int
var values = []Data{Data{1}, Data{9}, Data{5}, Data{2}, Data{4}, Data{3}, Data{8}, Data{6}, Data{7}}
sort.Sort(cmp(values[:9]))
for i = 0; i < 9; i++ {
fmt.Printf("%dn", values[i].v)
}
}
Quicksort3
This is a MAGIC.
Some "standard" C functions are treated specially so that they get translated into the Go's
counterparts.
print() ➡️ fmt.Printf()
qsort() ➡️ sort.Sort()
memset() / memmove()
strcpy() / strcat / strlen() / strcmp()
malloc() / strdup() / free()
abort() '
DISCLAIMER: this is just informational; not a complete list.
Quicksort3
If you want to get the MAGIC happening right, You need to make sure...
The prototype:
void qsort(void *d, int l, int s, int(*cmp)(void *, void *));
The third argument cmp() function:
void cmp(void *a, void *b)
{
type_in_question *a_, *b_;
a_ = a;
b_ = b;
}
not
void cmp(void *a, void *b)
{
type_in_question *a_ = a, *b_ = b;
}
Quicksort3
Call to qsort():
qsort(list, nitems, sizeof(list[0]), cmp);
not
qsort(list, nitems, sizeof(type_in_question), cmp);
Want to go further?
See src/rsc.io/c2go/typecheck.go.
More magics can be added!
Thank you
2015/6/21 1:15:00(2015/6/21%201:15:00)
Tags: golang, c2go(#ZgotmplZ)
Moriyoshi Koizumi
Open Collector, Inc.
mozo@mozo.jp(mailto:mozo@mozo.jp)
http://mozo.jp/(http://mozo.jp/)
@moriyoshit(http://twitter.com/moriyoshit)
All I know about rsc.io/c2go

More Related Content

What's hot

C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st StudyChris Ohk
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd StudyChris Ohk
 
Powered by Python - PyCon Germany 2016
Powered by Python - PyCon Germany 2016Powered by Python - PyCon Germany 2016
Powered by Python - PyCon Germany 2016Steffen Wenz
 
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!..."A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...akaptur
 
Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CSteffen Wenz
 
Bytes in the Machine: Inside the CPython interpreter
Bytes in the Machine: Inside the CPython interpreterBytes in the Machine: Inside the CPython interpreter
Bytes in the Machine: Inside the CPython interpreterakaptur
 
JavaScript - Agora nervoso
JavaScript - Agora nervosoJavaScript - Agora nervoso
JavaScript - Agora nervosoLuis Vendrame
 
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...akaptur
 
Implementing Software Machines in C and Go
Implementing Software Machines in C and GoImplementing Software Machines in C and Go
Implementing Software Machines in C and GoEleanor McHugh
 
Python opcodes
Python opcodesPython opcodes
Python opcodesalexgolec
 
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java script
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java scriptCodiLime Tech Talk - Grzegorz Rozdzialik: What the java script
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java scriptCodiLime
 
TCO in Python via bytecode manipulation.
TCO in Python via bytecode manipulation.TCO in Python via bytecode manipulation.
TCO in Python via bytecode manipulation.lnikolaeva
 
A CTF Hackers Toolbox
A CTF Hackers ToolboxA CTF Hackers Toolbox
A CTF Hackers ToolboxStefan
 
PHP in 2018 - Q4 - AFUP Limoges
PHP in 2018 - Q4 - AFUP LimogesPHP in 2018 - Q4 - AFUP Limoges
PHP in 2018 - Q4 - AFUP Limoges✅ William Pinaud
 

What's hot (20)

C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st Study
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd Study
 
Powered by Python - PyCon Germany 2016
Powered by Python - PyCon Germany 2016Powered by Python - PyCon Germany 2016
Powered by Python - PyCon Germany 2016
 
dplyr
dplyrdplyr
dplyr
 
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!..."A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
 
Rcpp11 genentech
Rcpp11 genentechRcpp11 genentech
Rcpp11 genentech
 
Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in C
 
Bytes in the Machine: Inside the CPython interpreter
Bytes in the Machine: Inside the CPython interpreterBytes in the Machine: Inside the CPython interpreter
Bytes in the Machine: Inside the CPython interpreter
 
JavaScript - Agora nervoso
JavaScript - Agora nervosoJavaScript - Agora nervoso
JavaScript - Agora nervoso
 
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
 
Implementing Software Machines in C and Go
Implementing Software Machines in C and GoImplementing Software Machines in C and Go
Implementing Software Machines in C and Go
 
Python opcodes
Python opcodesPython opcodes
Python opcodes
 
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java script
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java scriptCodiLime Tech Talk - Grzegorz Rozdzialik: What the java script
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java script
 
TCO in Python via bytecode manipulation.
TCO in Python via bytecode manipulation.TCO in Python via bytecode manipulation.
TCO in Python via bytecode manipulation.
 
C++ programs
C++ programsC++ programs
C++ programs
 
A CTF Hackers Toolbox
A CTF Hackers ToolboxA CTF Hackers Toolbox
A CTF Hackers Toolbox
 
Rcpp11 useR2014
Rcpp11 useR2014Rcpp11 useR2014
Rcpp11 useR2014
 
PHP in 2018 - Q4 - AFUP Limoges
PHP in 2018 - Q4 - AFUP LimogesPHP in 2018 - Q4 - AFUP Limoges
PHP in 2018 - Q4 - AFUP Limoges
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
Vcs23
Vcs23Vcs23
Vcs23
 

Viewers also liked

Goをカンストさせる話
Goをカンストさせる話Goをカンストさせる話
Goをカンストさせる話Moriyoshi Koizumi
 
Authentication, Authorization, OAuth, OpenID Connect and Pyramid
Authentication, Authorization, OAuth, OpenID Connect and PyramidAuthentication, Authorization, OAuth, OpenID Connect and Pyramid
Authentication, Authorization, OAuth, OpenID Connect and PyramidMoriyoshi Koizumi
 
HLSについて知っていることを話します
HLSについて知っていることを話しますHLSについて知っていることを話します
HLSについて知っていることを話しますMoriyoshi Koizumi
 
X / DRM (Direct Rendering Manager) Architectural Overview
X / DRM (Direct Rendering Manager) Architectural OverviewX / DRM (Direct Rendering Manager) Architectural Overview
X / DRM (Direct Rendering Manager) Architectural OverviewMoriyoshi Koizumi
 
Pyramidのrendererをカスタマイズする
PyramidのrendererをカスタマイズするPyramidのrendererをカスタマイズする
PyramidのrendererをカスタマイズするMoriyoshi Koizumi
 
今年使ってみて良かった、Pythonモジュール、パッケージ、ツール
今年使ってみて良かった、Pythonモジュール、パッケージ、ツール今年使ってみて良かった、Pythonモジュール、パッケージ、ツール
今年使ってみて良かった、Pythonモジュール、パッケージ、ツールaoshiman
 
暗号技術入門 秘密の国のアリス 総集編
暗号技術入門 秘密の国のアリス 総集編暗号技術入門 秘密の国のアリス 総集編
暗号技術入門 秘密の国のアリス 総集編京大 マイコンクラブ
 
FM音源をいじれるWebサービスを作った
FM音源をいじれるWebサービスを作ったFM音源をいじれるWebサービスを作った
FM音源をいじれるWebサービスを作ったCHY72
 
数値解析と物理学
数値解析と物理学数値解析と物理学
数値解析と物理学すずしめ
 
タイ文字と若干情報科学[修正版]
タイ文字と若干情報科学[修正版]タイ文字と若干情報科学[修正版]
タイ文字と若干情報科学[修正版]. きぷ
 
Goでヤフーの分散オブジェクトストレージを作った話 Go Conference 2017 Spring
Goでヤフーの分散オブジェクトストレージを作った話 Go Conference 2017 SpringGoでヤフーの分散オブジェクトストレージを作った話 Go Conference 2017 Spring
Goでヤフーの分散オブジェクトストレージを作った話 Go Conference 2017 SpringYahoo!デベロッパーネットワーク
 
フォントの選び方・使い方
フォントの選び方・使い方フォントの選び方・使い方
フォントの選び方・使い方k maztani
 
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.kiki utagawa
 

Viewers also liked (17)

Goをカンストさせる話
Goをカンストさせる話Goをカンストさせる話
Goをカンストさせる話
 
Authentication, Authorization, OAuth, OpenID Connect and Pyramid
Authentication, Authorization, OAuth, OpenID Connect and PyramidAuthentication, Authorization, OAuth, OpenID Connect and Pyramid
Authentication, Authorization, OAuth, OpenID Connect and Pyramid
 
HLSについて知っていることを話します
HLSについて知っていることを話しますHLSについて知っていることを話します
HLSについて知っていることを話します
 
Ik in action
Ik in actionIk in action
Ik in action
 
X / DRM (Direct Rendering Manager) Architectural Overview
X / DRM (Direct Rendering Manager) Architectural OverviewX / DRM (Direct Rendering Manager) Architectural Overview
X / DRM (Direct Rendering Manager) Architectural Overview
 
Pyramidのrendererをカスタマイズする
PyramidのrendererをカスタマイズするPyramidのrendererをカスタマイズする
Pyramidのrendererをカスタマイズする
 
今年使ってみて良かった、Pythonモジュール、パッケージ、ツール
今年使ってみて良かった、Pythonモジュール、パッケージ、ツール今年使ってみて良かった、Pythonモジュール、パッケージ、ツール
今年使ってみて良かった、Pythonモジュール、パッケージ、ツール
 
暗号技術入門 秘密の国のアリス 総集編
暗号技術入門 秘密の国のアリス 総集編暗号技術入門 秘密の国のアリス 総集編
暗号技術入門 秘密の国のアリス 総集編
 
PHP7を魔改造した話
PHP7を魔改造した話PHP7を魔改造した話
PHP7を魔改造した話
 
Altseed
AltseedAltseed
Altseed
 
FM音源をいじれるWebサービスを作った
FM音源をいじれるWebサービスを作ったFM音源をいじれるWebサービスを作った
FM音源をいじれるWebサービスを作った
 
hideya流 テストプレイ観察術
hideya流 テストプレイ観察術hideya流 テストプレイ観察術
hideya流 テストプレイ観察術
 
数値解析と物理学
数値解析と物理学数値解析と物理学
数値解析と物理学
 
タイ文字と若干情報科学[修正版]
タイ文字と若干情報科学[修正版]タイ文字と若干情報科学[修正版]
タイ文字と若干情報科学[修正版]
 
Goでヤフーの分散オブジェクトストレージを作った話 Go Conference 2017 Spring
Goでヤフーの分散オブジェクトストレージを作った話 Go Conference 2017 SpringGoでヤフーの分散オブジェクトストレージを作った話 Go Conference 2017 Spring
Goでヤフーの分散オブジェクトストレージを作った話 Go Conference 2017 Spring
 
フォントの選び方・使い方
フォントの選び方・使い方フォントの選び方・使い方
フォントの選び方・使い方
 
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
 

Similar to All I know about rsc.io/c2go

Similar to All I know about rsc.io/c2go (20)

Boosting Developer Productivity with Clang
Boosting Developer Productivity with ClangBoosting Developer Productivity with Clang
Boosting Developer Productivity with Clang
 
Vcs16
Vcs16Vcs16
Vcs16
 
10. R getting spatial
10.  R getting spatial10.  R getting spatial
10. R getting spatial
 
Computer Networks Lab File
Computer Networks Lab FileComputer Networks Lab File
Computer Networks Lab File
 
R getting spatial
R getting spatialR getting spatial
R getting spatial
 
Go Says WAT?
Go Says WAT?Go Says WAT?
Go Says WAT?
 
CppTutorial.ppt
CppTutorial.pptCppTutorial.ppt
CppTutorial.ppt
 
Groovy
GroovyGroovy
Groovy
 
深入淺出C語言
深入淺出C語言深入淺出C語言
深入淺出C語言
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
TDC2018SP | Trilha .Net - Novidades do C# 7 e 8
TDC2018SP | Trilha .Net - Novidades do C# 7 e 8TDC2018SP | Trilha .Net - Novidades do C# 7 e 8
TDC2018SP | Trilha .Net - Novidades do C# 7 e 8
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
Novidades do c# 7 e 8
Novidades do c# 7 e 8Novidades do c# 7 e 8
Novidades do c# 7 e 8
 
Python-GTK
Python-GTKPython-GTK
Python-GTK
 
Day 1
Day 1Day 1
Day 1
 
C lab programs
C lab programsC lab programs
C lab programs
 
C lab programs
C lab programsC lab programs
C lab programs
 
Writing MySQL UDFs
Writing MySQL UDFsWriting MySQL UDFs
Writing MySQL UDFs
 
Implementing Software Machines in Go and C
Implementing Software Machines in Go and CImplementing Software Machines in Go and C
Implementing Software Machines in Go and C
 
Data Analysis in Python
Data Analysis in PythonData Analysis in Python
Data Analysis in Python
 

More from Moriyoshi Koizumi

More from Moriyoshi Koizumi (20)

Uguisudani
UguisudaniUguisudani
Uguisudani
 
よいことも悪いこともぜんぶPHPが教えてくれた
よいことも悪いこともぜんぶPHPが教えてくれたよいことも悪いこともぜんぶPHPが教えてくれた
よいことも悪いこともぜんぶPHPが教えてくれた
 
Nginx lua
Nginx luaNginx lua
Nginx lua
 
Haxeについて
HaxeについてHaxeについて
Haxeについて
 
Gocon2013
Gocon2013Gocon2013
Gocon2013
 
PHP language update 201211
PHP language update 201211PHP language update 201211
PHP language update 201211
 
mod_himoteからはじめよう
mod_himoteからはじめようmod_himoteからはじめよう
mod_himoteからはじめよう
 
HPHPは約束の地なのか
HPHPは約束の地なのかHPHPは約束の地なのか
HPHPは約束の地なのか
 
Pyfes201110
Pyfes201110Pyfes201110
Pyfes201110
 
Phjosh(仮)プロジェクト
Phjosh(仮)プロジェクトPhjosh(仮)プロジェクト
Phjosh(仮)プロジェクト
 
Aaなゲームをjsで
AaなゲームをjsでAaなゲームをjsで
Aaなゲームをjsで
 
Aaなゲームをjsで
AaなゲームをjsでAaなゲームをjsで
Aaなゲームをjsで
 
ctypes拡張モジュール
ctypes拡張モジュールctypes拡張モジュール
ctypes拡張モジュール
 
LLの虎 semifinal: 殺伐Python
LLの虎 semifinal: 殺伐PythonLLの虎 semifinal: 殺伐Python
LLの虎 semifinal: 殺伐Python
 
Introducing E-Cell 3.2
Introducing E-Cell 3.2Introducing E-Cell 3.2
Introducing E-Cell 3.2
 
GoでKVSを書けるのか
GoでKVSを書けるのかGoでKVSを書けるのか
GoでKVSを書けるのか
 
PHPのすべらない話#3
PHPのすべらない話#3PHPのすべらない話#3
PHPのすべらない話#3
 
10〜30分で何となく分かるGo
10〜30分で何となく分かるGo10〜30分で何となく分かるGo
10〜30分で何となく分かるGo
 
PHPのすべらない話
PHPのすべらない話PHPのすべらない話
PHPのすべらない話
 
Phpをいじり倒す10の方法
Phpをいじり倒す10の方法Phpをいじり倒す10の方法
Phpをいじり倒す10の方法
 

Recently uploaded

Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdfSuman Jyoti
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...tanu pandey
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spaintimesproduction05
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Christo Ananth
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 

Recently uploaded (20)

(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 

All I know about rsc.io/c2go

  • 1. All I know about rsc.io/c2go Moriyoshi Koizumi Open Collector, Inc.
  • 2. Hey I can be found amongst CONTRIBUTORS :)
  • 3. rsc.io/c2go C to Go translation tool behind the Go's self-hosting effort. Written by rsc. Apparently not intended to be a generic translation tool. $ go get rsc.io/c2go
  • 4. こんにちは、世界 void print(char *fmt, ...); void main() { print("Hello, %s.n", "world"); } ⬇️ bin/c2go -dst $GOPATH hello.c package main func main() { fmt.Printf("Hello, %s.n", "world") } (generated under $GOPATH/src/main)
  • 6. Quicksort1 Involves pointer arithmetics, compound types, and function pointers.
  • 7. Quicksort1 (code) int print(const char *fmt, ...); typedef struct Data { int v; } Data; void swap(Data *a, Data *b) { Data v = *a; *a = *b; *b = v; } int cmp(Data *a, Data *b) { if (a->v < b->v) return -1; else if (a->v > b->v) return 1; else return 0; } ...
  • 8. Quicksort1 (code) ... void _qsort(Data *d, int l, int (*cmp)(Data *a, Data *b)) { int pivot, le, i, j; Data pv; if (l == 0) return; le = l - 1; pivot = l / 2; pv = d[pivot]; swap(&d[pivot], &d[le]); i = 0; for (j = 0; j < le; j++) { if (cmp(&d[j], &pv) < 0) { swap(&d[i], &d[j]); i++; } } swap(&d[i], &d[le]); _qsort(d, i, cmp); if (l > i) _qsort(&d[i + 1], l - (i + 1), cmp); } ...
  • 9. Quicksort1 (code) ... void main() { int i; Data values[] = { { 1 }, { 9 }, { 5 }, { 2 }, { 4 }, { 3 }, { 8 }, { 6 }, { 7 } }; _qsort(values, 9, cmp); for (i = 0; i < 9; i++) print("%dn", values[i].v); }
  • 10. Quicksort1 Compile: $ cc -o qsort1 qsort1.c $GO_1_4_ROOT/pkg/obj/darwin_amd64/lib9.a Run: $ ./qsort1 1 2 3 4 5 6 7 8 9
  • 11. Quicksort1 Translate: $ c2go -dst $GOPATH qsort1.c No worry about the message; this is a warning. main/qsort1.go: cannot find copyright notice in C file qsort1.c Generated code: package main type Data struct { v int } func swap(a *Data, b *Data) { var v Data = *a *a = *b *b = v } ...
  • 12. Quicksort1 ... func _qsort(d *Data, l int, cmp func(*Data, *Data) int) { var pivot int var le int var i int var j int var pv Data if l == 0 { return } le = l - 1 pivot = l / 2 pv = d[pivot] swap(&d[pivot], &d[le]) ... Looks like it went well so far, but Aw, Snap! # src/main/qsort1.go:24: invalid operation: d[pivot] (type *Data does not support indexing) src/main/qsort1.go:25: invalid operation: d[pivot] (type *Data does not support indexing) src/main/qsort1.go:25: invalid operation: d[le] (type *Data does not support indexing) ...
  • 14. Quicksort2 int print(const char *fmt, ...); typedef struct Data { int v; } Data; typedef struct DataSlice { Data *p; int len; int cap; } DataSlice;
  • 15. Quicksort2 ... void swap(Data *a, Data *b) { Data v = *a; *a = *b; *b = v; } int cmp(Data *a, Data *b) { if (a->v < b->v) return -1; else if (a->v > b->v) return 1; else return 0; } ...
  • 16. Quicksort2 ... void _qsort(DataSlice *ds, int (*cmp)(Data *a, Data *b)) { int pivot, le, i, j; Data pv; if (ds->len == 0) return; le = ds->len - 1; pivot = ds->len / 2; pv = ds->p[pivot]; swap(&ds->p[pivot], &ds->p[le]); i = 0; for (j = 0; j < le; j++) { if (cmp(&ds->p[j], &pv) < 0) { swap(&ds->p[i], &ds->p[j]); i++; } } swap(&ds->p[i], &ds->p[le]); ...
  • 17. Quicksort2 ... { DataSlice ns = { ds->p, i, ds->cap }; _qsort(&ns, cmp); } if (ds->len > i) { DataSlice ns = { &ds->p[i + 1], ds->len - (i + 1), ds->cap - (i + 1) }; _qsort(&ns, cmp); } } void main() { int i; Data values[] = { { 1 }, { 9 }, { 5 }, { 2 }, { 4 }, { 3 }, { 8 }, { 6 }, { 7 } }; DataSlice s = { values, 9, 9 }; _qsort(&s, cmp); for (i = 0; i < 9; i++) print("%dn", values[i].v); }
  • 18. Quicksort2 my.cfg: slice DataSlice.p DataSlice.len DataSlice.cap diff { - var ns = DataSlice{ds.p, i, cap(ds.p)} + var ns = DataSlice{ds.p[0:i]} } diff { - var ns = DataSlice{&ds.p[i+1], len(ds.p) - (i + 1), cap(ds.p) - (i + 1)} + var ns = DataSlice{ds.p[i+1:]} } diff { - var s = DataSlice{values, 9, 9} + var s = DataSlice{values} }
  • 19. Quicksort2 Translate: c2go -c my.cfg -dst $GOPATH qsort2.c Forget all the unfamiliar messages; these are warnings: qsort2.c:39: too many fields in braced initializer of DataSlice qsort2.c:43: too many fields in braced initializer of DataSlice qsort2.c:61: too many fields in braced initializer of DataSlice main/qsort2.go: cannot find copyright notice in C file qsort2.c
  • 20. Quicksort2 Generated Code: ... func _qsort(ds *DataSlice, cmp func(*Data, *Data) int) { var pivot int var le int var i int var j int var pv Data if len(ds.p) == 0 { return } le = len(ds.p) - 1 pivot = len(ds.p) / 2 pv = ds.p[pivot] swap(&ds.p[pivot], &ds.p[le]) i = 0 for j = 0; j < le; j++ { if cmp(&ds.p[j], &pv) < 0 { ... Looks pretty promising!
  • 21. Quicksort2 $ go run $GOPATH/src/main/qsort2.go # command-line-arguments src/main/qsort2.go:66: undefined: fmt in fmt.Printf Seems we had to add import "fmt" manually $ package main import "fmt" // here type Data struct { v int } func swap(a *Data, b *Data) { ...
  • 22. Quicksort2 Let's give it a second try: $ go run $GOPATH/src/main/qsort2.go 1 2 3 4 5 6 7 8 9 WOW %
  • 23. Quicksort2 The configuration file gives more control over the generated code. Its directives are not totally demystified yet.
  • 25. Quicksort3 This is CHEATING void qsort(void *d, int l, int s, int(*cmp)(void *, void *)); int print(char *fmt, ...); typedef struct Data { int v; } Data; int cmp(void *a, void *b) { Data *_a, *_b; _a = a; _b = b; if (_a->v < _b->v) return -1; else if (_a->v > _b->v) return 1; else return 0; } ...
  • 26. Quicksort3 ... void main() { int i; Data values[] = { { 1 }, { 9 }, { 5 }, { 2 }, { 4 }, { 3 }, { 8 }, { 6 }, { 7 } }; qsort(values, 9, sizeof(values[0]), cmp); for (i = 0; i < 9; i++) print("%dn", values[i].v); }
  • 27. Quicksort3 Translate: $ c2go -dst $GOPATH qsort3.c Generated code: package main type Data struct { v int } type cmp []Data func (x cmp) Len() int { return len(x) } func (x cmp) Swap(i, j int) { x[i], x[j] = x[j], x[i] } ...
  • 28. Quicksort3 ... func (x cmp) Less(i, j int) bool { var _a *Data var _b *Data _a = &x[i] _b = &x[j] if _a.v < _b.v { return true } else if _a.v > _b.v { return 1 < 0 } else { return false } } func main() { var i int var values = []Data{Data{1}, Data{9}, Data{5}, Data{2}, Data{4}, Data{3}, Data{8}, Data{6}, Data{7}} sort.Sort(cmp(values[:9])) for i = 0; i < 9; i++ { fmt.Printf("%dn", values[i].v) } }
  • 29. Quicksort3 This is a MAGIC. Some "standard" C functions are treated specially so that they get translated into the Go's counterparts. print() ➡️ fmt.Printf() qsort() ➡️ sort.Sort() memset() / memmove() strcpy() / strcat / strlen() / strcmp() malloc() / strdup() / free() abort() ' DISCLAIMER: this is just informational; not a complete list.
  • 30. Quicksort3 If you want to get the MAGIC happening right, You need to make sure... The prototype: void qsort(void *d, int l, int s, int(*cmp)(void *, void *)); The third argument cmp() function: void cmp(void *a, void *b) { type_in_question *a_, *b_; a_ = a; b_ = b; } not void cmp(void *a, void *b) { type_in_question *a_ = a, *b_ = b; }
  • 31. Quicksort3 Call to qsort(): qsort(list, nitems, sizeof(list[0]), cmp); not qsort(list, nitems, sizeof(type_in_question), cmp); Want to go further? See src/rsc.io/c2go/typecheck.go. More magics can be added!
  • 32. Thank you 2015/6/21 1:15:00(2015/6/21%201:15:00) Tags: golang, c2go(#ZgotmplZ) Moriyoshi Koizumi Open Collector, Inc. mozo@mozo.jp(mailto:mozo@mozo.jp) http://mozo.jp/(http://mozo.jp/) @moriyoshit(http://twitter.com/moriyoshit)