Commit a657af9b authored by Etienne Renault's avatar Etienne Renault
Browse files

extract_variables: outdated and no longer in use

* tools/extract_variables/main.go: remove it.
parent 090d649f
Pipeline #11095 passed with stage
in 33 seconds
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"os"
"strconv"
)
type variable struct {
name string
initialValue int
}
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: " + os.Args[0] + " <file>")
os.Exit(1)
}
src, err := ioutil.ReadFile(os.Args[1])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, os.Args[1], src, 0)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
variables := make([]variable, 0)
ast.Inspect(f, func(node ast.Node) bool {
switch node := node.(type) {
// a := 1
case *ast.AssignStmt:
var name string
var initialValue int
if len(node.Lhs) != 1 || len(node.Rhs) != 1 {
fmt.Println("Multiple variables short assignments are not supported")
os.Exit(1)
}
switch lhs := node.Lhs[0].(type) {
case *ast.Ident:
name = lhs.Name
default:
// Is that possible in Go?
fmt.Println("Non-identifier on the left-hand side")
os.Exit(1)
}
switch rhs := node.Rhs[0].(type) {
case *ast.BasicLit:
if rhs.Kind != token.INT {
fmt.Println("Non-integer values are not supported")
os.Exit(1)
}
initialValue, err = strconv.Atoi(rhs.Value)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
variables = append(variables, variable{name, initialValue})
// var x int32 = 1
case *ast.ValueSpec:
var name string
var initialValue int
// Not supported yet
if len(node.Names) != 1 {
fmt.Println("Multiple constants declarations are not supported")
os.Exit(1)
}
name = node.Names[0].Name
switch rhs := node.Values[0].(type) {
case *ast.BasicLit:
if rhs.Kind != token.INT {
fmt.Println("Non-integer values are not supported")
os.Exit(1)
}
initialValue, err = strconv.Atoi(rhs.Value)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
variables = append(variables, variable{name, initialValue})
}
return true
})
for _, v := range variables {
fmt.Println(v.name + " := " + strconv.Itoa(v.initialValue))
}
}
Markdown is supported
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