-
Notifications
You must be signed in to change notification settings - Fork 18
/
koa.go
54 lines (42 loc) · 1.01 KB
/
koa.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package koa
import (
"encoding/binary"
"github.com/DE-labtory/koa/abi"
"github.com/DE-labtory/koa/parse"
"github.com/DE-labtory/koa/translate"
"github.com/DE-labtory/koa/vm"
)
func Compile(input string) (translate.Asm, abi.ABI, error) {
ast, err := parse.Parse(
parse.NewTokenBuffer(
parse.NewLexer(input)))
if err != nil {
return translate.Asm{}, abi.ABI{}, err
}
asm, err := translate.CompileContract(*ast)
if err != nil {
return asm, abi.ABI{}, err
}
a, err := translate.ExtractAbi(*ast)
if err != nil {
return asm, abi.ABI{}, err
}
return asm, *a, nil
}
func Execute(rawByteCode []byte, function []byte, args []byte) ([]byte, error) {
callFunc := &vm.CallFunc{
Func: function,
Args: args,
}
stack, err := vm.Execute(rawByteCode, vm.NewMemory(), callFunc)
if err != nil {
return nil, err
}
output := Bytes(int64(stack.Pop()))
return output, nil
}
func Bytes(item int64) []byte {
byteSlice := make([]byte, 8)
binary.BigEndian.PutUint64(byteSlice, uint64(item))
return byteSlice
}