ast

package
v0.0.0-...-0e42c13 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2023 License: BSD-3-Clause Imports: 16 Imported by: 0

Documentation

Overview

Package ast 实现了一个抽象语法树

创建抽象语法树

创建抽象语法树推荐使用 pkg/gitee.com/u-language/u-language/pkg/parser 提供的简便函数

从词法分析结果创建抽象语法树,只需要调用 NewTree

如果要进行错误处理请自行使用 pkg/gitee.com/u-language/u-language/pkg/errcode

辅助语义检查

  • 会检查符号是否和其他符号重名
  • 会检查枚举值是否重名
  • 会检查是否调用禁止调用的init或main函数

Index

Constants

View Source
const (
	File = iota
)

Variables

View Source
var Builtin_func_info = map[string]SymbolInfo{
	enum.Printf:        NewSymbolInfo_Func(&FuncInfo{Name: enum.Printf, Parame: []astdata.Parame{astdata.NewNameAndType("format", NewObject(TypeObj, enum.String))}}),
	enum.Float:         NewSymbolInfo_Func(&FuncInfo{Name: enum.Float, RetValue: []astdata.RetValue{astdata.NewNameAndType("", NewObject(TypeObj, enum.Float))}}),
	enum.Int:           NewSymbolInfo_Func(&FuncInfo{Name: enum.Int, RetValue: []astdata.RetValue{astdata.NewNameAndType("", NewObject(TypeObj, enum.Int))}}),
	enum.Malloc:        NewSymbolInfo_Func(&FuncInfo{Name: enum.Malloc, Parame: []astdata.Parame{astdata.NewNameAndType("type", NewObject(TypeObj, "T"))}, RetValue: []astdata.RetValue{astdata.NewNameAndType("ptr", NewObject(TypeObj, "&T"))}}),
	enum.Free:          NewSymbolInfo_Func(&FuncInfo{Name: enum.Free, Parame: []astdata.Parame{astdata.NewNameAndType("ptr", NewObject(TypeObj, "&T"))}}),
	enum.UnsafeAdd:     NewSymbolInfo_Func(&FuncInfo{Name: enum.UnsafeAdd, Parame: []astdata.Parame{astdata.NewNameAndType("ptr", NewObject(TypeObj, "&T")), astdata.NewNameAndType("offset", NewObject(TypeObj, enum.Int))}, RetValue: []astdata.RetValue{astdata.NewNameAndType("ptr", NewObject(TypeObj, "&T"))}}),
	enum.UnsafeConvert: NewSymbolInfo_Func(&FuncInfo{Name: enum.UnsafeConvert, Parame: []astdata.Parame{astdata.NewNameAndType("ptr", NewObject(TypeObj, enum.UnsafePointer)), astdata.NewNameAndType("type", NewObject(TypeObj, "T"))}, RetValue: []astdata.RetValue{astdata.NewNameAndType("ptr", NewObject(TypeObj, "&T"))}}),
	enum.UnsafeSizeof:  NewSymbolInfo_Func(&FuncInfo{Name: enum.UnsafeSizeof, Parame: []astdata.Parame{astdata.NewNameAndType("type", NewObject(TypeObj, "T"))}, RetValue: []astdata.RetValue{astdata.NewNameAndType("size", NewObject(TypeObj, enum.Int))}}),
	enum.MallocSize:    NewSymbolInfo_Func(&FuncInfo{Name: enum.MallocSize, Parame: []astdata.Parame{astdata.NewNameAndType("size", NewObject(TypeObj, enum.Int))}, RetValue: []astdata.RetValue{astdata.NewNameAndType("ptr", NewObject(TypeObj, enum.UnsafePointer))}}),
	enum.MemPoolFree:   SymbolInfo{},
	enum.MemPoolNew:    SymbolInfo{},
	enum.UnsafePointer: SymbolInfo{},
}

预定义的函数或视为函数调用的类型转换

View Source
var Deferfunc func() = utils.Deferfunc
View Source
var FirstLineNoPackageDeclOn = false

缩写 The first line does not have a package declaration 缩写成FirstLineNoPackageDecl 控制是否允许第一行没有包声明

View Source
var NdSotfOn = false

有测试依赖函数体外的非声明语句,设置为true可以恢复之前的行为 缩写 Non declarative statements outside of a function 缩写成NdSotf

