Add genFilenameLineComments.

This commit is contained in:
Nigel Tao committed 2017-11-03 12:28:15 +11:00
1 parent d7c910fd1b
commit acec19e969
1 file changed
+22 -3
+22 -3
View File
@@ -16,22 +16,41 @@ package cgen
import ( import (
"fmt" "fmt"
"strings"
a "github.com/google/puffs/lang/ast" a "github.com/google/puffs/lang/ast"
t "github.com/google/puffs/lang/token" t "github.com/google/puffs/lang/token"
) )
// genFilenameLineComments is whether to print "// foo.puffs:123\n" comments in
// the generated code. This can be useful for debugging, although it is not
// enabled by default as it can lead to many spurious changes in the generated
// C code (due to line numbers changing) when editing Puffs code.
const genFilenameLineComments = false
func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error { func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
if depth > a.MaxBodyDepth { if depth > a.MaxBodyDepth {
return fmt.Errorf("body recursion depth too large") return fmt.Errorf("body recursion depth too large")
} }
depth++ depth++
switch n.Kind() { if n.Kind() == a.KAssert {
case a.KAssert:
// Assertions only apply at compile-time. // Assertions only apply at compile-time.
return nil return nil
}
if genFilenameLineComments {
filename, line := n.Raw().FilenameLine()
if i := strings.LastIndexByte(filename, '/'); i >= 0 {
filename = filename[i+1:]
}
if i := strings.LastIndexByte(filename, '\\'); i >= 0 {
filename = filename[i+1:]
}
b.printf("// %s:%d\n", filename, line)
}
switch n.Kind() {
case a.KAssign: case a.KAssign:
n := n.Assign() n := n.Assign()
if err := g.writeSuspendibles(b, n.LHS(), depth); err != nil { if err := g.writeSuspendibles(b, n.LHS(), depth); err != nil {
@@ -95,7 +114,7 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
} }
} }
if bif := n.BodyIfFalse(); len(bif) > 0 { if bif := n.BodyIfFalse(); len(bif) > 0 {
b.writes("} else {") b.writes("} else {\n")
for _, o := range bif { for _, o := range bif {
if err := g.writeStatement(b, o, depth); err != nil { if err := g.writeStatement(b, o, depth); err != nil {
return err return err