Reworked data section so it stores single initialization argument

This commit is contained in:
Sasha Koshka 2022-08-17 00:49:49 -04:00
parent bd42c95de0
commit 0dd9368393
3 changed files with 68 additions and 44 deletions

View File

@ -36,9 +36,8 @@ func (parser *ParsingOperation) parseDataSection () (
section.value, err = parser.parseInitializationValues(0)
if err != nil { return }
} else {
var argument Argument
argument, err = parser.parseArgument()
section.value = append(section.value, argument)
section.value, err = parser.parseArgument()
if err != nil { return }
err = parser.expect(lexer.TokenKindNewline)
if err != nil { return }
@ -55,8 +54,8 @@ func (parser *ParsingOperation) parseDataSection () (
func (parser *ParsingOperation) parseInitializationValues (
baseIndent int,
) (
values []Argument,
err error,
value Argument,
err error,
) {
// check if line is indented one more than baseIndent
if !parser.token.Is(lexer.TokenKindIndent) { return }
@ -65,23 +64,20 @@ func (parser *ParsingOperation) parseInitializationValues (
err = parser.nextToken()
if err != nil { return }
// TODO: make data sections have only one value argument and create a
// similar data structure to ObjectAttributes for arrays
if parser.token.Is(lexer.TokenKindDot) {
var attributes ObjectAttributes
attributes, err = parser.parseObjectInitializationValues (
baseIndent + 1)
value.location = parser.token.Location()
values = []Argument {
Argument {
location: attributes.location,
value: &attributes,
kind: ArgumentKindObjectAttributes,
},
}
} else {
values, err = parser.parseArrayInitializationValues (
if parser.token.Is(lexer.TokenKindDot) {
var values ObjectInitializationValues
values, err = parser.parseObjectInitializationValues (
baseIndent + 1)
value.kind = ArgumentKindObjectInitializationValues
value.value = &values
} else {
var values ArrayInitializationValues
value.value, err = parser.parseArrayInitializationValues (
baseIndent + 1)
value.kind = ArgumentKindArrayInitializationValues
value.value = &values
}
return
@ -92,7 +88,7 @@ func (parser *ParsingOperation) parseInitializationValues (
func (parser *ParsingOperation) parseObjectInitializationValues (
indent int,
) (
values ObjectAttributes,
values ObjectInitializationValues,
err error,
) {
values.attributes = make(map[string] Argument)
@ -142,9 +138,11 @@ func (parser *ParsingOperation) parseObjectInitializationValues (
func (parser *ParsingOperation) parseArrayInitializationValues (
indent int,
) (
values []Argument,
values ArrayInitializationValues,
err error,
) {
values.location = parser.token.Location()
for {
if parser.token.Is(lexer.TokenKindNewline) {
err = parser.nextToken()
@ -159,7 +157,7 @@ func (parser *ParsingOperation) parseArrayInitializationValues (
var argument Argument
argument, err = parser.parseArgument()
if err != nil { return }
values = append(values, argument)
values.values = append(values.values, argument)
}
return

View File

@ -78,14 +78,14 @@ func (declaration *Declaration) ToString () (output string) {
return
}
func (attributes *ObjectAttributes) ToString (
indent int,
func (attributes *ObjectInitializationValues) ToString (
indent int,
) (
output string,
) {
for name, value := range attributes.attributes {
output += doIndent(indent, ".", name, " ")
if value.kind == ArgumentKindObjectAttributes {
if value.kind == ArgumentKindObjectInitializationValues {
output += "\n"
output += value.ToString(indent + 1, true)
} else {
@ -96,6 +96,18 @@ func (attributes *ObjectAttributes) ToString (
return
}
func (values *ArrayInitializationValues) ToString (
indent int,
) (
output string,
) {
for _, value := range values.values {
output += value.ToString(indent, true)
}
return
}
func (phrase *Phrase) ToString (indent int, breakLine bool) (output string) {
if breakLine {
output += doIndent (
@ -139,17 +151,19 @@ func (argument *Argument) ToString (indent int, breakLine bool) (output string)
switch argument.kind {
case ArgumentKindPhrase:
output += doIndent (
indent,
argument.value.(*Phrase).ToString (
output += argument.value.(*Phrase).ToString (
indent,
breakLine))
breakLine)
case ArgumentKindObjectAttributes:
case ArgumentKindObjectInitializationValues:
// this should only appear in contexts where breakLine is true
output += doIndent (
indent,
argument.value.(*ObjectAttributes).ToString (indent))
output += argument.value.(*ObjectInitializationValues).
ToString (indent)
case ArgumentKindArrayInitializationValues:
// this should only appear in contexts where breakLine is true
output += argument.value.(*ArrayInitializationValues).
ToString (indent)
case ArgumentKindIdentifier:
output += doIndent (
@ -193,16 +207,18 @@ func (section *DataSection) ToString (indent int) (output string) {
section.name, ":",
section.what.ToString())
if len(section.value) == 0 {
isComplexInitialization :=
section.value.kind == ArgumentKindObjectInitializationValues ||
section.value.kind == ArgumentKindArrayInitializationValues
if section.value.value == nil {
output += "\n"
} else if len(section.value) == 1 {
output += " " + section.value[0].ToString(0, false)
} else if isComplexInitialization {
output += "\n"
output += section.value.ToString(indent + 1, true)
} else {
output += " " + section.value.ToString(0, false)
output += "\n"
for _, argument := range(section.value) {
output += argument.ToString(indent + 1, true)
}
}
return
}

View File

@ -60,13 +60,20 @@ type Declaration struct {
what Type
}
// ObjectAttributes represents a list of object member initialization
// ObjectInitializationValues represents a list of object member initialization
// attributes.
type ObjectAttributes struct {
type ObjectInitializationValues struct {
location file.Location
attributes map[string] Argument
}
// ArrayInitializationValues represents a list of attributes initializing an
// array.
type ArrayInitializationValues struct {
location file.Location
values []Argument
}
// Phrase represents a function call or operator. In ARF they are the same
// syntactical concept.
type Phrase struct {
@ -94,7 +101,10 @@ const (
// .name value
// but like, a lot of them
ArgumentKindObjectAttributes
ArgumentKindObjectInitializationValues
// value value...
ArgumentKindArrayInitializationValues
// name.name
// name.name.name
@ -144,6 +154,6 @@ type DataSection struct {
name string
what Type
value []Argument
value Argument
permission types.Permission
}