View Source
var (
	NodeEnumSrtMap = [...]string{
		VAR:                "var (变量节点)",
		FUNC:               "func (函数节点)",
		ASSIGN:             "assign (赋值节点)",
		OPEXPR:             "opexpr (运算表达式节点)",
		OBJECT:             "object (对象节点)",
		BlankLine:          "blankline (空行节点)",
		Rbrace:             "rbrace(右大括号节点)",
		Lbrace:             "lbrace (左大括号节点)",
		IF:                 "if (if 节点)",
		Else:               "else (else 节点)",
		MLCStart:           "Start of multiline comment (多行注释开头)",
		MLCEnd:             "End of multiline comment(多行注释结束)",
		MLCIn:              "In multiline comments (在多行注释中)",
		For:                "for(for语句)",
		Call:               "call(调用节点)",
		Return:             "return (返回语句)",
		GOTO:               "goto (语句)",
		Label:              "label (标签)",
		Break:              "break",
		Continue:           "continue",
		Const:              "const (常量节点)",
		Struct:             "strcut (结构体)",
		ObjectsEnum:        "ObjectsEnum (多个object)",
		SelfOpStmtEnum:     "SelfOpStmtEnum (自操作语句)",
		Switch:             "switch",
		Case:               "case",
		Default:            "default",
		Method:             "method (方法)",
		AutoFree:           "autofree (自动释放块)",
		Enum:               "enum (枚举声明)",
		ContinuityTypeEnum: "ContinuityTypeEnum (连续类型)",
		IndexExprEnum:      "IndexExprEnum (索引表达式)",
		GenInst:            "GenericInstantiation (泛型实例化)",
		Deref:              "dereference (解引用节点)",
	}
)
View Source
var (
	ObjKindSrtMap = [...]string{
		NoKind:    "nokind (未定义的枚举)",
		INTOBJ:    "intobj (整数)",
		FLOATOBJ:  "floatobj (浮点数)",
		BoolObj:   "boolobj (布尔值)",
		StringObj: "stringobj(字符串)",
		SymbolObj: "SymbolObj (符号)",
		LeaObj:    "leaObj (取地址)",
		DerefObj:  "derefObj (解引用)",
		StructPtr: "StructPtr (结构体指针)",
		NilObj:    "NilObj (指针的零值)",
		EnumObj:   "EnumObj (枚举值)",
		TypeObj:   "TypeObj(类型)",
	}
)
View Source
var (
	TypeEnumStrMap = map[string]struct{}{
		"int":              struct{}{},
		"string":           struct{}{},
		"bool":             struct{}{},
		"float":            struct{}{},
		enum.UnsafePointer: struct{}{},
		"any":              struct{}{},
	}
)

Functions

func Instatination

func Instatination(findTree func(string) *Tree, t *Tree, GenInstNodes *Sbt, LineNum int, Base string, ActualType []astdata.Typ, str string)

func RetType

func RetType(ptr *Object, sbt *Sbt) string

RetType获取Object的类型,对于未知的,将panic

  • ptr是被获取类型的的Object,不能为nil
  • sbt是Object所在区块的符号表,不能为nil

func RetTypeSize deprecated

func RetTypeSize(Type string) int64

Deprecated:暂不维护 RetTypeSize 返回类型字节大小

Types

type ASSIGNInfo

type ASSIGNInfo struct {
	Ptr     *ASSIGNNode
	LineNum int
}

type ASSIGNNode

type ASSIGNNode = AssignmentNode

func NewASSIGNNode

func NewASSIGNNode(Dest Expr, Src Expr) *ASSIGNNode

NewASSIGNNode 创建赋值节点

func (*ASSIGNNode) String

func (a *ASSIGNNode) String() string

func (*ASSIGNNode) Type

func (a *ASSIGNNode) Type() NodeEnum

type AssignmentNode

type AssignmentNode struct {
	//目的操作数,源操作数
	Dest Expr
	Src  Expr
}

赋值节点

type AutoFreeNode

type AutoFreeNode struct {
	Expr Expr
	// contains filtered or unexported fields
}

自动释放块节点

func NewAutoFreeNode

func NewAutoFreeNode(Thread bool) *AutoFreeNode

func (*AutoFreeNode) String

func (a *AutoFreeNode) String() string

func (*AutoFreeNode) Type

func (a *AutoFreeNode) Type() NodeEnum

type BlankLineNode

type BlankLineNode struct{}

空行节点

func NewBlinkLineNode

func NewBlinkLineNode() BlankLineNode

func (BlankLineNode) C

func (b BlankLineNode) C(buf *strings.Builder)

func (BlankLineNode) CType

func (b BlankLineNode) CType() cenum.NodeEnum

func (BlankLineNode) String

func (b BlankLineNode) String() string

func (BlankLineNode) Type

func (b BlankLineNode) Type() NodeEnum

type BreakStmt

type BreakStmt struct{}

break语句

func NewBreakStmt

func NewBreakStmt() BreakStmt

func (BreakStmt) C

func (b BreakStmt) C(buf *strings.Builder)

func (BreakStmt) CType

func (b BreakStmt) CType() cenum.NodeEnum

func (BreakStmt) String

func (b BreakStmt) String() string

func (BreakStmt) Type

func (b BreakStmt) Type() NodeEnum

type CallNode

type CallNode struct {
	FuncName   FuncNameNode //调用函数名
	Parame     []Expr       //Parameter
	InAutoFree bool
}

