-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
387 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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”按钮,可以看到输出结果。 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# 1. 安装及入门 | ||
|
||
本章讲述安装流程和凹语言的程序的目录结构。 | ||
本章讲述凹语言例子、安装凹语言程序、IDE插件和凹语言的程序的目录结构等内容。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
# 5. 复合数据类型 | ||
|
||
复合数据类型是内置的复杂类型的基础。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
# 6. 自定义类型 | ||
|
||
自定义类型主要是结构体、以及围绕结构体方法产生的接口抽象。 |
Oops, something went wrong.