symbol

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2022 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PrimitiveInt    = "int"
	PrimitiveString = "string"
	PrimitiveFloat  = "float"
	PrimitiveBool   = "bool"
	PrimitiveNil    = "nil"
	PrimitiveAny    = "any"
	PrimitiveByte   = "byte"
)
View Source
const OperatorName = "operator"

Variables

View Source
var (
	Int    = &PrimitiveType{name: PrimitiveInt}
	String = &PrimitiveType{name: PrimitiveString}
	Float  = &PrimitiveType{name: PrimitiveFloat}
	Bool   = &PrimitiveType{name: PrimitiveBool}
	Byte   = &PrimitiveType{name: PrimitiveByte}
	Void   = &VoidType{}
	Nil    = &PrimitiveType{name: PrimitiveNil}
	Any    = &PrimitiveType{name: PrimitiveAny}
)

Functions

func GetType added in v0.0.8

func GetType(t1, t2 Type, left, right interface{}) (Type, Type)

GetType 运行时根据实际值推算出类型 *

if( n[0] + n[1] == 3){
	println(3);
}

推断出 n[0] + n[1] 的 type 为 int,因为这个表达式 type 在编译器计算不出来

func MatchNil added in v0.0.6

func MatchNil(t Type) bool

MatchNil nil 可以满足所有类型,除了基本类型

func Value2Float added in v0.0.6

func Value2Float(v interface{}) float64

Types

type BlockScope

type BlockScope struct {
	// contains filtered or unexported fields
}

func (BlockScope) AddSymbol

func (s BlockScope) AddSymbol(symbol Symbol)

func (BlockScope) ContainsSymbol

func (s BlockScope) ContainsSymbol(symbol Symbol) bool

func (BlockScope) GetClass added in v0.0.6

func (s BlockScope) GetClass(name string) *Class

func (BlockScope) GetCtx

func (s BlockScope) GetCtx() antlr.ParserRuleContext

func (BlockScope) GetFunction

func (s BlockScope) GetFunction(name string, paramTypes []Type) *Func

func (BlockScope) GetFunctionVariable added in v0.0.6

func (s BlockScope) GetFunctionVariable(name string, paramTypes []Type) *Variable

func (BlockScope) GetSymbols

func (s BlockScope) GetSymbols() []Symbol

func (BlockScope) GetVariable

func (s BlockScope) GetVariable(name string) *Variable

func (BlockScope) SetCtx

func (s BlockScope) SetCtx(ctx antlr.ParserRuleContext)

func (*BlockScope) String

func (v *BlockScope) String() string

type Class added in v0.0.6

type Class struct {
	// contains filtered or unexported fields
}

func GetClass added in v0.0.6

func GetClass(scope Scope, name string) *Class

GetClass 在 scope 中查找 class 的 symbol

func NewClass added in v0.0.6

func NewClass(ctx antlr.ParserRuleContext, name string) *Class

func (*Class) AddSymbol added in v0.0.6

func (c *Class) AddSymbol(symbol Symbol)

AddSymbol 要实现改函数,不然走的是 scope 的函数

func (*Class) ContainsSymbol added in v0.0.6

func (c *Class) ContainsSymbol(symbol Symbol) bool

func (Class) GetClass added in v0.0.6

func (s Class) GetClass(name string) *Class

func (*Class) GetClassFunctionVariable added in v0.0.6

func (c *Class) GetClassFunctionVariable(name string, paramTypes []Type) *Variable

GetClassFunctionVariable 查找类中的函数变量

func (*Class) GetConstructorFunc added in v0.0.6

func (c *Class) GetConstructorFunc(paramTypes []Type) *Func

GetConstructorFunc 查找显式的构造函数,函数名称就是类名称

func (Class) GetCtx added in v0.0.6

func (s Class) GetCtx() antlr.ParserRuleContext

func (*Class) GetDefaultConstructorFunc added in v0.0.6

func (c *Class) GetDefaultConstructorFunc() *DefaultConstructorFunc

func (*Class) GetFunction added in v0.0.6

