reorganize: consolidate demo directories

- Consolidate _demo, _pydemo, _embdemo into single _demo directory structure
- Organize demos by language: _demo/{go,py,c,embed}/
- Categorize demos based on imports:
- Python library demos (py imports) → _demo/py/
- C/C++ library demos (c/cpp imports) → _demo/c/
- Go-specific demos → _demo/go/
- Embedded demos → _demo/embed/
- Move C-related demos (asm*, cabi*, cgo*, linkname, targetsbuild) from go/ to c/
- Update all path references in README.md and GitHub workflows
- Improve demo organization and navigation as requested in #1256

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Li Jie
2025-09-09 15:06:55 +08:00
parent 849b23079b
commit 64df39b3c5
96 changed files with 61 additions and 52 deletions

View File

@@ -22,14 +22,14 @@ jobs:
steps:
- name: Download model file
run: |
mkdir -p ./_demo/llama2-c
wget -P ./_demo/llama2-c https://huggingface.co/karpathy/tinyllamas/resolve/main/stories15M.bin
mkdir -p ./_demo/c/llama2-c
wget -P ./_demo/c/llama2-c https://huggingface.co/karpathy/tinyllamas/resolve/main/stories15M.bin
- name: Upload model as artifact
uses: actions/upload-artifact@v4
with:
name: llama2-model
path: ./_demo/llama2-c/stories15M.bin
path: ./_demo/c/llama2-c/stories15M.bin
retention-days: 1
llgo:
@@ -54,7 +54,7 @@ jobs:
uses: actions/download-artifact@v5
with:
name: llama2-model
path: ./_demo/llama2-c/
path: ./_demo/c/llama2-c/
- name: Download platform-specific demo libs
run: |
if ${{ startsWith(matrix.os, 'macos') }}; then
@@ -63,8 +63,8 @@ jobs:
DEMO_PKG="cargs_linux_amd64.zip"
fi
mkdir -p ./_demo/cargs/libs
cd ./_demo/cargs/libs
mkdir -p ./_demo/c/cargs/libs
cd ./_demo/c/cargs/libs
wget https://github.com/goplus/llpkg/releases/download/cargs/v1.0.0/${DEMO_PKG}
unzip ${DEMO_PKG}
@@ -104,7 +104,7 @@ jobs:
echo "Testing demo without RPATH (should fail)..."
export LLGO_FULL_RPATH=false
pkg-config --libs cargs
if (cd ./_demo/cargs && llgo run .); then
if (cd ./_demo/c/cargs && llgo run .); then
echo "ERROR: cargs demo should have failed without RPATH!"
exit 1
else
@@ -278,7 +278,7 @@ jobs:
- name: Test Cross Compilation (wasm)
shell: bash
working-directory: _demo
working-directory: _demo/c
run: |
echo "Testing cross-compilation wasm with Go 1.24.2"

View File

@@ -43,5 +43,5 @@ jobs:
- name: Build targets
run: |
cd _demo/targetsbuild
cd _demo/embed/targetsbuild
bash build.sh

View File

