Commit 4ad2dd7f authored by Hugo Moreau's avatar Hugo Moreau
Browse files

Double brackets: Handling complex nested brackets

Double brackets transform can now handle IndexExpr using IndexExpr
recusrively in order to use an array as an index.

 * transform/double_brackets.go : Here.
parent 5b63fa54
Pipeline #27065 passed with stage
in 2 minutes and 39 seconds
...@@ -29,10 +29,12 @@ The DoubleBrackets's purpose is to integrate array ...@@ -29,10 +29,12 @@ The DoubleBrackets's purpose is to integrate array
Before: Before:
``` ```
G2PState[1][GP2State[2]] = 1 G2PState[1][GP2State[2]] = 1
G2PState[1][G2PState[2][1]] = 1
``` ```
After: After:
``` ```
G2PState[1 + GP2State[2]] = 1 G2PState[1 + GP2State[2]] = 1
G2PState[1 + G2PState[2 + 1]] = 1
*/ */
type DoubleBrackets struct { type DoubleBrackets struct {
...@@ -44,22 +46,7 @@ func (t *DoubleBrackets) Pre(meta *Meta, v *Visitor) bool { ...@@ -44,22 +46,7 @@ func (t *DoubleBrackets) Pre(meta *Meta, v *Visitor) bool {
c := v.Cursor() c := v.Cursor()
switch node := c.Node().(type) { switch node := c.Node().(type) {
case *ast.IndexExpr: case *ast.IndexExpr:
switch inner := node.X.(type) { c.Replace(indexExprRec(node, nil))
case *ast.IndexExpr:
switch name := inner.X.(type) {
case *ast.Ident:
c.Replace(&ast.IndexExpr{
X: name,
Index: &ast.BinaryExpr{
X: node.Index,
Op: token.ADD,
Y: inner.Index,
},
})
default:
panic("Complex nested brackets: Unhandled by Go2Pins")
}
}
} }
return true return true
} }
...@@ -68,3 +55,26 @@ func (t *DoubleBrackets) Pre(meta *Meta, v *Visitor) bool { ...@@ -68,3 +55,26 @@ func (t *DoubleBrackets) Pre(meta *Meta, v *Visitor) bool {
func (t *DoubleBrackets) Post(meta *Meta, v *Visitor) bool { func (t *DoubleBrackets) Post(meta *Meta, v *Visitor) bool {
return true return true
} }
func indexExprRec(node, father *ast.IndexExpr) *ast.IndexExpr {
switch inner := node.X.(type) {
case *ast.IndexExpr:
node = indexExprRec(inner, node)
case *ast.Ident:
if father != nil {
switch ind := father.Index.(type) {
case *ast.BasicLit:
if ind.Value == "0" {
return node
}
}
node.Index =
&ast.BinaryExpr{
X: father.Index,
Op: token.ADD,
Y: node.Index,
}
}
}
return node
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment