wrote dsl for ast boiler plate generation
This commit is contained in:
parent
b244f7e3b2
commit
e0dd8ff9d5
16 changed files with 915 additions and 60 deletions
55
ast/gen/debug.go
Normal file
55
ast/gen/debug.go
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
package main
|
||||
|
||||
type debugVisitor struct{}
|
||||
|
||||
func debug(node node) string {
|
||||
d := new(debugVisitor)
|
||||
v, _ := node.accept(d)
|
||||
return v
|
||||
}
|
||||
|
||||
// visitASTDefinitionsNode implements visitor.
|
||||
func (d *debugVisitor) visitASTDefinitionsNode(a *astDefinitionsNode) (string, error) {
|
||||
v := "#" + a.name + "\n\n"
|
||||
|
||||
for _, defNode := range a.definitions {
|
||||
def, _ := defNode.accept(d)
|
||||
v += def + "\n\n"
|
||||
}
|
||||
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// visitDefinition implements visitor.
|
||||
func (d *debugVisitor) visitDefinition(def *definitionNode) (string, error) {
|
||||
id, _ := def.identifier.accept(d)
|
||||
v := id + " [\n"
|
||||
|
||||
for _, fNode := range def.fields {
|
||||
field, _ := fNode.accept(d)
|
||||
v += "\t" + field + "\n"
|
||||
}
|
||||
|
||||
v += "]\n\n"
|
||||
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// visitField implements visitor.
|
||||
func (d *debugVisitor) visitField(g *fieldNode) (string, error) {
|
||||
left, _ := g.left.accept(d)
|
||||
right, _ := g.right.accept(d)
|
||||
return left + " = " + right + ";", nil
|
||||
}
|
||||
|
||||
// visitIdentifier implements visitor.
|
||||
func (d *debugVisitor) visitIdentifier(i *identifierNode) (string, error) {
|
||||
return i.value, nil
|
||||
}
|
||||
|
||||
// visitName implements visitor.
|
||||
func (d *debugVisitor) visitName(n *nameNode) (string, error) {
|
||||
return n.value, nil
|
||||
}
|
||||
|
||||
var _ visitor = new(debugVisitor)
|
||||
Loading…
Add table
Add a link
Reference in a new issue