Compare commits

..

No commits in common. "master" and "v0.7.0" have entirely different histories.

555 changed files with 10785 additions and 50100 deletions

View File

@ -23,7 +23,7 @@ jobs:
go-version: 1.17
- run: make -C internal/app/wawasm
- run: make -C waroot/examples/snake publish
- run: make -C _examples/snake publish
- name: Deploy
uses: peaceiris/actions-gh-pages@v3

View File

@ -30,9 +30,10 @@ jobs:
- run: go install
- run: wa -h
- run: cd waroot && wa run examples/prime
- run: cd waroot && wa run hello.wa
- run: cd waroot && wa run -target=chrome hello.wa
- run: wa hello.wa
- run: wa run _examples/prime
- run: wa run hello.wa
- run: wa run -target=chrome hello.wa
- uses: shogo82148/actions-goveralls@v1
with:
@ -59,9 +60,10 @@ jobs:
- run: go install
- run: wa -h
- run: cd waroot && wa run examples/prime
- run: cd waroot && wa run hello.wa
- run: cd waroot && wa run -target=chrome hello.wa
- run: wa hello.wa
- run: wa run _examples/prime
- run: wa run hello.wa
- run: wa run -target=chrome hello.wa
build-and-test-macos:
runs-on: macos-12
@ -84,6 +86,7 @@ jobs:
- run: go install
- run: wa -h
- run: cd waroot && wa run examples/prime
- run: cd waroot && wa run hello.wa
- run: cd waroot && wa run -target=chrome hello.wa
- run: wa hello.wa
- run: wa run _examples/prime
- run: wa run hello.wa
- run: wa run -target=chrome hello.wa

1
.gitignore vendored
View File

@ -1,7 +1,6 @@
/.fleet
/.vscode
/_build*
/hello
wa
wa.exe

View File

@ -1,86 +0,0 @@
# This is an example .goreleaser.yml file with some sane defaults.
# Make sure to check the documentation at http://goreleaser.com
before:
hooks:
- go version
builds:
- id: default
env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin
goarch:
- amd64
- arm64
# 使用 ldflags="-s -w" 去掉符号表和调试信息,以减少发布包的大小
ldflags:
- -s
- -w
ignore:
- goos: linux
goarch: arm64
- goos: windows
goarch: arm64
main: .
archives:
- id: default
builds:
- default
name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
format_overrides:
- goos: windows
format: zip
checksum:
name_template: 'checksums.txt'
changelog:
use: github
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
release:
github:
owner: wa-lang
name: wa
draft: false
footer: |
Homepage https://wa-lang.org
brews:
- tap:
owner: wa-lang
name: homebrew-tap
url_template: "https://github.com/wa-lang/wa/releases/download/{{ .Tag }}/{{ .ArtifactName }}"
download_strategy: CurlDownloadStrategy
# Git author used to commit to the repository.
# Defaults are shown.
commit_author:
name: GoReleaser Bot
email: goreleaser@carlosbecker.com
folder: HomebrewFormula
homepage: "https://github.com/wa-lang/wa"
description: "The Wa Programming Language"
license: "AGPL-3.0 license"
skip_upload: false
test: |
system "#{bin}/wa"
scoop:
url_template: "https://github.com/wa-lang/wa/releases/download/{{ .Tag }}/{{ .ArtifactName }}"
bucket:
owner: wa-lang
name: scoop-bucket
branch: master
token: "{{ .Env.GITHUB_TOKEN }}"
folder: Scoops
commit_author:
name: goreleaserbot
email: bot@goreleaser.com
commit_msg_template: "Scoop update for {{ .ProjectName }} version {{ .Tag }}"
homepage: "http://github.com/wa-lang/wa"
description: "The Wa Programming Language"
license: AGPL-3.0 license

View File

@ -29,9 +29,9 @@ stages:
chmod +X output/wa-lang-linux64/wa
./output/wa-lang-linux64/wa
./output/wa-lang-linux64/wa -h
#./output/wa-lang-linux64/wa waroot/hello.wa
#./output/wa-lang-linux64/wa run waroot/examples/prime
#./output/wa-lang-linux64/wa run waroot/hello.wa
./output/wa-lang-linux64/wa hello.wa
./output/wa-lang-linux64/wa run _examples/prime
./output/wa-lang-linux64/wa run hello.wa
# 非必填字段开启后表示将构建产物暂存但不会上传到制品库中7天后自动清除
artifacts:

View File

@ -29,9 +29,9 @@ stages:
chmod +X output/wa-lang-linux64/wa
./output/wa-lang-linux64/wa
./output/wa-lang-linux64/wa -h
#./output/wa-lang-linux64/wa waroot/hello.wa
#./output/wa-lang-linux64/wa run waroot/examples/prime
#./output/wa-lang-linux64/wa run waroot/hello.wa
./output/wa-lang-linux64/wa hello.wa
./output/wa-lang-linux64/wa run _examples/prime
./output/wa-lang-linux64/wa run hello.wa
# 非必填字段开启后表示将构建产物暂存但不会上传到制品库中24小时后自动清除
artifacts:

View File

@ -29,9 +29,9 @@ stages:
chmod +X output/wa-lang-linux64/wa
./output/wa-lang-linux64/wa
./output/wa-lang-linux64/wa -h
#./output/wa-lang-linux64/wa waroot/hello.wa
#./output/wa-lang-linux64/wa run waroot/examples/prime
#./output/wa-lang-linux64/wa run waroot/hello.wa
./output/wa-lang-linux64/wa hello.wa
./output/wa-lang-linux64/wa run _examples/prime
./output/wa-lang-linux64/wa run hello.wa
# 非必填字段开启后表示将构建产物暂存但不会上传到制品库中7天后自动清除
artifacts:

View File

@ -1,15 +1,17 @@
# 版权 @2019 凹语言 作者。保留所有权利。
hello:
go install
cd waroot && go run ../main.go run hello.wa
go run main.go run hello.wa
prime:
cd waroot && go run ../main.go run examples/prime
go run main.go run _examples/prime
build-wasm:
GOARCH=wasm GOOS=js go build -o wa.out.wasm ./main_wasm.go
win-exe-icon:
windres -o main_rc_windows.syso main.rc
arduino-run:
go run main.go -target=arduino arduino.wa
@ -19,17 +21,14 @@ arduino-build:
xxd -i a.out.wasm > app.wasm.h
ci-test-all:
go install
go test ./...
@echo "== std test begin =="
go run main.go test std
go run main.go test fmt
@echo "== std ok =="
go run main.go run ./waroot/hello.wa
cd waroot && go run ../main.go run hello.wa
go run main.go hello.wa
make -C ./waroot/examples ci-test-all
make -C ./_examples ci-test-all
clean:
-rm a.out*

View File

