From fc1568aece3cba460bee138314355b24b55eed34 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sat, 20 Aug 2022 12:40:44 -0400 Subject: [PATCH] Updated ToString methods to match new tree structure --- parser/tree-tostring.go | 77 +++++++++++++++++++++++++++-------------- parser/tree.go | 22 ++++++------ 2 files changed, 62 insertions(+), 37 deletions(-) diff --git a/parser/tree-tostring.go b/parser/tree-tostring.go index 6dcb2ca..fde0ee3 100644 --- a/parser/tree-tostring.go +++ b/parser/tree-tostring.go @@ -253,39 +253,64 @@ func (section *DataSection) ToString (indent int) (output string) { } func (section *TypeSection) ToString (indent int) (output string) { - output += section.root.ToString(indent, true) - return -} + output += doIndent ( + indent, + "type ", + section.permission.ToString(), " ", + section.name, ":", + section.inherits.ToString()) -func (node TypeNode) ToString (indent int, isRoot bool) (output string) { - output += doIndent(indent) - if isRoot { - output += "type " - } - - output += node.permission.ToString() + " " - output += node.name + ":" - output += node.what.ToString() - isComplexInitialization := - node.defaultValue.kind == ArgumentKindObjectInitializationValues || - node.defaultValue.kind == ArgumentKindArrayInitializationValues - - if node.defaultValue.value == nil { + section.defaultValue.kind == ArgumentKindObjectInitializationValues || + section.defaultValue.kind == ArgumentKindArrayInitializationValues + + if section.defaultValue.value == nil { output += "\n" - if len(node.children) > 0 { - for _, name := range sortMapKeysAlphabetically(node.children) { - child := node.children[name] - output += child.ToString(indent + 1, false) - } - } } else if isComplexInitialization { output += "\n" - output += node.defaultValue.ToString(indent + 1, true) + output += section.defaultValue.ToString(indent + 1, true) } else { - output += " " + node.defaultValue.ToString(0, false) - output += "\n" + output += " " + section.defaultValue.ToString(0, false) + output += "\n" + } + return +} + +func (member *ObjtMember) ToString (indent int) (output string) { + output += doIndent(indent) + + output += member.permission.ToString() + " " + output += member.name + ":" + output += member.what.ToString() + + isComplexInitialization := + member.defaultValue.kind == ArgumentKindObjectInitializationValues || + member.defaultValue.kind == ArgumentKindArrayInitializationValues + + if member.defaultValue.value == nil { + output += "\n" + } else if isComplexInitialization { + output += "\n" + output += member.defaultValue.ToString(indent + 1, true) + } else { + output += " " + member.defaultValue.ToString(0, false) + output += "\n" } return } + +func (section *ObjtSection) ToString (indent int) (output string) { + output += doIndent ( + indent, + "objt ", + section.permission.ToString(), " ", + section.name, ":", + section.inherits.ToString(), "\n") + + for _, member := range section.members { + output += member.ToString(indent + 1) + } + return +} + diff --git a/parser/tree.go b/parser/tree.go index d8f502e..a13a7e5 100644 --- a/parser/tree.go +++ b/parser/tree.go @@ -166,21 +166,11 @@ type TypeSection struct { location file.Location name string - what Type + inherits Type permission types.Permission defaultValue Argument } -// ObjtSection represents an object type definition -type ObjtSection struct { - location file.Location - name string - - what Type - permission types.Permission - children map[string] ObjtMember -} - // ObjtMember represents a part of an object type definition. type ObjtMember struct { location file.Location @@ -190,3 +180,13 @@ type ObjtMember struct { permission types.Permission defaultValue Argument } + +// ObjtSection represents an object type definition +type ObjtSection struct { + location file.Location + name string + + inherits Type + permission types.Permission + members map[string] ObjtMember +}