From fa953baf942ad33f53d58e68246bcfefdce0afa2 Mon Sep 17 00:00:00 2001 From: konstantin8105 Date: Thu, 19 Sep 2024 18:27:27 +0300 Subject: [PATCH] prepare matrix --- tests/array.c | 8 ++++++ transpiler/declarations.go | 54 +++++++++++++++++++++++++++++++++++++- types/cast.go | 20 +++++++++++--- 3 files changed, 78 insertions(+), 4 deletions(-) diff --git a/tests/array.c b/tests/array.c index 0dc19f8e..e6f4cd22 100644 --- a/tests/array.c +++ b/tests/array.c @@ -827,6 +827,13 @@ void test_array_nil() } } +// void test_array_init2d() { +// int array2D[30][50]; +// array2D[29][49] = 42; +// int (*pArray2D)[50] = array2D; +// is_eq(pArray2D[29][49], 42); +// } + int main() { plan(222); @@ -1370,6 +1377,7 @@ int main() START_TEST(matrix_init); START_TEST(post_pointer); START_TEST(array_nil); + // TODO: START_TEST(array_init2d); done_testing(); } diff --git a/transpiler/declarations.go b/transpiler/declarations.go index 9a6f9379..ae06974c 100644 --- a/transpiler/declarations.go +++ b/transpiler/declarations.go @@ -708,6 +708,59 @@ func transpileVarDecl(p *program.Program, n *ast.VarDecl) ( preStmts, postStmts = combinePreAndPostStmts(preStmts, postStmts, newPre, newPost) // Allocate slice so that it operates like a fixed size array. + // if baseType, baseSizes := types.GetArrayMartix(n.Type); 1 < len(baseSizes) { + // var goArrayType string + // goArrayType, err = types.ResolveType(p, baseType) + // if err != nil { + // p.AddMessage(p.GenerateWarningMessage(err, n)) + // err = nil // Error is ignored + // } + // if len(baseSizes) == 1 { + // defaultValue = []goast.Expr{ + // util.NewCallExpr( + // "make", + // &goast.ArrayType{ + // Elt: util.NewTypeIdent(goArrayType), + // }, + // util.NewIntLit(baseSizes[0]), + // ), + // } + // } else if strings.Contains(goArrayType, "["){ + // src := fmt.Sprintf(`package main + // func main() { + // x := func() [][]%s { + // as := make([][]%s, %d) + // for i := range as { + // as[i] = make([]%s, %d) + // } + // }() + // }`, + // goArrayType, + // goArrayType, baseSizes[0], + // goArrayType, baseSizes[1], + // ) + // + // // Create the AST by parsing src. + // fset := token.NewFileSet() // positions are relative to fset + // f, err := parser.ParseFile(fset, "", src, 0) + // if err != nil { + // err = fmt.Errorf("cannot parse source \"%s\" : %v", + // src, err) + // p.AddMessage(p.GenerateWarningMessage(err, n)) + // err = nil // ignore error + // } + // if 0 < len(f.Decls) { + // if fd, ok := f.Decls[0].(*goast.FuncDecl); ok { + // if 0 < len(fd.Body.List) { + // if s, ok := fd.Body.List[0].(*goast.AssignStmt); ok { + // defaultValue = s.Rhs + // } + // } + // } + // } + // } + // } + arrayType, arraySize := types.GetArrayTypeAndSize(n.Type) if arraySize != -1 && defaultValue == nil { @@ -717,7 +770,6 @@ func transpileVarDecl(p *program.Program, n *ast.VarDecl) ( p.AddMessage(p.GenerateWarningMessage(err, n)) err = nil // Error is ignored } - defaultValue = []goast.Expr{ util.NewCallExpr( "make", diff --git a/types/cast.go b/types/cast.go index afc9d633..38d8a295 100644 --- a/types/cast.go +++ b/types/cast.go @@ -13,16 +13,30 @@ import ( "github.com/Konstantin8105/c4go/util" ) +func GetArrayMartix(matrixType string) (baseType string, sizes []int) { + for { + inType, inSize := GetArrayTypeAndSize(matrixType) + matrixType= inType + if inSize < 0 { + break + } + sizes = append(sizes, inSize) + } + return matrixType, sizes +} + // GetArrayTypeAndSize returns the size and type of a fixed array. If the type // is not an array with a fixed size then the the size will be -1 and the // returned type should be ignored. -func GetArrayTypeAndSize(s string) (string, int) { +func GetArrayTypeAndSize(s string) (t string, size int) { s = strings.Replace(s, "(", "", -1) s = strings.Replace(s, ")", "", -1) match := util.GetRegex(`([\w\* ]*)\[(\d+)\]((\[\d+\])*)`).FindStringSubmatch(s) - if len(match) > 0 { + if 0 < len(match) { var t = fmt.Sprintf("%s%s", match[1], match[3]) - return strings.Trim(t, " "), util.Atoi(match[2]) + t = strings.Trim(t, " ") + size = util.Atoi(match[2]) + return t, size } return s, -1