ast

package
v0.0.0-...-a214d60 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2024 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Overview

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

创建抽象语法树

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

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

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

辅助语义检查

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

Index

Constants

This section is empty.

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 Num0 = NewObject(INTOBJ, "0")
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) C

func (a *ASSIGNNode) C(buf *strings.Builder)

func (*ASSIGNNode) SetNosemicolon

func (a *ASSIGNNode) SetNosemicolon(nosemicolon bool)

func (*ASSIGNNode) String

func (a *ASSIGNNode) String() string

type AssignmentNode

type AssignmentNode struct {
	//目的操作数,源操作数
	Dest Expr
	Src  Expr
	// contains filtered or unexported fields
}

赋值节点

type AutoFreeNode

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

自动释放块节点

func NewAutoFreeNode

func NewAutoFreeNode(FileName string) *AutoFreeNode

func (*AutoFreeNode) C

func (a *AutoFreeNode) C(buf *strings.Builder)

func (*AutoFreeNode) String

func (a *AutoFreeNode) String() string

type BreakStmt

type BreakStmt struct{}

break语句

func NewBreakStmt

func NewBreakStmt() BreakStmt

func (BreakStmt) C

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

func (BreakStmt) String

func (b BreakStmt) String() string

type CDecl

type CDecl interface {
	Node
	CDecl(buf *strings.Builder)
}

CDecl 表示一个支持生成C头文件的一个声明语句的类型

type CHeaderFile

type CHeaderFile struct {
	// 目前可能的节点
	// - 常量节点
	// - 变量节点
	// - 结构体节点
	// - 函数节点
	// - 方法节点
	// - 枚举声明
	N CDecl
	//line 行数-1的值
	Line int
}

type CType

type CType interface {
	CType(buf *strings.Builder)
}

type CallNode

type CallNode struct {
	FuncName      FuncNameNode //调用函数名
	Parame        []Expr       //Parameter
	CCallFuncName string
	InAutoFree    bool
	// contains filtered or unexported fields
}

函数调用节点

func NewCallExpr

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

NewCallNode 创建函数调用节点

func (*CallNode) C

func (a *CallNode) C(buf *strings.Builder)

func (*CallNode) SetNosemicolon

func (c *CallNode) SetNosemicolon(nosemicolon bool)

func (*CallNode) String

func (a *CallNode) String() string

type CaseNode

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

case节点

func NewCaseNode

func NewCaseNode() *CaseNode

func (*CaseNode) C

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

func (*CaseNode) String

func (c *CaseNode) String() string

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) C

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

func (*ConstNode) CDecl

func (c *ConstNode) CDecl(buf *strings.Builder)

func (*ConstNode) String

func (n *ConstNode) String() string

type ContinueStmt

type ContinueStmt struct{}

continue语句

func NewContinueStmt

func NewContinueStmt() ContinueStmt

func (ContinueStmt) C

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

func (ContinueStmt) String

func (c ContinueStmt) String() string

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

type DefaultNode

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

defaultcase节点

func NewDefaultNode

func NewDefaultNode() *DefaultNode

func (*DefaultNode) C

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

func (*DefaultNode) String

func (d *DefaultNode) String() string

type Dereference

type Dereference struct {
	Value Expr
}

Dereference 解引用节点

func (*Dereference) C

func (d *Dereference) C(buf *strings.Builder)

func (*Dereference) String

func (d *Dereference) String() string

type ElseNode

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

else节点

func NewElseNode

func NewElseNode() *ElseNode

NewElseNode 创建else节点

func (*ElseNode) C

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

func (*ElseNode) String

func (l *ElseNode) String() string

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) CDecl

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

func (*EnumDecl) String

func (s *EnumDecl) String() string

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() *ForNode

NewForNode 创建for节点

func (*ForNode) C

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

func (*ForNode) String

func (l *ForNode) String() string

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) *FuncNode

NewFuncNode 创建函数节点

func (*FuncNode) C

func (f *FuncNode) C(buf *strings.Builder)

func (*FuncNode) CDecl

func (f *FuncNode) CDecl(buf *strings.Builder)

func (*FuncNode) String

func (f *FuncNode) String() string

type GenericInstantiation

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

GenericInstantiation 泛型实例化

func (*GenericInstantiation) C

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

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) String

