From e885af997d542672c41140995a71771036b2f85b Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sun, 16 Oct 2022 02:04:38 -0400 Subject: [PATCH] Store enum default value as argument, error on empty enum --- analyzer/enum-section.go | 13 +++++++++- analyzer/enum-section_test.go | 40 ++++++++++++++++++++++++++++- tests/analyzer/enumSection/main.arf | 6 ++++- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/analyzer/enum-section.go b/analyzer/enum-section.go index 8be1cf0..82a4580 100644 --- a/analyzer/enum-section.go +++ b/analyzer/enum-section.go @@ -8,7 +8,8 @@ import "git.tebibyte.media/arf/arf/infoerr" type EnumSection struct { sectionBase what Type - members []EnumMember + members []EnumMember + argument Argument } // EnumMember is a member of an enumerated type. @@ -34,6 +35,7 @@ func (section EnumSection) ToString (indent int) (output string) { output += section.permission.ToString() + " " output += section.where.ToString() output += "\n" + output += section.argument.ToString(indent + 1) output += section.what.ToString(indent + 1) for _, member := range section.members { output += member.ToString(indent + 1) @@ -194,6 +196,15 @@ func (analyzer analysisOperation) analyzeEnumSection () ( } } + if len(outputSection.members) < 1 { + err = outputSection.NewError ( + "cannot create an enum with no members", + infoerr.ErrorKindError) + return + } + + outputSection.argument = outputSection.members[0].argument + outputSection.complete = true return } diff --git a/analyzer/enum-section_test.go b/analyzer/enum-section_test.go index 1650a70..a2f22a6 100644 --- a/analyzer/enum-section_test.go +++ b/analyzer/enum-section_test.go @@ -4,6 +4,44 @@ import "testing" func TestEnumSection (test *testing.T) { checkTree ("../tests/analyzer/enumSection", false, -` +`enumSection ro ../tests/analyzer/enumSection.aWeekday + arg uint 1 + type 1 basic Int + member sunday + arg uint 1 + member monday + arg uint 2 + member tuesday + arg uint 3 + member wednesday + arg uint 4 + member thursday + arg uint 5 + member friday + arg uint 6 + member saturday + arg uint 7 +typeSection ro ../tests/analyzer/enumSection.bColor + type 1 basic U32 +enumSection ro ../tests/analyzer/enumSection.cNamedColor + arg uint 16711680 + type 1 basic bColor + member red + arg uint 16711680 + member green + arg uint 65280 + member blue + arg uint 255 +enumSection ro ../tests/analyzer/enumSection.dFromFarAway + arg uint 5 + type 1 basic dInheritFromOther + member bird + arg uint 5 + member bread + arg uint 4 +typeSection ro ../tests/analyzer/typeSection/required.aBasic + type 1 basic Int +typeSection ro ../tests/analyzer/typeSection.dInheritFromOther + type 1 basic aBasic `, test) } diff --git a/tests/analyzer/enumSection/main.arf b/tests/analyzer/enumSection/main.arf index 1b37c1c..3a7df5a 100644 --- a/tests/analyzer/enumSection/main.arf +++ b/tests/analyzer/enumSection/main.arf @@ -5,7 +5,7 @@ require '../typeSection' enum ro aWeekday:Int - sunday - monday - - tuesday 3 + - tuesday - wednesday - thursday - friday @@ -17,3 +17,7 @@ enum ro cNamedColor:bColor - red 0xFF0000 - green 0x00FF00 - blue 0x0000FF + +enum ro dFromFarAway:typeSection.dInheritFromOther + - bird + - bread 4