Changes 'compiler' to work with any type

This commit is contained in:
obscuren 2014-03-21 15:27:18 +01:00
parent 2ea4c632d1
commit 9a9e252cab
2 changed files with 49 additions and 27 deletions

View File

@ -2,7 +2,6 @@ package ethchain
import ( import (
"bytes" "bytes"
"fmt"
"github.com/ethereum/eth-go/ethdb" "github.com/ethereum/eth-go/ethdb"
"github.com/ethereum/eth-go/ethutil" "github.com/ethereum/eth-go/ethutil"
"math/big" "math/big"
@ -130,27 +129,26 @@ func TestRun3(t *testing.T) {
}) })
tx := NewTransaction(ContractAddr, ethutil.Big("100000000000000000000000000000000000000000000000000"), script) tx := NewTransaction(ContractAddr, ethutil.Big("100000000000000000000000000000000000000000000000000"), script)
addr := tx.Hash()[12:] addr := tx.Hash()[12:]
fmt.Printf("addr contract %x\n", addr)
contract := MakeContract(tx, state) contract := MakeContract(tx, state)
state.UpdateContract(contract) state.UpdateContract(contract)
callerScript := Compile([]string{ callerScript := ethutil.Compile(
"PUSH", "1337", // Argument "PUSH", 1337, // Argument
"PUSH", "65", // argument mem offset "PUSH", 65, // argument mem offset
"MSTORE", "MSTORE",
"PUSH", "64", // ret size "PUSH", 64, // ret size
"PUSH", "0", // ret offset "PUSH", 0, // ret offset
"PUSH", "32", // arg size "PUSH", 32, // arg size
"PUSH", "65", // arg offset "PUSH", 65, // arg offset
"PUSH", "1000", /// Gas "PUSH", 1000, /// Gas
"PUSH", "0", /// value "PUSH", 0, /// value
"PUSH", string(addr), // Sender "PUSH", addr, // Sender
"CALL", "CALL",
"PUSH", "64", "PUSH", 64,
"PUSH", "0", "PUSH", 0,
"RETURN", "RETURN",
}) )
callerTx := NewTransaction(ContractAddr, ethutil.Big("100000000000000000000000000000000000000000000000000"), callerScript) callerTx := NewTransaction(ContractAddr, ethutil.Big("100000000000000000000000000000000000000000000000000"), callerScript)
// Contract addr as test address // Contract addr as test address

View File

@ -84,20 +84,30 @@ func IsOpCode(s string) bool {
return false return false
} }
func CompileInstr(s string) ([]byte, error) { func CompileInstr(s interface{}) ([]byte, error) {
isOp := IsOpCode(s) switch s.(type) {
case string:
str := s.(string)
isOp := IsOpCode(str)
if isOp { if isOp {
return []byte{OpCodes[s]}, nil return []byte{OpCodes[str]}, nil
} }
num := new(big.Int) num := new(big.Int)
_, success := num.SetString(s, 0) _, success := num.SetString(str, 0)
// Assume regular bytes during compilation // Assume regular bytes during compilation
if !success { if !success {
num.SetBytes([]byte(s)) num.SetBytes([]byte(str))
} }
return num.Bytes(), nil return num.Bytes(), nil
case int:
return big.NewInt(int64(s.(int))).Bytes(), nil
case []byte:
return BigD(s.([]byte)).Bytes(), nil
}
return nil, nil
} }
func Instr(instr string) (int, []string, error) { func Instr(instr string) (int, []string, error) {
@ -118,3 +128,17 @@ func Instr(instr string) (int, []string, error) {
return op, args[1:7], nil 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
}