Skip to content

Commit

Permalink
pretty printer: print Do, Default
Browse files Browse the repository at this point in the history
  • Loading branch information
z7zmey committed Mar 28, 2018
1 parent 09fada0 commit 845ac94
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
29 changes: 29 additions & 0 deletions printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ func getPrintFuncByNode(n node.Node) func(o io.Writer, n node.Node) {
return printStmtContinue
case *stmt.Declare:
return printStmtDeclare
case *stmt.Default:
return printStmtDefault
case *stmt.Do:
return printStmtDo

case *stmt.StmtList:
return printStmtStmtList
Expand Down Expand Up @@ -1458,6 +1462,31 @@ func printStmtDeclare(o io.Writer, n node.Node) {
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) {
nn := n.(*stmt.StmtList)

Expand Down
55 changes: 55 additions & 0 deletions printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
o := bytes.NewBufferString("")

Expand Down

0 comments on commit 845ac94

Please sign in to comment.