@@ -1,11 +1,11 @@
#!/bin/bash
set -e
# llgo run subdirectories under _demo and _pydemo that contain *.go files
# llgo run subdirectories under _demo that contain *.go files
total=0
failed=0
failed_cases=""
for d in ./_demo/* ./_pydemo/*; do
for d in ./_demo/go/* ./_demo/py/* ./_demo/c/*; do
if [ -d "$d" ] && [ -n "$(ls "$d"/*.go 2>/dev/null)" ]; then
total=$((total+1))
echo "Testing $d"

View File

@@ -63,14 +63,14 @@ This is a simple example of calling the C `printf` function to print `Hello worl
The `_demo` directory contains some C standard libary related demos (it start with `_` to prevent the `go` command from compiling it):
* [hello](_demo/hello/hello.go): call C `printf` to print `Hello world`
* [concat](_demo/concat/concat.go): call C `fprintf` with `stderr`
* [qsort](_demo/qsort/qsort.go): call C function with a callback (eg. `qsort`)
* [hello](_demo/c/hello/hello.go): call C `printf` to print `Hello world`
* [concat](_demo/c/concat/concat.go): call C `fprintf` with `stderr`
* [qsort](_demo/c/qsort/qsort.go): call C function with a callback (eg. `qsort`)
To run these demos (If you haven't installed `llgo` yet, please refer to [How to install](#how-to-install)):
```sh
cd <demo-directory> # eg. cd _demo/hello
cd <demo-directory> # eg. cd _demo/c/hello
llgo run .
```
@@ -88,7 +88,7 @@ import _ "unsafe" // for go:linkname
func Sqrt(x float64) float64
```
You can directly integrate it into [your own code](_demo/linkname/linkname.go):
You can directly integrate it into [your own code](_demo/c/linkname/linkname.go):
<!-- embedme doc/_readme/llgo_call_c/call_c.go -->
@@ -204,17 +204,17 @@ func main() {
Here we define two 3x3 matrices a and b, add them to get x, and then print the result.
The `_pydemo` directory contains some python related demos:
The `_demo/py/` directory contains some python related demos:
* [callpy](_pydemo/callpy/callpy.go): call Python standard library function `math.sqrt`
* [pi](_pydemo/pi/pi.go): print python constants `math.pi`
* [statistics](_pydemo/statistics/statistics.go): define a python list and call `statistics.mean` to get the mean
* [matrix](_pydemo/matrix/matrix.go): a basic `numpy` demo
* [callpy](_demo/py/callpy/callpy.go): call Python standard library function `math.sqrt`
* [pi](_demo/py/pi/pi.go): print python constants `math.pi`
* [statistics](_demo/py/statistics/statistics.go): define a python list and call `statistics.mean` to get the mean
* [matrix](_demo/py/matrix/matrix.go): a basic `numpy` demo
To run these demos (If you haven't installed `llgo` yet, please refer to [How to install](#how-to-install)):
```sh
cd <demo-directory> # eg. cd _pydemo/callpy
cd <demo-directory> # eg. cd _demo/py/callpy
llgo run .
```
@@ -242,7 +242,7 @@ The currently supported libraries include:
Here are some examples related to them:
* [llama2-c](_demo/llama2-c): inference Llama 2 (It's the first llgo AI example)
* [llama2-c](_demo/c/llama2-c): inference Llama 2 (It's the first llgo AI example)
* [mkjson](https://github.com/goplus/lib/tree/main/c/cjson/_demo/mkjson/mkjson.go): create a json object and print it
* [sqlitedemo](https://github.com/goplus/lib/tree/main/c/sqlite/_demo/sqlitedemo/demo.go): a basic sqlite demo
* [tetris](https://github.com/goplus/lib/tree/main/c/raylib/_demo/tetris/tetris.go): a tetris game based on raylib
@@ -252,11 +252,11 @@ Here are some examples related to them:
All Go syntax (including `cgo`) is already supported. Here are some examples:
* [concat](_demo/concat/concat.go): define a variadic function
* [genints](_demo/genints/genints.go): various forms of closure usage (including C function, recv.method and anonymous function)
* [concat](_demo/c/concat/concat.go): define a variadic function
* [genints](_demo/c/genints/genints.go): various forms of closure usage (including C function, recv.method and anonymous function)
* [errors](_cmptest/errors/errors.go): demo to implement error interface
* [defer](_cmptest/defer/defer.go): defer demo
* [goroutine](_demo/goroutine/goroutine.go): goroutine demo
* [goroutine](_demo/go/goroutine/goroutine.go): goroutine demo
### Defer

View File

@@ -106,8 +106,8 @@ import (
"fmt"
"unsafe"
"github.com/goplus/llgo/_demo/cgofull/pymod1"
"github.com/goplus/llgo/_demo/cgofull/pymod2"
"github.com/goplus/llgo/_demo/c/cgofull/pymod1"
"github.com/goplus/llgo/_demo/c/cgofull/pymod2"
)
//export go_callback_not_use_in_go

View File

@@ -3,7 +3,7 @@ package main
import (
"github.com/goplus/lib/c"
"github.com/goplus/lib/c/math"
"github.com/goplus/llgo/_demo/cppintf/foo"
"github.com/goplus/llgo/_demo/c/cppintf/foo"
)
type Bar struct {

View File

@@ -5,7 +5,7 @@ import (
"github.com/goplus/lib/c"
"github.com/goplus/lib/c/math"
"github.com/goplus/llgo/_demo/cppmintf/foo"
"github.com/goplus/llgo/_demo/c/cppmintf/foo"
)
type Bar struct {

5
_demo/c/go.mod Normal file
View File

@@ -0,0 +1,5 @@
module github.com/goplus/llgo/_demo/c
go 1.20
require github.com/goplus/lib v0.3.0

2
_demo/c/go.sum Normal file
View File

@@ -0,0 +1,2 @@
github.com/goplus/lib v0.3.0 h1:y0ZGb5Q/RikW1oMMB4Di7XIZIpuzh/7mlrR8HNbxXCA=
github.com/goplus/lib v0.3.0/go.mod h1:SgJv3oPqLLHCu0gcL46ejOP3x7/2ry2Jtxu7ta32kp0=

View File

Before

Width:  |  Height:  |  Size: 183 KiB

After

Width:  |  Height:  |  Size: 183 KiB

View File

@@ -36,7 +36,6 @@ loop: // parse command line arguments
// build the Tokenizer via the tokenizer .bin file
var tokenizer llama2.Tokenizer
llama2.BuildTokenizer(&tokenizer, tokenizerPath, transformer.Config.VocabSize)
// build the Sampler
var sampler llama2.Sampler
llama2.BuildSampler(&sampler, transformer.Config.VocabSize, temperature, topp, rngSeed)

5
_demo/embed/go.mod Normal file
View File

@@ -0,0 +1,5 @@
module github.com/goplus/llgo/_demo/embed
go 1.20
require github.com/goplus/lib v0.3.0

2
_demo/embed/go.sum Normal file
View File

@@ -0,0 +1,2 @@
github.com/goplus/lib v0.3.0 h1:y0ZGb5Q/RikW1oMMB4Di7XIZIpuzh/7mlrR8HNbxXCA=
github.com/goplus/lib v0.3.0/go.mod h1:SgJv3oPqLLHCu0gcL46ejOP3x7/2ry2Jtxu7ta32kp0=

View File

@@ -111,7 +111,7 @@ if [ $# -eq 1 ]; then
done < "$target_file"
else
# Use targets from *.json files
for target_file in ../../targets/*.json; do
for target_file in ../../../targets/*.json; do
# Extract target name from filename (remove path and .json extension)
target=$(basename "$target_file" .json)
targets_to_build+=("$target")
@@ -127,9 +127,9 @@ for target in "${targets_to_build[@]}"; do
continue
fi
output=$(../../llgo.sh build -target $target -o hello.out . 2>&1)
output=$(../../../llgo.sh build -target $target -o hello.elf . 2>&1)
if [ $? -eq 0 ]; then
echo$target `file hello.out`
echo$target `file hello.elf`
successful_targets+=("$target")
else
# Check if output contains warning messages

View File

@@ -0,0 +1,6 @@
package main
import _ "github.com/goplus/llgo/_demo/embed/targetsbuild/C"
func main() {
}

View File

@@ -3,8 +3,8 @@ package main
import (
"time"
"github.com/goplus/llgo/_demo/async/async"
"github.com/goplus/llgo/_demo/async/timeout"
"github.com/goplus/llgo/_demo/go/async/async"
"github.com/goplus/llgo/_demo/go/async/timeout"
)
func Sleep(i int, d time.Duration) async.Future[int] {

View File

@@ -3,7 +3,7 @@ package timeout
import (
"time"
"github.com/goplus/llgo/_demo/async/async"
"github.com/goplus/llgo/_demo/go/async/async"
)
func Timeout(d time.Duration) async.Future[async.Void] {

3
_demo/go/go.mod Normal file
View File

@@ -0,0 +1,3 @@
module github.com/goplus/llgo/_demo/go
go 1.20

0
_demo/go/go.sum Normal file
View File

View File

@@ -1,4 +1,4 @@
module github.com/goplus/llgo/_demo
module github.com/goplus/llgo/_demo/py
go 1.20

View File

@@ -1,6 +0,0 @@
package main
import _ "github.com/goplus/llgo/_demo/targetsbuild/C"
func main() {
}

View File

@@ -1,5 +0,0 @@
module github.com/goplus/llgo/_pydemo
go 1.20
require github.com/goplus/lib v0.2.0

View File

@@ -1,2 +0,0 @@
github.com/goplus/lib v0.2.0 h1:AjqkN1XK5H23wZMMlpaUYAMCDAdSBQ2NMFrLtSh7W4g=
github.com/goplus/lib v0.2.0/go.mod h1:SgJv3oPqLLHCu0gcL46ejOP3x7/2ry2Jtxu7ta32kp0=