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.1. hello world

Printing "hello world" is a common example for PL. The Wa-lang example prints "你好,凹语言!" in Chinese, means "Hello, Wa-lang".

1.1.1 你好,凹语言!

Create hello.wa file with the following content:

// 版权 @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
}

Among them, // starts with a line comment, the import keyword imports two packages of the standard library, and the global keyword defines a global variable and gives an initial value of 2023. The func keyword defines the main function and the add function. The main function is the entry point of the program, which prints "你好,凹语言!" through the built-in println function, while using the Println string and integer expression results of the fmt package. The global year variable is also used in the main function. In addition, the add function is called and the return value is printed. The add function has 2 input parameters and a return value.

If the wa command of the Wa-lang has been installed locally (refer to Section 1.2 for the installation), you can enter the following command to execute:

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

1.1.2 Playground online

The Wa-lang is a general-purpose programming language designed for WebAssembly. From its inception, the browser has been the first support target. Playground can be accessed through https://wa-lang.org/playground. The interface is as follows:

Click the "RUN" button to see the output results.


Known issues:

  • The online playground only supports single file mode and does not support multi-file project mode yet. This problem does not affect syntax compatibility. Subsequent corrections to this problem will not affect the existing source code. Developers using Wa-lang do not need to deal with this issue specially.