Skip to content

Commit

Permalink
Merge branch 'master' of github.com:CityOfZion/neo-storm
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony De Meulemeester committed Oct 29, 2018
2 parents cbfc899 + 06964e6 commit 13b9dfa
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 5 deletions.
22 changes: 20 additions & 2 deletions compiler/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,14 +372,26 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
// example:
// const x = 10
// x + 2 will results into 12
if tinfo := c.typeInfo.Types[n]; tinfo.Value != nil {
tinfo := c.typeInfo.Types[n]
if tinfo.Value != nil {
c.emitLoadConst(tinfo)
return nil
}

ast.Walk(c, n.X)
ast.Walk(c, n.Y)
c.convertToken(n.Op)

// VM has separate opcode for string concatenation
if n.Op == token.ADD {
typ, ok := tinfo.Type.Underlying().(*types.Basic)
if ok && typ.Kind() == types.String {
emitOpcode(c.prog, vm.CAT)
} else {
emitOpcode(c.prog, vm.ADD)
}
} else {
c.convertToken(n.Op)
}
return nil
}

Expand Down Expand Up @@ -454,6 +466,12 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
emitOpcode(c.prog, vm.DROP)
}
}
if numArgs > 3 {
for i := 1; i < numArgs; i++ {
emitInt(c.prog, int64(i))
emitOpcode(c.prog, vm.ROLL)
}
}
}

// Check builtin first to avoid nil pointer on funcScope!
Expand Down
7 changes: 4 additions & 3 deletions compiler/emit.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func emitInt(w *bytes.Buffer, i int64) error {
return emitOpcode(w, vm.PUSHF)
}
if i > 0 && i < 16 {
val := vm.Instruction((int(vm.PUSH1) - 1 + int(i)))
val := vm.Instruction(int(vm.PUSH1) - 1 + int(i))
return emitOpcode(w, val)
}

Expand Down Expand Up @@ -113,8 +113,9 @@ func arrayReverse(b []byte) []byte {
return b
}
dest := make([]byte, len(b))
for i, j := 0, len(b)-1; i < j+1; i, j = i+1, j-1 {
dest[i], dest[j] = b[j], b[i]
ln := len(b) - 1
for i := 0; i <= ln/2; i++ {
dest[i], dest[ln-i] = b[ln-i], b[i]
}
return dest
}
38 changes: 38 additions & 0 deletions compiler/emit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package compiler

import (
"bytes"
"reflect"
"testing"
)

func TestArrayReverse(t *testing.T) {
var cases = []struct {
actual []byte
expect []byte
}{
{
actual: []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
expect: []byte{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
},
{
actual: []byte{0, 1, 2, 3, 4, 5, 6, 7, 8},
expect: []byte{8, 7, 6, 5, 4, 3, 2, 1, 0},
},
{
actual: []byte{0, 1, 2, 3, 4, 5, 6, 7},
expect: []byte{7, 6, 5, 4, 3, 2, 1, 0},
},
{
actual: []byte{0},
expect: []byte{0},
},
}

for _, item := range cases {
res := arrayReverse(item.actual)
if !bytes.Equal(res, item.expect) || !reflect.DeepEqual(res, item.expect) {
t.Fatalf("arrayReverse works wrong:\n \t actual: %#v \n \t expect: %#v", res, item.expect)
}
}
}
20 changes: 20 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module github.com/CityOfZion/neo-storm

require (
github.com/CityOfZion/neo-go v0.0.0-20180819184710-d77354db66d5
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-redis/redis v6.13.2+incompatible // indirect
github.com/go-yaml/yaml v2.1.0+incompatible // indirect
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect
github.com/onsi/gomega v1.4.2 // indirect
github.com/pkg/errors v0.8.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.0.6 // indirect
github.com/stretchr/testify v1.2.2 // indirect
github.com/syndtr/goleveldb v0.0.0-20180815032940-ae2bd5eed72d // indirect
github.com/urfave/cli v1.20.0
golang.org/x/crypto v0.0.0-20180820150726-614d502a4dac // indirect
golang.org/x/tools v0.0.0-20180820211100-c1406c36efe2
gopkg.in/airbrake/gobrake.v2 v2.0.9 // indirect
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2 // indirect
)
56 changes: 56 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
github.com/CityOfZion/neo-go v0.0.0-20180819184710-d77354db66d5 h1:cOwCZEEHc54BKT3bGuNuVU1WAACEoPw+0O4RF4Gfqdw=
github.com/CityOfZion/neo-go v0.0.0-20180819184710-d77354db66d5/go.mod h1:rnq1z/odE2goww7o2G4q4XVXG3du0Vbfot+IMPsd9MA=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-redis/redis v6.13.2+incompatible h1:kfEWSpgBs4XmuzGg7nYPqhQejjzU9eKdIL0PmE2TtRY=
github.com/go-redis/redis v6.13.2+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-yaml/yaml v2.1.0+incompatible h1:RYi2hDdss1u4YE7GwixGzWwVo47T8UQwnTLB6vQiq+o=
github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db h1:woRePGFeVFfLKN/pOkfl+p/TAqKOfFu+7KPlMVpok/w=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/onsi/ginkgo v1.6.0 h1:Ix8l273rp3QzYgXSR+c8d1fTG7UPgYkOSELPhiY/YGw=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.2 h1:3mYCb7aPxS/RU7TI1y4rkEn1oKmPRjNJLNEXgw7MH2I=
github.com/onsi/gomega v1.4.2/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.0.6 h1:hcP1GmhGigz/O7h1WVUM5KklBp1JoNS9FggWKdj/j3s=
github.com/sirupsen/logrus v1.0.6/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/syndtr/goleveldb v0.0.0-20180815032940-ae2bd5eed72d h1:4J9HCZVpvDmj2tiKGSTUnb3Ok/9CEQb9oqu9LHKQQpc=
github.com/syndtr/goleveldb v0.0.0-20180815032940-ae2bd5eed72d/go.mod h1:Z4AUp2Km+PwemOoO/VB5AOx9XSsIItzFjoJlOSiYmn0=
github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
golang.org/x/crypto v0.0.0-20180820150726-614d502a4dac h1:7d7lG9fHOLdL6jZPtnV4LpI41SbohIJ1Atq7U991dMg=
golang.org/x/crypto v0.0.0-20180820150726-614d502a4dac/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20180820211100-c1406c36efe2 h1:KroSIosN3MmXy3yAfpbGp3N+b2EvUbjiDXEggSH3RBY=
golang.org/x/tools v0.0.0-20180820211100-c1406c36efe2/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/airbrake/gobrake.v2 v2.0.9 h1:7z2uVWwn7oVeeugY1DtlPAy5H+KYgB1KeKTnqjNatLo=
gopkg.in/airbrake/gobrake.v2 v2.0.9/go.mod h1:/h5ZAUhDkGaJfjzjKLSjv6zCL6O0LLBxU4K+aSYdM/U=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2 h1:OAj3g0cR6Dx/R07QgQe8wkA9RNjB2u4i700xBkIT4e0=
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2/go.mod h1:Xk6kEKp8OKb+X14hQBKWaSkCsqBpgog8nAV2xsGOxlo=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

0 comments on commit 13b9dfa

Please sign in to comment.