Beispiel #1
0
// General compile function
func Compile(script string, silent bool) (ret []byte, err error) {
	if len(script) > 2 {
		line := strings.Split(script, "\n")[0]

		if len(line) > 1 && line[0:2] == "#!" {
			switch line {
			/*
				case "#!serpent":
					byteCode, err := serpent.Compile(script)
					if err != nil {
						return nil, err
					}

					return byteCode, nil
			*/
			}
		} else {

			compiler := mutan.NewCompiler(backend.NewEthereumBackend())
			compiler.Silent = silent
			byteCode, errors := compiler.Compile(strings.NewReader(script))
			if len(errors) > 0 {
				var errs string
				for _, er := range errors {
					if er != nil {
						errs += er.Error()
					}
				}
				return nil, fmt.Errorf("%v", errs)
			}

			return byteCode, nil
		}
	}

	return nil, nil
}
Beispiel #2
0
func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: %s [options] filename\n", os.Args[0])
		flag.PrintDefaults()
	}
	flag.Parse()

	if *ShowVersion {
		fmt.Fprintf(os.Stdout, "Mutan %s (EVM 0.6, Frontend %s) (%s, %s)\n", mutan.Version, Version, runtime.GOOS, runtime.Version())
		os.Exit(0)
	}

	var (
		reader   io.Reader
		compiler = mutan.NewCompiler(backend.NewEthereumBackend())
	)

	if len(*StrCode) > 0 {
		reader = strings.NewReader(*StrCode)
	} else {
		if len(flag.Arg(0)) > 0 {
			file, err := os.Open(os.Args[len(os.Args)-1])
			if err != nil {
				Panic("%v\n", err)
			}

			reader = file
		} else {
			Panic("no file specified\n")
		}
	}

	asm, err := compiler.Assemble(reader)
	if err != nil {
		Panic("%v\n", err)
	}

	if *ShowAssembler {
		for _, c := range asm {
			switch c := c.(type) {
			case vm.OpCode:
				fmt.Printf("%v ", c)
			case string:
				fmt.Printf("%s ", c)
			default:
				fmt.Printf("%x ", c)
			}
		}
		fmt.Println()
	} else {
		bytes, err := compiler.AssemblerStage(asm...)
		if err != nil {
			Panic("%v\n", err)
		}

		if *ByteArray {
			fmt.Println(bytes)
		} else {
			fmt.Printf("%x\n", bytes)
		}
	}
}