@ -10,7 +10,6 @@
![](docs/images/logo/logo-animate1.svg)
- 主页: [https://wa-lang.org](https://wa-lang.org)
- 参考手册: [https://wa-lang.org/man/](https://wa-lang.org/man/)
- 仓库(Gitee): [https://gitee.com/wa-lang/wa](https://gitee.com/wa-lang/wa)
- 仓库(Github): [https://github.com/wa-lang/wa](https://github.com/wa-lang/wa)
- Playground: [https://wa-lang.org/playground](https://wa-lang.org/playground)
@ -37,6 +36,14 @@
![](https://wa-lang.org/st0018-03.jpg)
## 本地安装和测试:
Go >= 1.17
1. `go install wa-lang.org/wa@latest`
2. `wa init -name=_examples/hi`
3. `wa run _examples/hi`
## 例子: 凹语言
打印字符和调用函数:
@ -44,11 +51,9 @@
```wa
import "fmt"
global year: i32 = 2023
func main {
println("hello, Wa!")
println(add(40, 2), year)
println("你好,凹语言!")
println(add(40, 2))
fmt.Println(1+1)
}
@ -61,9 +66,9 @@ func add(a: i32, b: i32) => i32 {
运行并输出结果:
```
$ wa run hello.wa
$ go run main.go hello.wa
你好,凹语言!
42 2023
42
2
```
@ -76,7 +81,7 @@ $ wa run hello.wa
func main {
for n := 2; n <= 30; n = n + 1 {
isPrime: int = 1
var isPrime int = 1
for i := 2; i*i <= n; i = i + 1 {
if x := n % i; x == 0 {
isPrime = 0
@ -92,7 +97,7 @@ func main {
运行并输出结果:
```
$ cd waroot && wa run examples/prime
$ go run main.go run _examples/prime
2
3
5
@ -129,23 +134,5 @@ $ cd waroot && wa run examples/prime
运行的结果和英文语法的示例相同。
更多例子 [waroot/examples](waroot/examples)
更多例子 [_examples](_examples)
## 贡献者名单
|贡献者|贡献点|
| --- | --- |
|柴树杉| 35000|
|丁尔男| 42500|
|史斌 | 29000|
|扈梦明| 14000|
|赵普明| 17000|
|宋汝阳| 2000|
|刘云峰| 1000|
|王湘南| 1000|
|王泽龙| 1000|
|吴烜 | 3000|
|刘斌 | 2500|
|尹贻浩| 2000|
贡献点变更记录见 [waroot/cplog](waroot/cplog) 目录。

View File

@ -8,8 +8,10 @@
<div align="center">
[![Build Status](https://github.com/wa-lang/wa/actions/workflows/wa.yml/badge.svg)](https://github.com/wa-lang/wa/actions/workflows/wa.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/wa-lang/wa)](https://goreportcard.com/report/github.com/wa-lang/wa)
[![Coverage Status](https://coveralls.io/repos/github/wa-lang/wa/badge.svg)](https://coveralls.io/github/wa-lang/wa)
[![GitHub release](https://img.shields.io/github/v/tag/wa-lang/wa.svg?label=release)](https://github.com/wa-lang/wa/releases)
[![Go Reference](https://pkg.go.dev/badge/wa-lang.org/wa.svg)](https://pkg.go.dev/wa-lang.org/wa)
[![license](https://img.shields.io/github/license/wa-lang/wa.svg)](https://github.com/wa-lang/wa/blob/master/LICENSE)
</div>
@ -19,8 +21,7 @@ Instead of requiring complex toolchains to set up, you can simply go install it
![](docs/images/logo/logo-animate1.svg)
- Home: [https://wa-lang.github.io/](https://wa-lang.github.io/)
- Manual: [https://wa-lang.github.io/man/en/](https://wa-lang.github.io/man/en/)
- Home: [https://wa-lang.org](https://wa-lang.org)
- Github: [https://github.com/wa-lang/wa](https://github.com/wa-lang/wa)
- Playground: [https://wa-lang.org/playground](https://wa-lang.org/playground)
@ -35,22 +36,32 @@ Instead of requiring complex toolchains to set up, you can simply go install it
## Snake Game
- Play: [https://wa-lang.org/wa/snake/](https://wa-lang.org/wa/snake/)
- Code: [waroot/examples/snake/README-en.md](waroot/examples/snake/README-en.md)
- Code: [_examples/snake/README-en.md](_examples/snake/README-en.md)
![](https://wa-lang.org/st0018-03.jpg)
## Install and Run:
Go >= 1.17
1. `go install wa-lang.org/wa@latest`
2. `wa init -name=_examples/hi`
3. `wa run _examples/hi`
> The Wa project is still in very early stage. If you want to submit PR, please read the [Contribution Guide(Chinese)](https://wa-lang.org/community/contribute.html). We do not accept PR only about 3rdparty changes.
## Example: Print Wa
Print rune and call function
```wa
import "fmt"
// Copyright @2019-2022 The Wa author. All rights reserved.
global year: i32 = 2023
import "fmt"
func main {
println("hello, Wa!")
println(add(40, 2), year)
println(add(40, 2))
fmt.Println(1+1)
}
@ -63,9 +74,9 @@ func add(a: i32, b: i32) => i32 {
Execute the program:
```
$ wa run hello.wa
$ go run main.go hello.wa
hello, Wa!
42 2023
42
2
```
@ -73,10 +84,10 @@ hello, Wa!
Print prime numbers up to 30:
```wa
```
func main {
for n := 2; n <= 30; n = n + 1 {
isPrime: int = 1
var isPrime int = 1
for i := 2; i*i <= n; i = i + 1 {
if x := n % i; x == 0 {
isPrime = 0
@ -92,7 +103,7 @@ func main {
Execute the program:
```
$ cd waroot && wa run examples/prime
$ go run main.go run _examples/prime
2
3
5
@ -131,21 +142,5 @@ Print prime numbers up to 30:
Output is the same as the previous example.
More examples [waroot/examples](waroot/examples)
More examples [_examples](_examples)
## Contributors
|Contributor|Contribution points|
| --- | --- |
|柴树杉| 35000|
|丁尔男| 42500|
|史斌 | 29000|
|扈梦明| 14000|
|赵普明| 17000|
|宋汝阳| 2000|
|刘云峰| 1000|
|王湘南| 1000|
|王泽龙| 1000|
|吴烜 | 3000|
|刘斌 | 2500|
|尹贻浩| 2000|

11
_dev/play-chai/Makefile Normal file
View File

@ -0,0 +1,11 @@
# 版权 @2023 凹语言 作者。保留所有权利。
dev:
go run . -dbg=a1.out.dbg -wat=a1.out.wat
-rm wa.wasm
GOOS=js GOARCH=wasm go build -o wa.wasm
node run-go-wasm.js wa.wasm -dbg=a2.out.dbg -wat=a2.out.wat
clean:
-rm *.out.* *.wasm

81
_dev/play-chai/base.go Normal file
View File

@ -0,0 +1,81 @@
// 版权 @2022 凹语言 作者。保留所有权利。
package main
import (
"wa-lang.org/wa/api"
"wa-lang.org/wa/internal/backends/compiler_wat"
"wa-lang.org/wa/internal/config"
"wa-lang.org/wa/internal/loader"
"wa-lang.org/wa/internal/logger"
)
var waError error
func waGetError() error {
return waError
}
func waGetErrorText() string {
if waError != nil {
return waError.Error()
} else {
return ""
}
}
func waSetError(err error) {
waError = err
}
func waClearError() {
waError = nil
}
func waDebugString(filename, code string) string {
cfg := api.DefaultConfig()
cfg.WaArch = api.WaArch_wasm
cfg.WaOS = api.WaOS_chrome
prog, err := loader.LoadProgramFile(cfg, filename, code)
if err != nil || prog == nil {
return err.Error()
}
return prog.DebugString()
}
func waGenerateWat(filename, code string) string {
cfg := api.DefaultConfig()
cfg.WaArch = api.WaArch_wasm
cfg.WaOS = api.WaOS_chrome
wat, err := waBuildFile(cfg, filename, code)
if err != nil {
if waGetError() == nil {
waSetError(err)
}
return ""
}
return string(wat)
}
func waFormatCode(filename, code string) string {
newCode, err := api.FormatCode(filename, code)
if err != nil {
if waGetError() == nil {
waSetError(err)
}
return code
}
return newCode
}
// 构建 wat 目标
func waBuildFile(cfg *config.Config, filename string, src interface{}) (wat []byte, err error) {
prog, err := loader.LoadProgramFile(cfg, filename, src)
if err != nil || prog == nil {
logger.Tracef(&config.EnableTrace_api, "LoadProgramVFS failed, err = %v", err)
return nil, err
}
watOut, err := compiler_wat.New().Compile(prog, "main")
return []byte(watOut), err
}

56
_dev/play-chai/main.go Normal file
View File

@ -0,0 +1,56 @@
// 版权 @2022 凹语言 作者。保留所有权利。
package main
import (
"flag"
"fmt"
"os"
)
var (
flagDbgOut = flag.String("dbg", "a.out.dbg", "debug output file")
flagWatOut = flag.String("wat", "a.out.wat", "wat output file")
)
func main() {
flag.Parse()
os.Remove(*flagDbgOut)
os.Remove(*flagWatOut)
fmt.Println("=== WARN: only for build wa.wasm === ")
waClearError()
outDbg := waDebugString(waName, waCode)
outWat := waGenerateWat(waName, waCode)
if serr := waGetErrorText(); serr != "" {
fmt.Println("ERR:", serr)
os.Exit(1)
}
os.WriteFile(*flagDbgOut, []byte(outDbg), 0666)
os.WriteFile(*flagWatOut, []byte(outWat), 0666)
fmt.Println("Done")
}
const waName = "hello.wa"
const waCode = `// 版权 @2019 凹语言 作者。保留所有权利。
import "fmt"
import "runtime"
func main {
println("你好,凹语言!", runtime.WAOS)
println(add(40, 2))
fmt.Println(1+1)
}
func add(a: i32, b: i32) => i32 {
return a+b
}
`

View File

@ -0,0 +1,638 @@
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Go1.17.13
(() => {
// Map multiple JavaScript environments to a single common API,
// preferring web standards over Node.js API.
//
// Environments considered:
// - Browsers
// - Node.js
// - Electron
// - Parcel
// - Webpack
if (typeof global !== "undefined") {
// global already exists
} else if (typeof window !== "undefined") {
window.global = window;
} else if (typeof self !== "undefined") {
self.global = self;
} else {
throw new Error("cannot export Go (neither global, window nor self is defined)");
}
if (!global.require && typeof require !== "undefined") {
global.require = require;
}
if (!global.fs && global.require) {
const fs = require("fs");
if (typeof fs === "object" && fs !== null && Object.keys(fs).length !== 0) {
global.fs = fs;
}
}
const enosys = () => {
const err = new Error("not implemented");
err.code = "ENOSYS";
return err;
};
if (!global.fs) {
let outputBuf = "";
global.fs = {
constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 }, // unused
writeSync(fd, buf) {
outputBuf += decoder.decode(buf);
const nl = outputBuf.lastIndexOf("\n");
if (nl != -1) {
console.log(outputBuf.substr(0, nl));
outputBuf = outputBuf.substr(nl + 1);
}
return buf.length;
},
write(fd, buf, offset, length, position, callback) {
if (offset !== 0 || length !== buf.length || position !== null) {
callback(enosys());
return;
}
const n = this.writeSync(fd, buf);
callback(null, n);
},
chmod(path, mode, callback) { callback(enosys()); },
chown(path, uid, gid, callback) { callback(enosys()); },
close(fd, callback) { callback(enosys()); },
fchmod(fd, mode, callback) { callback(enosys()); },
fchown(fd, uid, gid, callback) { callback(enosys()); },
fstat(fd, callback) { callback(enosys()); },
fsync(fd, callback) { callback(null); },
ftruncate(fd, length, callback) { callback(enosys()); },
lchown(path, uid, gid, callback) { callback(enosys()); },
link(path, link, callback) { callback(enosys()); },
lstat(path, callback) { callback(enosys()); },
mkdir(path, perm, callback) { callback(enosys()); },
open(path, flags, mode, callback) { callback(enosys()); },
read(fd, buffer, offset, length, position, callback) { callback(enosys()); },
readdir(path, callback) { callback(enosys()); },
readlink(path, callback) { callback(enosys()); },
rename(from, to, callback) { callback(enosys()); },
rmdir(path, callback) { callback(enosys()); },
stat(path, callback) { callback(enosys()); },
symlink(path, link, callback) { callback(enosys()); },
truncate(path, length, callback) { callback(enosys()); },
unlink(path, callback) { callback(enosys()); },
utimes(path, atime, mtime, callback) { callback(enosys()); },
};
}
if (!global.process) {
global.process = {
getuid() { return -1; },
getgid() { return -1; },
geteuid() { return -1; },
getegid() { return -1; },
getgroups() { throw enosys(); },
pid: -1,
ppid: -1,
umask() { throw enosys(); },
cwd() { throw enosys(); },
chdir() { throw enosys(); },
}
}
if (!global.crypto && global.require) {
const nodeCrypto = require("crypto");
global.crypto = {
getRandomValues(b) {
nodeCrypto.randomFillSync(b);
},
};
}
if (!global.crypto) {
throw new Error("global.crypto is not available, polyfill required (getRandomValues only)");
}
if (!global.performance) {
global.performance = {
now() {
const [sec, nsec] = process.hrtime();
return sec * 1000 + nsec / 1000000;
},
};
}
if (!global.TextEncoder && global.require) {
global.TextEncoder = require("util").TextEncoder;
}
if (!global.TextEncoder) {
throw new Error("global.TextEncoder is not available, polyfill required");
}
if (!global.TextDecoder && global.require) {
global.TextDecoder = require("util").TextDecoder;
}
if (!global.TextDecoder) {
throw new Error("global.TextDecoder is not available, polyfill required");
}
// End of polyfills for common API.
const encoder = new TextEncoder("utf-8");
const decoder = new TextDecoder("utf-8");
global.Go = class {
constructor() {
this.argv = ["js"];
this.env = {};
this.exit = (code) => {
if (code !== 0) {
console.warn("exit code:", code);
}
};
this._exitPromise = new Promise((resolve) => {
this._resolveExitPromise = resolve;
});
this._pendingEvent = null;
this._scheduledTimeouts = new Map();
this._nextCallbackTimeoutID = 1;
const setInt64 = (addr, v) => {
this.mem.setUint32(addr + 0, v, true);
this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
}
const getInt64 = (addr) => {
const low = this.mem.getUint32(addr + 0, true);
const high = this.mem.getInt32(addr + 4, true);
return low + high * 4294967296;
}
const loadValue = (addr) => {
const f = this.mem.getFloat64(addr, true);
if (f === 0) {
return undefined;
}
if (!isNaN(f)) {
return f;
}
const id = this.mem.getUint32(addr, true);
return this._values[id];
}
const storeValue = (addr, v) => {
const nanHead = 0x7FF80000;
if (typeof v === "number" && v !== 0) {
if (isNaN(v)) {
this.mem.setUint32(addr + 4, nanHead, true);
this.mem.setUint32(addr, 0, true);
return;
}
this.mem.setFloat64(addr, v, true);
return;
}
if (v === undefined) {
this.mem.setFloat64(addr, 0, true);
return;
}
let id = this._ids.get(v);
if (id === undefined) {
id = this._idPool.pop();
if (id === undefined) {
id = this._values.length;
}
this._values[id] = v;
this._goRefCounts[id] = 0;
this._ids.set(v, id);
}
this._goRefCounts[id]++;
let typeFlag = 0;
switch (typeof v) {
case "object":
if (v !== null) {
typeFlag = 1;
}
break;
case "string":
typeFlag = 2;
break;
case "symbol":
typeFlag = 3;
break;
case "function":
typeFlag = 4;
break;
}
this.mem.setUint32(addr + 4, nanHead | typeFlag, true);
this.mem.setUint32(addr, id, true);
}
const loadSlice = (addr) => {
const array = getInt64(addr + 0);
const len = getInt64(addr + 8);
return new Uint8Array(this._inst.exports.mem.buffer, array, len);
}
const loadSliceOfValues = (addr) => {
const array = getInt64(addr + 0);
const len = getInt64(addr + 8);
const a = new Array(len);
for (let i = 0; i < len; i++) {
a[i] = loadValue(array + i * 8);
}
return a;
}
const loadString = (addr) => {
const saddr = getInt64(addr + 0);
const len = getInt64(addr + 8);
return decoder.decode(new DataView(this._inst.exports.mem.buffer, saddr, len));
}
const timeOrigin = Date.now() - performance.now();
this.importObject = {
go: {
// Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
// may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported
// function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
// This changes the SP, thus we have to update the SP used by the imported function.
// func wasmExit(code int32)
"runtime.wasmExit": (sp) => {
sp >>>= 0;
const code = this.mem.getInt32(sp + 8, true);
this.exited = true;
delete this._inst;
delete this._values;
delete this._goRefCounts;
delete this._ids;
delete this._idPool;
this.exit(code);
},
// func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
"runtime.wasmWrite": (sp) => {
sp >>>= 0;
const fd = getInt64(sp + 8);
const p = getInt64(sp + 16);
const n = this.mem.getInt32(sp + 24, true);
fs.writeSync(fd, new Uint8Array(this._inst.exports.mem.buffer, p, n));
},
// func resetMemoryDataView()
"runtime.resetMemoryDataView": (sp) => {
sp >>>= 0;
this.mem = new DataView(this._inst.exports.mem.buffer);
},
// func nanotime1() int64
"runtime.nanotime1": (sp) => {
sp >>>= 0;
setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
},
// func walltime() (sec int64, nsec int32)
"runtime.walltime": (sp) => {
sp >>>= 0;
const msec = (new Date).getTime();
setInt64(sp + 8, msec / 1000);
this.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true);
},
// func scheduleTimeoutEvent(delay int64) int32
"runtime.scheduleTimeoutEvent": (sp) => {
sp >>>= 0;
const id = this._nextCallbackTimeoutID;
this._nextCallbackTimeoutID++;
this._scheduledTimeouts.set(id, setTimeout(
() => {
this._resume();
while (this._scheduledTimeouts.has(id)) {
// for some reason Go failed to register the timeout event, log and try again
// (temporary workaround for https://github.com/golang/go/issues/28975)
console.warn("scheduleTimeoutEvent: missed timeout event");
this._resume();
}
},
getInt64(sp + 8) + 1, // setTimeout has been seen to fire up to 1 millisecond early
));
this.mem.setInt32(sp + 16, id, true);
},
// func clearTimeoutEvent(id int32)
"runtime.clearTimeoutEvent": (sp) => {
sp >>>= 0;
const id = this.mem.getInt32(sp + 8, true);
clearTimeout(this._scheduledTimeouts.get(id));
this._scheduledTimeouts.delete(id);
},
// func getRandomData(r []byte)
"runtime.getRandomData": (sp) => {
sp >>>= 0;
crypto.getRandomValues(loadSlice(sp + 8));
},
// func finalizeRef(v ref)
"syscall/js.finalizeRef": (sp) => {
sp >>>= 0;
const id = this.mem.getUint32(sp + 8, true);
this._goRefCounts[id]--;
if (this._goRefCounts[id] === 0) {
const v = this._values[id];
this._values[id] = null;
this._ids.delete(v);
this._idPool.push(id);
}
},
// func stringVal(value string) ref
"syscall/js.stringVal": (sp) => {
sp >>>= 0;
storeValue(sp + 24, loadString(sp + 8));
},
// func valueGet(v ref, p string) ref
"syscall/js.valueGet": (sp) => {
sp >>>= 0;
const result = Reflect.get(loadValue(sp + 8), loadString(sp + 16));
sp = this._inst.exports.getsp() >>> 0; // see comment above
storeValue(sp + 32, result);
},
// func valueSet(v ref, p string, x ref)
"syscall/js.valueSet": (sp) => {
sp >>>= 0;
Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));
},
// func valueDelete(v ref, p string)
"syscall/js.valueDelete": (sp) => {
sp >>>= 0;
Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16));
},
// func valueIndex(v ref, i int) ref
"syscall/js.valueIndex": (sp) => {
sp >>>= 0;
storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));
},
// valueSetIndex(v ref, i int, x ref)
"syscall/js.valueSetIndex": (sp) => {
sp >>>= 0;
Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24));
},
// func valueCall(v ref, m string, args []ref) (ref, bool)
"syscall/js.valueCall": (sp) => {
sp >>>= 0;
try {
const v = loadValue(sp + 8);
const m = Reflect.get(v, loadString(sp + 16));
const args = loadSliceOfValues(sp + 32);
const result = Reflect.apply(m, v, args);
sp = this._inst.exports.getsp() >>> 0; // see comment above
storeValue(sp + 56, result);
this.mem.setUint8(sp + 64, 1);
} catch (err) {
sp = this._inst.exports.getsp() >>> 0; // see comment above
storeValue(sp + 56, err);
this.mem.setUint8(sp + 64, 0);
}
},
// func valueInvoke(v ref, args []ref) (ref, bool)
"syscall/js.valueInvoke": (sp) => {
sp >>>= 0;
try {
const v = loadValue(sp + 8);
const args = loadSliceOfValues(sp + 16);
const result = Reflect.apply(v, undefined, args);
sp = this._inst.exports.getsp() >>> 0; // see comment above
storeValue(sp + 40, result);
this.mem.setUint8(sp + 48, 1);
} catch (err) {
sp = this._inst.exports.getsp() >>> 0; // see comment above
storeValue(sp + 40, err);
this.mem.setUint8(sp + 48, 0);
}
},
// func valueNew(v ref, args []ref) (ref, bool)
"syscall/js.valueNew": (sp) => {
sp >>>= 0;
try {
const v = loadValue(sp + 8);
const args = loadSliceOfValues(sp + 16);
const result = Reflect.construct(v, args);
sp = this._inst.exports.getsp() >>> 0; // see comment above
storeValue(sp + 40, result);
this.mem.setUint8(sp + 48, 1);
} catch (err) {
sp = this._inst.exports.getsp() >>> 0; // see comment above
storeValue(sp + 40, err);
this.mem.setUint8(sp + 48, 0);
}
},
// func valueLength(v ref) int
"syscall/js.valueLength": (sp) => {
sp >>>= 0;
setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
},
// valuePrepareString(v ref) (ref, int)
"syscall/js.valuePrepareString": (sp) => {
sp >>>= 0;
const str = encoder.encode(String(loadValue(sp + 8)));
storeValue(sp + 16, str);
setInt64(sp + 24, str.length);
},
// valueLoadString(v ref, b []byte)
"syscall/js.valueLoadString": (sp) => {
sp >>>= 0;
const str = loadValue(sp + 8);
loadSlice(sp + 16).set(str);
},
// func valueInstanceOf(v ref, t ref) bool
"syscall/js.valueInstanceOf": (sp) => {
sp >>>= 0;
this.mem.setUint8(sp + 24, (loadValue(sp + 8) instanceof loadValue(sp + 16)) ? 1 : 0);
},
// func copyBytesToGo(dst []byte, src ref) (int, bool)
"syscall/js.copyBytesToGo": (sp) => {
sp >>>= 0;
const dst = loadSlice(sp + 8);
const src = loadValue(sp + 32);
if (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) {
this.mem.setUint8(sp + 48, 0);
return;
}
const toCopy = src.subarray(0, dst.length);
dst.set(toCopy);
setInt64(sp + 40, toCopy.length);
this.mem.setUint8(sp + 48, 1);
},
// func copyBytesToJS(dst ref, src []byte) (int, bool)
"syscall/js.copyBytesToJS": (sp) => {
sp >>>= 0;
const dst = loadValue(sp + 8);
const src = loadSlice(sp + 16);
if (!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)) {
this.mem.setUint8(sp + 48, 0);
return;
}
const toCopy = src.subarray(0, dst.length);
dst.set(toCopy);
setInt64(sp + 40, toCopy.length);
this.mem.setUint8(sp + 48, 1);
},
"debug": (value) => {
console.log(value);
},
}
};
}
async run(instance) {
if (!(instance instanceof WebAssembly.Instance)) {
throw new Error("Go.run: WebAssembly.Instance expected");
}
this._inst = instance;
this.mem = new DataView(this._inst.exports.mem.buffer);
this._values = [ // JS values that Go currently has references to, indexed by reference id
NaN,
0,
null,
true,
false,
global,
this,
];
this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id
this._ids = new Map([ // mapping from JS values to reference ids
[0, 1],
[null, 2],
[true, 3],
[false, 4],
[global, 5],
[this, 6],
]);
this._idPool = []; // unused ids that have been garbage collected
this.exited = false; // whether the Go program has exited
// Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.
let offset = 4096;
const strPtr = (str) => {
const ptr = offset;
const bytes = encoder.encode(str + "\0");
new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);
offset += bytes.length;
if (offset % 8 !== 0) {
offset += 8 - (offset % 8);
}
return ptr;
};
const argc = this.argv.length;
const argvPtrs = [];
this.argv.forEach((arg) => {
argvPtrs.push(strPtr(arg));
});
argvPtrs.push(0);
const keys = Object.keys(this.env).sort();
keys.forEach((key) => {
argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
});
argvPtrs.push(0);
const argv = offset;
argvPtrs.forEach((ptr) => {
this.mem.setUint32(offset, ptr, true);
this.mem.setUint32(offset + 4, 0, true);
offset += 8;
});
// The linker guarantees global data starts from at least wasmMinDataAddr.
// Keep in sync with cmd/link/internal/ld/data.go:wasmMinDataAddr.
const wasmMinDataAddr = 4096 + 8192;
if (offset >= wasmMinDataAddr) {
throw new Error("total length of command line and environment variables exceeds limit");
}
this._inst.exports.run(argc, argv);
if (this.exited) {
this._resolveExitPromise();
}
await this._exitPromise;
}
_resume() {
if (this.exited) {
throw new Error("Go program has already exited");
}
this._inst.exports.resume();
if (this.exited) {
this._resolveExitPromise();
}
}
_makeFuncWrapper(id) {
const go = this;
return function () {
const event = { id: id, this: this, args: arguments };
go._pendingEvent = event;
go._resume();
return event.result;
};
}
}
if (
typeof module !== "undefined" &&
global.require &&
global.require.main === module &&
global.process &&
global.process.versions &&
!global.process.versions.electron
) {
if (process.argv.length < 3) {
console.error("usage: go_js_wasm_exec [wasm binary] [arguments]");
process.exit(1);
}
const go = new Go();
go.argv = process.argv.slice(2);
go.env = Object.assign({ TMPDIR: require("os").tmpdir() }, process.env);
go.exit = process.exit;
WebAssembly.instantiate(fs.readFileSync(process.argv[2]), go.importObject).then((result) => {
process.on("exit", (code) => { // Node.js exits if no event handler is pending
if (code === 0 && !go.exited) {
// deadlock, make Go print error and stack traces
go._pendingEvent = { id: 0 };
go._resume();
}
});
return go.run(result.instance);
}).catch((err) => {
console.error(err);
process.exit(1);
});
}
})();

28
_examples/Makefile Normal file
View File

@ -0,0 +1,28 @@
# 版权 @2023 凹语言 作者。保留所有权利。
default:
ci-test-all:
@echo "== _examples test begin =="
# loop forever
# cd ./arduino && make
# cd ./arduino-wat && make
cd ./brainfuck && make
cd ./expr && make
cd ./hello && make
cd ./pkg && make
cd ./prime && make
cd ./reftoptr && make
# snake
cd ./snake && make publish
go run ../main.go interface_named.wa
go run ../main.go struct.wa
@echo "== _examples ok =="
clean:

View File

@ -1,7 +1,7 @@
# 版权 @2022 凹语言 作者。保留所有权利。
default:
go run ../../../main.go run -target=arduino hello.wat
go run ../../main.go -target=arduino hello.wat
clean:
-rm *.wasm

View File

@ -1,8 +1,8 @@
default:
go run ../../../main.go run -target=arduino app.wa
go run ../../main.go -target=arduino app.wa
build:
go run ../../../main.go build -target=arduino app.wa
go run ../../main.go build -target=arduino app.wa
wat2wasm a.out.wat -o app.wasm
xxd -i app.wasm > app.wasm.h

View File

@ -2,7 +2,7 @@
import "syscall/arduino"
global LED = arduino.GetPinLED()
var LED = arduino.GetPinLED()
func init {
arduino.PinMode(LED, 1)

View File

@ -0,0 +1,132 @@
unsigned char app_wasm[] = {
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x2a, 0x08, 0x60,
0x01, 0x7f, 0x00, 0x60, 0x02, 0x7f, 0x7f, 0x00, 0x60, 0x00, 0x01, 0x7f,
0x60, 0x01, 0x7f, 0x01, 0x7f, 0x60, 0x04, 0x7f, 0x7f, 0x7f, 0x7f, 0x01,
0x7f, 0x60, 0x01, 0x7f, 0x02, 0x7f, 0x7f, 0x60, 0x00, 0x00, 0x60, 0x03,
0x7f, 0x7f, 0x7f, 0x00, 0x02, 0x6f, 0x06, 0x07, 0x61, 0x72, 0x64, 0x75,
0x69, 0x6e, 0x6f, 0x05, 0x64, 0x65, 0x6c, 0x61, 0x79, 0x00, 0x00, 0x07,
0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x07, 0x70, 0x69, 0x6e, 0x4d,
0x6f, 0x64, 0x65, 0x00, 0x01, 0x07, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e,
0x6f, 0x0c, 0x64, 0x69, 0x67, 0x69, 0x74, 0x61, 0x6c, 0x57, 0x72, 0x69,
0x74, 0x65, 0x00, 0x01, 0x07, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f,
0x06, 0x6d, 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x00, 0x02, 0x07, 0x61, 0x72,
0x64, 0x75, 0x69, 0x6e, 0x6f, 0x09, 0x67, 0x65, 0x74, 0x50, 0x69, 0x6e,
0x4c, 0x45, 0x44, 0x00, 0x02, 0x07, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e,
0x6f, 0x05, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x00, 0x01, 0x03, 0x13, 0x12,
0x03, 0x00, 0x04, 0x05, 0x03, 0x00, 0x06, 0x06, 0x00, 0x00, 0x01, 0x06,
0x06, 0x06, 0x06, 0x07, 0x07, 0x06, 0x04, 0x04, 0x01, 0x70, 0x00, 0x01,
0x05, 0x03, 0x01, 0x00, 0x01, 0x06, 0x43, 0x0d, 0x7f, 0x01, 0x41, 0x80,
0x10, 0x0b, 0x7f, 0x01, 0x41, 0x00, 0x0b, 0x7f, 0x01, 0x41, 0x00, 0x0b,
0x7f, 0x01, 0x41, 0x00, 0x0b, 0x7f, 0x01, 0x41, 0x00, 0x0b, 0x7f, 0x01,
0x41, 0x00, 0x0b, 0x7f, 0x01, 0x41, 0x00, 0x0b, 0x7f, 0x01, 0x41, 0x00,
0x0b, 0x7f, 0x01, 0x41, 0x00, 0x0b, 0x7f, 0x01, 0x41, 0x00, 0x0b, 0x7f,
0x01, 0x41, 0x00, 0x0b, 0x7f, 0x01, 0x41, 0x00, 0x0b, 0x7f, 0x01, 0x41,
0x00, 0x0b, 0x07, 0xdc, 0x01, 0x0c, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72,
0x79, 0x02, 0x00, 0x0c, 0x72, 0x75, 0x6e, 0x74, 0x69, 0x6d, 0x65, 0x2e,
0x69, 0x6e, 0x69, 0x74, 0x00, 0x0d, 0x13, 0x24, 0x72, 0x75, 0x6e, 0x74,
0x69, 0x6d, 0x65, 0x2e, 0x77, 0x61, 0x50, 0x72, 0x69, 0x6e, 0x74, 0x49,
0x33, 0x32, 0x00, 0x0e, 0x14, 0x24, 0x72, 0x75, 0x6e, 0x74, 0x69, 0x6d,
0x65, 0x2e, 0x77, 0x61, 0x50, 0x72, 0x69, 0x6e, 0x74, 0x52, 0x75, 0x6e,
0x65, 0x00, 0x0f, 0x0f, 0x24, 0x72, 0x75, 0x6e, 0x74, 0x69, 0x6d, 0x65,
0x2e, 0x77, 0x61, 0x50, 0x75, 0x74, 0x73, 0x00, 0x10, 0x0f, 0x5f, 0x5f,
0x6d, 0x61, 0x69, 0x6e, 0x5f, 0x5f, 0x2e, 0x69, 0x6e, 0x69, 0x74, 0x23,
0x31, 0x00, 0x11, 0x0d, 0x5f, 0x5f, 0x6d, 0x61, 0x69, 0x6e, 0x5f, 0x5f,
0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x12, 0x0d, 0x5f, 0x5f, 0x6d, 0x61,
0x69, 0x6e, 0x5f, 0x5f, 0x2e, 0x69, 0x6e, 0x69, 0x74, 0x00, 0x13, 0x14,
0x73, 0x79, 0x73, 0x63, 0x61, 0x6c, 0x6c, 0x24, 0x61, 0x72, 0x64, 0x75,
0x69, 0x6e, 0x6f, 0x2e, 0x69, 0x6e, 0x69, 0x74, 0x00, 0x14, 0x17, 0x73,
0x79, 0x73, 0x63, 0x61, 0x6c, 0x6c, 0x24, 0x61, 0x72, 0x64, 0x75, 0x69,
0x6e, 0x6f, 0x2e, 0x50, 0x72, 0x69, 0x6e, 0x74, 0x6c, 0x6e, 0x00, 0x15,
0x15, 0x73, 0x79, 0x73, 0x63, 0x61, 0x6c, 0x6c, 0x24, 0x61, 0x72, 0x64,
0x75, 0x69, 0x6e, 0x6f, 0x2e, 0x50, 0x72, 0x69, 0x6e, 0x74, 0x00, 0x16,
0x06, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x17, 0x0a, 0xcb, 0x07,
0x12, 0x0b, 0x00, 0x23, 0x00, 0x23, 0x00, 0x20, 0x00, 0x6a, 0x24, 0x00,
0x0b, 0x02, 0x00, 0x0b, 0x25, 0x00, 0x20, 0x00, 0x20, 0x00, 0x04, 0x40,
0x20, 0x00, 0x41, 0x01, 0x36, 0x00, 0x00, 0x20, 0x00, 0x20, 0x01, 0x36,
0x00, 0x04, 0x20, 0x00, 0x20, 0x02, 0x36, 0x00, 0x08, 0x20, 0x00, 0x20,
0x03, 0x36, 0x00, 0x0c, 0x0b, 0x0b, 0x06, 0x00, 0x20, 0x00, 0x20, 0x00,
0x0b, 0x16, 0x00, 0x20, 0x00, 0x20, 0x00, 0x04, 0x40, 0x20, 0x00, 0x20,
0x00, 0x28, 0x00, 0x00, 0x41, 0x01, 0x6a, 0x36, 0x00, 0x00, 0x0b, 0x0b,
0x6d, 0x01, 0x05, 0x7f, 0x20, 0x00, 0x41, 0x00, 0x46, 0x04, 0x40, 0x0f,
0x0b, 0x20, 0x00, 0x28, 0x00, 0x00, 0x41, 0x01, 0x6b, 0x21, 0x01, 0x20,
0x01, 0x04, 0x40, 0x20, 0x00, 0x20, 0x01, 0x36, 0x00, 0x00, 0x05, 0x20,
0x00, 0x28, 0x00, 0x08, 0x21, 0x03, 0x20, 0x03, 0x04, 0x40, 0x20, 0x00,
0x28, 0x00, 0x04, 0x21, 0x02, 0x20, 0x02, 0x04, 0x40, 0x20, 0x00, 0x28,
0x00, 0x0c, 0x21, 0x04, 0x20, 0x00, 0x41, 0x10, 0x6a, 0x21, 0x05, 0x03,
0x40, 0x20, 0x05, 0x20, 0x03, 0x11, 0x00, 0x00, 0x20, 0x02, 0x41, 0x01,
0x6b, 0x21, 0x02, 0x20, 0x02, 0x04, 0x40, 0x20, 0x05, 0x20, 0x04, 0x6a,
0x21, 0x05, 0x0c, 0x01, 0x0b, 0x0b, 0x0b, 0x0b, 0x20, 0x00, 0x10, 0x07,
0x0b, 0x0b, 0x2c, 0x00, 0x41, 0x04, 0x10, 0x06, 0x24, 0x03, 0x41, 0x04,
0x10, 0x06, 0x24, 0x04, 0x41, 0x04, 0x10, 0x06, 0x24, 0x05, 0x41, 0x04,
0x10, 0x06, 0x24, 0x07, 0x41, 0x04, 0x10, 0x06, 0x24, 0x08, 0x41, 0x0c,
0x10, 0x06, 0x24, 0x09, 0x41, 0x04, 0x10, 0x06, 0x24, 0x0b, 0x0b, 0x6c,
0x01, 0x03, 0x7f, 0x41, 0x00, 0x21, 0x00, 0x41, 0x00, 0x21, 0x01, 0x41,
0x00, 0x21, 0x02, 0x02, 0x40, 0x03, 0x40, 0x02, 0x40, 0x02, 0x40, 0x02,
0x40, 0x02, 0x40, 0x20, 0x00, 0x0e, 0x03, 0x00, 0x01, 0x02, 0x00, 0x0b,
0x41, 0x00, 0x21, 0x01, 0x23, 0x0a, 0x21, 0x02, 0x20, 0x02, 0x04, 0x40,
0x0c, 0x02, 0x05, 0x0c, 0x01, 0x0b, 0x0b, 0x41, 0x01, 0x21, 0x01, 0x41,
0x01, 0x24, 0x0a, 0x10, 0x14, 0x23, 0x09, 0x41, 0x00, 0x10, 0x0a, 0x23,
0x09, 0x28, 0x00, 0x00, 0x10, 0x0b, 0x36, 0x00, 0x00, 0x23, 0x09, 0x41,
0x08, 0x36, 0x00, 0x04, 0x23, 0x09, 0x41, 0x07, 0x36, 0x00, 0x08, 0x0c,
0x00, 0x0b, 0x41, 0x02, 0x21, 0x01, 0x0c, 0x02, 0x0b, 0x0b, 0x0b, 0x0b,
0x24, 0x01, 0x02, 0x7f, 0x41, 0x00, 0x21, 0x01, 0x41, 0x00, 0x21, 0x02,
0x02, 0x40, 0x03, 0x40, 0x02, 0x40, 0x02, 0x40, 0x20, 0x01, 0x0e, 0x01,
0x00, 0x00, 0x0b, 0x41, 0x00, 0x21, 0x02, 0x0c, 0x02, 0x0b, 0x0b, 0x0b,
0x0b, 0x24, 0x01, 0x02, 0x7f, 0x41, 0x00, 0x21, 0x01, 0x41, 0x00, 0x21,
0x02, 0x02, 0x40, 0x03, 0x40, 0x02, 0x40, 0x02, 0x40, 0x20, 0x01, 0x0e,
0x01, 0x00, 0x00, 0x0b, 0x41, 0x00, 0x21, 0x02, 0x0c, 0x02, 0x0b, 0x0b,
0x0b, 0x0b, 0x2a, 0x01, 0x02, 0x7f, 0x41, 0x00, 0x21, 0x02, 0x41, 0x00,
0x21, 0x03, 0x02, 0x40, 0x03, 0x40, 0x02, 0x40, 0x02, 0x40, 0x20, 0x02,
0x0e, 0x01, 0x00, 0x00, 0x0b, 0x41, 0x00, 0x21, 0x03, 0x20, 0x00, 0x20,
0x01, 0x10, 0x05, 0x0c, 0x02, 0x0b, 0x0b, 0x0b, 0x0b, 0x49, 0x01, 0x03,
0x7f, 0x41, 0x00, 0x21, 0x00, 0x41, 0x00, 0x21, 0x01, 0x41, 0x00, 0x21,
0x02, 0x02, 0x40, 0x03, 0x40, 0x02, 0x40, 0x02, 0x40, 0x20, 0x00, 0x0e,
0x01, 0x00, 0x00, 0x0b, 0x41, 0x00, 0x21, 0x01, 0x23, 0x0b, 0x28, 0x00,
0x00, 0x21, 0x02, 0x20, 0x02, 0x41, 0x01, 0x10, 0x01, 0x41, 0x00, 0x10,
0x0a, 0x41, 0x0f, 0x41, 0x25, 0x10, 0x16, 0x41, 0x00, 0x10, 0x0a, 0x41,
0x34, 0x41, 0x14, 0x10, 0x16, 0x0c, 0x02, 0x0b, 0x0b, 0x0b, 0x0b, 0x74,
0x01, 0x06, 0x7f, 0x41, 0x00, 0x21, 0x00, 0x41, 0x00, 0x21, 0x01, 0x41,
0x00, 0x21, 0x02, 0x41, 0x00, 0x21, 0x03, 0x41, 0x00, 0x21, 0x04, 0x41,
0x00, 0x21, 0x05, 0x02, 0x40, 0x03, 0x40, 0x02, 0x40, 0x02, 0x40, 0x02,
0x40, 0x20, 0x00, 0x0e, 0x02, 0x00, 0x01, 0x00, 0x0b, 0x41, 0x00, 0x21,
0x01, 0x0c, 0x00, 0x0b, 0x41, 0x01, 0x21, 0x01, 0x23, 0x0b, 0x28, 0x00,
0x00, 0x21, 0x02, 0x23, 0x05, 0x28, 0x00, 0x00, 0x21, 0x03, 0x20, 0x02,
0x20, 0x03, 0x10, 0x02, 0x41, 0xe4, 0x00, 0x10, 0x00, 0x23, 0x0b, 0x28,
0x00, 0x00, 0x21, 0x04, 0x23, 0x03, 0x28, 0x00, 0x00, 0x21, 0x05, 0x20,
0x04, 0x20, 0x05, 0x10, 0x02, 0x41, 0x84, 0x07, 0x10, 0x00, 0x41, 0x01,
0x21, 0x00, 0x0c, 0x01, 0x0b, 0x0b, 0x0b, 0x0b, 0x5f, 0x01, 0x04, 0x7f,
0x41, 0x00, 0x21, 0x00, 0x41, 0x00, 0x21, 0x01, 0x41, 0x00, 0x21, 0x02,
0x41, 0x00, 0x21, 0x03, 0x02, 0x40, 0x03, 0x40, 0x02, 0x40, 0x02, 0x40,
0x02, 0x40, 0x02, 0x40, 0x20, 0x00, 0x0e, 0x03, 0x00, 0x01, 0x02, 0x00,
0x0b, 0x41, 0x00, 0x21, 0x01, 0x23, 0x0c, 0x21, 0x02, 0x20, 0x02, 0x04,
0x40, 0x0c, 0x02, 0x05, 0x0c, 0x01, 0x0b, 0x0b, 0x41, 0x01, 0x21, 0x01,
0x41, 0x01, 0x24, 0x0c, 0x10, 0x14, 0x10, 0x04, 0x21, 0x03, 0x23, 0x0b,
0x20, 0x03, 0x36, 0x00, 0x00, 0x10, 0x11, 0x0c, 0x00, 0x0b, 0x41, 0x02,
0x21, 0x01, 0x0c, 0x02, 0x0b, 0x0b, 0x0b, 0x0b, 0x6f, 0x01, 0x03, 0x7f,
0x41, 0x00, 0x21, 0x00, 0x41, 0x00, 0x21, 0x01, 0x41, 0x00, 0x21, 0x02,
0x02, 0x40, 0x03, 0x40, 0x02, 0x40, 0x02, 0x40, 0x02, 0x40, 0x02, 0x40,
0x20, 0x00, 0x0e, 0x03, 0x00, 0x01, 0x02, 0x00, 0x0b, 0x41, 0x00, 0x21,
0x01, 0x23, 0x06, 0x21, 0x02, 0x20, 0x02, 0x04, 0x40, 0x0c, 0x02, 0x05,
0x0c, 0x01, 0x0b, 0x0b, 0x41, 0x01, 0x21, 0x01, 0x41, 0x01, 0x24, 0x06,
0x23, 0x03, 0x41, 0x00, 0x36, 0x00, 0x00, 0x23, 0x05, 0x41, 0x01, 0x36,
0x00, 0x00, 0x23, 0x04, 0x41, 0x00, 0x36, 0x00, 0x00, 0x23, 0x08, 0x41,
0x01, 0x36, 0x00, 0x00, 0x23, 0x07, 0x41, 0x02, 0x36, 0x00, 0x00, 0x0c,
0x00, 0x0b, 0x41, 0x02, 0x21, 0x01, 0x0c, 0x02, 0x0b, 0x0b, 0x0b, 0x0b,
0x32, 0x01, 0x02, 0x7f, 0x41, 0x00, 0x21, 0x03, 0x41, 0x00, 0x21, 0x04,
0x02, 0x40, 0x03, 0x40, 0x02, 0x40, 0x02, 0x40, 0x20, 0x03, 0x0e, 0x01,
0x00, 0x00, 0x0b, 0x41, 0x00, 0x21, 0x04, 0x20, 0x01, 0x20, 0x02, 0x10,
0x10, 0x41, 0x0a, 0x10, 0x0f, 0x0c, 0x02, 0x0b, 0x0b, 0x0b, 0x20, 0x00,
0x10, 0x0b, 0x0b, 0x2e, 0x01, 0x02, 0x7f, 0x41, 0x00, 0x21, 0x03, 0x41,
0x00, 0x21, 0x04, 0x02, 0x40, 0x03, 0x40, 0x02, 0x40, 0x02, 0x40, 0x20,
0x03, 0x0e, 0x01, 0x00, 0x00, 0x0b, 0x41, 0x00, 0x21, 0x04, 0x20, 0x01,
0x20, 0x02, 0x10, 0x10, 0x0c, 0x02, 0x0b, 0x0b, 0x0b, 0x20, 0x00, 0x10,
0x0b, 0x0b, 0x08, 0x00, 0x10, 0x0c, 0x10, 0x13, 0x10, 0x12, 0x0b, 0x0b,
0x4e, 0x01, 0x00, 0x41, 0x00, 0x0b, 0x48, 0x24, 0x24, 0x77, 0x61, 0x64,
0x73, 0x24, 0x24, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0xe5, 0x87,
0xb9, 0xe8, 0xaf, 0xad, 0xe8, 0xa8, 0x80, 0x28, 0x57, 0x61, 0x29, 0x2f,
0x41, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x20, 0x69, 0x73, 0x20, 0x72,
0x75, 0x6e, 0x6e, 0x69, 0x6e, 0x67, 0x20, 0x2e, 0x2e, 0x2e, 0x0a, 0x68,
0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x77, 0x61, 0x2d, 0x6c, 0x61,
0x6e, 0x67, 0x2e, 0x6f, 0x72, 0x67, 0x0a
};
unsigned int app_wasm_len = 1543;

View File

@ -1,7 +1,7 @@
// 版权 @2022 凹语言 作者。保留所有权利。
// Wa/Arduino 源代码位置
// https://gitee.com/wa-lang/wa/tree/master/waroot/examples/arduino
// https://gitee.com/wa-lang/wa/tree/master/_examples/arduino
// 导入 Wasm3 头文件
//

View File

@ -1,6 +1,6 @@
# 版权 @2022 凹语言 作者。保留所有权利。
default:
go run ../../../main.go
go run ../../main.go
clean:

View File

@ -2,17 +2,17 @@
// BF 虚拟机
type BrainFuck struct {
mem: [30000]byte
code: string
pos: int
pc: int
mem :[30000]byte
code :string
pos :int
pc :int
}
func NewBrainFuck(code: string) => *BrainFuck {
return &BrainFuck{code: code}
}
func BrainFuck.Run {
func BrainFuck.Run() {
for ; this.pc != len(this.code); this.pc++ {
switch x := this.code[this.pc]; x {
case '>':

View File

@ -2,7 +2,7 @@
import "brainfuck/bfpkg"
func main {
func main() {
// print hi
const code = "++++++++++[>++++++++++<-]>++++.+."
vm := bfpkg.NewBrainFuck(code)

View File

@ -0,0 +1,14 @@
{
"name": "_examples/brainfuck",
"pkgpath": "brainfuck",
"version": "0.0.1",
"authors": ["author", "author2"],
"description": "module description",
"readme": "README.md",
"homepage": "",
"repository": "",
"license": "",
"license_file": "",
"keywords": [],
"categories": []
}

View File

@ -3,7 +3,7 @@
# go install wa-lang.org/wa@dev-wasi
# WA:=wa
WA:=go run ../../../main.go
WA:=go run ../../main.go
run:
$(WA) run hello.wa

View File

@ -7,9 +7,9 @@ func main {
println("你好,凹语言!", runtime.WAOS)
println(add(40, 2))
fmt.Println(1 + 1)
fmt.Println(1+1)
}
func add(a: i32, b: i32) => i32 {
return a + b
return a+b
}

8
_examples/expr/Makefile Normal file
View File

@ -0,0 +1,8 @@
# 版权 @2023 凹语言 作者。保留所有权利。
default:
go run ../../main.go yacc -l -p=expr -c="copyright.txt" -o="y.wa" expr.y
go run ../../main.go y.wa
clean:
-rm y.* a.out*

8
_examples/expr/run.bat Normal file
View File

@ -0,0 +1,8 @@
:: 版权 @2023 凹语言 作者。保留所有权利。
setlocal
cd %~dp0
go run ../../main.go yacc -l -p=expr -c="copyright.txt" -o="y.wa" expr.y
go run ../../main.go "y.wa"

View File

@ -7,13 +7,13 @@
import "strconv" => __yystrconv__
type exprSymType struct {
yys: int
num: int
yys :int
num :int
}
const NUM = 57346
global exprToknames = [...]string{
var exprToknames = [...]string{
"$end",
"error",
"$unk",
@ -26,7 +26,7 @@ global exprToknames = [...]string{
"NUM",
}
global exprStatenames = [...]string{}
var exprStatenames = [...]string{}
const exprEofCode = 1
const exprErrCode = 2
@ -36,13 +36,13 @@ const exprInitialStackSize = 16
const eof = 0
type exprToken struct {
Kind: int
Value: int
Kind :int
Value :int
}
type exprLexer struct {
tokens: []exprToken
pos: int
tokens :[]exprToken
pos :int
}
func exprLexer.Lex(yylval: *exprSymType) => int {
@ -79,7 +79,7 @@ func main {
})
}
global exprExca = [...]int{
var exprExca = [...]int{
-1, 1,
1, -1,
-2, 0,
@ -89,45 +89,45 @@ const exprPrivate = 57344
const exprLast = 23
global exprAct = [...]int{
var exprAct = [...]int{
7, 4, 5, 2, 21, 9, 6, 8, 12, 13,
9, 1, 8, 16, 3, 19, 20, 17, 18, 14,
15, 10, 11,
}
global exprPact = [...]int{
var exprPact = [...]int{
-3, -1000, -1000, 17, -3, -3, 13, -1000, -1000, -3,
2, 2, -1000, -1000, 2, 2, -5, 13, 13, -1000,
-1000, -1000,
}
global exprPgo = [...]int{
var exprPgo = [...]int{
0, 3, 14, 6, 0, 11,
}
global exprR1 = [...]int{
var exprR1 = [...]int{
0, 5, 1, 1, 1, 2, 2, 2, 3, 3,
3, 4, 4,
}
global exprR2 = [...]int{
var exprR2 = [...]int{
0, 1, 1, 2, 2, 1, 3, 3, 1, 3,
3, 1, 3,
}
global exprChk = [...]int{
var exprChk = [...]int{
-1000, -5, -1, -2, 4, 5, -3, -4, 10, 8,
4, 5, -1, -1, 6, 7, -1, -3, -3, -4,
-4, 9,
}
global exprDef = [...]int{
var exprDef = [...]int{
0, -2, 1, 2, 0, 0, 5, 8, 11, 0,
0, 0, 3, 4, 0, 0, 0, 6, 7, 9,
10, 12,
}
global exprTok1 = [...]int{
var exprTok1 = [...]int{
1, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
@ -135,33 +135,33 @@ global exprTok1 = [...]int{
8, 9, 6, 4, 3, 5, 3, 7,
}
global exprTok2 = [...]int{
var exprTok2 = [...]int{
2, 3, 10,
}
global exprTok3 = [...]int{
var exprTok3 = [...]int{
0,
}
type exprErrorMessageInfo struct {
state: int
token: int
msg: string
state :int
token :int
msg :string
}
global exprErrorMessages = [...]exprErrorMessageInfo{}
var exprErrorMessages = [...]exprErrorMessageInfo{}
/* parser for yacc output */
global (
var (
exprDebug = 0
exprErrorVerbose = false
)
type exprParser struct {
lval: exprSymType
stack: [exprInitialStackSize]exprSymType
char: int
lval :exprSymType
stack :[exprInitialStackSize]exprSymType
char :int
}
func exprParser.Lookahead => int {
@ -300,9 +300,9 @@ func exprParse(exprlex: *exprLexer) => int {
}
func exprParser.Parse(exprlex: *exprLexer) => int {
exprn: int
exprVAL: exprSymType
exprDollar: []exprSymType
var exprn: int
var exprVAL: exprSymType
var exprDollar: []exprSymType
_ = exprDollar // silence set and not used
exprS := this.stack[:]
@ -316,7 +316,7 @@ func exprParser.Parse(exprlex: *exprLexer) => int {
const __goto_exprstack = -1
const __goto_exprnewstate = -2
const __goto_exprdefault = -3
__goto_x := __goto_exprstack
var __goto_x = __goto_exprstack
Loop:
for {

1
_examples/hello/LICENSE Normal file
View File

@ -0,0 +1 @@
版权 @2022 _examples/hello 作者。保留所有权利。

View File

@ -1,10 +1,10 @@
# 版权 @2023 凹语言 作者。保留所有权利。
default:
go run ../../../main.go run .
go run ../../main.go run .
test:
go run ../../../main.go test .
go run ../../main.go test .
clean:
-rm a.out*

View File

@ -1,4 +1,4 @@
# Pkg: hello
# Pkg: _examples/hello
欢迎使用 Wa 语言!

View File

@ -1,12 +1,12 @@
// 版权 @2022 hello 作者。保留所有权利。
// 版权 @2022 _examples/hello 作者。保留所有权利。
import "fmt"
import "3rdparty/pkg"
import "myapp/mymath"
func main {
fmt.Println(40 + 2)
pkg.Println(100 + 2)
fmt.Println(40+2)
pkg.Println(100+2)
println(mymath.I8Max)
println(sum(100))
@ -14,7 +14,7 @@ func main {
}
func sum(n: int) => int {
v: int
var v: int
for i := 1; i <= n; i++ {
v += i
}

View File

@ -1,4 +1,4 @@
// 版权 @2023 hello 作者。保留所有权利。
// 版权 @2023 _examples/hello 作者。保留所有权利。
func TestFailed {
ok := true

View File

@ -1,4 +1,4 @@
// 版权 @2023 hello 作者。保留所有权利。
// 版权 @2023 _examples/hello 作者。保留所有权利。
func init {
// println("zz_test.wa")

View File

@ -0,0 +1,5 @@
// 版权 @2022 _examples/hello 作者。保留所有权利。
func Println(x: int) {
println(x)
}

View File

@ -0,0 +1,14 @@
{
"name": "_examples/hello",
"pkgpath": "myapp",
"version": "0.0.1",
"authors": ["author", "author2"],
"description": "module description",
"readme": "README.md",
"homepage": "",
"repository": "",
"license": "",
"license_file": "LICENSE",
"keywords": [],
"categories": []
}

View File

@ -0,0 +1,89 @@
// 版权 @2021 凹语言 作者。保留所有权利。
type S1 struct {
a: i32
}
type S2 struct {
b: i32
}
type I1 interface {
f()
}
type I2 interface {
f2()
}
func S1.f() {
println("This is S1, this.a==", this.a)
}
func S2.f(){
println("This is S2, this.b==", this.b)
}
func main() {
v1 := S1{a: 13}
var i1: I1 = &v1 //具体类型到具名接口
v1.f() //直接调用
i1.f() //接口调用
doConcreteType(i1)
i1.f()
v2 := S2{b: 42}
i1 = &v2 //具体类型到具名接口
i1.f()
doConcreteType(i1)
i1.f()
var ni: interface{} = &v1 //具体类型到空接口
i1 = ni.(I1) //接口动态互转
i1.f()
ni = &v2 //具体类型到空接口
i1 = ni.(I1) //接口动态互转
i1.f()
var ival: i32 = 777
ni = ival
doConcreteType(ni)
doConcreteType(v1)
doConcreteType(v2)
doConcreteType("你好凹语言")
//i2 := ni.(I2) //接口互转由于v2未实现I2这会触发异常
//i2.f2()
var anoi: interface{ f() } = &v1 //具体类型到匿名接口
doConcreteType(anoi) //匿名接口向具名接口转换
anoi.f()
}
func doConcreteType(i: interface{}) {
//接口到具体类型断言
switch c := i.(type){
case *S1:
println("*S1")
c.a *= 2
case *S2:
println("*S2")
c.b *= 2
case i32:
println("i32: ", c)
case string:
println("string: ", c)
case S1:
println("S1, S1.a==", c.a)
case S2:
println("S2, S2.b==", c.b)
}
}

View File

@ -6,7 +6,7 @@
func write_port(p: i16, v: i8)
// Delay a period of time
global cnt: i8
var cnt: i8
func delay(n: i32) {
for i := i32(0); i < n; i++ {
@ -14,7 +14,7 @@ func delay(n: i32) {
}
}
func main {
func main() {
// Set Pin #5 (connected with the builtin LED) of Port B to output mode.
write_port(0x24, 0x20)
for b := 0; ; b++ {

View File

@ -1,13 +1,13 @@
// Test the llvm backend.
// Test read/write arrays.
func main {
func main() {
test_array(2, 4)
test_array(7, 9)
}
func test_array(a: int, b: int) {
arr: [10][10]int
var arr: [10][10]int
for i := 0; i < 10; i++ {
for j := 0; j < 10; j++ {
arr[i][j] = i * j

View File

@ -5,10 +5,10 @@ const size = 32
type fff32 f32
global arr0: [size]fff32
global arr1: [size]fff32
var arr0: [size]fff32
var arr1: [size]fff32
func test_init {
func test_init() {
for i := 0; i < size; i++ {
arr0[i] = fff32(i * i)
}
@ -22,7 +22,7 @@ func getarr0b(arr: *[size]fff32, i: int) => fff32 {
return arr[i]
}
func main {
func main() {
test_init()
for i := 6; i < 20; i += 2 {
println(getarr0a(arr0, i), ", ", getarr0b(&arr0, i+1))

View File

@ -5,10 +5,10 @@ const size = 32
type fff32 f32
global arr1: [size]fff32
var arr1: [size]fff32
func makearr0a(i: int) => [size]fff32 {
r: [size]fff32
var r: [size]fff32
for j := 0; j < size; j++ {
r[j] = fff32(i * j)
}
@ -22,7 +22,7 @@ func makearr0b(i: int) => *[size]fff32 {
return &arr1
}
func main {
func main() {
r0, r1 := makearr0a(10), makearr0b(20)
for i := 6; i < 20; i += 2 {
println(r0[i], ", ", r1[i])

View File

@ -1,14 +1,14 @@
// Test the llvm backend.
// Test bitwise logic.
global a: i64 = 0x55555555
global b: i64 = 0x33333333
global c: i16 = 0x5555
global d: i16 = 0x3333
global e: u8 = 0x55
global f: u8 = 0x33
var a: i64 = 0x55555555
var b: i64 = 0x33333333
var c: i16 = 0x5555
var d: i16 = 0x3333
var e: u8 = 0x55
var f: u8 = 0x33
func main {
func main() {
println("not ", 0x55555555, " = ", bw_not(0x55555555))
println("not ", 0xaaaaaaaa, " = ", bw_not(0xaaaaaaaa))
println(a, " and ", b, " = ", bw_and(a, b), "(", 0x11111111, ")")

View File

@ -1,7 +1,7 @@
// Test the llvm backend.
// Test comparasions.
func main {
func main() {
test_cmp0(20, 19)
test_cmp0(20, 20)
test_cmp1(-3, -3)

View File

@ -1,7 +1,7 @@
// Test the llvm backend.
// Test integer -> integer convertion.
func main {
func main() {
println(i32(convert_i32_to_i16(65537)))
println(u32(convert_i32_to_u16(65537)))
println(convert_i16_to_i32(100))

View File

@ -5,6 +5,6 @@ func convert_f64_to_f32(a: f64) => f32 {
return f32(a)
}
func main {
func main() {
println(convert_f64_to_f32(3.1415926535))
}

View File

@ -1,7 +1,7 @@
// Test the llvm backend.
// Test integer -> 'floating point' convertion.
func main {
func main() {
println(convert_i16_to_f64(100))
println(convert_i16_to_f64(-100))
println(convert_u16_to_f64(100))

View File

@ -1,7 +1,7 @@
// Test the llvm backend.
// Test 'floating point' -> integer convertion.
func main {
func main() {
println(convert_f64_to_i32(100.001))
println(convert_f64_to_u32(100.001))
println(convert_f64_to_i32(99.99))

View File

@ -1,5 +1,5 @@
// Test the llvm backend.
// Test f32 operations.
// Test float32 operations.
type fff32 f32
@ -19,7 +19,7 @@ func circle_area(r: fff32) => fff32 {
return r * r * 3.1415926
}
func main {
func main() {
println(getPi())
println(getE())
println(minusf32(getPi()))

View File

@ -10,6 +10,6 @@ func test_global_consts() {
}
}
func main {
func main() {
test_global_consts()
}

View File

@ -2,18 +2,18 @@
// Test read/write global variables.
type ty0 struct {
v0: int
v1: f64
v0 :int
v1 :float64
}
global gv0: ty0 = ty0{5555, 3.1415926}
global gv1: [4]int = [4]int{81, 82, 17, 76}
var gv0: ty0 = ty0{5555, 3.1415926}
var gv1: [4]int = [4]int{81, 82, 17, 76}
func test_global_variable() {
println("gv0: {", gv0.v0, ", ", gv0.v1, "}")
println("gv1: {", gv1[0], ", ", gv1[1], ", ", gv1[2], ", ", gv1[3], "}")
}
func main {
func main() {
test_global_variable()
}

View File

@ -2,12 +2,12 @@
// Test read/write global variables.
type ty0 struct {
v0: int
v1: f64
v0 :int
v1 :float64
}
global gv0: ty0 = ty0{5555, 3.1415926}
global gv1: [4]int = [4]int{81, 82, 17, 76}
var gv0: ty0 = ty0{5555, 3.1415926}
var gv1: [4]int = [4]int{81, 82, 17, 76}
func test_global_variable() {
gv0.v0 = 8888
@ -20,6 +20,6 @@ func test_global_variable() {
println("gv1: {", gv1[0], ", ", gv1[1], ", ", gv1[2], ", ", gv1[3], "}")
}
func main {
func main() {
test_global_variable()
}

View File

@ -1,6 +1,6 @@
// Test the llvm backend.
func main {
func main() {
a := 0.0
for y := 1.5; y > -1.5; y = y - 0.2 {
for x := -1.5; x < 1.5; x = x + 0.1 {

View File

@ -2,19 +2,19 @@
// Test anonymous functions and closure functions.
type pair struct {
i: f32
j: f32
i :f32
j :f32
}
func main {
func main() {
print("Hello, ")
func() {
println("World!")
}()
i: int = 31
j: [4]f32 = [4]f32{1, 2.2, 5.5, 9.8}
k: pair = pair{3.14, 2.718}
var i: int = 31
var j: [4]f32 = [4]f32{1, 2.2, 5.5, 9.8}
var k: pair = pair{3.14, 2.718}
show := func(q: int) {
println(i, " + ", q, " = ", i+q)
println("{", j[0], ", ", j[1], ", ", j[2], ", ", j[3], "}")

View File

@ -1,7 +1,7 @@
// Test the llvm backend.
// Test loops.
func main {
func main() {
test_loop(10)
}

View File

@ -1,7 +1,7 @@
// Test the llvm backend.
// Test loops.
func main {
func main() {
test_loop(10, 10)
}

View File

@ -2,8 +2,8 @@
// Test multiple return values.
type pair struct {
a: f32
b: f32
a :f32
b :f32
}
func calc() => ([4]f32, pair, f32) {
@ -12,7 +12,7 @@ func calc() => ([4]f32, pair, f32) {
return a, b, 2.4 + 1.3
}
func main {
func main() {
a, b, c := calc()
println("{", a[0], ", ", a[1], ", ", a[2], ", ", a[3], "}")
println("{", b.a, ", ", b.b, "}")

View File

@ -1,8 +1,8 @@
// Test the llvm backend.
// Test read/write via a pointer.
global gint0: int = 100
global gint1: int = 200
var gint0: int = 100
var gint1: int = 200
func get_int_addr(a: int) => *int {
if a > 0 {
@ -23,7 +23,7 @@ func test_pointer(a: int) {
println(gint0, ", ", gint1)
}
func main {
func main() {
test_pointer(1)
test_pointer(-1)
}

View File

@ -1,8 +1,8 @@
// Test the llvm backend.
func main {
func main() {
for n := 2; n <= 30; n = n + 1 {
isPrime: int = 1
var isPrime: int = 1
for i := 2; i*i <= n; i = i + 1 {
if x := n % i; x == 0 {
isPrime = 0

View File

@ -1,6 +1,6 @@
// Test the llvm backend.
// Test printing strings.
func main {
func main() {
println("您好,凹语言!\n\"Hello, wa-lang!\"")
}

View File

@ -1,7 +1,7 @@
// Test the llvm backend.
// Test printing integers.
func main {
func main() {
println(test_calc1(6, 9, 8), ", ", 5, ", ", test_calc5(6, 9, 8))
}

View File

@ -1,7 +1,7 @@
// Test the llvm backend.
// Test printing f64 values.
// Test printing float64 values.
func main {
func main() {
println(5.44, ", ", test_calc3(6, 9, 8))
}

View File

@ -1,7 +1,7 @@
// Test the llvm backend.
// Test printing small type values.
func main {
func main() {
test_print(-100, 100, -100, 100, 5)
}

View File

@ -1,13 +1,13 @@
// Test the llvm backend.
// Test logical/arithmetic shift operations.
global ga: i32 = -15
global gb: i32 = 15
global gc: i64 = -15
global gd: i64 = 15
global ge: u16 = 0xffcc
var ga: i32 = -15
var gb: i32 = 15
var gc: i64 = -15
var gd: i64 = 15
var ge: u16 = 0xffcc
func main {
func main() {
println(ga, " << ", 2, " = ", test_shl_0(ga, 2))
println(gb, " << ", 2, " = ", test_shl_1(gb, 2))
println(ga, " << ", 2, " = ", test_shl_2(ga, 2))

View File

@ -1,7 +1,7 @@
// Test the llvm backend.
// Test read/write structs.
func main {
func main() {
test_struct(10)
test_struct(11)
test_struct(2)
@ -10,10 +10,10 @@ func main {
}
type t0 struct {
a: i64
b: i32
c: [4]f64
d: u32
a :int64
b :int32
c :[4]f64
d :uint32
}
func test_struct(a: int) {

View File

@ -1,7 +1,7 @@
// Test the llvm backend.
// Test read/write structs.
func main {
func main() {
test_struct1(10, 0, 0)
test_struct1(11, 1, 0)
test_struct1(2, 0, 1)
@ -10,10 +10,10 @@ func main {
}
type t0 struct {
a: i64
b: i32
c: [4]f64
d: u32
a :int64
b :int32
c :[4]f64
d :uint32
}
func test_struct1(a: int, b: int, c: int) {

View File

@ -6,11 +6,11 @@ type fff32 f32
const size = 16
type struct_t0 struct {
arr0: [size]int
arr1: [size]fff32
arr0 :[size]int
arr1 :[size]fff32
}
global st0: struct_t0
var st0: struct_t0
func test_init() {
for i := 0; i < size; i++ {
@ -27,7 +27,7 @@ func getst0f32(st0: *struct_t0, i: int) => fff32 {
return st0.arr1[i]
}
func main {
func main() {
test_init()
for i := 6; i < 14; i += 2 {
println(getst0int(st0, i), ", ", getst0f32(&st0, i+1))

View File

@ -4,14 +4,14 @@
const size = 16
type struct_t0 struct {
arr0: [size]int
arr1: [size]f32
arr0 :[size]int
arr1 :[size]f32
}
global st1: struct_t0
var st1: struct_t0
func maket0a(i: int) => struct_t0 {
r: struct_t0
var r: struct_t0
for j := 0; j < 16; j++ {
r.arr0[j] = 100 - j - i
r.arr1[j] = 200.0 - f32(j+i)
@ -27,7 +27,7 @@ func maket0b(i: int) => *struct_t0 {
return &st1
}
func main {
func main() {
r0, r1 := maket0a(20), maket0b(30)
for i := 0; i < 16; i++ {
println(r0.arr0[i], ", ", r0.arr1[i], " : ", r1.arr0[i], ", ", r1.arr1[i])

View File

@ -1,7 +1,7 @@
// Test the llvm backend.
// Test unconditional jump.
func main {
func main() {
test_jmp(4)
}

View File

@ -1,14 +1,16 @@
// 版权 @2021 凹语言 作者。保留所有权利。
global I: [8]i32
package main
var I [8]i32
func Gen_arr(param: [8]i32) => [8]i32 {
param[3] += 2
return param
}
func main {
j: [8]i32
func main() {
var j [8]i32
j[3] = 40
I = Gen_arr(j)

18
_examples/misc/array_1.wa Normal file
View File

@ -0,0 +1,18 @@
// 版权 @2022 凹语言 作者。保留所有权利。
func main {
test_array(2, 4)
test_array(7, 9)
}
func test_array(a: int, b: int) {
var arr [10][10]int
for i := 0; i < 10; i++ {
for j := 0; j < 10; j++ {
arr[i][j] = i * j
}
}
println("arr[", a, "][", b, "] = ", arr[a][b])
println("arr[", b, "][", a, "] = ", arr[b][a])
}

View File

@ -1,25 +1,25 @@
// 版权 @2021 凹语言 作者。保留所有权利。
type FP func(i: i32) => i32
type FP func (i: i32) => i32
type ST struct {
i: i32
}
func ST.meth_p(p: i32) => i32 {
this.i += p
return this.i
func (t *ST) meth_p(p: i32) => i32 {
t.i += p
return t.i
}
func ST.meth_v(p: i32) => i32 {
this.i += p
return this.i
func (t ST) meth_v(p: i32) => i32 {
t.i += p
return t.i
}
global g_f: FP
var g_f: FP
func main {
o: ST
func main() {
var o ST
o.i = 11
g_f = o.meth_p
println(g_f(11))
@ -27,20 +27,20 @@ func main {
g_f = o.meth_v
println(g_f(11))
println(o.i)
n := i32(21)
g_f = func(i: i32) => i32 {
g_f = func(i: i32) => i32{
n += i
return n
}
println(g_f(22))
println(n)
func(i: i32) {
n += i
}(22)
println(n)
g_f = Double
println(g_f(13))
}

View File

@ -1,16 +1,17 @@
// 版权 @2021 凹语言 作者。保留所有权利。
global counter: i32 = 13
global G: *i32
package main
func gen_i() => *i32 {
i: i32
var counter i32 = 13
var G *i32
func gen_i() => *i32{
var i i32
i = counter
counter++
return &i
}
func main {
func main() {
G = gen_i()
println(*G)
G = gen_i()

View File

@ -1,8 +1,8 @@
// 版权 @2021 凹语言 作者。保留所有权利。
func main {
i := ST{i: 1, j: 2}
j := ST{i: 3, j: 4}
func main() {
i := ST{i: 1, j:2}
j := ST{i: 3, j:4}
i, j = Swap(i, j)
println(i.i)
println(i.j)
@ -10,10 +10,10 @@ func main {
println(j.j)
}
func Swap(i, j: ST) => (ST, ST) {
func Swap(i, j: ST) => (ST, ST){
return j, i
}
type ST struct {
type ST struct{
i, j: i32
}
}

View File

@ -0,0 +1,428 @@
// 版权 @2022 凹语言 作者。保留所有权利。
func main {
println("您好,凹语言!\nHello, wa-lang!")
println("幸运整数:", test_calc1(6, 9, 8), ", ", 5, ", ", test_calc5(6, 9, 8))
println("幸运浮点数:", 5.44, ", ", test_calc3(6, 9, 8))
test_print(-100, 100, -100, 100, 5)
test_cmp0(20, 19)
test_cmp0(20, 20)
test_cmp1(-3, -3)
test_cmp1(-3, -4)
test_cmp2(10, 10)
test_cmp2(10, 10.0001)
test_jmp(4)
test_loop0(10)
test_loop1(10, 10)
test_array(2, 4)
test_array(7, 9)
test_struct0(10)
test_struct0(11)
test_struct0(2)
test_struct0(13)
test_struct0(0)
test_struct1(10, 0, 0)
test_struct1(11, 1, 0)
test_struct1(2, 0, 1)
test_struct1(13, 1, 0)
test_struct1(2, 1, 0)
test_convert0()
test_convert1()
test_convert2()
test_convert3()
test_global_consts()
test_global_variables_0()
test_global_variables_1()
test_pointer(1)
test_pointer(-1)
test_f32()
test_struct2()
test_struct3()
test_struct4()
test_array2()
test_array3()
test_array4()
bye()
}
func bye() {
println("再见!\nbye!")
}
func test_calc0(a i32, b i32, c i32) i32 {
return (-a) * 5 / (b - c + 2)
}
func test_calc1(a i32, b i32, c i32) i32 {
return test_calc0(a, b, c)
}
func test_calc2(a f64, b f64, c f64) f64 {
return (-a) * 5 / (b - c + 2)
}
func test_calc3(a f64, b f64, c f64) f64 {
return test_calc2(a, b, c)
}
func test_calc4(a u16, b u16, c u16) u16 {
return (-a) * 5 / (b - c + 2)
}
func test_calc5(a u16, b u16, c u16) u16 {
return test_calc4(-a, b, c)
}
func test_cmp0(a u16, b u16) {
if a > b {
println(a, " > ", b)
} else {
println(a, " <= ", b)
}
}
func test_cmp1(a i64, b i64) {
if a <= b {
println(a, " <= ", b)
} else {
println(a, " > ", b)
}
}
func test_cmp2(a f64, b f64) {
if a != b {
println(a, " != ", b)
} else {
println(a, " == ", b)
}
}
func test_jmp(a i32) {
for {
if a > 10 {
return
}
println(a)
a++
}
}
func test_loop0(a i16) {
for i := i16(0); i < a; i++ {
println(i, " * ", i, " = ", i * i)
}
}
func test_loop1(a u64, b u64) {
for i := u64(0); i < a; i++ {
for j := u64(0); j < b; j++ {
println(i, " * ", j, " = ", i * j)
}
}
}
func test_array(a int, b int) {
var arr [10][10]int
for i := 0; i < 10; i++ {
for j := 0; j < 10; j++ {
arr[i][j] = i * j
}
}
println("arr[", a, "][", b, "] = ", arr[a][b])
println("arr[", b, "][", a, "] = ", arr[b][a])
}
type t0 struct {
a int64
b int32
c [4]f64
d uint32
}
func test_struct0(a int) {
s0 := t0{17, -668, [4]f64{2.71828, 0.717, 3.14159}, 234}
switch a {
case 10:
println(s0.a)
case 11:
println(s0.b)
case 13:
println(s0.d)
default:
println(s0.c[a])
}
}
func test_struct1(a int, b int, c int) {
s0 := [2]t0{{11, -12, [4]f64{0.1, -0.2}, 13}, {21, -22, [4]f64{1.1, -1.2}, 24}}
switch a {
case 10:
println(s0[b].a)
case 11:
println(s0[b].b)
case 13:
println(s0[b].d)
default:
println(s0[b].c[c])
}
}
type struct_t0 struct {
arr0 [16]int
arr1 [16]f32
}
var st0 struct_t0
var st1 struct_t0
func test_struct2() {
for i := 0; i < 16; i++ {
st0.arr0[i] = i * i
st0.arr1[i] = f32(i) * 2.0
}
}
func getst0int(st0 struct_t0, i int) int {
return st0.arr0[i]
}
func getst0f32(st0 *struct_t0, i int) f32 {
return st0.arr1[i]
}
func test_struct3() {
for i := 6; i < 14; i += 2 {
println(getst0int(st0, i), ", ", getst0f32(&st0, i + 1))
}
}
func maket0a(i int) struct_t0 {
var r struct_t0
for j := 0; j < 16; j++ {
r.arr0[j] = 100 - j - i
r.arr1[j] = 200.0 - f32(j + i)
}
return r
}
func maket0b(i int) *struct_t0 {
for j := 0; j < 16; j++ {
st1.arr0[j] = 100 - j - i
st1.arr1[j] = 200.0 - f32(j + i)
}
return &st1
}
func test_struct4() {
r0, r1 := maket0a(20), maket0b(30)
for i := 0; i < 16; i++ {
println(r0.arr0[i], ", ", r0.arr1[i], " : ", r1.arr0[i], ", ", r1.arr1[i])
}
}
type fff32 f32
var arr0 [32]fff32
var arr1 [32]fff32
func test_array2() {
for i := 0; i < 32; i++ {
arr0[i] = fff32(i * i)
}
}
func getarr0a(arr [32]fff32, i int) fff32 {
return arr[i]
}
func getarr0b(arr *[32]fff32, i int) fff32 {
return arr[i]
}
func makearr0a(i int) [32]fff32 {
var r [32]fff32
for j := 0; j < 32; j++ {
r[j] = fff32(i * j)
}
return r
}
func makearr0b(i int) *[32]fff32 {
for j := 0; j < 32; j++ {
arr1[j] = 2000 - fff32(i * j)
}
return &arr1
}
func test_array3() {
for i := 6; i < 20; i += 2 {
println(getarr0a(arr0, i), ", ", getarr0b(&arr0, i + 1))
}
}
func test_array4() {
r0, r1 := makearr0a(10), makearr0b(20)
for i := 6; i < 20; i += 2 {
println(r0[i], ", ", r1[i])
}
}
func convert_i32_to_i16(a i32) i16 {
return i16(a)
}
func convert_i32_to_u16(a i32) u16 {
return u16(a)
}
func convert_i16_to_i32(a i16) i32 {
return i32(a)
}
func convert_u16_to_i32(a u16) i32 {
return i32(a)
}
func test_convert0() {
println(i32(convert_i32_to_i16(65537)))
println(u32(convert_i32_to_u16(65537)))
println(convert_i16_to_i32(100))
println(convert_i16_to_i32(-100))
println(convert_u16_to_i32(100))
println(convert_u16_to_i32(65530))
}
func convert_f64_to_f32(a f64) f32 {
return f32(a)
}
func test_convert1() {
println(f64(convert_f64_to_f32(3.1415926535)))
}
func convert_i16_to_f64(a i16) f64 {
return f64(a)
}
func convert_u16_to_f64(a u16) f64 {
return f64(a)
}
func test_convert2() {
println(convert_i16_to_f64(100))
println(convert_i16_to_f64(-100))
println(convert_u16_to_f64(100))
println(convert_u16_to_f64(65530))
}
func convert_f64_to_i32(a f64) i32 {
return i32(a)
}
func convert_f64_to_u32(a f64) u32 {
return u32(a)
}
func test_convert3() {
println(convert_f64_to_i32(100.001))
println(convert_f64_to_u32(100.001))
println(convert_f64_to_i32(99.99))
println(convert_f64_to_u32(99.99))
println(convert_f64_to_i32(-100.001))
println(convert_f64_to_u32(-100.001))
}
func test_print(a i16, b u16, c i8, d u8, e f32) {
println(a, ", ", b, ", ", c, ", ", d, ", ", e/2)
}
const gbegin = 10
const gend = 20
func test_global_consts() {
for i := gbegin; i < gend; i++ {
println(i, " - ", gbegin, " = ", i - gbegin)
}
}
type ty0 struct {
v0 int
v1 float64
}
var gv0 ty0 = ty0{5555, 3.1415926}
var gv1 [4]int = [4]int{81, 82, 17, 76}
func test_global_variables_0() {
println("gv0: {", gv0.v0, ", ", gv0.v1, "}")
println("gv1: {", gv1[0], ", ", gv1[1], ", ", gv1[2], ", ", gv1[3], "}")
}
func test_global_variables_1() {
gv0.v0 = 8888
gv0.v1 = 2.71828
gv1[0] += 2
gv1[1] += 3
gv1[2] += 4
gv1[3] += 5
println("gv0: {", gv0.v0, ", ", gv0.v1, "}")
println("gv1: {", gv1[0], ", ", gv1[1], ", ", gv1[2], ", ", gv1[3], "}")
}
var gint0 int = 100
var gint1 int = 200
func get_int_addr(a int) *int {
if a > 0 {
return &gint0
} else {
return &gint1
}
}
func set_int(a *int) {
*a += 10
}
func test_pointer(a int) {
println(gint0, ", ", gint1)
p := get_int_addr(a)
set_int(p)
println(gint0, ", ", gint1)
}
func getPi() f32 {
return 3.1415926535
}
func getE() f32 {
return 2.71828
}
func minusf32(a f32) f32 {
return 0 - a
}
func circle_area(r f32) f32 {
return r * r * 3.1415926
}
func test_f32() {
println(getPi())
println(getE())
println(minusf32(getPi()))
println(minusf32(-1.3))
println(circle_area(1.1))
println(circle_area(getE()))
}

View File

@ -1,8 +1,8 @@
// 版权 @2019 凹语言 作者。保留所有权利。
func main {
func main() {
for n := 2; n <= 30; n = n + 1 {
isPrime: int = 1
var isPrime int = 1
for i := 2; i*i <= n; i = i + 1 {
if x := n % i; x == 0 {
isPrime = 0

View File

@ -1,6 +1,6 @@
// 版权 @2021 凹语言 作者。保留所有权利。
func main {
func main() {
p := new_int()
println(*p)
proc(p)
@ -8,7 +8,7 @@ func main {
}
func new_int() => *i32 {
i: i32
var i: i32
i = 42
return &i
}

View File

@ -1,6 +1,6 @@
// 版权 @2021 凹语言 作者。保留所有权利。
func main {
func main() {
a := [4]i32{12, 34, 56, 78}
s := a[0:1]
s[0] = 42
@ -9,7 +9,7 @@ func main {
println(a[1]) # 13
println(len(s)) # 2
k: []i32
var k: []i32
k = append(k, 99, 81, 170)
println(k[0]) # 99
@ -22,4 +22,4 @@ func main {
l[0] = 111
println(l[0]) #111
println(k[0]) #99
}
}

View File

@ -1,13 +1,14 @@
func gen_str() => string {
return "123456"
}
func main {
func main() {
println("你好,凹语言!")
s1 := gen_str()
s2 := s1[0:3]
s2 = s2 + "abc"
println(s1)
println(s2)
println(s2)
}

View File

@ -1,6 +1,6 @@
// 版权 @2021 凹语言 作者。保留所有权利。
func main {
func main() {
i := gen_scref()
println(*i.a)
println(i.b)
@ -20,8 +20,8 @@ type sc struct {
}
func gen_scref() => *sc {
v: sc
i: i32
var v: sc
var i: i32
i = 13
v.a = &i
v.b = 42
@ -29,8 +29,8 @@ func gen_scref() => *sc {
}
func gen_sc() => sc {
i: i32 = 26
v: sc
var i: i32 = 26
var v: sc
v.a = &i
v.b = 58
return v

1
_examples/pkg/LICENSE Normal file
View File

@ -0,0 +1 @@
版权 @2022 _examples/hello 作者。保留所有权利。

View File

@ -1,10 +1,10 @@
# 版权 @2023 凹语言 作者。保留所有权利。
default:
go run ../../../main.go run .
go run ../../main.go run .
test:
go run ../../../main.go test .
go run ../../main.go test .
clean:
-rm a.out*

View File

@ -1,4 +1,4 @@
# Pkg: hello
# Pkg: _examples/hello
欢迎使用 Wa 语言!

View File

@ -1,4 +1,4 @@
// 版权 @2022 hello 作者。保留所有权利。
// 版权 @2022 _examples/hello 作者。保留所有权利。
import (
"pkg/mypkg"
@ -9,7 +9,7 @@ type ST struct {
B: i32
}
global G: ST
var G: ST
func Do {
G.A = "13"
@ -24,7 +24,7 @@ func main {
Do()
println(G.A)
println(G.B)
println(mypkg.G.A)
println(mypkg.G.B)
}

View File

@ -1,11 +1,11 @@
// 版权 @2022 hello 作者。保留所有权利。
// 版权 @2022 _examples/hello 作者。保留所有权利。
type ST struct {
A: i32
B: string
}
global G: ST
var G: ST
func Do(x: i32) {
G.A = x

View File

@ -0,0 +1,5 @@
// 版权 @2022 _examples/hello 作者。保留所有权利。
func Println(x int) {
println(x)
}

14
_examples/pkg/wa.mod.json Normal file
View File

@ -0,0 +1,14 @@
{
"name": "_examples/hello",
"pkgpath": "pkg",
"version": "0.0.1",
"authors": ["author", "author2"],
"description": "module description",
"readme": "README.md",
"homepage": "",
"repository": "",
"license": "",
"license_file": "LICENSE",
"keywords": [],
"categories": []
}

View File

@ -1,10 +1,10 @@
# 版权 @2023 凹语言 作者。保留所有权利。
default:
go run ../../../main.go run .
go run ../../main.go run .
test:
go run ../../../main.go test .
go run ../../main.go test .
clean:
-rm a.out*

Some files were not shown because too many files have changed in this diff Show More