func (c *Class) GetFunction(name string, paramTypes []Type) *Func

GetFunction 查找类中的函数

func (Class) GetFunctionVariable added in v0.0.6

func (s Class) GetFunctionVariable(name string, paramTypes []Type) *Variable

func (Class) GetSymbols added in v0.0.6

func (s Class) GetSymbols() []Symbol

func (*Class) GetVariable added in v0.0.6

func (c *Class) GetVariable(name string) *Variable

GetVariable 在 class 中查找变量

func (*Class) IsArray added in v0.0.11

func (c *Class) IsArray() bool

func (*Class) IsType added in v0.0.6

func (c *Class) IsType(t Type) bool

func (*Class) SetArray added in v0.0.11

func (c *Class) SetArray(isArray bool)

func (Class) SetCtx added in v0.0.6

func (s Class) SetCtx(ctx antlr.ParserRuleContext)

func (*Class) String added in v0.0.6

func (c *Class) String() string

type DeclareFunctionType added in v0.0.6

type DeclareFunctionType struct {
	// contains filtered or unexported fields
}

func NewDeclareFunctionType added in v0.0.6

func NewDeclareFunctionType(returnType Type) *DeclareFunctionType

NewDeclareFunctionType 函数类型的变量 func int(int) f2 = f1(); f2 的类型

func (*DeclareFunctionType) AppendParameterType added in v0.0.6

func (d *DeclareFunctionType) AppendParameterType(t Type)

func (*DeclareFunctionType) GetEncloseScope added in v0.0.6

func (d *DeclareFunctionType) GetEncloseScope() Scope

func (*DeclareFunctionType) GetName added in v0.0.6

func (d *DeclareFunctionType) GetName() string

func (*DeclareFunctionType) GetParameterType added in v0.0.6

func (d *DeclareFunctionType) GetParameterType() []Type

func (*DeclareFunctionType) GetReturnType added in v0.0.6

func (d *DeclareFunctionType) GetReturnType() Type

func (*DeclareFunctionType) IsArray added in v0.0.11

func (d *DeclareFunctionType) IsArray() bool

func (*DeclareFunctionType) IsType added in v0.0.6

func (d *DeclareFunctionType) IsType(t Type) bool

func (*DeclareFunctionType) MatchParameterTypes added in v0.0.6

func (d *DeclareFunctionType) MatchParameterTypes(types []Type) bool

func (*DeclareFunctionType) SetArray added in v0.0.11

func (d *DeclareFunctionType) SetArray(isArray bool)

type DefaultConstructorFunc added in v0.0.6

type DefaultConstructorFunc struct {
	// contains filtered or unexported fields
}

func NewDefaultConstructorFunc added in v0.0.6

func NewDefaultConstructorFunc(name string, class *Class) *DefaultConstructorFunc

func (DefaultConstructorFunc) AddSymbol added in v0.0.6

func (s DefaultConstructorFunc) AddSymbol(symbol Symbol)

func (DefaultConstructorFunc) ContainsSymbol added in v0.0.6

func (s DefaultConstructorFunc) ContainsSymbol(symbol Symbol) bool

func (*DefaultConstructorFunc) GetClass added in v0.0.6

func (d *DefaultConstructorFunc) GetClass() *Class

GetClass 获取构造函数对应的 class

func (DefaultConstructorFunc) GetCtx added in v0.0.6

func (s DefaultConstructorFunc) GetCtx() antlr.ParserRuleContext

func (DefaultConstructorFunc) GetFunction added in v0.0.6

func (s DefaultConstructorFunc) GetFunction(name string, paramTypes []Type) *Func

func (DefaultConstructorFunc) GetFunctionVariable added in v0.0.6

func (s DefaultConstructorFunc) GetFunctionVariable(name string, paramTypes []Type) *Variable

func (DefaultConstructorFunc) GetSymbols added in v0.0.6

func (s DefaultConstructorFunc) GetSymbols() []Symbol

func (DefaultConstructorFunc) GetVariable added in v0.0.6

