script_unix.go 931 Bytes
Newer Older
1 2
// +build !windows

obscuren's avatar
obscuren committed
3 4 5 6
package ethutil

import (
	"fmt"
obscuren's avatar
obscuren committed
7 8
	"strings"

obscuren's avatar
obscuren committed
9
	"github.com/ethereum/serpent-go"
obscuren's avatar
obscuren committed
10
	"github.com/obscuren/mutan"
obscuren's avatar
obscuren committed
11
	"github.com/obscuren/mutan/backends"
obscuren's avatar
obscuren committed
12 13 14
)

// General compile function
15
func Compile(script string, silent bool) (ret []byte, err error) {
16 17
	if len(script) > 2 {
		line := strings.Split(script, "\n")[0]
obscuren's avatar
obscuren committed
18

obscuren's avatar
obscuren committed
19
		if len(line) > 1 && line[0:2] == "#!" {
20
			switch line {
obscuren's avatar
obscuren committed
21 22 23 24 25
			case "#!serpent":
				byteCode, err := serpent.Compile(script)
				if err != nil {
					return nil, err
				}
26

obscuren's avatar
obscuren committed
27
				return byteCode, nil
28 29
			}
		} else {
obscuren's avatar
obscuren committed
30

31
			compiler := mutan.NewCompiler(backend.NewEthereumBackend())
32
			compiler.Silent = silent
33 34 35 36 37 38 39
			byteCode, errors := compiler.Compile(strings.NewReader(script))
			if len(errors) > 0 {
				var errs string
				for _, er := range errors {
					if er != nil {
						errs += er.Error()
					}
obscuren's avatar
obscuren committed
40
				}
41
				return nil, fmt.Errorf("%v", errs)
obscuren's avatar
obscuren committed
42 43
			}

44 45
			return byteCode, nil
		}
obscuren's avatar
obscuren committed
46
	}
47 48

	return nil, nil
obscuren's avatar
obscuren committed
49
}