Skip to content

Commit

Permalink
z z
Browse files Browse the repository at this point in the history
  • Loading branch information
chai2010 committed Jul 28, 2023
1 parent bcd4068 commit e4aaef3
Show file tree
Hide file tree
Showing 15 changed files with 387 additions and 33 deletions.
8 changes: 5 additions & 3 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
[前言](preface.md)

* [安装及入门](chs/1.安装及入门/readme.md)
- [安装](chs/1.安装及入门/1.1.安装.md)
- [你好,世界](chs/1.安装及入门/1.2.你好世界.md)
- [你好,凹语言](chs/1.安装及入门/1.1.你好凹语言.md)
- [安装凹语言](chs/1.安装及入门/1.2.安装凹语言.md)
- [IDE插件](chs/1.安装及入门/1.3.IDE插件.md)
- [目录结构](chs/1.安装及入门/1.4.目录结构.md)
* [程序结构](chs/2.程序结构/readme.md)
- [全局声明](chs/2.程序结构/2.1.全局声明.md)
- [全局变量声明](chs/2.程序结构/2.2.全局变量声明.md)
Expand Down Expand Up @@ -37,4 +39,4 @@
- [空接口-万能封包器(TODO)](chs/7.接口/7.3.空接口-万能封包器.md)
- [接口类型断言总结(TODO)](chs/7.接口/7.4.接口类型断言总结.md)
* [与宿主环境交互(TODO)](chs/8.与宿主环境交互/readme.md)

* [附录](chs/9.附录/readme.md)
46 changes: 46 additions & 0 deletions chs/1.安装及入门/1.1.你好凹语言.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
## 1.1. 你好,凹语言

打印“hello world”是C语言之后的惯用案例,凹语言例子打印的是中文“你好,凹语言!”。

### 1.1.1 你好,凹语言!

创建 hello.wa 文件,内容如下:

```wa
// 版权 @2019 凹语言 作者。保留所有权利。
import "fmt"
import "runtime"
global year: i32 = 2023
func main {
println("你好,凹语言!", runtime.WAOS)
println(add(40, 2), year)
fmt.Println("1+1 =", 1+1)
}
func add(a: i32, b: i32) => i32 {
return a+b
}
```

其中`//`开始的是行注释,`import`关键字导入了2个包准库的包,`global`关键字定义了一个全局变量,并给了2023的初始值。`func`关键字定义了`main`函数和`add`函数。`main`函数是程序的入口,其中通过内置的`println`函数打印了“你好,凹语言!”,同时使用`fmt`包的`Println`字符串和整数表达式的结果。在`main`函数还使用了全局的`year`变量,此外还调用了`add`函数并打印了返回值。`add`函数有2个输入参数和一个返回值。

如果在本地已经安装有凹语言的`wa`命令,可以输入以下命令执行:

```
$ wa run hello.wa
你好,凹语言! wasi
42
1+1 = 2
```

### 1.1.2 在线的 Playground