func (s DefaultConstructorFunc) GetVariable(name string) *Variable

func (DefaultConstructorFunc) SetCtx added in v0.0.6

func (s DefaultConstructorFunc) SetCtx(ctx antlr.ParserRuleContext)

func (*DefaultConstructorFunc) String added in v0.0.6

func (d *DefaultConstructorFunc) String() string

type Func

type Func struct {
	// contains filtered or unexported fields
}

func GetFunction

func GetFunction(scope Scope, name string, paramTypes []Type) *Func

GetFunction 在 scope 中查询函数,需要入参相同

func NewFunc

func NewFunc(ctx antlr.ParserRuleContext, name string, encloseScope Scope) *Func

func (Func) AddSymbol

func (s Func) AddSymbol(symbol Symbol)

func (*Func) AppendParameter

func (f *Func) AppendParameter(v *Variable)

func (Func) ContainsSymbol

func (s Func) ContainsSymbol(symbol Symbol) bool

func (Func) GetClass added in v0.0.6

func (s Func) GetClass(name string) *Class

func (*Func) GetClosureVar added in v0.0.6

func (f *Func) GetClosureVar() container.Set

func (Func) GetCtx

func (s Func) GetCtx() antlr.ParserRuleContext

func (*Func) GetEncloseScope

func (f *Func) GetEncloseScope() Scope

func (Func) GetFunction

func (s Func) GetFunction(name string, paramTypes []Type) *Func

func (Func) GetFunctionVariable added in v0.0.6

func (s Func) GetFunctionVariable(name string, paramTypes []Type) *Variable

func (*Func) GetName

func (f *Func) GetName() string

func (*Func) GetParameterType

func (f *Func) GetParameterType() []Type

GetParameterType 获取和初始化

func (*Func) GetReturnType

func (f *Func) GetReturnType() Type

func (Func) GetSymbols

func (s Func) GetSymbols() []Symbol

func (Func) GetVariable

func (s Func) GetVariable(name string) *Variable

func (*Func) IsArray added in v0.0.11

func (f *Func) IsArray() bool

func (*Func) IsConstructor added in v0.0.6

func (f *Func) IsConstructor() bool

IsConstructor 当前函数是否为构造函数,也就是函数名称是否与函数声明所在的 class 名称相同

func (*Func) IsMethod added in v0.0.6

func (f *Func) IsMethod() bool

IsMethod 是否是类的函数

func (*Func) IsType

func (f *Func) IsType(t Type) bool

func (*Func) MatchParameterTypes

func (f *Func) MatchParameterTypes(paramTypes []Type) bool

MatchParameterTypes 通过参数类型匹配函数是否一致

func (*Func) SetArray added in v0.0.11

func (f *Func) SetArray(isArray bool)

func (*Func) SetClosureVar added in v0.0.6

func (f *Func) SetClosureVar(closureVar container.Set)

func (Func) SetCtx

func (s Func) SetCtx(ctx antlr.ParserRuleContext)

func (*Func) SetEncloseScope

func (f *Func) SetEncloseScope(scope Scope)

func (*Func) SetName

func (f *Func) SetName(name string)

func (*Func) SetReturnType

func (f *Func) SetReturnType(t Type)

func (*Func) String

func (f *Func) String() string

type FuncType

type FuncType interface {
	Type
	GetReturnType() Type

	GetParameterType() []Type

	// MatchParameterTypes 检查参数类型是否和函数匹配
	MatchParameterTypes(types []Type) bool
}

type OpOverload added in v0.0.7

type OpOverload struct {
	// contains filtered or unexported fields
}

OpOverload 重载符

func NewOpOverload added in v0.0.7

func NewOpOverload(function *Func, tokenType int) *OpOverload

func (*OpOverload) GetFunc added in v0.0.7

func (o *OpOverload) GetFunc() *Func

func (*OpOverload) GetTokenType added in v0.0.7

func (o *OpOverload) GetTokenType() int

type PrimitiveType

type PrimitiveType struct {
	// contains filtered or unexported fields
}

func (*PrimitiveType) GetEncloseScope

