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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
package parse
import (
"reflect"
"testing"
"github.com/mmcloughlin/addchain/acc/ast"
)
func TestOperands(t *testing.T) {
exprs := map[string]ast.Expr{
"1": ast.Operand(0),
"[23]": ast.Operand(23),
"[0x2a]": ast.Operand(0x2a),
"[0644]": ast.Operand(0o644),
"(1)": ast.Operand(0),
"( [3] )": ast.Operand(3),
"_101": ast.Identifier("_101"),
"x5": ast.Identifier("x5"),
"( x5)": ast.Identifier("x5"),
}
for expr, idx := range exprs {
AssertParseResult(t, expr, idx)
AssertParseResult(t, expr+"\n", idx)
}
}
func TestExpressions(t *testing.T) {
cases := []struct {
Source string
Expect ast.Expr
}{
{
Source: "1 + 1",
Expect: ast.Add{X: ast.Operand(0), Y: ast.Operand(0)},
},
{
Source: "1 add 1",
Expect: ast.Add{X: ast.Operand(0), Y: ast.Operand(0)},
},
{
Source: "1 + 1 + 1",
Expect: ast.Add{
X: ast.Add{X: ast.Operand(0), Y: ast.Operand(0)},
Y: ast.Operand(0),
},
},
{
Source: "1 add 1 add 1",
Expect: ast.Add{
X: ast.Add{X: ast.Operand(0), Y: ast.Operand(0)},
Y: ast.Operand(0),
},
},
{
Source: "1 << 5",
Expect: ast.Shift{X: ast.Operand(0), S: 5},
},
{
Source: "[3] shl 5 add 1",
Expect: ast.Add{
X: ast.Shift{X: ast.Operand(3), S: 5},
Y: ast.Operand(0),
},
},
{
Source: "[3] << 5 + [6] << 9",
Expect: ast.Add{
X: ast.Shift{X: ast.Operand(3), S: 5},
Y: ast.Shift{X: ast.Operand(6), S: 9},
},
},
{
Source: "([3] + [8]) << 2",
Expect: ast.Shift{
X: ast.Add{X: ast.Operand(3), Y: ast.Operand(8)},
S: 2,
},
},
{
Source: "2 * [3]",
Expect: ast.Double{
X: ast.Operand(3),
},
},
{
Source: "dbl [3] add [5] << 3",
Expect: ast.Add{
X: ast.Double{X: ast.Operand(3)},
Y: ast.Shift{X: ast.Operand(5), S: 3},
},
},
}
for _, c := range cases {
AssertParseResult(t, c.Source, c.Expect)
}
}
func TestChains(t *testing.T) {
cases := []struct {
Source string
Expect *ast.Chain
}{
{
Source: "x = 1 + 1\nreturn x<<3",
Expect: &ast.Chain{
Statements: []ast.Statement{
{
Name: "x",
Expr: ast.Add{
X: ast.Operand(0),
Y: ast.Operand(0),
},
},
{
Expr: ast.Shift{
X: ast.Identifier("x"),
S: 3,
},
},
},
},
},
}
for _, c := range cases {
AssertParse(t, c.Source, c.Expect)
}
}
func AssertParseResult(t *testing.T, src string, expect ast.Expr) {
t.Helper()
AssertParse(t, src, &ast.Chain{
Statements: []ast.Statement{{Expr: expect}},
})
}
func AssertParse(t *testing.T, src string, expect *ast.Chain) {
t.Helper()
t.Logf("expr=%q", src)
got, err := String(src)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expect, got) {
t.Fatalf("got %#v; expected %#v", got, expect)
}
}
|