api

package
v0.14.1-dev Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 1, 2024 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

凹语言™ 功能 API 包。

Index

Examples

Constants

View Source
const (
	WaBackend_Default = config.WaBackend_Default // 默认

	WaBackend_wat = config.WaBackend_wat // 输出 wat
)

编译器后端类型

View Source
const (
	WaOS_Default = config.WaOS_Default // 默认

	WaOS_js      = config.WaOS_js      // 浏览器 js
	WaOS_wasi    = config.WaOS_wasi    // WASI 接口
	WaOS_unknown = config.WaOS_unknown // Unknown
)

目标平台类型, 可管理后缀名

View Source
const (
	WaArch_Default = config.WaArch_Default // 默认
	WaArch_wasm    = config.WaArch_wasm    // wasm 平台
)

体系结构类型

Variables

View Source
var (
	FlagDebugMode = &config.DebugMode

	FlagEnableTrace_api      = &config.EnableTrace_api
	FlagEnableTrace_app      = &config.EnableTrace_app
	FlagEnableTrace_compiler = &config.EnableTrace_compiler
	FlagEnableTrace_loader   = &config.EnableTrace_loader
)

调试参数

Functions

func BuildFile

func BuildFile(cfg *config.Config, filename string, src interface{}) (mainFunc string, wat []byte, err error)

构建 wat 目标

func BuildVFS

func BuildVFS(cfg *config.Config, vfs *config.PkgVFS, appPkg string) (wat []byte, err error)

构建 wat 目标

func FormatCode

func FormatCode(filename, code string) (string, error)

格式化代码

Example
package main

import (
	"fmt"

	"wa-lang.org/wa/api"
)

func main() {
	s, err := api.FormatCode("hello.wa", "func add(a:i32, b:i32)=>i32 {return a+b}")
	if err != nil {
		panic(err)
	}
	fmt.Println(s)

}
Output:

func add(a: i32, b: i32) => i32 { return a + b }

func GetCodeSyntax added in v0.8.0

func GetCodeSyntax(filename string, code []byte) string

获取代码语法类型

func RunCode

func RunCode(cfg *config.Config, filename, code string, args ...string) (stdoutStderr []byte, err error)

执行凹代码

Example
package main

import (
	"fmt"
	"log"

	"wa-lang.org/wa/api"
)

func main() {
	const code = `
		global gBase: i32 = 1000

		func main() {
			println(add(40, 2) + gBase)
		}

		func add(a: i32, b: i32) => i32 {
			return a+b
		}
	`

	output, err := api.RunCode(api.DefaultConfig(), "hello.wa", code, "__main__.main")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Print(string(output))

}
Output:

1042
Example (Args)
package main

import (
	"fmt"
	"log"

	"wa-lang.org/wa/api"
)

func main() {
	const code = `
		import "os"

		func main {
			for i, s := range os.Args[1:] {
				println(i, ":", s)
			}
		}
	`

	cfg := api.DefaultConfig()
	cfg.WaOS = api.WaOS_wasi

	args := []string{"aa", "bb"}
	output, err := api.RunCode(cfg, "hello.wa", code, args...)
	if err != nil {
		if len(output) != 0 {
			log.Println(string(output))
		}
		log.Fatal(err)
	}

	fmt.Print(string(output))

}
Output:

0 : aa
1 : bb
Example (Wz)
package main

import (
	"fmt"
	"log"

	"wa-lang.org/wa/api"
)

func main() {
	const code = `
		#wa:syntax=wz

		引于 "书"

		【启】:
		  书·说:"你好,凹语言中文版!"
		。
	`

	output, err := api.RunCode(api.DefaultConfig(), "hello.wa", code, "__main__.main")
	if err != nil {
		if len(output) != 0 {
			log.Println(string(output))
		}
		log.Fatal(err)
	}

	fmt.Print(string(output))

}
Output:

你好,凹语言中文版!

Types

type Config

type Config = config.Config

配置参数, 包含文件系统和 OS 等信息

func DefaultConfig

func DefaultConfig() *Config

默认配置

type Manifest

type Manifest = config.Manifest

模块元信息, 主包路径

func LoadManifest

func LoadManifest(vfs fs.FS, appPath string) (p *Manifest, err error)

加载 WaModFile 文件 如果 vfs 为空则从本地文件系统读取

type Manifest_package

type Manifest_package = config.Manifest_package

模块元信息中的包信息

type PkgVFS

type PkgVFS = config.PkgVFS

包虚拟文件系统

type Program

type Program = loader.Program

程序对象, 包含全量的 AST 和 SSA 信息, 经过语义检查

func LoadProgram

func LoadProgram(cfg *config.Config, appPath string) (*Program, error)

加载程序 入口 appPath 是包对应目录的路径

func LoadProgramFile

func LoadProgramFile(cfg *config.Config, filename string, src interface{}) (*Program, error)

加载单文件程序 入口 appPath 是包对应目录的路径

func LoadProgramVFS

func LoadProgramVFS(vfs *config.PkgVFS, cfg *config.Config, pkgPath string) (*Program, error)

基于 VFS 加载程序 入口 pkgPath 是包路径, 必须是 vfs.App 子包

type StdSize

type StdSize = config.StdSizes

指针和整数大小

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL