gopl-zh.github.com/vendor/gopl.io/ch7/eval/print.go
github-actions[bot] ca571eb691 deploy: 06a1bdf735
2022-08-04 06:59:33 +00:00

53 lines
940 B
Go

// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
package eval
import (
"bytes"
"fmt"
)
// Format formats an expression as a string.
// It does not attempt to remove unnecessary parens.
func Format(e Expr) string {
var buf bytes.Buffer
write(&buf, e)
return buf.String()
}
func write(buf *bytes.Buffer, e Expr) {
switch e := e.(type) {
case literal:
fmt.Fprintf(buf, "%g", e)
case Var:
fmt.Fprintf(buf, "%s", e)
case unary:
fmt.Fprintf(buf, "(%c", e.op)
write(buf, e.x)
buf.WriteByte(')')
case binary:
buf.WriteByte('(')
write(buf, e.x)
fmt.Fprintf(buf, " %c ", e.op)
write(buf, e.y)
buf.WriteByte(')')
case call:
fmt.Fprintf(buf, "%s(", e.fn)
for i, arg := range e.args {
if i > 0 {
buf.WriteString(", ")
}
write(buf, arg)
}
buf.WriteByte(')')
default:
panic(fmt.Sprintf("unknown Expr: %T", e))
}
}