pretty printer: print Do, Default

This commit is contained in:
z7zmey 2018-03-29 00:04:09 +03:00
parent 09fada08da
commit 845ac94414
2 changed files with 84 additions and 0 deletions

View File

@ -317,6 +317,10 @@ func getPrintFuncByNode(n node.Node) func(o io.Writer, n node.Node) {
return printStmtContinue return printStmtContinue
case *stmt.Declare: case *stmt.Declare:
return printStmtDeclare return printStmtDeclare
case *stmt.Default:
return printStmtDefault
case *stmt.Do:
return printStmtDo
case *stmt.StmtList: case *stmt.StmtList:
return printStmtStmtList return printStmtStmtList
@ -1458,6 +1462,31 @@ func printStmtDeclare(o io.Writer, n node.Node) {
printStmt(o, nn.Stmt) printStmt(o, nn.Stmt)
} }
func printStmtDefault(o io.Writer, n node.Node) {
nn := n.(*stmt.Default)
io.WriteString(o, "default:\n")
printNodes(o, nn.Stmts)
}
func printStmtDo(o io.Writer, n node.Node) {
nn := n.(*stmt.Do)
io.WriteString(o, "do")
switch s := nn.Stmt.(type) {
case *stmt.StmtList:
io.WriteString(o, " {\n")
printNodes(o, s.Stmts)
io.WriteString(o, "} ")
default:
io.WriteString(o, "\n")
Print(o, s)
}
io.WriteString(o, "while (")
Print(o, nn.Cond)
io.WriteString(o, ");\n")
}
func printStmtStmtList(o io.Writer, n node.Node) { func printStmtStmtList(o io.Writer, n node.Node) {
nn := n.(*stmt.StmtList) nn := n.(*stmt.StmtList)

View File

@ -2326,6 +2326,61 @@ func TestPrintStmtDeclareNop(t *testing.T) {
} }
} }
func TestPrintStmtDefalut(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.Default{
Stmts: []node.Node{
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
},
})
expected := "default:\n$a;\n"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintStmtDo_Expression(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.Do{
Cond: &scalar.Lnumber{Value: "1"},
Stmt: &stmt.Expression{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
},
})
expected := "do\n$a;\nwhile (1);\n"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintStmtDo_StmtList(t *testing.T) {
o := bytes.NewBufferString("")
printer.Print(o, &stmt.Do{
Cond: &scalar.Lnumber{Value: "1"},
Stmt: &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
},
},
})
expected := "do {\n$a;\n} while (1);\n"
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintStmtList(t *testing.T) { func TestPrintStmtList(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")