The content on this page is written in Chinese, and then traslated into English by machine. More accurate traslations are welcome at: https://github.com/wa-lang/man/tree/master/en
Ending's law: "Any application that can be compiled to WebAssembly, will be compiled to WebAssembly eventually."
1.3. wa command line
This section introduces the main functions of the wa
command.
1.3.1. Help message
Enter the wa
command or wa -h
to view command line help information, as follows:
$ wa
NAME:
Wa - Wa is a tool for managing Wa source code.
USAGE:
wa [global options] command [command options] [arguments...]
VERSION:
v0.8.1-mvp
COMMANDS:
play start Wa playground
init init a sketch Wa module
build compile Wa source code
run compile and run Wa program
fmt format Wa source code file
test test Wa packages
yacc generates parsers for LALR(1) grammars
logo print Wa text format logo
GLOBAL OPTIONS:
--debug, -d set debug mode (default: false)
--trace value, -t value set trace mode (*|app|compiler|loader)
--help, -h show help (default: false)
--version, -v print the version (default: false)
COPYRIGHT:
Copyright 2018 The Wa Authors. All rights reserved.
See "https://wa-lang.org" for more information.
It has the following subcommands:
- play:Start the local version of the playground;
- init: initialize a Wa-lang project;
- build: compile Wa-lang program;
- run: compile and execute the Wa-lang program;
- fmt: format the Wa-lang source code file;
- test: execute the unit test of the Wa-lang project;
- yacc: The W-lang version of yacc, used to generate parser code for LALR(1) grammar rules;
- logo: Print the logo in Wa-lang and text version.
The build command has been shown in Section 1.1.1, and the play command has been shown in Section 1.2.5. This section briefly introduces other subcommands.
1.3.2 Initialize project
In Section 1.1.1 we have shown that wa run hello.wa
executes a Wa-lang program in a separate file. But a single-file Wa-lang program has a huge limitation - it has only one file and cannot reference non-standard library code. For larger Wa-lang it is recommended to organize them in project mode.
Use the wa init
command to initialize a Wa project. Take a look at the command line help first:
$ wa init -h
NAME:
wa init - init a sketch Wa module
USAGE:
wa init [command options] [arguments...]
OPTIONS:
--name value, -n value set app name (default: "hello")
--pkgpath value, -p value set pkgpath file (default: "myapp")
--update, -u update example (default: false)
--help, -h show help (default: false)
This command has two important parameters, -name
and -pkgpath
, which correspond to the name of the project and the corresponding package path respectively. Each parameter has a default value, and you can use wa init
to generate a hello project.
$ wa init
$ tree hello
hello
├── LICENSE
├── README.md
├── src
│ ├── main.wa
│ ├── mymath
│ │ └── math.wa
│ ├── mypkg
│ │ └── pkg.wa
│ └── zz_test.wa
├── vendor
│ └── 3rdparty
│ └── pkg
│ └── pkg.wa
└── wa.mod
7 directories, 8 files
The structure of the project will be introduced in Section 1.4.
1.3.3 Compile and execute
Enter the hello directory in the command line environment and enter wa build
to build the wasm module in the output directory:
$ wa build
$ tree output/
output/
├── hello.wasm
└── hello.wat
1 directory, 2 files
The default output is the WASI specificated output/hello.wat
and output/hello.wasm
files. The exported wasm module can be executed with standard tools. You can also use the wa
command to execute:
$ wa run ./output/hello.wasm
你好,凹语言!
5050
...
If you execute the wa run
command without entry, it means that the current Wa-lang project will be compiled and executed. output/hello.wasm
will be built first and then executed.
1.3.4 Format code
The wa fmt
command is used to format code, and its command line help information is as follows:
$ wa fmt -h
NAME:
wa fmt - format Wa source code file
USAGE:
wa fmt [command options] [<file.wa>|<path>|<path>/...]
OPTIONS:
--help, -h show help (default: false)
The command line argument is the path to be formatted:
wa fmt file.wa
formats the specified .wa filewa fmt path
formats all .wa files in the specified directorywa fmt path/...
recursively formats the .wa file in the specified path, including subdirectories
If no parameters are specified, all .wa files in the current directory will be formatted by default. If the current directory belongs to the Wa-lang project, the .wa files in all subdirectories will be formatted by default.
1.3.5 Unit test
The project generated by default will have a src/zz_test.wa
test file with the following content:
func TestSum {
assert(sum(100) == 5050, "sum(100) failed")
}
func ExampleSum {
println(sum(100))
// Output:
// 5050
}
The result of testing sum(100)
in the TestSum
test function via the built-in assert
function is 5050. In the ExampleSum
example test function, pass //Output:
to test that the output meets the expected results.
Execute the test through the wa test
command in the command line environment of the project directory:
$ wa test
ok myapp 104ms
1.3.6 Wa-lang version of yacc
yacc is a program for generating syntax parsers, a tool for compiler enthusiasts. The yacc of the Wa-lang language is transplanted from goyacc. For detailed usage, please refer to the related articles in the SmallTalk section of the wa-lang official website .
1.3.7 Print Logo
wa logo
can output some logo patterns in text format, and readers can explore by themselves through the wa logo -h
command.