函数调用节点

func NewCallExpr

func NewCallExpr(funcname FuncNameNode, InAutoFree bool, parame ...Expr) *CallNode

NewCallNode 创建函数调用节点

func (*CallNode) String

func (a *CallNode) String() string

func (*CallNode) Type

func (a *CallNode) Type() NodeEnum

type CaseNode

type CaseNode struct {
	Expr  Expr
	Sbt   *Sbt
	Left  int
	Right int
}

case节点

func NewCaseNode

func NewCaseNode(Thread bool) *CaseNode

func (*CaseNode) String

func (c *CaseNode) String() string

func (*CaseNode) Type

func (c *CaseNode) Type() NodeEnum

type ConstNode

type ConstNode struct {
	TYPE  astdata.Typ
	Value Expr
	Name  string
}

常量声明节点

func NewConstNode

func NewConstNode(name string, Type astdata.Typ) *ConstNode

NewConstNode 创建常量声明节点

func (*ConstNode) String

func (n *ConstNode) String() string

func (*ConstNode) Type

func (n *ConstNode) Type() NodeEnum

type ContinueStmt

type ContinueStmt struct{}

continue语句

func NewContinueStmt

func NewContinueStmt() ContinueStmt

func (ContinueStmt) C

func (c ContinueStmt) C(buf *strings.Builder)

func (ContinueStmt) CType

func (c ContinueStmt) CType() cenum.NodeEnum

func (ContinueStmt) String

func (c ContinueStmt) String() string

func (ContinueStmt) Type

func (c ContinueStmt) Type() NodeEnum

type ContinuityType

type ContinuityType struct {
	Len  Expr
	TYPE astdata.Typ
	UStr string
}

ContinuityType 连续类型

func NewContinuityType

func NewContinuityType(Len Expr, Type astdata.Typ, UStr string) *ContinuityType

func (*ContinuityType) Copy

func (c *ContinuityType) Copy() astdata.Typ

func (*ContinuityType) SetLea

func (c *ContinuityType) SetLea(i int)

func (*ContinuityType) SetTyp

func (c *ContinuityType) SetTyp()

func (*ContinuityType) String

func (c *ContinuityType) String() string

func (*ContinuityType) Typ

func (c *ContinuityType) Typ() string

func (*ContinuityType) Type

func (c *ContinuityType) Type() NodeEnum

type DefaultNode

type DefaultNode struct {
	Sbt   *Sbt
	Left  int
	Right int
}

defaultcase节点

func NewDefaultNode

func NewDefaultNode(Thread bool) *DefaultNode

func (*DefaultNode) C

func (c *DefaultNode) C(buf *strings.Builder)

func (*DefaultNode) CType

func (d *DefaultNode) CType() cenum.NodeEnum

func (*DefaultNode) String

func (d *DefaultNode) String() string

func (*DefaultNode) Type

func (d *DefaultNode) Type() NodeEnum

type Dereference

type Dereference struct {
	Value Expr
}

Dereference 解引用节点

func (*Dereference) String

func (d *Dereference) String() string

func (*Dereference) Type

func (d *Dereference) Type() NodeEnum

type ElseNode

type ElseNode struct {
	BoolExpr Expr
	// contains filtered or unexported fields
}

else节点

func NewElseNode

func NewElseNode(Thread bool) *ElseNode

NewElseNode 创建else节点

func (*ElseNode) String

func (l *ElseNode) String() string

func (*ElseNode) Type

func (l *ElseNode) Type() NodeEnum

type EnumDecl

type EnumDecl struct {
	Name  string
	Enums []string
}

EnumDecl 枚举声明

func NewEnumDecl

func NewEnumDecl(Name string) *EnumDecl

func (*EnumDecl) C

func (s *EnumDecl) C(buf *strings.Builder)

func (EnumDecl) CType

func (e EnumDecl) CType() cenum.NodeEnum

func (*EnumDecl) String

func (s *EnumDecl) String() string

func (*EnumDecl) Type

func (s *EnumDecl) Type() NodeEnum

type Expr

type Expr interface {
	Node
	// contains filtered or unexported methods
}

表达式接口,代表一个表达式

type Field

type Field = astdata.NameAndType

type ForNode

type ForNode struct {
	InitStmt Node
	BoolExpr Expr
	EndStmt  Node
	// contains filtered or unexported fields
}

for节点

func NewForNode

func NewForNode(Thread bool) *ForNode

NewForNode 创建for节点

func (*ForNode) String

func (l *ForNode) String() string

func (*ForNode) Type

func (l *ForNode) Type() NodeEnum

type FuncInfo

type FuncInfo struct {
	Name       string
	FileName   string
	Parame     []astdata.Parame
	RetValue   []astdata.RetValue
	TypeParame []astdata.NameAndType
	LineNum    int
}

