|
|
@@ -338,6 +338,7 @@ func TestParsingInfixExpressions(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// 操作符 优先级测试
|
|
|
func TestOperatorPrecedenceParsing(t *testing.T) {
|
|
|
tests := []struct {
|
|
|
input string
|
|
|
@@ -416,10 +417,17 @@ func TestOperatorPrecedenceParsing(t *testing.T) {
|
|
|
{
|
|
|
"!(true == true)", "(!(true == true))",
|
|
|
},
|
|
|
+ {
|
|
|
+ "a * [1, 2, 3, 4][b * c] *d",
|
|
|
+ "((a * ([1, 2, 3, 4][(b * c)])) * d)",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "add(a * b[2], b[1], 2 * [1, 2][1])",
|
|
|
+ "add((a * (b[2])), (b[1]), (2 * ([1, 2][1])))",
|
|
|
+ },
|
|
|
}
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
- fmt.Printf("=====================================parse %s, begin============================\n", tt.input)
|
|
|
l := lexer.New(tt.input)
|
|
|
p := New(l)
|
|
|
program := p.ParseProgram()
|
|
|
@@ -427,9 +435,8 @@ func TestOperatorPrecedenceParsing(t *testing.T) {
|
|
|
|
|
|
actual := program.String()
|
|
|
if actual != tt.expected {
|
|
|
- t.Errorf("exptected=%q, got=%q", tt.expected, actual)
|
|
|
+ t.Errorf("exptected=%q,\n got=%q", tt.expected, actual)
|
|
|
}
|
|
|
- fmt.Printf("=====================================parse %s, end============================\n", tt.input)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -732,3 +739,24 @@ func TestArrayLiteralsParsing(t *testing.T) {
|
|
|
testInfixExpression(t, array.Element[1], 2, "*", 2)
|
|
|
testInfixExpression(t, array.Element[2], 3, "+", 3)
|
|
|
}
|
|
|
+
|
|
|
+func TestIndexExpressionParsing(t *testing.T) {
|
|
|
+ input := "myArray[1+1]"
|
|
|
+
|
|
|
+ l := lexer.New(input)
|
|
|
+ p := New(l)
|
|
|
+ program := p.ParseProgram()
|
|
|
+ checkParseErrors(t, p)
|
|
|
+
|
|
|
+ stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
|
|
|
+ indexExp, ok := stmt.Expression.(*ast.IndexExpression)
|
|
|
+ if !ok {
|
|
|
+ t.Fatalf("exp not *ast.IndexExpression. got=%T", stmt.Expression)
|
|
|
+ }
|
|
|
+ if !testIdentifier(t, indexExp, "myArray") {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if !testInfixExpression(t, indexExp.Index, 1, "+", 1) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+}
|