doc: scripts to run embedmd on markdown files

This will help keep READMEs in sync with code.

Also adds a README for the add example.

Updates #14
This commit is contained in:
Michael McLoughlin
2018-12-31 20:25:29 -08:00
parent 816fa4c0e6
commit 4550badf58
4 changed files with 59 additions and 1 deletions

49
examples/add/README.md Normal file
View File

@@ -0,0 +1,49 @@
# add
Add two numbers with `avo`.
The [code generator](asm.go) is as follows:
[embedmd]:# (asm.go)
```go
// +build ignore
package main
import (
. "github.com/mmcloughlin/avo/build"
)
func main() {
TEXT("Add", "func(x, y uint64) uint64")
Doc("Add adds x and y.")
x := Load(Param("x"), GP64v())
y := Load(Param("y"), GP64v())
ADDQ(x, y)
Store(y, ReturnIndex(0))
RET()
Generate()
}
```
We use a [`go:generate`](https://blog.golang.org/generate) line to produce the assembly and Go stub files together.
[embedmd]:# (add_test.go go /.*go:generate.*/)
```go
//go:generate go run asm.go -out add.s -stubs stub.go
```
This produces [`add.s`](add.s) as follows:
[embedmd]:# (add.s)
```s
// Code generated by command: go run asm.go -out add.s -stubs stub.go. DO NOT EDIT.
// func Add(x uint64, y uint64) uint64
TEXT ·Add(SB), $0-24
MOVQ x(FP), AX
MOVQ y+8(FP), CX
ADDQ AX, CX
MOVQ CX, ret+16(FP)
RET
```

View File

@@ -8,3 +8,6 @@ go get -u \
# Install golangci-lint
golangci_lint_version='v1.12.3'
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $GOPATH/bin ${golangci_lint_version}
# embedmd required for documentation generation
go get -u github.com/campoy/embedmd

3
script/doc Executable file
View File

@@ -0,0 +1,3 @@
#!/bin/bash -ex
find . -name '*.md' | xargs embedmd -w

View File

@@ -8,7 +8,7 @@ post=$(go list ./... | grep -E 'avo/(examples|tests)')
go install ./internal/cmd/avogen
# Attempt to delete generated files, to prove we can recreate them.
grep -REl 'Code generated.*DO NOT EDIT\.$' . | xargs rm -v
grep -REl 'Code generated.*DO NOT EDIT\.$' . | grep -v '.md$' | xargs rm -v
# Generate once.
go generate -v -x ${core}
@@ -19,3 +19,6 @@ go generate -v -x ${core}
# Generate dependent packages.
go generate -v -x ${post}
# Regenerate documentation.
./script/doc