OSDN Git Service

add commandline option AST for equity tool (#34)
authoroysheng <33340252+oysheng@users.noreply.github.com>
Fri, 28 Dec 2018 02:31:55 +0000 (10:31 +0800)
committerPaladz <yzhu101@uottawa.ca>
Fri, 28 Dec 2018 02:31:55 +0000 (10:31 +0800)
equity/main.go
equity/util/common.go [new file with mode: 0644]

index 38e445b..ecb9636 100644 (file)
@@ -10,25 +10,28 @@ import (
        "github.com/spf13/cobra"
 
        "github.com/equity/compiler"
-       equityutil "github.com/equity/equity/util"
+       equ "github.com/equity/equity/util"
 )
 
 const (
        strBin      string = "bin"
        strShift    string = "shift"
        strInstance string = "instance"
+       strAst      string = "ast"
 )
 
 var (
        bin      = false
        shift    = false
        instance = false
+       ast      = false
 )
 
 func init() {
        equityCmd.PersistentFlags().BoolVar(&bin, strBin, false, "Binary of the contracts in hex.")
        equityCmd.PersistentFlags().BoolVar(&shift, strShift, false, "Function shift of the contracts.")
        equityCmd.PersistentFlags().BoolVar(&instance, strInstance, false, "Object of the Instantiated contracts.")
+       equityCmd.PersistentFlags().BoolVar(&ast, strAst, false, "AST of the contracts.")
 }
 
 func main() {
@@ -77,7 +80,7 @@ var equityCmd = &cobra.Command{
 
                        if shift {
                                fmt.Println("Clause shift:")
-                               clauseMap, err := equityutil.Shift(contract)
+                               clauseMap, err := equ.Shift(contract)
                                if err != nil {
                                        fmt.Println("Statistics contract clause shift error:", err)
                                        os.Exit(0)
@@ -106,19 +109,29 @@ var equityCmd = &cobra.Command{
                                        os.Exit(0)
                                }
 
-                               contractArgs, err := equityutil.ConvertArguments(contract, args[1:len(contract.Params)+1])
+                               contractArgs, err := equ.ConvertArguments(contract, args[1:len(contract.Params)+1])
                                if err != nil {
                                        fmt.Println("Convert arguments into contract parameters error:", err)
                                        os.Exit(0)
                                }
 
-                               instantProg, err := equityutil.InstantiateContract(contract, contractArgs)
+                               instantProg, err := equ.InstantiateContract(contract, contractArgs)
                                if err != nil {
                                        fmt.Println("Instantiate contract error:", err)
                                        os.Exit(0)
                                }
                                fmt.Printf("%v\n\n", hex.EncodeToString(instantProg))
                        }
+
+                       if ast {
+                               fmt.Println("Ast:")
+                               rawData, err := equ.JSONMarshal(contract, true)
+                               if err != nil {
+                                       fmt.Println("Marshal the struct of contract to json error:", err)
+                                       os.Exit(0)
+                               }
+                               fmt.Println(string(rawData))
+                       }
                }
        },
 }
diff --git a/equity/util/common.go b/equity/util/common.go
new file mode 100644 (file)
index 0000000..778f65e
--- /dev/null
@@ -0,0 +1,18 @@
+package equity
+
+import (
+       "bytes"
+       "encoding/json"
+)
+
+// JSONMarshal escapes the special characters from JSON results
+func JSONMarshal(v interface{}, safeEncoding bool) ([]byte, error) {
+       b, err := json.MarshalIndent(v, "", "  ")
+
+       if safeEncoding {
+               b = bytes.Replace(b, []byte("\\u003c"), []byte("<"), -1)
+               b = bytes.Replace(b, []byte("\\u003e"), []byte(">"), -1)
+               b = bytes.Replace(b, []byte("\\u0026"), []byte("&"), -1)
+       }
+       return b, err
+}