Restructured type definitions to use a node tree

This commit is contained in:
Sasha Koshka 2022-08-18 23:38:32 -04:00
parent 717474a59e
commit 69aaae8f14
4 changed files with 27 additions and 23 deletions

View File

@ -27,7 +27,7 @@ func (parser *ParsingOperation) parseBody () (err error) {
parser.tree.typeSections =
make(map[string] *TypeSection)
}
parser.tree.typeSections[section.name] = section
parser.tree.typeSections[section.root.name] = section
if err != nil { return }
case "face":
case "enum":

View File

@ -253,22 +253,30 @@ func (section *DataSection) ToString (indent int) (output string) {
}
func (section *TypeSection) ToString (indent int) (output string) {
output += doIndent (
indent,
"type ",
section.permission.ToString(), " ",
section.name, ":",
section.inherits.ToString())
output += section.root.ToString(indent, true)
return
}
func (node TypeNode) ToString (indent int, isRoot bool) (output string) {
doIndent(indent)
if isRoot {
output += "type "
}
if section.defaultValue.value == nil {
if len(section.members) > 0 {
output += node.permission.ToString() + " "
output += node.name + ":"
output += node.what.ToString()
if node.defaultValue.value == nil {
if len(node.children) > 0 {
// TODO: print out members
} else {
output += "\n"
}
} else {
output += " " + section.defaultValue.ToString(0, false)
output += " " + node.defaultValue.ToString(0, false)
output += "\n"
}
return
}

View File

@ -161,24 +161,19 @@ type DataSection struct {
value Argument
}
// TypeMember represents member data
type TypeMember struct {
// TypeNode represents a part of a type.
type TypeNode struct {
location file.Location
name string
what Type
permission types.Permission
defaultValue Argument
children map[string] TypeNode
}
// TypeSection represents a type definition.
type TypeSection struct {
location file.Location
name string
inherits Type
permission types.Permission
defaultValue Argument
// this should be 1 dimensional for now.
members map[string] TypeMember
root TypeNode
}

View File

@ -4,6 +4,7 @@ import "git.tebibyte.media/sashakoshka/arf/types"
import "git.tebibyte.media/sashakoshka/arf/lexer"
// import "git.tebibyte.media/sashakoshka/arf/infoerr"
// parseTypeSection parses a type definition.
func (parser *ParsingOperation) parseTypeSection () (
section *TypeSection,
err error,
@ -16,19 +17,19 @@ func (parser *ParsingOperation) parseTypeSection () (
// get permission
err = parser.nextToken(lexer.TokenKindPermission)
if err != nil { return }
section.permission = parser.token.Value().(types.Permission)
section.root.permission = parser.token.Value().(types.Permission)
// get name
err = parser.nextToken(lexer.TokenKindName)
if err != nil { return }
section.name = parser.token.Value().(string)
section.root.name = parser.token.Value().(string)
// get inherited type
err = parser.nextToken(lexer.TokenKindColon)
if err != nil { return }
err = parser.nextToken()
if err != nil { return }
section.inherits, err = parser.parseType()
section.root.what, err = parser.parseType()
if err != nil { return }
if parser.token.Is(lexer.TokenKindNewline) {
@ -38,7 +39,7 @@ func (parser *ParsingOperation) parseTypeSection () (
// section.value, err = parser.parseInitializationValues(0)
// if err != nil { return }
} else {
section.defaultValue, err = parser.parseArgument()
section.root.defaultValue, err = parser.parseArgument()
if err != nil { return }
err = parser.expect(lexer.TokenKindNewline)