-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.go
37 lines (30 loc) · 876 Bytes
/
lib.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
// Want to call a Go function from kaleidoscope?
// Good news! Here's how:
// <detailed instructions>
// We've separated external Go and C funcs into lib.go/lib.c because of
// a Cgo limitation. Quoting the Go Blog:
// "[I]f your program uses any //export directives, then the C code in
// the comment may only include declarations (extern int f();), not
// definitions (int f() { return 1; }). You can use //export directives
// to make Go functions accessible to C code."[^1](http://blog.golang.org/c-go-cgo)
// #include <stdio.h>
import "C"
import "fmt"
//export cgoputchard
func cgoputchard(x C.double) C.double {
C.putchar(C.int(x))
C.fflush(C.stdout)
return 0
}
//export goputchard
func goputchard(x float64) float64 {
fmt.Printf("%c", rune(x))
return 0
}
// Helpers:
//export printd
func printd(x float64) float64 {
fmt.Println(x)
return 0
}