10 "github.com/spf13/cobra"
12 "github.com/equity/compiler"
13 equ "github.com/equity/equity/util"
18 strShift string = "shift"
19 strInstance string = "instance"
21 strVersion string = "version"
33 equityCmd.PersistentFlags().BoolVar(&bin, strBin, false, "Binary of the contracts in hex.")
34 equityCmd.PersistentFlags().BoolVar(&shift, strShift, false, "Function shift of the contracts.")
35 equityCmd.PersistentFlags().BoolVar(&instance, strInstance, false, "Object of the Instantiated contracts.")
36 equityCmd.PersistentFlags().BoolVar(&ast, strAst, false, "AST of the contracts.")
37 equityCmd.PersistentFlags().BoolVar(&version, strVersion, false, "Version of equity compiler.")
41 runtime.GOMAXPROCS(runtime.NumCPU())
42 if err := equityCmd.Execute(); err != nil {
47 var equityCmd = &cobra.Command{
48 Use: "equity <input_file>",
49 Short: "equity commandline compiler",
50 Example: "equity contract_name [contract_args...] --bin --instance",
51 Args: cobra.RangeArgs(0, 100),
52 Run: func(cmd *cobra.Command, args []string) {
54 version := compiler.VersionWithCommit(compiler.GitCommit)
55 fmt.Println("equity, the equity compiler commandline interface")
56 fmt.Println("Version:", version)
65 if err := handleCompiled(args); err != nil {
71 func handleCompiled(args []string) error {
72 contractFile, err := os.Open(args[0])
74 fmt.Printf("An error [%v] occurred on opening the file, please check whether the file exists or can be accessed.\n", err)
77 defer contractFile.Close()
79 reader := bufio.NewReader(contractFile)
80 contracts, err := compiler.Compile(reader)
82 fmt.Println("Compile contract failed:", err)
86 // Print the result for all contracts
87 for i, contract := range contracts {
88 fmt.Printf("======= %v =======\n", contract.Name)
90 fmt.Println("Binary:")
91 fmt.Printf("%v\n\n", hex.EncodeToString(contract.Body))
95 fmt.Println("Clause shift:")
96 clauseMap, err := equ.Shift(contract)
98 fmt.Println("Statistics contract clause shift error:", err)
102 for clause, shift := range clauseMap {
103 fmt.Printf(" %s: %v\n", clause, shift)
105 fmt.Printf("\nNOTE: \n If the contract contains only one clause, Users don't need clause selector when unlock contract." +
106 "\n Furthermore, there is no signification for ending clause shift except for display.\n\n")
110 if i != len(contracts)-1 {
114 fmt.Println("Instantiated program:")
115 if len(args)-1 < len(contract.Params) {
116 fmt.Printf("Error: The number of input arguments %d is less than the number of contract parameters %d\n", len(args)-1, len(contract.Params))
117 usage := fmt.Sprintf("Usage:\n equity %s", args[0])
118 for _, param := range contract.Params {
119 usage = usage + " <" + param.Name + ">"
121 fmt.Printf("%s\n\n", usage)
125 contractArgs, err := equ.ConvertArguments(contract, args[1:len(contract.Params)+1])
127 fmt.Println("Convert arguments into contract parameters error:", err)
131 instantProg, err := equ.InstantiateContract(contract, contractArgs)
133 fmt.Println("Instantiate contract error:", err)
136 fmt.Printf("%v\n\n", hex.EncodeToString(instantProg))
141 rawData, err := equ.JSONMarshal(contract, true)
143 fmt.Println("Marshal the struct of contract to json error:", err)
146 fmt.Println(string(rawData))