Skip to content

Commit

Permalink
prepare matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin8105 committed Sep 19, 2024
1 parent 951bec6 commit fa953ba
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
8 changes: 8 additions & 0 deletions tests/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
54 changes: 53 additions & 1 deletion transpiler/declarations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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",
Expand Down
20 changes: 17 additions & 3 deletions types/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fa953ba

Please sign in to comment.