diff --git a/pkg/codegen/codegen.go b/pkg/codegen/codegen.go index 7565420..bdf5eef 100644 --- a/pkg/codegen/codegen.go +++ b/pkg/codegen/codegen.go @@ -334,10 +334,14 @@ func (ctx *Context) genStructTypeDeclare(node *mTypes.Node) { for n := node.Child; n != nil; n = n.Next { // Note: is NOT TRUE rootTy, _, _ := mTypes.GetLLVMTypeRec(ctx.mod, n.Type, ctx.prog.Prelude) - typsArr = append(typsArr, rootTy) f := structField[n.Val] f.Pos = pos - f.Type = rootTy + if _, ok := rootTy.(*types.StructType); ok { + f.Type = &types.PointerType{ElemType: rootTy} + } else { + f.Type = rootTy + } + typsArr = append(typsArr, f.Type) structField[n.Val] = f pos++ } diff --git a/pkg/types/ir.go b/pkg/types/ir.go index 837e87d..0ae8725 100644 --- a/pkg/types/ir.go +++ b/pkg/types/ir.go @@ -153,8 +153,21 @@ func GetBitWidth(t types.Type) uint64 { } func GetVectorTypeFromPtr(v value.Value) (*types.StructType, types.Type) { - structPtr := v.Type().(*types.PointerType) - structedVecType := structPtr.ElemType.(*types.StructType) + structPtr, ok := v.Type().(*types.PointerType) + if !ok { + return nil, nil + } + + structedVecType, ok := structPtr.ElemType.(*types.StructType) + if !ok { + return nil, nil + } + + vecTypeField := structedVecType.Fields + if len(vecTypeField) == 0 { + return nil, nil + } + elemType := structedVecType.Fields[0].(*types.PointerType).ElemType return structedVecType, elemType diff --git a/script/test-full.sh b/script/test-full.sh index b076b0f..d89426b 100755 --- a/script/test-full.sh +++ b/script/test-full.sh @@ -282,6 +282,7 @@ testexec(){ assertexec '(defschema Person {:name :: string :age :: int :isMale :: bool})(def main :: int (fn [] (let [node :: Person {:age 20 :name "richard" :isMale true}] (prn (get node :name)) (prn (get node :age)) (prn (get node :isMale))))))' "richard\\\n20\\\ntrue\\\n" assertexec '(defschema Person {:name :: string :age :: int :isMale :: bool :height :: double})(def main :: int (fn [] (let [node :: Person {:age 20 :name "richard" :isMale true :height 5.8}] (prn (get node :name)) (prn (get node :age)) (prn (get node :isMale)) (prn (get node :height))))))' "richard\\\n20\\\ntrue\\\n5.8\\\n" assertexec '(defschema Person {:name :: string :age :: int :isMale :: bool}) (def f :: Person (fn [] (let [node :: Person {:age 20 :name "richard" :isMale true}] node))) (def main :: int (fn [] (prn (get f :age))))' "20\\\n" + assertexec '(defschema Person {:name :: string :age :: int :isMale :: bool :height :: double :v :: [int]}) (def main :: int (fn [] (let [node :: Person {:age 20 :name "richard" :isMale true :height 5.8 :v [4, 3, 2]}] (prn (get node :name)) (prn (get node :age)) (prn (get node :isMale)) (prn (get node :height)) (prn (get node :v)) )))' "richard\\\n20\\\ntrue\\\n5.8\\\n[4, 3, 2]\\\n" ############# # Prelude functions