提交 69671b1d 编写于 作者: 3 3dgen

添加ctypes包

上级 046f84f0
//ctypes包定义C-IR的类型
package ctypes
import (
"fmt"
"strings"
"github.com/wa-lang/wa/internal/logger"
)
/**************************************
Type: C-IR类型接口
以下类型实现了 Type 接口:
*types.VoidType
*types.FuncType
*types.IntType
*types.FloatType
**************************************/
type Type interface {
fmt.Stringer
// CIRString 返回该类型的CIR表达
CIRString() string
// Name 返回该类型的名字,例如字符串类型的Name()=="string",而其CIRString()=="std::string"
Name() string
// Equal 判断u是否与当前类型相同
Equal(u Type) bool
}
var (
Void = &VoidType{}
Bool = &BoolType{}
Int8 = &IntType{Kind: IntKindI8}
Uint8 = &IntType{Kind: IntKindU8}
Int16 = &IntType{Kind: IntKindI16}
Uint16 = &IntType{Kind: IntKindU16}
Int32 = &IntType{Kind: IntKindI32}
Uint32 = &IntType{Kind: IntKindU32}
Int64 = &IntType{Kind: IntKindI64}
Uint64 = &IntType{Kind: IntKindU64}
Float = &FloatType{Kind: FloatKindFloat}
Double = &FloatType{Kind: FloatKindDouble}
)
/**************************************
VoidType:
**************************************/
type VoidType struct {
}
// String 返回该类型的字符串表达
func (t *VoidType) String() string {
return t.Name()
}
// CIRString 返回该类型的CIR语法表达
func (t *VoidType) CIRString() string {
return "void"
}
func (t *VoidType) Name() string {
return "void"
}
// Equal 判断u是否与当前类型相同
func (t *VoidType) Equal(u Type) bool {
if _, ok := u.(*VoidType); ok {
return true
}
return false
}
/**************************************
BoolType:
**************************************/
type BoolType struct {
}
// String 返回该类型的字符串表达
func (t *BoolType) String() string {
return t.Name()
}
// CIRString 返回该类型的CIR语法表达
func (t *BoolType) CIRString() string {
return "bool"
}
func (t *BoolType) Name() string {
return "bool"
}
// Equal 判断u是否与当前类型相同
func (t *BoolType) Equal(u Type) bool {
if _, ok := u.(*BoolType); ok {
return true
}
return false
}
/**************************************
FuncType:
**************************************/
type FuncType struct {
// 返回值类型
Ret Type
// 参数
Params []Type
}
// NewFunc 根据指定的返回值和参数类型创建函数类型
func NewFuncType(ret Type, params []Type) *FuncType {
return &FuncType{
Ret: ret,
Params: params,
}
}
// String 返回该类型的字符串表达
func (t *FuncType) String() string {
return t.CIRString()
}
// CIRString 返回该类型的CIR语法表达
func (t *FuncType) CIRString() string {
buf := &strings.Builder{}
fmt.Fprintf(buf, "%s (", t.Ret.CIRString())
for i, param := range t.Params {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(param.String())
}
buf.WriteString(")")
return buf.String()
}
func (t *FuncType) Name() string {
logger.Fatal("Todo: FuncType.Name()")
return ""
}
// Equal 判断u是否与当前类型相同
func (t *FuncType) Equal(u Type) bool {
if u, ok := u.(*FuncType); ok {
if !t.Ret.Equal(u.Ret) {
return false
}
if len(t.Params) != len(u.Params) {
return false
}
for i := range t.Params {
if !t.Params[i].Equal(u.Params[i]) {
return false
}
}
return true
}
return false
}
/**************************************
IntType:
**************************************/
type IntType struct {
// 整数类型
Kind IntKind
}
// IntKind: 整数类型
type IntKind uint8
const (
// 8位有符号整数
IntKindI8 IntKind = iota
// 8位无符号整数
IntKindU8
// 16位有符号整数
IntKindI16
// 16位无符号整数
IntKindU16
// 32位有符号整数
IntKindI32
// 32位无符号整数
IntKindU32
// 64位有符号整数
IntKindI64
// 64位无符号整数
IntKindU64
)
var intKindName = [...][2]string{
IntKindI8: {"int8_t", "int8"},
IntKindU8: {"uint8_t", "uint8"},
IntKindI16: {"int16_t", "int16"},
IntKindU16: {"uint16_t", "uint16"},
IntKindI32: {"int32_t", "int32"},
IntKindU32: {"uint32_t", "uint32"},
IntKindI64: {"int64_t", "int64"},
IntKindU64: {"uint64_t", "uint64"},
}
func (i IntKind) cirString() string {
if int(i) >= len(intKindName) {
panic("Invalid IntKind.")
}
return intKindName[i][0]
}
func (i IntKind) name() string {
if int(i) >= len(intKindName) {
panic("Invalid IntKind.")
}
return intKindName[i][1]
}
// GetBitSize() 返回整数的位宽
func (i IntKind) GetBitSize() int {
_IntKindSize := [...]int{
IntKindI8: 8,
IntKindU8: 8,
IntKindI16: 16,
IntKindU16: 16,
IntKindI32: 32,
IntKindU32: 32,
IntKindI64: 64,
IntKindU64: 64,
}
if int(i) >= len(_IntKindSize) {
panic("Invalid IntKind.")
}
return _IntKindSize[i]
}
// NewInt 根据给定参数创建整数类型
func NewInt(kind IntKind) *IntType {
return &IntType{
Kind: kind,
}
}
// String 返回该类型的字符串表达
func (t *IntType) String() string {
return t.Name()
}
// CIRString 返回该类型的CIR语法表达
func (t *IntType) CIRString() string {
return t.Kind.cirString()
}
func (t *IntType) Name() string {
return t.Kind.name()
}
// Equal 判断u是否与当前类型相同
func (t *IntType) Equal(u Type) bool {
if u, ok := u.(*IntType); ok {
return u.Kind == t.Kind
}
return false
}
/*-----------------------------------*/
// FloatType: C-IR的浮点数类型
type FloatType struct {
// 浮点数类型
Kind FloatKind
}
// FloatKind: 浮点数类型
type FloatKind uint8
const (
// 32位浮点数,IEEE 754 单精度
FloatKindFloat FloatKind = iota
// 64位浮点数,IEEE 754 双精度
FloatKindDouble
)
func (i FloatKind) String() string {
_FloatKindName := [...]string{
FloatKindFloat: "float",
FloatKindDouble: "double",
}
if i >= FloatKind(len(_FloatKindName)) {
panic("Invalid FloatKind.")
}
return _FloatKindName[i]
}
// NewFloat 根据给定参数创建浮点数类型
func NewFloat(kind FloatKind) *FloatType {
return &FloatType{
Kind: kind,
}
}
// String 返回该类型的字符串表达
func (t *FloatType) String() string {
return t.Name()
}
// CIRString 返回该类型的CIR语法表达
func (t *FloatType) CIRString() string {
return t.Kind.String()
}
func (t *FloatType) Name() string {
return t.Kind.String()
}
// Equal 判断u是否与当前类型相同
func (t *FloatType) Equal(u Type) bool {
if u, ok := u.(*FloatType); ok {
return u.Kind == t.Kind
}
return false
}
/**************************************
PointerType:
**************************************/
type PointerType struct {
Base Type
}
func NewPointerType(base Type) *PointerType {
return &PointerType{Base: base}
}
// String 返回该类型的字符串表达
func (t *PointerType) String() string {
return t.CIRString()
}
// CIRString 返回该类型的CIR语法表达
func (t *PointerType) CIRString() string {
return t.Base.CIRString() + "*"
}
func (t *PointerType) Name() string {
logger.Fatal("Todo: PointerType.Name()")
return ""
}
// Equal 判断u是否与当前类型相同
func (t *PointerType) Equal(u Type) bool {
if ut, ok := u.(*PointerType); ok {
return t.Base.Equal(ut.Base)
}
return false
}
/**************************************
RefType:
**************************************/
type RefType struct {
Base Type
}
func NewRefType(base Type) *RefType {
return &RefType{Base: base}
}
// String 返回该类型的字符串表达
func (t *RefType) String() string {
return t.CIRString()
}
// CIRString 返回该类型的CIR语法表达
func (t *RefType) CIRString() string {
return "$wartc::Ref<" + t.Base.CIRString() + ">"
}
func (t *RefType) Name() string {
logger.Fatal("Todo: RefType.Name()")
return ""
}
// Equal 判断u是否与当前类型相同
func (t *RefType) Equal(u Type) bool {
if ut, ok := u.(*RefType); ok {
return t.Base.Equal(ut.Base)
}
return false
}
/**************************************
StringType:
**************************************/
type StringType struct {
}
// String 返回该类型的字符串表达
func (t *StringType) String() string {
return t.CIRString()
}
// CIRString 返回该类型的CIR语法表达
func (t *StringType) CIRString() string {
return "std::string"
}
func (t *StringType) Name() string {
return "string"
}
// Equal 判断u是否与当前类型相同
func (t *StringType) Equal(u Type) bool {
if _, ok := u.(*StringType); ok {
return true
}
return false
}
package ctypes
import (
"strconv"
"github.com/wa-lang/wa/internal/logger"
)
/**************************************
Tuple:
**************************************/
type Tuple struct {
Vars []Type
}
func NewTuple(v []Type) *Tuple {
return &Tuple{Vars: v}
}
func (t *Tuple) String() string {
return t.CIRString()
}
func (t *Tuple) CIRString() string {
s := "$TupleStart_"
for _, n := range t.Vars {
s += n.Name()
s += "_"
}
s += "$TupleEnd"
return s
}
func (t *Tuple) Name() string {
return t.CIRString()
}
func (t *Tuple) Equal(u Type) bool {
if u, ok := u.(*Tuple); ok {
return t.CIRString() == u.CIRString()
}
return false
}
func (t *Tuple) GenStruct() Struct {
var m []Field
for i, v := range t.Vars {
m = append(m, *NewField("$m"+strconv.Itoa(i), v))
}
return *NewStruct(t.CIRString(), m)
}
/**************************************
Struct:
**************************************/
type Field struct {
name string
typ Type
}
func (i *Field) CIRString() string {
return i.name
}
func NewField(n string, t Type) *Field {
return &Field{name: n, typ: t}
}
func (i *Field) Type() Type {
return i.typ
}
type Struct struct {
name string
Members []Field
}
func NewStruct(name string, m []Field) *Struct {
return &Struct{name: name, Members: m}
}
func (t *Struct) String() string {
return t.CIRString()
}
func (t *Struct) CIRString() string {
return t.name
}
func (t *Struct) Name() string {
return t.CIRString()
}
func (t *Struct) Equal(u Type) bool {
if u, ok := u.(*Struct); ok {
return t.CIRString() == u.CIRString()
}
return false
}
/**************************************
Array:
**************************************/
type Array struct {
len int64
elem Type
}
func NewArray(len int64, elem Type) *Array {
return &Array{len: len, elem: elem}
}
func (t *Array) GetElem() Type {
return t.elem
}
func (t *Array) GetLen() int64 {
return t.len
}
func (t *Array) String() string {
return t.CIRString()
}
func (t *Array) CIRString() string {
return "std::array<" + t.elem.CIRString() + ", " + strconv.Itoa(int(t.len)) + ">"
}
func (t *Array) Name() string {
return t.CIRString()
}
func (t *Array) Equal(u Type) bool {
if u, ok := u.(*Array); ok {
return t.CIRString() == u.CIRString()
}
return false
}
/**************************************
Slice:
**************************************/
type Slice struct {
elem Type
}
func NewSlice(elem Type) *Slice {
return &Slice{elem: elem}
}
func (t *Slice) GetElem() Type {
return t.elem
}
func (t *Slice) String() string {
return t.CIRString()
}
func (t *Slice) CIRString() string {
return "$wartc::Slice<" + t.elem.CIRString() + ">"
}
func (t *Slice) Name() string {
logger.Fatal("Todo: Slice.Name()")
return ""
}
func (t *Slice) Equal(u Type) bool {
if u, ok := u.(*Slice); ok {
return t.elem.Equal(u.elem)
}
return false
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册