FuncInfo 函数信息

func (*FuncInfo) Copy

func (f *FuncInfo) Copy() *FuncInfo

func (*FuncInfo) String

func (f *FuncInfo) String() string

type FuncNameNode

type FuncNameNode interface {
	Node
	// FuncName 返回人类可读的函数名表示,对于选择器,返回的只应该用于调试
	FuncName() string
}

FuncName 表示抽象语法树节点,这个节点可以表示函数名 可能的类型是 Object Objects

type FuncNode

type FuncNode struct {
	*FuncInfo
	// contains filtered or unexported fields
}

函数节点

func NewFuncNode

func NewFuncNode(info *FuncInfo, Thread bool) *FuncNode

NewFuncNode 创建函数节点

func (*FuncNode) String

func (f *FuncNode) String() string

func (*FuncNode) Type

func (f *FuncNode) Type() NodeEnum

type GenericInstantiation

type GenericInstantiation struct {
	BaseName   astdata.Typ
	LineNum    int
	ActualType []astdata.Typ
	FileName   string
}

GenericInstantiation 泛型实例化

func (*GenericInstantiation) Copy

func (g *GenericInstantiation) Copy() astdata.Typ

func (*GenericInstantiation) FuncName

func (g *GenericInstantiation) FuncName() string

func (*GenericInstantiation) SetLea

func (g *GenericInstantiation) SetLea(i int)

func (*GenericInstantiation) SetTyp

func (g *GenericInstantiation) SetTyp()

func (*GenericInstantiation) String

func (g *GenericInstantiation) String() string

func (*GenericInstantiation) Typ

func (g *GenericInstantiation) Typ() string

func (*GenericInstantiation) Type

func (g *GenericInstantiation) Type() NodeEnum

type GetImport

type GetImport interface {
	//导入表
	GetImportTable() *Sbt
	//自己及依赖导入的包
	GerImportPackage() *map[string]*Package
	//自己导入的包
	GetImportLoacl() *[]data.IsItARecursiveType
}

type GotoStmt

type GotoStmt struct {
	Label string
}

goto语句

func NewGotoStmt

func NewGotoStmt(Label string) *GotoStmt

func (*GotoStmt) C

func (g *GotoStmt) C(buf *strings.Builder)

func (*GotoStmt) CType

func (g *GotoStmt) CType() cenum.NodeEnum

func (*GotoStmt) String

func (g *GotoStmt) String() string

func (*GotoStmt) Type

func (g *GotoStmt) Type() NodeEnum

type IfNode

type IfNode struct {
	BoolExpr Expr
	// contains filtered or unexported fields
}

if节点

func NewIfNode

func NewIfNode(Thread bool) *IfNode

NewIfNode 创建if节点

func (*IfNode) String

func (l *IfNode) String() string

func (*IfNode) Type

func (l *IfNode) Type() NodeEnum

type ImportInfo

type ImportInfo struct {
	Path string
}

ImportInfo 导入信息

func (ImportInfo) String

func (info ImportInfo) String() string

type IndexExpr

type IndexExpr struct {
	X     Expr
	Index Expr
}

IndexExpr 索引表达式 缩写index expression 缩写成 IndexExpr

func NewIndexExpr

func NewIndexExpr(X Expr, Index Expr) *IndexExpr

func (*IndexExpr) String

func (l *IndexExpr) String() string

func (*IndexExpr) Type

func (l *IndexExpr) Type() NodeEnum

type LabelNode

type LabelNode struct {
	Value string
}

标签

func NewLabelNode

func NewLabelNode(value string) *LabelNode

func (*LabelNode) C

func (l *LabelNode) C(buf *strings.Builder)

func (*LabelNode) CType

func (l *LabelNode) CType() cenum.NodeEnum

func (*LabelNode) String

func (b *LabelNode) String() string

func (*LabelNode) Type

func (b *LabelNode) Type() NodeEnum

type MLCEndNode

type MLCEndNode struct{}

多行注释结束节点

func NewMLCEndNode

func NewMLCEndNode() MLCEndNode

func (MLCEndNode) String

func (b MLCEndNode) String() string

func (MLCEndNode) Type

func (b MLCEndNode) Type() NodeEnum

type MLCInNode

type MLCInNode struct{}

多行注释中节点 In multiline comments 为了统一多行注释前缀写为MLCIn

func NewMLCInNode

func NewMLCInNode() MLCInNode

func (MLCInNode) String

func (b MLCInNode) String() string

func (MLCInNode) Type

func (b MLCInNode) Type() NodeEnum

type MLCStartNode

type MLCStartNode struct{}

多行注释开头节点

func NewMLCStartNode

func NewMLCStartNode() MLCStartNode

func (MLCStartNode) String

func (b MLCStartNode) String() string

func (MLCStartNode) Type

func (b MLCStartNode) Type() NodeEnum

