Compiler update (#21)
* added seperate folders for cmd packages. * Fix netmodes in test + reverse bigint bytes * glide get deps * add, sub, mul, div * booleans * strings * binary expressions * if statements * function calls * composite literals (slice, array) * Added lots of test cases and update readme.
This commit is contained in:
parent
f7d57e4e49
commit
b257a06f3e
18 changed files with 1253 additions and 494 deletions
72
pkg/vm/compiler/tests/binary_expr_test.go
Normal file
72
pkg/vm/compiler/tests/binary_expr_test.go
Normal file
|
@ -0,0 +1,72 @@
|
|||
package compiler_test
|
||||
|
||||
var binaryExprTestCases = []testCase{
|
||||
{
|
||||
"simple add",
|
||||
`
|
||||
package testcase
|
||||
func Main() int {
|
||||
x := 2 + 2
|
||||
return x
|
||||
}
|
||||
`,
|
||||
"52c56b546c766b00527ac46203006c766b00c3616c7566",
|
||||
},
|
||||
{
|
||||
"simple sub",
|
||||
`
|
||||
package testcase
|
||||
func Main() int {
|
||||
x := 2 - 2
|
||||
return x
|
||||
}
|
||||
`,
|
||||
"52c56b006c766b00527ac46203006c766b00c3616c7566",
|
||||
},
|
||||
{
|
||||
"simple div",
|
||||
`
|
||||
package testcase
|
||||
func Main() int {
|
||||
x := 2 / 2
|
||||
return x
|
||||
}
|
||||
`,
|
||||
"52c56b516c766b00527ac46203006c766b00c3616c7566",
|
||||
},
|
||||
{
|
||||
"simple mul",
|
||||
`
|
||||
package testcase
|
||||
func Main() int {
|
||||
x := 4 * 2
|
||||
return x
|
||||
}
|
||||
`,
|
||||
"52c56b586c766b00527ac46203006c766b00c3616c7566",
|
||||
},
|
||||
{
|
||||
"simple binary expr in return",
|
||||
`
|
||||
package testcase
|
||||
func Main() int {
|
||||
x := 2
|
||||
return 2 + x
|
||||
}
|
||||
`,
|
||||
"52c56b526c766b00527ac4620300526c766b00c393616c7566",
|
||||
},
|
||||
{
|
||||
"complex binary expr",
|
||||
`
|
||||
package testcase
|
||||
func Main() int {
|
||||
x := 4
|
||||
y := 8
|
||||
z := x + 2 + 2 - 8
|
||||
return y * z
|
||||
}
|
||||
`,
|
||||
"54c56b546c766b00527ac4586c766b51527ac46c766b00c35293529358946c766b52527ac46203006c766b51c36c766b52c395616c7566",
|
||||
},
|
||||
}
|
65
pkg/vm/compiler/tests/compiler_test.go
Normal file
65
pkg/vm/compiler/tests/compiler_test.go
Normal file
|
@ -0,0 +1,65 @@
|
|||
package compiler_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/CityOfZion/neo-go/pkg/vm"
|
||||
"github.com/CityOfZion/neo-go/pkg/vm/compiler"
|
||||
)
|
||||
|
||||
type testCase struct {
|
||||
name string
|
||||
src string
|
||||
result string
|
||||
}
|
||||
|
||||
func TestAllCases(t *testing.T) {
|
||||
testCases := []testCase{}
|
||||
testCases = append(testCases, stringTestCases...)
|
||||
testCases = append(testCases, binaryExprTestCases...)
|
||||
testCases = append(testCases, ifStatementTestCases...)
|
||||
testCases = append(testCases, functionCallTestCases...)
|
||||
|
||||
for _, tc := range testCases {
|
||||
c := compiler.New()
|
||||
if err := c.Compile(strings.NewReader(tc.src)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
expectedResult, err := hex.DecodeString(tc.result)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if bytes.Compare(c.Buffer().Bytes(), expectedResult) != 0 {
|
||||
t.Log(hex.EncodeToString(c.Buffer().Bytes()))
|
||||
want, _ := hex.DecodeString(tc.result)
|
||||
dumpOpCodeSideBySide(c.Buffer().Bytes(), want)
|
||||
t.Fatalf("compiling %s failed", tc.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func dumpOpCodeSideBySide(have, want []byte) {
|
||||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0)
|
||||
fmt.Fprintln(w, "INDEX\tHAVE OPCODE\tDESC\tWANT OPCODE\tDESC\tDIFF")
|
||||
|
||||
for i := 0; i < len(have); i++ {
|
||||
if len(want) <= i {
|
||||
break
|
||||
}
|
||||
diff := ""
|
||||
if have[i] != want[i] {
|
||||
diff = "<<"
|
||||
}
|
||||
fmt.Fprintf(w, "%d\t0x%2x\t%s\t0x%2x\t%s\t%s\n",
|
||||
i, have[i], vm.OpCode(have[i]), want[i], vm.OpCode(want[i]), diff)
|
||||
}
|
||||
w.Flush()
|
||||
}
|
61
pkg/vm/compiler/tests/function_call_test.go
Normal file
61
pkg/vm/compiler/tests/function_call_test.go
Normal file
|
@ -0,0 +1,61 @@
|
|||
package compiler_test
|
||||
|
||||
var functionCallTestCases = []testCase{
|
||||
{
|
||||
"simple function call",
|
||||
`
|
||||
package testcase
|
||||
func Main() int {
|
||||
x := 10
|
||||
y := getSomeInteger()
|
||||
return x + y
|
||||
}
|
||||
|
||||
func getSomeInteger() int {
|
||||
x := 10
|
||||
return x
|
||||
}
|
||||
`,
|
||||
"53c56b5a6c766b00527ac461651c006c766b51527ac46203006c766b00c36c766b51c393616c756652c56b5a6c766b00527ac46203006c766b00c3616c7566",
|
||||
},
|
||||
{
|
||||
"multiple function calls",
|
||||
`
|
||||
package testcase
|
||||
func Main() int {
|
||||
x := 10
|
||||
y := getSomeInteger()
|
||||
return x + y
|
||||
}
|
||||
|
||||
func getSomeInteger() int {
|
||||
x := 10
|
||||
y := getSomeOtherInt()
|
||||
return x + y
|
||||
}
|
||||
|
||||
func getSomeOtherInt() int {
|
||||
x := 8
|
||||
return x
|
||||
}
|
||||
`,
|
||||
"53c56b5a6c766b00527ac461651c006c766b51527ac46203006c766b00c36c766b51c393616c756653c56b5a6c766b00527ac461651c006c766b51527ac46203006c766b00c36c766b51c393616c756652c56b586c766b00527ac46203006c766b00c3616c7566",
|
||||
},
|
||||
{
|
||||
"function call with arguments",
|
||||
`
|
||||
package testcase
|
||||
func Main() int {
|
||||
x := 10
|
||||
y := getSomeInteger(x)
|
||||
return y
|
||||
}
|
||||
|
||||
func getSomeInteger(x int) int {
|
||||
y := 8
|
||||
return x + y
|
||||
}
|
||||
`,
|
||||
"53c56b5a6c766b00527ac46c766b00c3616516006c766b51527ac46203006c766b51c3616c756653c56b6c766b00527ac4586c766b51527ac46203006c766b00c36c766b51c393616c7566",
|
||||
},
|
||||
}
|
74
pkg/vm/compiler/tests/if_statement_test.go
Normal file
74
pkg/vm/compiler/tests/if_statement_test.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
package compiler_test
|
||||
|
||||
var ifStatementTestCases = []testCase{
|
||||
{
|
||||
"if statement LT",
|
||||
`
|
||||
package testcase
|
||||
func Main() int {
|
||||
x := 10
|
||||
if x < 100 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
`,
|
||||
"54c56b5a6c766b00527ac46c766b00c301649f640b0062030051616c756662030000616c7566",
|
||||
},
|
||||
{
|
||||
"if statement GT",
|
||||
`
|
||||
package testcase
|
||||
func Main() int {
|
||||
x := 10
|
||||
if x > 100 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
`,
|
||||
"54c56b5a6c766b00527ac46c766b00c30164a0640b0062030051616c756662030000616c7566",
|
||||
},
|
||||
{
|
||||
"if statement GTE",
|
||||
`
|
||||
package testcase
|
||||
func Main() int {
|
||||
x := 10
|
||||
if x >= 100 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
`,
|
||||
"54c56b5a6c766b00527ac46c766b00c30164a2640b0062030051616c756662030000616c7566",
|
||||
},
|
||||
{
|
||||
"complex if statement with LAND",
|
||||
`
|
||||
package testcase
|
||||
func Main() int {
|
||||
x := 10
|
||||
if x >= 10 && x <= 20 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
`,
|
||||
"54c56b5a6c766b00527ac46c766b00c35aa26416006c766b00c30114a1640b0062030051616c756662030000616c7566",
|
||||
},
|
||||
{
|
||||
"complex if statement with LOR",
|
||||
`
|
||||
package testcase
|
||||
func Main() int {
|
||||
x := 10
|
||||
if x >= 10 || x <= 20 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
`,
|
||||
"54c56b5a6c766b00527ac46c766b00c35aa2630e006c766b00c30114a1640b0062030051616c756662030000616c7566",
|
||||
},
|
||||
}
|
15
pkg/vm/compiler/tests/string_test.go
Normal file
15
pkg/vm/compiler/tests/string_test.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
package compiler_test
|
||||
|
||||
var stringTestCases = []testCase{
|
||||
{
|
||||
"simple string",
|
||||
`
|
||||
package testcase
|
||||
func Main() string {
|
||||
x := "NEO"
|
||||
return x
|
||||
}
|
||||
`,
|
||||
"52c56b034e454f6c766b00527ac46203006c766b00c3616c7566",
|
||||
},
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue