How to (better) initialize a UDT

General FreeBASIC programming questions.
Ed Davis
Posts: 10
Joined: Jul 28, 2008 23:24

How to (better) initialize a UDT

Postby Ed Davis » Feb 24, 2016 12:43

How to (better) initialize the Atr_s UDT below?

Given:

Code: Select all

enum Token_type
    tk_eoi
    tk_if
    ...
    tk_variable
end enum

type Atr_s
    tok                 as Token_type
    text                as string
    right_associative   as boolean
    is_binary           as boolean
    is_unary            as boolean
    precedence          as integer
end type

dim shared atr(tk_eoi to tk_variable) as Atr_s

I'm currently initializing atr via:

Code: Select all

    atr(tk_eoi     ).tok = tk_eoi
    atr(tk_if      ).tok = tk_if
    ...
    atr(tk_variable).tok = tk_variable

    atr(tk_eoi     ).text = "EOI"
    atr(tk_if      ).text = "if"
    ...
    atr(tk_variable).text = "Variable"

    atr(tk_eoi     ).right_associative = false
    atr(tk_if      ).right_associative = false
    ...
    atr(tk_variable).right_associative = false

    atr(tk_eoi     ).is_binary = false
    atr(tk_if      ).is_binary = false
    ...
    atr(tk_variable).is_binary = false

    atr(tk_eoi     ).is_unary = false
    atr(tk_if      ).is_unary = false
    ...
    atr(tk_variable).is_unary = false

    atr(tk_eoi     ).precedence = -1
    atr(tk_if      ).precedence = -1
    ...
    atr(tk_variable).precedence = -1


I would like to do something like:

Code: Select all

struct {
    TokenType   tok;
    char       *text;
    bool        right_associative, is_binary, is_unary;
    int         precedence;
} atr[] = {
    {EOI,      "EOI"     ,  false, false, false, -1},
    {If,       "If"      ,  false, false, false, -1},
    ...
    {Variable, "Variable",  false, false, false, -1},
};


Or at least be able to initialize the table a row at a time.

Any ideas?

Thanks!
fxm
Posts: 9304
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to (better) initialize a UDT

Postby fxm » Feb 24, 2016 14:03

UDT direct initializing with strings is not supported at this time, therefore a constructor must be defined.

Syntax without initialisez:

Code: Select all

enum Token_type
    tk_eoi
    tk_if
'   .....
    tk_variable
end enum

type Atr_s
    declare constructor ()
    declare constructor ( _
                        byval t as Token_type, _
                        byref te as string, _
                        byval ra as boolean, _
                        byval ib as boolean, _
                        byval iu as boolean, _
                        byval pr as integer _
                        )
    tok                 as Token_type
    text                as string
    right_associative   as boolean
    is_binary           as boolean
    is_unary            as boolean
    precedence          as integer
end type

constructor Atr_s ()
end constructor

constructor Atr_s ( _
                  byval t as Token_type, _
                  byref te as string, _
                  byval ra as boolean, _
                  byval ib as boolean, _
                  byval iu as boolean, _
                  byval pr as integer _
                  )
    tok                 = t
    text                = te
    right_associative   = ra
    is_binary           = ib
    is_unary            = iu
    precedence          = pr
end constructor

dim shared atr(tk_eoi to tk_variable) as Atr_s

    atr(tk_eoi     ) = type(tk_eoi     , "EOI"     , false, false, false, -1)
'   .....
    atr(tk_if      ) = type(tk_if      , "if"      , false, false, false, -1)
    atr(tk_variable) = type(tk_variable, "Variable", false, false, false, -1)

Syntax with initializer:

Code: Select all

enum Token_type
    tk_eoi
    tk_if
'   .....
    tk_variable
end enum

type Atr_s
    declare constructor ()
    declare constructor ( _
                        byval t as Token_type, _
                        byref te as string, _
                        byval ra as boolean, _
                        byval ib as boolean, _
                        byval iu as boolean, _
                        byval pr as integer _
                        )
    tok                 as Token_type
    text                as string
    right_associative   as boolean
    is_binary           as boolean
    is_unary            as boolean
    precedence          as integer
end type

constructor Atr_s ()
end constructor

constructor Atr_s ( _
                  byval t as Token_type, _
                  byref te as string, _
                  byval ra as boolean, _
                  byval ib as boolean, _
                  byval iu as boolean, _
                  byval pr as integer _
                  )
    tok                 = t
    text                = te
    right_associative   = ra
    is_binary           = ib
    is_unary            = iu
    precedence          = pr
end constructor

dim shared atr(tk_eoi to tk_variable) as Atr_s = _
    { _
    type(tk_eoi     , "EOI"     , false, false, false, -1), _
    type(tk_eoi     , "EOI"     , false, false, false, -1), _
    /'.....'/ _
    type(tk_variable, "Variable", false, false, false, -1) _
    }
Ed Davis
Posts: 10
Joined: Jul 28, 2008 23:24

Re: How to (better) initialize a UDT

Postby Ed Davis » Feb 26, 2016 3:30

fxm wrote:UDT direct initializing with strings is not supported at this time, therefore a constructor must be defined.
....


That worked _really_ well! Thank you!
dkl
Site Admin
Posts: 3210
Joined: Jul 28, 2005 14:45
Location: Germany

Re: How to (better) initialize a UDT

Postby dkl » Feb 26, 2016 16:38

Hi,

if the text field should just point to some static, constant string data, and doesn't need to be changed at run-time, you could also use a ZString Ptr (FreeBASIC's equivalent to C's char*) instead of a dynamic String:

Code: Select all

type UDT
   text as const zstring ptr
end type

dim shared info(0 to ...) as UDT => { _
   (@"a"), _
   (@"b"), _
   (@"c"), _
   (@"d"), _
   (@"e"), _
   (@"f")  _
}

Return to “General”

Who is online

Users browsing this forum: No registered users and 1 guest