Commit 35a52b1a authored by Hugo Moreau's avatar Hugo Moreau Committed by Etienne Renault
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 4548aff6
......@@ -29,10 +29,12 @@ The DoubleBrackets's purpose is to integrate array
Before:
```
G2PState[1][GP2State[2]] = 1
G2PState[1][G2PState[2][1]] = 1
```
After:
```
G2PState[1 + GP2State[2]] = 1
G2PState[1 + G2PState[2 + 1]] = 1
*/
type DoubleBrackets struct {
......@@ -44,22 +46,7 @@ func (t *DoubleBrackets) Pre(meta *Meta, v *Visitor) bool {
c := v.Cursor()
switch node := c.Node().(type) {
case *ast.IndexExpr:
switch inner := node.X.(type) {
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")
}
}
c.Replace(indexExprRec(node, nil))
}
return true
}
......@@ -68,3 +55,26 @@ func (t *DoubleBrackets) Pre(meta *Meta, v *Visitor) bool {
func (t *DoubleBrackets) Post(meta *Meta, v *Visitor) bool {
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