ast

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// 字面量,e.g. 50
	Literal = iota
	// 操作符, e.g. + - * /
	Operator

	// 参数变量
	Parameter
)

Variables

This section is empty.

Functions

func ErrPos

func ErrPos(s string, pos int) string

对错误包装,进行可视化展示

func ExprASTResult

func ExprASTResult(expr ExprAST, exprDatas ...ParameterExprData) float64

一个典型的后序遍历求解算法

Types

type AST

type AST struct {
	// 词法分析的结果
	Tokens []*Token

	// 错误收集
	Err error
	// contains filtered or unexported fields
}

AST 生成器结构体

func NewAST

func NewAST(toks []*Token, s string) *AST

生成一个 AST 结构指针

func (*AST) ParseExpression

func (a *AST) ParseExpression() ExprAST

语法分析器入口

type BinaryExprAST

type BinaryExprAST struct {
	// 操作符
	Op string
	// 左右节点,可能是 数字表达式节点/操作表达式节点/nil
	Lhs,
	Rhs ExprAST
}

操作表达式节点

type ExprAST

type ExprAST interface {
	// contains filtered or unexported methods
}

基础表达式节点接口

func GetExpAST

func GetExpAST(source string) (ExprAST, error)

type NumberExprAST

type NumberExprAST struct {
	// 具体的值
	Val float64
}

数字表达式节点

type ParameterExprAST

type ParameterExprAST struct {
	Val string
}

type ParameterExprData

type ParameterExprData interface {
	FloatExprData(string) (float64, error)
}

type Parser

type Parser struct {
	// 输入的字符串
	Source string
	// contains filtered or unexported fields
}

定义一个结构体,描述一个词法分析器

type Token

type Token struct {
	// 原始字符
	Tok string
	// 类型,有 Literal、Operator 两种
	Type int

	Offset int
}

func Parse

func Parse(s string) ([]*Token, error)

封装词法分析过程,直接调用该函数即可解析字符串为[]Token

Jump to

Keyboard shortcuts

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