func (g *GotoStmt) String() string

type IfNode

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

if节点

func NewIfNode

func NewIfNode() *IfNode

NewIfNode 创建if节点

func (*IfNode) C

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

func (*IfNode) String

func (l *IfNode) String() string

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) C

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

func (*IndexExpr) String

func (l *IndexExpr) String() string

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) String

func (b *LabelNode) String() string

type MethodNode

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

MethodNode 方法节点

func NewMethodNode

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

NewMethodNode 创建函数节点

func (*MethodNode) C

func (m *MethodNode) C(buf *strings.Builder)

func (*MethodNode) CDecl

func (m *MethodNode) CDecl(buf *strings.Builder)

func (*MethodNode) String

func (m *MethodNode) String() string

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 {
	String() string
	C(buf *strings.Builder)
}

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

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 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) C

func (o *Object) C(buf *strings.Builder)

func (*Object) CType

func (o *Object) CType(buf *strings.Builder)

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

type Objects

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

func NewObjects

func NewObjects(slice []*Object) *Objects

func (*Objects) C

func (o *Objects) C(buf *strings.Builder)

func (*Objects) CType

func (o *Objects) CType(buf *strings.Builder)

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

type OpExpr

type OpExpr = OperationExpr

运算表达式节点

func NewOpExpr

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

NewOpExpr 创建运算表达式节点

func (*OpExpr) C

func (a *OpExpr) C(buf *strings.Builder)

func (*OpExpr) String

func (a *OpExpr) String() string

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]
	//是否有init函数
	IsInitFunc atomic.Bool
	//是否有自动释放块
	IsHaveAutoFree atomic.Bool
	//C代码的头文件包含的节点
	CHeaderFile data.Slice[CHeaderFile]
	//并发
	Thread bool
	// contains filtered or unexported fields
}

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

func NewPackage

func NewPackage(dir string, thread bool, errctx *errcode.ErrCtx, sbt *Sbt, ImportPackage *map[string]*Package) *Package

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

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

func (*Package) AddFile

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

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

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

func (*Package) Dep

func (p *Package) Dep(i int) data.IsItARecursiveType

func (*Package) DepLen

func (p *Package) DepLen() int

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
	//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) String

func (b RbraceNode) String() string

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) C

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

func (*ReturnNode) String

func (l *ReturnNode) String() string

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) 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
	// contains filtered or unexported fields
}

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

func NewSelfOpStmt

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

func (*SelfOpStmt) C

func (o *SelfOpStmt) C(buf *strings.Builder)

func (*SelfOpStmt) SetNosemicolon

func (o *SelfOpStmt) SetNosemicolon(nosemicolon bool)

func (*SelfOpStmt) String

func (o *SelfOpStmt) String() string

type StructDecl

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

StructDecl 结构体声明

缩写:struct declaration 缩写成:StructDecl

func (*StructDecl) C

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

func (*StructDecl) CDecl

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

func (*StructDecl) Dep

func (info *StructDecl) Dep(i int) data.IsItARecursiveType

func (*StructDecl) DepLen

func (info *StructDecl) DepLen() int

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

type SwitchNode

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

switch节点

func NewSwitchNode

func NewSwitchNode() *SwitchNode

func (*SwitchNode) C

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

func (*SwitchNode) String

func (s *SwitchNode) String() string

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

	//自己及依赖导入的包
	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

	//C代码的头文件包含的节点
	CHeaderFile *data.Slice[CHeaderFile]
	//是否并发
	Thread bool
	// contains filtered or unexported fields
}

一个抽象语法树

func NewTree

func NewTree(ft *lex.FileToken, errctx *errcode.ErrCtx, sbt *Sbt, ImportPackage *map[string]*Package) *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      //变量名
	CType string
	//后两个用于出错时报错
	FileName string
	LineNum  int
	IsFunc   bool
	// contains filtered or unexported fields
}

变量声明节点

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) C

func (v *VarNode) C(buf *strings.Builder)

func (*VarNode) CDecl

func (v *VarNode) CDecl(buf *strings.Builder)

func (*VarNode) SetNosemicolon

func (n *VarNode) SetNosemicolon(nosemicolon bool)

func (*VarNode) String

func (n *VarNode) String() string

Jump to

Keyboard shortcuts

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