func (b *PrimitiveType) GetEncloseScope() Scope

func (*PrimitiveType) GetName

func (b *PrimitiveType) GetName() string

func (*PrimitiveType) IsArray added in v0.0.6

func (b *PrimitiveType) IsArray() bool

func (*PrimitiveType) IsType

func (b *PrimitiveType) IsType(t Type) bool

func (*PrimitiveType) SetArray added in v0.0.11

func (b *PrimitiveType) SetArray(isArray bool)

func (*PrimitiveType) String

func (b *PrimitiveType) String() string

type Scope

type Scope interface {
	Symbol
	AddSymbol(symbol Symbol)
	ContainsSymbol(symbol Symbol) bool
	GetSymbols() []Symbol
	SetCtx(ctx antlr.ParserRuleContext)
	GetCtx() antlr.ParserRuleContext
	GetVariable(name string) *Variable
	GetFunction(name string, paramTypes []Type) *Func
	GetFunctionVariable(name string, paramTypes []Type) *Variable
	GetClass(name string) *Class
	String() string
}

func NewBlockScope

func NewBlockScope(ctx antlr.ParserRuleContext, name string, s Scope) Scope

type Symbol

type Symbol interface {
	SetName(name string)
	GetName() string
	GetEncloseScope() Scope
	SetEncloseScope(scope Scope)
}

type Type

type Type interface {
	GetName() string
	GetEncloseScope() Scope

	// IsType 判断类型是否相同,或者是不是 is-a ,是否为子类
	IsType(t Type) bool

	IsArray() bool
	SetArray(isArray bool)
}

func GetUpperType added in v0.0.6

func GetUpperType(ctx antlr.ParserRuleContext, t1, t2 Type) Type

GetUpperType 根据两个参数类型,推导返回的类型

func GetUpperTypeWithValue added in v0.0.8

func GetUpperTypeWithValue(ctx antlr.ParserRuleContext, v1, v2 interface{}) Type

GetUpperTypeWithValue 根据两个参数值,返回推到类型

func NewPrimitiveType added in v0.0.12

func NewPrimitiveType(name string, isArray bool) Type

func NewPrimitiveTypeWithType added in v0.0.12

func NewPrimitiveTypeWithType(t Type) Type

type Variable

type Variable struct {
	// contains filtered or unexported fields
}

func GetFunctionVariable added in v0.0.6

func GetFunctionVariable(scope Scope, name string, paramTypes []Type) *Variable

GetFunctionVariable 在当前 scope 中查找函数变量,其中变量中的函数的入参得匹配。

func GetVariable

func GetVariable(scope Scope, name string) *Variable

GetVariable 从 scope 中通过变量名称查询变量

func NewVariable

func NewVariable(ctx antlr.ParserRuleContext, name string, enclose Scope, isVariableArgs bool) *Variable

func (Variable) GetEncloseScope

func (s Variable) GetEncloseScope() Scope

func (Variable) GetName

func (s Variable) GetName() string

func (*Variable) GetType

func (v *Variable) GetType() Type

func (*Variable) IsArray added in v0.0.11

func (v *Variable) IsArray() bool

func (*Variable) SetArray added in v0.0.11

func (v *Variable) SetArray(isArray bool)

func (Variable) SetEncloseScope

func (s Variable) SetEncloseScope(scope Scope)

func (Variable) SetName

func (s Variable) SetName(name string)

func (*Variable) SetType

func (v *Variable) SetType(t Type)

func (*Variable) String

func (v *Variable) String() string

type VoidType

type VoidType struct {
}

func (*VoidType) GetEncloseScope

func (v *VoidType) GetEncloseScope() Scope

func (*VoidType) GetName

func (v *VoidType) GetName() string

func (*VoidType) IsArray added in v0.0.11

func (v *VoidType) IsArray() bool

func (*VoidType) IsType

func (v *VoidType) IsType(t Type) bool

func (*VoidType) SetArray added in v0.0.11

func (v *VoidType) SetArray(isArray bool)

Jump to

Keyboard shortcuts

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