Coding and general discussion relating to the compiler
Moderators: David Barker, Jerry Messina
-
Jerry Messina
- Swordfish Developer
- Posts: 1473
- Joined: Fri Jan 30, 2009 6:27 pm
- Location: US
Post
by Jerry Messina » Wed Jan 11, 2012 2:56 pm
I get a compiler error when I try to use a const that has an underscore as the first char in the identifier when accessing bits
Code: Select all
const BIT0 = 0
const _BIT1 = 1
RCON.BIT0 = 1 // works
RCON.bits(BIT0) = 1 // works
RCON._BIT1 = 0 // error - identifier not declared: RCON._BIT1
RCON.bits(_BIT1) = 1 // works
-
David Barker
- Swordfish Developer
- Posts: 1214
- Joined: Tue Oct 03, 2006 7:01 pm
- Location: Saltburn by the Sea, UK
-
Contact:
Post
by David Barker » Wed Jan 11, 2012 6:17 pm
I think mikroE compilers work this way, but not Swordfish (it never has). If you want to use underscored values (rather than the inbuilt bit0 etc) you need to define you own structure. For example,
Code: Select all
structure TRCON
Value as byte
_bit0 as Value.Bit0
_bit1 as Value.Bit1
_bit2 as Value.Bit2
_bit3 as Value.Bit3
_bit4 as Value.Bit4
_bit5 as Value.Bit5
_bit6 as Value.Bit6
_bit7 as Value.Bit7
end structure
dim RCON as TRCON absolute $0FD0
RCON._Bit0 = 0
-
Jerry Messina
- Swordfish Developer
- Posts: 1473
- Joined: Fri Jan 30, 2009 6:27 pm
- Location: US
Post
by Jerry Messina » Wed Jan 11, 2012 7:07 pm
Oops... it seems I picked a really bad example using names BIT0, BIT1, etc! I forgot they were already reserved (the underscore was a red herring).
I was just trying to use a const name in place if a number to identify a bit, like
Code: Select all
const MY_BIT = 0
const _MY_BIT = 1
RCON.0 = 1 // works of course
RCON.MY_BIT = 1 // doesn't work... identifier not declared
RCON.bits(MY_BIT) = 1 // works
RCON.bits(_MY_BIT) = 1 // works
Guess I'll just use the 'RCON.bits(MY_BIT)' form. Using the structure would make for a lot more work at this point. Thanks, though.
-
David Barker
- Swordfish Developer
- Posts: 1214
- Joined: Tue Oct 03, 2006 7:01 pm
- Location: Saltburn by the Sea, UK
-
Contact:
Post
by David Barker » Wed Jan 11, 2012 7:25 pm
Yes, I did understand what you meant. You cannot do what you are trying to do in Swordfish, other than in the way I showed earlier. The 'numeric' notation (.0, .1 etc) was really just implemented to provide support for people that had been used to PBP, PROTON etc. Swordfish is more like C# or Pascal in that the dot notation is used to reference either a module (or namespace) and structures. I don't know of any other langugae (other than mikroE) which allows the type of definition you showed earlier. Either way, it's not a compiler error but just the way it's designed.
By the way, hope you had a good xmas and new year Jerry!!!
-
Jerry Messina
- Swordfish Developer
- Posts: 1473
- Joined: Fri Jan 30, 2009 6:27 pm
- Location: US
Post
by Jerry Messina » Wed Jan 11, 2012 7:54 pm
By design it is then! I'll go with whatever syntax works at this point.
So many languages, so few brain cells! It's funny how much time I spend relearning the same things I've probably already discovered at some point.
The nice thing about the structure approach is that it matches up pretty well with how things are done in C18, so if you have a lot of code to port it makes it that much easier.
And thanks. Hope your holiday was good also, and the same to everyone else who stops by here.