wat

package
v0.8.0-test1 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2023 License: AGPL-3.0 Imports: 4 Imported by: 0

README

Wat 文本到 wasm 转化

目标不是解析完整的 wat 语法, 而是能满足凹语言输出的 wat 格式.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DataSection

type DataSection struct {
	Doc    string // 注释
	Name   string // 名字
	Offset uint32 // 偏移量(0 表示自动计算)
	Value  []byte // 初始化值
}

初始化数据

type ElemSection

type ElemSection struct {
	Doc    string // 注释
	Offset uint32 // 偏移量(从 0 开始)
	Value  string // 初始化值, 引用的是其他 func 的名字
}

表格元素数据

type Field

type Field struct {
	Doc  string // 注释
	Name string // 变量名字
	Type string // 变量类型
}

参数和局部变量信息

type Func

type Func struct {
	Doc        string   // 注释
	Name       string   // 函数名
	ImportPath []string // 导入路径(仅针对导入对象)
	ExportName string   // 导出名字

	Type *FuncType // 函数类型
	Body *FuncBody // 函数体
}

函数定义

type FuncBody

type FuncBody struct {
	Locals []Field       // 局部变量
	Insts  []Instruction // 指令列表
}

函数定义

type FuncType

type FuncType struct {
	Params      []Field  // 参数名字
	ResultsType []string // 返回值类型
}

函数类型

type Global

type Global struct {
	Doc        string   // 注释
	Name       string   // 全局变量名
	ImportPath []string // 导入路径(仅针对导入对象)
	ExportName string   // 导出名字

	Type    string // 类型信息
	Value   string // 初始值(导入变量忽略)
	Mutable bool   // 是否可写

}

全局变量

type Instruction

type Instruction interface {
	DocString() string
	WATString() string // 支持缩进
	// contains filtered or unexported methods
}

指令对应接口

type Memory

type Memory struct {
	Doc        string   // 注释
	Name       string   // 内存对象的名字
	ImportPath []string // 导入路径(仅针对导入对象)
	ExportName string   // 导出名字

	Pages int           // 页数, 每页 64 KB
	Data  []DataSection // 初始化数据, 可能重叠
}

内存信息

type Module

type Module struct {
	Doc  string // 注释
	Name string // 模块的名字(可空)

	InitFn string // 初始化函数
	MainFn string // 开始函数名字(可空)

	Memory *Memory // 内存对象, 包含 Data 信息(可空)
	Table  *Table  // 表格对象, 包含 Elem 信息(可空)

	Globals []Global // 全局对象
	Funcs   []Func   // 函数对象
}

Module 表示一个 WASM 模块。

func ParseFile

func ParseFile(filename, input string) (*Module, error)

解析模块文件

type Pos

type Pos int

Pos 类似一个指针, 表示文件中的位置.

const NoPos Pos = 0

NoPos 类似指针的 nil 值, 表示一个无效的位置.

func (Pos) IsValid

func (p Pos) IsValid() bool

func (Pos) Position

func (pos Pos) Position(filename, src string) Position

Pos 装行列号位置

type Position

type Position struct {
	Filename string // 文件名
	Offset   int    // 偏移量, 从 0 开始
	Line     int    // 行号, 从 1 开始
	Column   int    // 列号, 从 1 开始
}

行列号位置

func (*Position) IsValid

func (pos *Position) IsValid() bool

func (Position) String

func (pos Position) String() string

String returns a string in one of several forms:

file:line:column    valid position with file name
file:line           valid position with file name but no column (column == 0)
line:column         valid position without file name
line                valid position without file name and no column (column == 0)
file                invalid position with file name
-                   invalid position without file name

type Table

type Table struct {
	Doc        string   // 注释
	Name       string   // Table对象的名字
	ImportPath []string // 导入路径(仅针对导入对象)
	ExportName string   // 导出名字

	Size int           // 表格容量
	Type string        // 元素类型, 默认为 anyfunc
	Elem []ElemSection // 初始化数据
}

Table 信息

type Token

type Token struct {
	Pos     Pos       // 位置
	Kind    TokenKind // 类型
	Literal string    // 程序中原始的字符串
}

Token 结构

func Tokenize

func Tokenize(filename, input string) (tokens, comments []Token)

将 wat 转化为 token 列表

func (Token) FloatValue

func (i Token) FloatValue() float64

func (Token) IntValue

func (i Token) IntValue() int64

func (Token) StrValue

func (i Token) StrValue() string

func (Token) String

func (tok Token) String() string

type TokenKind

type TokenKind int

记号类型

const (
	// 非法/结尾/注释
	ILLEGAL TokenKind = iota
	EOF
	COMMENT

	INSTRUCTION // 指令, 比如 global.get
	IDENT       // 表示符, 比如 $name
	INT         // 12345
	FLOAT       // 123.45
	CHAR        // 'a'
	STRING      // "abc"

	LPAREN // (
	RPAREN // )

	I32 // i32
	I64 // i64
	F32 // f32
	F64 // f64

	MODULE // module
	IMPORT // import
	EXPORT // export

	MEMORY // memory
	TABLE  // table
	GLOBAL // global
	LOCAL  // local
	DATA   // data
	ELEM   // elem
	TYPE   // type

	FUNC   // func
	PARAM  // param
	RESULT // result

	START // start

)

func (TokenKind) String

func (tokType TokenKind) String() string

Jump to

Keyboard shortcuts

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