type MethodNode

type MethodNode struct {
	*FuncInfo
	Typ string
	// contains filtered or unexported fields
}

MethodNode 方法节点

func NewMethodNode

func NewMethodNode(typ string, info *FuncInfo, Thread bool) *MethodNode

NewMethodNode 创建函数节点

func (*MethodNode) String

func (m *MethodNode) String() string

func (*MethodNode) Type

func (m *MethodNode) Type() NodeEnum

type MethodTableInfo

type MethodTableInfo struct {
	Infos *Sbt
}

func NewMethodTableInfo

func NewMethodTableInfo(Thread bool) MethodTableInfo

func (MethodTableInfo) String

func (info MethodTableInfo) String() string

type Node

type Node interface {
	Type() NodeEnum
	String() string
}

节点接口,代表一个抽象语法树节点

func ParserNode

func ParserNode(tree *Tree, lines []lex.Line, sbt *Sbt, state ParserNodeState) []Node

ParserNode 解析几行已经完成词法分析的代码,并返回自动生成的节点

  • tree是抽象语法树, Tree.LVarTable Tree.Sbt 不能为空
  • lines是词法分析结果
  • sbt是被使用的符号表
  • index是被解析开始行偏移量
  • end是被结束开始行偏移量
  • ret是结果保存切片,为nil将自动分配一个长度为len(lines)的
  • IsFunc是被解析结果是否在函数内
  • Thread控制是否并发

对同一个 Tree 同时调用 ParserNode 有数据竞争可能

type NodeEnum

type NodeEnum int

节点类型枚举

const (
	VAR NodeEnum = iota
	FUNC
	ASSIGN
	OPEXPR
	OBJECT
	BlankLine
	Rbrace
	Lbrace
	IF
	Else
	MLCStart
	MLCEnd
	MLCIn
	For
	Call
	Return
	GOTO
	Label
	Break
	Continue
	Const
	Struct
	ObjectsEnum
	SelfOpStmtEnum
	FuncInit
	Switch
	Case
	Default
	Method
	AutoFree
	Enum
	ContinuityTypeEnum
	IndexExprEnum
	GenInst
	Deref
)

func (NodeEnum) String

func (n NodeEnum) String() string

type ObjKind

type ObjKind int

对象类型枚举

const (
	NoKind ObjKind = 1 << iota
	INTOBJ
	FLOATOBJ
	BoolObj
	StringObj
	SymbolObj
	LeaObj
	DerefObj
	StructPtr
	NilObj
	EnumObj
	TypeObj
)

func (ObjKind) String

func (o ObjKind) String() string

type Object

type Object struct {
	Name string
	Kind ObjKind
}

对象节点

可能表示

  • 整数
  • 浮点数
  • 布尔值
  • 符号
  • 字符串

func NewObject

func NewObject(Kind ObjKind, Name string) *Object

创建对象节点

func (*Object) Copy

func (o *Object) Copy() astdata.Typ

func (*Object) FuncName

func (o *Object) FuncName() string

func (*Object) SetLea

func (o *Object) SetLea(i int)

func (*Object) SetTyp

func (o *Object) SetTyp()

func (*Object) String

func (o *Object) String() string

func (*Object) Typ

func (o *Object) Typ() string

func (*Object) Type

func (o *Object) Type() NodeEnum

type Objects

type Objects struct {
	T              string
	Slice          []*Object
	IsImportSymbol bool //是否是导入符号,由语义检查填充
}

func NewObjects

func NewObjects(slice []*Object) *Objects

func (*Objects) Copy

func (o *Objects) Copy() astdata.Typ

func (*Objects) FuncName

func (o *Objects) FuncName() string

func (*Objects) SetLea

func (o *Objects) SetLea(i int)

func (*Objects) SetTyp

func (o *Objects) SetTyp()

func (*Objects) String

func (o *Objects) String() string

func (*Objects) Typ

func (o *Objects) Typ() string

func (*Objects) Type

func (o *Objects) Type() NodeEnum

type OpExpr

type OpExpr = OperationExpr

运算表达式节点

func NewOpExpr

func NewOpExpr(OP enum.OPSymbol, Src1, Src2 Expr) *OpExpr

NewOpExpr 创建运算表达式节点

func (*OpExpr) String

func (a *OpExpr) String() string

func (*OpExpr) Type

func (a *OpExpr) Type() NodeEnum

type OperationExpr

type OperationExpr struct {
	Src1    Expr
	Src2    Expr
	OP      enum.OPSymbol
	IsStr   bool //是否是字符串类型,由check处理
	Inparen bool //是否在小括号内
}

运算表达式节点

type Package

