doc: make doc testable

This commit is contained in:
Li Jie
2024-11-06 10:28:08 +08:00
parent c7649766fd
commit 17d509a45a
13 changed files with 199 additions and 2 deletions

View File

@@ -0,0 +1,10 @@
package main
import _ "unsafe" // for go:linkname
//go:linkname Sqrt C.sqrt
func Sqrt(x float64) float64
func main() {
println("sqrt(2) =", Sqrt(2))
}

View File

@@ -0,0 +1,7 @@
package main
import "github.com/goplus/llgo/c/math"
func main() {
println("sqrt(2) =", math.Sqrt(2))
}

View File

@@ -0,0 +1,4 @@
import math
x = math.sqrt(2)
print("sqrt =", x)

View File

@@ -0,0 +1,12 @@
package main
import (
"github.com/goplus/llgo/py"
"github.com/goplus/llgo/py/math"
"github.com/goplus/llgo/py/std"
)
func main() {
x := math.Sqrt(py.Float(2)) // x = sqrt(2)
std.Print(py.Str("sqrt(2) ="), x) // print("sqrt(2) =", x)
}

View File

@@ -0,0 +1,22 @@
package main
import (
"github.com/goplus/llgo/py"
"github.com/goplus/llgo/py/numpy"
"github.com/goplus/llgo/py/std"
)
func main() {
a := py.List(
py.List(1.0, 2.0, 3.0),
py.List(4.0, 5.0, 6.0),
py.List(7.0, 8.0, 9.0),
)
b := py.List(
py.List(9.0, 8.0, 7.0),
py.List(6.0, 5.0, 4.0),
py.List(3.0, 2.0, 1.0),
)
x := numpy.Add(a, b)
std.Print(py.Str("a+b ="), x)
}

View File

@@ -0,0 +1,7 @@
package main
import "github.com/goplus/llgo/c"
func main() {
c.Printf(c.Str("Hello world\n"))
}

View File

@@ -0,0 +1,8 @@
# shellcheck disable=all
git clone https://github.com/goplus/llgo.git
cd llgo
go install -v ./cmd/...
go install -v ./chore/... # compile all tools except pydump
cd chore/_xtool
llgo install ./... # compile pydump
go install github.com/goplus/hdq/chore/pysigfetch@v0.8.1 # compile pysigfetch

View File

@@ -0,0 +1,5 @@
# shellcheck disable=all
brew update
brew install llvm@18 pkg-config bdw-gc openssl cjson
brew install python@3.12 # optional
go install -v github.com/goplus/llgo/cmd/llgo@latest

View File

@@ -0,0 +1,7 @@
# shellcheck disable=all
echo "deb http://apt.llvm.org/$(lsb_release -cs)/ llvm-toolchain-$(lsb_release -cs)-18 main" | sudo tee /etc/apt/sources.list.d/llvm.list
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
sudo apt-get update
sudo apt-get install -y llvm-18-dev clang-18 libclang-18-dev lld-18 pkg-config libgc-dev libssl-dev zlib1g-dev libcjson-dev python3.12-dev
sudo apt-get install -y python3.12-dev # optional
go install -v github.com/goplus/llgo/cmd/llgo@latest

View File

@@ -0,0 +1,10 @@
#!/bin/bash
cd ./doc/_readme/ || exit 1
llgo build -v ./...
pip3 install --user numpy
for dir in ./*/; do
if grep -q "func main()" "$dir"/*.go 2>/dev/null; then
echo "Running examples in $dir"
llgo run "$dir"
fi
done