From 3e9d168edd9e40138b295bb7d8adc92fa9430e78 Mon Sep 17 00:00:00 2001 From: Paul Chaignon Date: Fri, 1 Dec 2023 14:46:41 +0100 Subject: compiler: refactor recurFlattenFlags This commit refactors recurFlattenFlags using Go generics and new interfaces so that it also applies to a different set of flags types. In a subsequent commit, we will use that to perform the same recursive flattening for string flags. Signed-off-by: Paul Chaignon --- pkg/ast/ast.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'pkg/ast') diff --git a/pkg/ast/ast.go b/pkg/ast/ast.go index e225c69b2..2e8a7a015 100644 --- a/pkg/ast/ast.go +++ b/pkg/ast/ast.go @@ -27,6 +27,15 @@ type Node interface { walk(cb func(Node)) } +type Flags[T FlagValue] interface { + SetValues(values []T) + GetValues() []T +} + +type FlagValue interface { + GetName() string +} + // Top-level AST nodes. type NewLine struct { @@ -135,6 +144,14 @@ func (n *IntFlags) Info() (Pos, string, string) { return n.Pos, "flags", n.Name.Name } +func (n *IntFlags) SetValues(values []*Int) { + n.Values = values +} + +func (n *IntFlags) GetValues() []*Int { + return n.Values +} + type StrFlags struct { Pos Pos Name *Ident @@ -145,6 +162,14 @@ func (n *StrFlags) Info() (Pos, string, string) { return n.Pos, "string flags", n.Name.Name } +func (n *StrFlags) SetValues(values []*String) { + n.Values = values +} + +func (n *StrFlags) GetValues() []*String { + return n.Values +} + type TypeDef struct { Pos Pos Name *Ident @@ -180,6 +205,10 @@ func (n *String) Info() (Pos, string, string) { return n.Pos, tok2str[tokString], "" } +func (n *String) GetName() string { + return n.Value +} + type IntFmt int const ( @@ -210,6 +239,10 @@ func (n *Int) Info() (Pos, string, string) { return n.Pos, tok2str[tokInt], "" } +func (n *Int) GetName() string { + return n.Ident +} + type Type struct { Pos Pos // Only one of Value, Ident, String is filled. -- cgit mrf-deployment