type Package struct {
	//导入表
	ImportTable *Sbt

	PackageName atomic.Pointer[string]
	//在自动释放块被调用的函数名
	InAutoFreeFuncCallName data.RemoveDupStrck[FuncNameNode]
	//符号表
	Sbt *Sbt
	//泛型表
	GenericTable *Sbt
	//泛型实例化节点
	GenInstNodes *Sbt
	//自己及依赖导入的包
	ImportPackage map[string]*Package
	Dir           string
	//自己导入的包
	ImportLoacl []data.IsItARecursiveType
	//变量初始化
	VarInitTable_Global VarInitTable
	//保存抽象语法树
	Trees data.Slice[*Tree]
	//全局声明数量 Number of globar declarations
	GlobarDeclNum atomic.Uint32
	//是否有init函数
	IsInitFunc atomic.Bool
	//是否有自动释放块
	IsHaveAutoFree atomic.Bool
	//并发
	Thread bool
	// contains filtered or unexported fields
}

Package 管理一个包的所有源代码文件的抽象语法树

func NewPackage

func NewPackage(dir string, thread bool, errctx *errcode.ErrCtx) *Package

NewPackage 创建管理一个包的所有源代码文件的抽象语法树的 Package

  • thread是控制是否并发
  • errctx是错误处理上下文

func (*Package) AddFile

func (p *Package) AddFile(ft *lex.FileToken)

AddFile 将一个源代码文件的词法分析结果转换为抽象语法树保存

  • ft 是源代码文件的词法分析结果

func (*Package) Dep

func (p *Package) Dep() []data.IsItARecursiveType

func (*Package) FindTree

func (p *Package) FindTree(file string) *Tree

FindTree 寻找自己及依赖中是否有 file 的抽象语法树

func (*Package) GerImportPackage

func (p *Package) GerImportPackage() *map[string]*Package

func (*Package) GetImportLoacl

func (p *Package) GetImportLoacl() *[]data.IsItARecursiveType

func (*Package) GetImportTable

func (p *Package) GetImportTable() *Sbt

func (*Package) My

func (p *Package) My() string

func (*Package) Parser

func (p *Package) Parser()

func (*Package) String

func (p *Package) String() string

type ParserNodeState

type ParserNodeState struct {
	//解析结果的保存切片,如果为nil,[ParserNode]会自动创建一个
	Ret []Node
	//被解析的词法分析结果开头偏移量
	Index int
	//被解析的词法分析结果结尾偏移量(解析时这个偏移量的词法分析结果不会被处理)
	End int
	//是否在解析函数内,如果是,函数信息
	FuncInfo *FuncInfo
	//控制是否并发
	Thread bool
	//is recursion 是否递归
	IsRecursion bool
	//是否在自动释放块
	InAutoFree bool
}

ParserNodeState 是 ParserNode 的可选参数

type RbraceNode

type RbraceNode struct{}

右大括号节点

func NewRbraceNode

func NewRbraceNode() RbraceNode

func (RbraceNode) C

func (b RbraceNode) C(buf *strings.Builder)

func (RbraceNode) CType

func (b RbraceNode) CType() cenum.NodeEnum

func (RbraceNode) String

func (b RbraceNode) String() string

func (RbraceNode) Type

func (b RbraceNode) Type() NodeEnum

type RemoveGenericsInfo

type RemoveGenericsInfo struct {
	FileName string
	Nodes    []Node
	LineNum  int
}

func (RemoveGenericsInfo) String

func (info RemoveGenericsInfo) String() string

type ReturnNode

type ReturnNode struct {
	RetValue Expr
}

return节点

func NewReturnNode

func NewReturnNode() *ReturnNode

NewReturnNode 创建return节点

func (*ReturnNode) String

func (l *ReturnNode) String() string

func (*ReturnNode) Type

func (l *ReturnNode) Type() NodeEnum

type Sbt

type Sbt struct {

	//变量总大小,为兼容之前的VAT存在
	Size int64
	//是否并发
	Thread bool
	// contains filtered or unexported fields
}

Sbt 符号表 symbolTable

func NewSbt

func NewSbt(Thread bool) *Sbt

NewSbt 创建符号表

  • Thread控制是否可以并发

func (*Sbt) AddConst

func (s *Sbt) AddConst(n *ConstNode) errcode.ErrCode

AddConst 记录常量 有错返回错误码 - n 是被记录的常量节点

func (*Sbt) AddEnum

func (s *Sbt) AddEnum(n *EnumDecl) errcode.ErrCode

AddEnum 记录枚举 有错返回错误码 - n 是被记录的枚举节点

func (*Sbt) AddFunc

func (v *Sbt) AddFunc(info *FuncInfo) errcode.ErrCode

AddFunc 记录函数

  • info是函数信息

func (*Sbt) AddGenInst

func (s *Sbt) AddGenInst(node *GenericInstantiation)

AddGenInst 记录泛型实例化信息

  • node是泛型实例化节点

func (*Sbt) AddMethod

func (s *Sbt) AddMethod(Type string, method *MethodNode) errcode.ErrCode