凹语言是面向 WebAssembly 设计的通用编程语言,从诞生起就将浏览器作为第一支持目标。可以通过 [https://wa-lang.org/playground](https://wa-lang.org/playground) 访问 Playground,界面如下:

![](./images/playground-01.png)

点击“RUN”按钮,可以看到输出结果。
25 changes: 0 additions & 25 deletions chs/1.安装及入门/1.2.你好世界.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
## 1.1. 安装
## 1.2. 安装凹语言

凹语言支持主流的64位平台:linux/amd64、macOS/amd64、macOS/arm64、windows/amd64。有二进制和源代码安装2种形式
除了访问在线的凹语言 Playground,也可以在本地安装凹语言

### 1.1.1 二进制安装
### 1.2.1 二进制安装

从 Github 下载最新的二进制文件:https://github.com/wa-lang/wa/releases 。需要确保和本地平台对应,比如 v0.8.0 对应 macOS/amd64 平台下载的是 wa_0.8.0_darwin_amd64.tar.gz。

Expand Down Expand Up @@ -58,7 +58,7 @@ See "https://wa-lang.org" for more information.

到此安装工作完成。

### 1.1.2 从源码安装
### 1.2.2 从源码安装

本地要求安装 Go1.17+ 版本,然后执行以下命令安装最新的 `wa` 命令:

Expand All @@ -72,3 +72,28 @@ go install wa-lang.org/wa
第一次执行 `wa` 命令时会在命令同一个目录下生成一个 `wa.wat2wasm.exe` 命令。

到此安装工作完成。

### 1.2.3 Homebrew (MacOS & Linux)

对于 macOS 和 Linux 系统也可以通过 Homebrew 安装:

```
brew install wa-lang/tap/wa
```

### 1.2.4 Scoop (Windows)

对于 Windows 系统也可以通过 Scoop 安装:

```
scoop bucket add wa-lang https://github.com/wa-lang/scoop-bucket.git
scoop install wa-lang/wa
```

### 1.2.5 本地 Playground

安装成功之后,在命令行输入 `wa play` 可以打开本地 Playground。程序会默认打开浏览器页面,也可以输入 [http://localhost:2023/](http://localhost:2023/) 地址访问。效果如下:

![](./images/playground-local-01.png)

点击“执行”按钮,可以看到输出结果。
27 changes: 27 additions & 0 deletions chs/1.安装及入门/1.3.IDE插件.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## 1.3. IDE插件

现代化的编程语言一般都会为各种IDE和编辑器提供扩展,以提高编程的体验。凹语言为 VS Code、Fleet 和 Vim 提供了基本插件支持。

### 1.3.1 VS Code 插件

在 VS Code 扩展商店检索 “wa” 即可以查到凹语言插件。安装之后会有基本的语法高亮等功能。

VS Code 效果如下:

![](./images/vscode.png)

### 1.3.2 Fleet 插件

Fleet 插件仓库:[https://github.com/wa-lang/fleet-wa](https://github.com/wa-lang/fleet-wa)

根据仓库提示安装,效果如下:

![](./images/fleet-wa-screenshot.png)

### 1.3.3 Vim 插件

Fleet 插件仓库:[https://github.com/wa-lang/vim-wa](https://github.com/wa-lang/vim-wa)

根据仓库提示安装,效果如下:

![](./images/vim-wa-screenshot.png)
33 changes: 33 additions & 0 deletions chs/1.安装及入门/1.4.目录结构.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## 1.4. 目录结构

凹语言程序以包来组织代码,包可以是一个单文件,包也可以是一个目录。凹语言自带的 `waroot/examples/hello` 案例的是一个更为完整的工程,其目录结构如下:

```
examples/hello/
├── LICENSE
├── README.md
├── src
│ ├── main.wa
│ └── mymath
│ └── math.wa
├── vendor
│ └── 3rdparty
│ └── pkg
│ └── pkg.wa
└── wa.mod.json
```

除了版权文件、说明文件外,最重要的是 `wa.mod.json` 包工程文件,其定义了当前应用的包路径。此外 src 目录下的是当前包路径下的代码,是默认的 main 入口包。

`wa.mod.json` 文件内容如下:

```json
{
"name": "hello",
"pkgpath": "myapp",
"version": "0.0.1"
}
```

其中 pkgpath 表示当前包的路径,从而可以推导出 mymath 子目录对应的包路径为 `"myapp/mymath"`。vendor 目录是依赖的第三方代码,其中 `vendor/3rdparty/pkg` 对应的包路径为 `"3rdparty/pkg"`

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added chs/1.安装及入门/images/playground-01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added chs/1.安装及入门/images/vscode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion chs/1.安装及入门/readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# 1. 安装及入门

本章讲述安装流程和凹语言的程序的目录结构
本章讲述凹语言例子、安装凹语言程序、IDE插件和凹语言的程序的目录结构等内容
2 changes: 2 additions & 0 deletions chs/5.复合数据类型/readme.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# 5. 复合数据类型

复合数据类型是内置的复杂类型的基础。

2 changes: 2 additions & 0 deletions chs/6.自定义类型/readme.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# 6. 自定义类型

自定义类型主要是结构体、以及围绕结构体方法产生的接口抽象。
Loading

0 comments on commit e4aaef3

Please sign in to comment.