diff options
author | obscuren <geffobscura@gmail.com> | 2014-03-21 22:27:18 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-03-21 22:27:18 +0800 |
commit | 9a9e252cabdc6283d7f4e523860f0e4addf62152 (patch) | |
tree | 7bdbcab1d066d40861250af0c72446d88f946368 /ethutil | |
parent | 2ea4c632d1673b762c1af11582364d9faa08c413 (diff) | |
download | go-tangerine-9a9e252cabdc6283d7f4e523860f0e4addf62152.tar go-tangerine-9a9e252cabdc6283d7f4e523860f0e4addf62152.tar.gz go-tangerine-9a9e252cabdc6283d7f4e523860f0e4addf62152.tar.bz2 go-tangerine-9a9e252cabdc6283d7f4e523860f0e4addf62152.tar.lz go-tangerine-9a9e252cabdc6283d7f4e523860f0e4addf62152.tar.xz go-tangerine-9a9e252cabdc6283d7f4e523860f0e4addf62152.tar.zst go-tangerine-9a9e252cabdc6283d7f4e523860f0e4addf62152.zip |
Changes 'compiler' to work with any type
Diffstat (limited to 'ethutil')
-rw-r--r-- | ethutil/parsing.go | 46 |
1 files changed, 35 insertions, 11 deletions
diff --git a/ethutil/parsing.go b/ethutil/parsing.go index f24402623..8929f0829 100644 --- a/ethutil/parsing.go +++ b/ethutil/parsing.go @@ -84,20 +84,30 @@ func IsOpCode(s string) bool { return false } -func CompileInstr(s string) ([]byte, error) { - isOp := IsOpCode(s) - if isOp { - return []byte{OpCodes[s]}, nil - } +func CompileInstr(s interface{}) ([]byte, error) { + switch s.(type) { + case string: + str := s.(string) + isOp := IsOpCode(str) + if isOp { + return []byte{OpCodes[str]}, nil + } + + num := new(big.Int) + _, success := num.SetString(str, 0) + // Assume regular bytes during compilation + if !success { + num.SetBytes([]byte(str)) + } - num := new(big.Int) - _, success := num.SetString(s, 0) - // Assume regular bytes during compilation - if !success { - num.SetBytes([]byte(s)) + return num.Bytes(), nil + case int: + return big.NewInt(int64(s.(int))).Bytes(), nil + case []byte: + return BigD(s.([]byte)).Bytes(), nil } - return num.Bytes(), nil + return nil, nil } func Instr(instr string) (int, []string, error) { @@ -118,3 +128,17 @@ func Instr(instr string) (int, []string, error) { return op, args[1:7], nil } + +// Script compilation functions +// Compiles strings to machine code +func Compile(instructions ...interface{}) (script []string) { + script = make([]string, len(instructions)) + + for i, val := range instructions { + instr, _ := CompileInstr(val) + + script[i] = string(instr) + } + + return +} |