AddMethod 记录方法 有错返回错误码

  • Type是方法所属的类型名
  • FuncInfo是函数的信息

func (*Sbt) AddStruct

func (s *Sbt) AddStruct(name string, decl *StructDecl) errcode.ErrCode

AddStruct 记录结构体 有错返回错误码

  • name是变量名
  • FieldTable是字段表

func (*Sbt) AddVar

func (s *Sbt) AddVar(name string, Type astdata.Typ, IsStackAlloc bool, LineNum int, FileName string) errcode.ErrCode

AddVar 记录变量 有错返回错误码

  • name是变量名
  • Type是类型字符串
  • IsStackAlloc是否在栈中分配

func (*Sbt) Copy

func (v *Sbt) Copy() *Sbt

func (*Sbt) Have

func (v *Sbt) Have(name string) SymbolInfo

Have 查询是否记录了符号

  • name是符号名

如果符号不存在,被设计为程序不应继续执行,将panic

func (*Sbt) Have2

func (v *Sbt) Have2(name string) (SymbolInfo, bool)

Have2 查询是否记录了符号

  • name是符号名

如果符号不存在,被设计为程序不应继续执行,将panic

func (*Sbt) HaveImportSymbol

func (s *Sbt) HaveImportSymbol(packageName, name string) (SymbolInfo, bool)

HaveImportSymbol 查询导入符号

  • packageName是被导入的包名
  • name是被导入的符号名

func (*Sbt) HaveImportType

func (s *Sbt) HaveImportType(packageName, name string) SymbolInfo

HaveImportType 查询导入类型

  • packageName是被导入的包名
  • name是被导入的类型名

如果被导入的包没有类型名,将panic

func (*Sbt) HaveMethod

func (s *Sbt) HaveMethod(Type string, Name string) SymbolInfo

HaveMethod 查询是否记录了方法

  • Type是类型名
  • Name是函数的名字

如果符号不存在,返回空信息

func (*Sbt) HaveType

func (s *Sbt) HaveType(name string) SymbolInfo

HaveType 查询是否记录了指定类型的符号

  • name是符号名

如果符号不存在,被设计为程序不应继续执行,将panic

func (*Sbt) Range

func (v *Sbt) Range(f func(key string, value SymbolInfo) bool)

Range 循环遍历所有表中记录的符号信息

func (*Sbt) String

func (v *Sbt) String() string

type SelfOpStmt

type SelfOpStmt struct {
	Dest Expr
	OP   enum.OPSymbol
}

自操作语句 缩写: Self-operating statement 缩写成 SelfOpStmt 源操作数只可能是下列类型

func NewSelfOpStmt

func NewSelfOpStmt(OP enum.OPSymbol, Dest Expr) *SelfOpStmt

func (*SelfOpStmt) String

func (o *SelfOpStmt) String() string

func (*SelfOpStmt) Type

func (o *SelfOpStmt) Type() NodeEnum

type StructDecl

type StructDecl struct {
	Name       string
	FileName   string
	FieldTable []Field
	LineNum    int
	TypeParame []astdata.NameAndType
	// contains filtered or unexported fields
}

StructDecl 结构体声明

缩写:struct declaration 缩写成:StructDecl

func NewStructDecl

func NewStructDecl(Name string) *StructDecl

func (*StructDecl) C

func (s *StructDecl) C(buf *strings.Builder)

func (*StructDecl) CType

func (s *StructDecl) CType() cenum.NodeEnum

func (*StructDecl) Dep

func (info *StructDecl) Dep() []data.IsItARecursiveType

func (*StructDecl) FindField

func (info *StructDecl) FindField(Name string) Field

FindField 寻找结构体字段中是否有Name 返回值Field.Name==""表示结构体字段中没有要查询到

func (*StructDecl) My

func (info *StructDecl) My() string

func (*StructDecl) String

func (s *StructDecl) String() string

func (*StructDecl) Type

func (s *StructDecl) Type() NodeEnum

type SwitchNode

type SwitchNode struct {
	Expr Expr
	// contains filtered or unexported fields
}

switch节点

func NewSwitchNode

func NewSwitchNode(Thread bool) *SwitchNode

func (*SwitchNode) String

func (s *SwitchNode) String() string

func (*SwitchNode) Type

func (s *SwitchNode) Type() NodeEnum

type SymbolInfo

type SymbolInfo struct {
	//符号信息
	Info fmt.Stringer
	//类型枚举
	Kind enum.Symbol
}

SymbolInfo 符号信息

func NewSymbolInfo_Func

func NewSymbolInfo_Func(info *FuncInfo) SymbolInfo

NewSymbolInfo_Func 创建函数符号信息

func NewSymbolInfo_Var

func NewSymbolInfo_Var(info VarInfoSbt) SymbolInfo

func (*SymbolInfo) Name

func (s *SymbolInfo) Name() string

