Menu

#404 len/sizeof type parsing eats namespace prefix

closed
dkl
None
compiler
2019-12-07
2008-08-24
No

len/sizeof parse their argument as type first, then if that fails, as expression. The problem is that the type parsing will eat any namespace prefix in the hopes it is an access to a namespaced type, and if that fails, the expression parser is called without getting to see that namespace prefix. So it's just lost.

That causes the "variable not declared" errors in code like this:

namespace UDT
    dim shared i as integer
end namespace
print sizeof( UDT.i )

As a work-around it's possible to use parentheses, because then the type parser will immediately see that it's not a type, and then it will exit without having eaten the namespace prefix:

print sizeof( (UDT.i) )

Another example:

namespace foo
    const bar = "whee"
end namespace
print len(foo.bar)

Related

Bugs: #483
Bugs: #718
Feature Requests: #293

Discussion

  • fxm (freebasic.net)

    A workaround is to use double parentheses:
    print len((foo.bar))

    Same problem with the keywords 'Typeof()' or 'Sizeof()'.

    Referring to forum at post:
    http://www.freebasic.net/forum/viewtopic.php?p=194125#p194125

     
  • dkl

    dkl - 2014-01-29
    • labels: compiler -->
    • summary: len of const in namespace fails to compile --> len/sizeof type parsing eats namespace prefix
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,11 +1,19 @@
    -namespace foo
    +len/sizeof parse their argument as type first, then if that fails, as expression. The problem is that the type parsing will eat any namespace prefix in the hopes it is an access to a namespaced type, and if that fails, the expression parser is called without getting to see that namespace prefix. So it's just lost.
    +
    +That causes the `variable not declared` errors in code like this:
    +
    +    namespace UDT
    +        dim shared i as integer
    +    end namespace
    +    print sizeof( UDT.i )
    +
    +As a work-around it's possible to use parentheses, because then the type parser will immediately see that it's not a type, and then it will exit without having eaten the namespace prefix:
    +
    +    print sizeof( (UDT.i) )
    +
    +Another example:
    +
    +    namespace foo
             const bar = "whee"
    -end namespace
    -
    -print len(foo.bar)
    -
    -----------
    -
    -fails to compile with:
    -
    -error 41: Variable not declared, bar in 'print len(foo.bar)'
    +    end namespace
    +    print len(foo.bar)
    
    • assigned_to: André Victor T. Vicentini --> dkl
    • Component: --> compiler
     
  • dkl

    dkl - 2014-01-29
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,6 +1,6 @@
     len/sizeof parse their argument as type first, then if that fails, as expression. The problem is that the type parsing will eat any namespace prefix in the hopes it is an access to a namespaced type, and if that fails, the expression parser is called without getting to see that namespace prefix. So it's just lost.
    
    -That causes the `variable not declared` errors in code like this:
    +That causes the "`variable not declared`" errors in code like this:
    
         namespace UDT
             dim shared i as integer
    
     
  • Jeff Marshall

    Jeff Marshall - 2019-12-07

    Fixed in https://github.com/freebasic/fbc/pull/201

    Commit in [62fdd4]

    Some simple tests have been added to test suite for 'plain' uses of namespaces, nested namespaces, types, etc.

     

    Related

    Commit: [62fdd4]

  • Jeff Marshall

    Jeff Marshall - 2019-12-07
    • status: open --> closed
     

Log in to post a comment.