func (*SymbolInfo) PtrFuncInfo

func (s *SymbolInfo) PtrFuncInfo() *FuncInfo

PtrFuncInfo 将SymbolInfo的Info字段解释为FuncInfo,并返回指针

func (*SymbolInfo) PtrStructDecl

func (s *SymbolInfo) PtrStructDecl() *StructDecl

PtrStructDecl 将SymbolInfo的Info字段解释为StructInfo指针,并返回

func (*SymbolInfo) PtrVarInfoSbt

func (s *SymbolInfo) PtrVarInfoSbt() VarInfoSbt

PtrVarVarInfoSbt 将SymbolInfo的Info字段解释为VarInfoSbt,并返回

func (SymbolInfo) String

func (s SymbolInfo) String() string

func (*SymbolInfo) Type

func (s *SymbolInfo) Type() string

type Tree

type Tree struct {
	//自己导入的包
	ImportLoacl *[]data.IsItARecursiveType
	//是否有自动是否块
	IsHaveAutoFree *atomic.Bool
	//符号表
	Sbt *Sbt

	//全局声明数量
	GlobarDeclNum *atomic.Uint32
	//自己及依赖导入的包
	ImportPackage *map[string]*Package
	//是否有init函数
	IsInitFunc *atomic.Bool
	//变量初始化表
	VarInitTable_File *VarInitTable
	//在自动是否块被调用的函数名
	InAutoFreeFuncCallName *data.RemoveDupStrck[FuncNameNode]
	//导入表
	ImportTable *Sbt
	//泛型表
	GenericTable *Sbt
	//泛型实例化节点
	GenInstNodes *Sbt
	//包名
	PackageName string
	//文件名称
	Filename string
	//所有节点
	Nodes []Node

	//是否并发
	Thread bool
	// contains filtered or unexported fields
}

一个抽象语法树

func NewTree

func NewTree(ft *lex.FileToken, errctx *errcode.ErrCtx, Thread bool) *Tree

NewTree 创建抽象语法树,从词法分析结果开始

  • ft是词法分析结果
  • errctx是错误处理上下文
  • Thread控制是否并发

func (*Tree) FindTree

func (t *Tree) FindTree(file string) *Tree

func (*Tree) GerImportPackage

func (t *Tree) GerImportPackage() *map[string]*Package

func (*Tree) GetImportLoacl

func (t *Tree) GetImportLoacl() *[]data.IsItARecursiveType

func (*Tree) GetImportTable

func (t *Tree) GetImportTable() *Sbt

func (*Tree) Panic

func (t *Tree) Panic(line *lex.Line, msg errcode.Msg, err ...errcode.ErrCode)

报错

func (*Tree) String

func (t *Tree) String() string

type VarInfoSbt

type VarInfoSbt struct {
	Name string
	Type astdata.Typ
	//后两个用于出错时报错
	FileName string
	LineNum  int
	//是否是全局
	IsStackAlloc bool
}

VarInfoSbt 变量符号信息

func NewVarInfoSbt

func NewVarInfoSbt(Name string, Type astdata.Typ, IsStackAlloc bool, LineNum int, FileName string) VarInfoSbt

创建变量符号信息

func (VarInfoSbt) String

func (info VarInfoSbt) String() string

type VarInitTable

type VarInitTable struct {
	data.Slice[ASSIGNInfo]
	// contains filtered or unexported fields
}

VarInitTable 变量初始化表 实现了 sort.Interface 接口

func NewVarInitTable

func NewVarInitTable(Thread bool) VarInitTable

NewVarInitTable 创建变量初始化表

func (*VarInitTable) InitOrder

func (t *VarInitTable) InitOrder() []ASSIGNInfo

InitOrder 返回按行升序排列的变量初始化信息

func (*VarInitTable) Len

func (t *VarInitTable) Len() int

func (*VarInitTable) Less

func (t *VarInitTable) Less(i, j int) bool

func (*VarInitTable) Swap

func (t *VarInitTable) Swap(i, j int)

type VarNode

type VarNode struct {
	TYPE  astdata.Typ //变量类型
	Value Expr        //值
	Name  string      //变量名
	//后两个用于出错时报错
	FileName string
	LineNum  int
	IsFunc   bool
}

变量声明节点

func NewVarNode

func NewVarNode(name string, Type astdata.Typ, IsFunc bool, FileName string, LineNum int) *VarNode

NewVarNode 创建变量声明节点

func ParserVAR

func ParserVAR(t *Tree, line *lex.Line, FuncInfo *FuncInfo, sbt *Sbt, InAutoFree bool) *VarNode

解析变量 实现假定 line[0].Value[0].TYPE==VAR

func (*VarNode) String

func (n *VarNode) String() string

func (*VarNode) Type

func (n *VarNode) Type() NodeEnum

Jump to

Keyboard shortcuts

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